debugger.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFBug = void 0;
  27. let opMap;
  28. const FontInspector = function FontInspectorClosure() {
  29. let fonts;
  30. let active = false;
  31. const fontAttribute = "data-font-name";
  32. function removeSelection() {
  33. const divs = document.querySelectorAll(`span[${fontAttribute}]`);
  34. for (const div of divs) {
  35. div.className = "";
  36. }
  37. }
  38. function resetSelection() {
  39. const divs = document.querySelectorAll(`span[${fontAttribute}]`);
  40. for (const div of divs) {
  41. div.className = "debuggerHideText";
  42. }
  43. }
  44. function selectFont(fontName, show) {
  45. const divs = document.querySelectorAll(`span[${fontAttribute}=${fontName}]`);
  46. for (const div of divs) {
  47. div.className = show ? "debuggerShowText" : "debuggerHideText";
  48. }
  49. }
  50. function textLayerClick(e) {
  51. if (!e.target.dataset.fontName || e.target.tagName.toUpperCase() !== "SPAN") {
  52. return;
  53. }
  54. const fontName = e.target.dataset.fontName;
  55. const selects = document.getElementsByTagName("input");
  56. for (const select of selects) {
  57. if (select.dataset.fontName !== fontName) {
  58. continue;
  59. }
  60. select.checked = !select.checked;
  61. selectFont(fontName, select.checked);
  62. select.scrollIntoView();
  63. }
  64. }
  65. return {
  66. id: "FontInspector",
  67. name: "Font Inspector",
  68. panel: null,
  69. manager: null,
  70. init(pdfjsLib) {
  71. const panel = this.panel;
  72. const tmp = document.createElement("button");
  73. tmp.addEventListener("click", resetSelection);
  74. tmp.textContent = "Refresh";
  75. panel.appendChild(tmp);
  76. fonts = document.createElement("div");
  77. panel.appendChild(fonts);
  78. },
  79. cleanup() {
  80. fonts.textContent = "";
  81. },
  82. enabled: false,
  83. get active() {
  84. return active;
  85. },
  86. set active(value) {
  87. active = value;
  88. if (active) {
  89. document.body.addEventListener("click", textLayerClick, true);
  90. resetSelection();
  91. } else {
  92. document.body.removeEventListener("click", textLayerClick, true);
  93. removeSelection();
  94. }
  95. },
  96. fontAdded(fontObj, url) {
  97. function properties(obj, list) {
  98. const moreInfo = document.createElement("table");
  99. for (const entry of list) {
  100. const tr = document.createElement("tr");
  101. const td1 = document.createElement("td");
  102. td1.textContent = entry;
  103. tr.appendChild(td1);
  104. const td2 = document.createElement("td");
  105. td2.textContent = obj[entry].toString();
  106. tr.appendChild(td2);
  107. moreInfo.appendChild(tr);
  108. }
  109. return moreInfo;
  110. }
  111. const moreInfo = properties(fontObj, ["name", "type"]);
  112. const fontName = fontObj.loadedName;
  113. const font = document.createElement("div");
  114. const name = document.createElement("span");
  115. name.textContent = fontName;
  116. const download = document.createElement("a");
  117. if (url) {
  118. url = /url\(['"]?([^)"']+)/.exec(url);
  119. download.href = url[1];
  120. } else if (fontObj.data) {
  121. download.href = URL.createObjectURL(new Blob([fontObj.data], {
  122. type: fontObj.mimeType
  123. }));
  124. }
  125. download.textContent = "Download";
  126. const logIt = document.createElement("a");
  127. logIt.href = "";
  128. logIt.textContent = "Log";
  129. logIt.addEventListener("click", function (event) {
  130. event.preventDefault();
  131. console.log(fontObj);
  132. });
  133. const select = document.createElement("input");
  134. select.setAttribute("type", "checkbox");
  135. select.dataset.fontName = fontName;
  136. select.addEventListener("click", function () {
  137. selectFont(fontName, select.checked);
  138. });
  139. font.appendChild(select);
  140. font.appendChild(name);
  141. font.appendChild(document.createTextNode(" "));
  142. font.appendChild(download);
  143. font.appendChild(document.createTextNode(" "));
  144. font.appendChild(logIt);
  145. font.appendChild(moreInfo);
  146. fonts.appendChild(font);
  147. setTimeout(() => {
  148. if (this.active) {
  149. resetSelection();
  150. }
  151. }, 2000);
  152. }
  153. };
  154. }();
  155. const StepperManager = function StepperManagerClosure() {
  156. let steppers = [];
  157. let stepperDiv = null;
  158. let stepperControls = null;
  159. let stepperChooser = null;
  160. let breakPoints = Object.create(null);
  161. return {
  162. id: "Stepper",
  163. name: "Stepper",
  164. panel: null,
  165. manager: null,
  166. init(pdfjsLib) {
  167. const self = this;
  168. stepperControls = document.createElement("div");
  169. stepperChooser = document.createElement("select");
  170. stepperChooser.addEventListener("change", function (event) {
  171. self.selectStepper(this.value);
  172. });
  173. stepperControls.appendChild(stepperChooser);
  174. stepperDiv = document.createElement("div");
  175. this.panel.appendChild(stepperControls);
  176. this.panel.appendChild(stepperDiv);
  177. if (sessionStorage.getItem("pdfjsBreakPoints")) {
  178. breakPoints = JSON.parse(sessionStorage.getItem("pdfjsBreakPoints"));
  179. }
  180. opMap = Object.create(null);
  181. for (const key in pdfjsLib.OPS) {
  182. opMap[pdfjsLib.OPS[key]] = key;
  183. }
  184. },
  185. cleanup() {
  186. stepperChooser.textContent = "";
  187. stepperDiv.textContent = "";
  188. steppers = [];
  189. },
  190. enabled: false,
  191. active: false,
  192. create(pageIndex) {
  193. const debug = document.createElement("div");
  194. debug.id = "stepper" + pageIndex;
  195. debug.hidden = true;
  196. debug.className = "stepper";
  197. stepperDiv.appendChild(debug);
  198. const b = document.createElement("option");
  199. b.textContent = "Page " + (pageIndex + 1);
  200. b.value = pageIndex;
  201. stepperChooser.appendChild(b);
  202. const initBreakPoints = breakPoints[pageIndex] || [];
  203. const stepper = new Stepper(debug, pageIndex, initBreakPoints);
  204. steppers.push(stepper);
  205. if (steppers.length === 1) {
  206. this.selectStepper(pageIndex, false);
  207. }
  208. return stepper;
  209. },
  210. selectStepper(pageIndex, selectPanel) {
  211. pageIndex |= 0;
  212. if (selectPanel) {
  213. this.manager.selectPanel(this);
  214. }
  215. for (const stepper of steppers) {
  216. stepper.panel.hidden = stepper.pageIndex !== pageIndex;
  217. }
  218. for (const option of stepperChooser.options) {
  219. option.selected = (option.value | 0) === pageIndex;
  220. }
  221. },
  222. saveBreakPoints(pageIndex, bps) {
  223. breakPoints[pageIndex] = bps;
  224. sessionStorage.setItem("pdfjsBreakPoints", JSON.stringify(breakPoints));
  225. }
  226. };
  227. }();
  228. const Stepper = function StepperClosure() {
  229. function c(tag, textContent) {
  230. const d = document.createElement(tag);
  231. if (textContent) {
  232. d.textContent = textContent;
  233. }
  234. return d;
  235. }
  236. function simplifyArgs(args) {
  237. if (typeof args === "string") {
  238. const MAX_STRING_LENGTH = 75;
  239. return args.length <= MAX_STRING_LENGTH ? args : args.substring(0, MAX_STRING_LENGTH) + "...";
  240. }
  241. if (typeof args !== "object" || args === null) {
  242. return args;
  243. }
  244. if ("length" in args) {
  245. const MAX_ITEMS = 10,
  246. simpleArgs = [];
  247. let i, ii;
  248. for (i = 0, ii = Math.min(MAX_ITEMS, args.length); i < ii; i++) {
  249. simpleArgs.push(simplifyArgs(args[i]));
  250. }
  251. if (i < args.length) {
  252. simpleArgs.push("...");
  253. }
  254. return simpleArgs;
  255. }
  256. const simpleObj = {};
  257. for (const key in args) {
  258. simpleObj[key] = simplifyArgs(args[key]);
  259. }
  260. return simpleObj;
  261. }
  262. class Stepper {
  263. constructor(panel, pageIndex, initialBreakPoints) {
  264. this.panel = panel;
  265. this.breakPoint = 0;
  266. this.nextBreakPoint = null;
  267. this.pageIndex = pageIndex;
  268. this.breakPoints = initialBreakPoints;
  269. this.currentIdx = -1;
  270. this.operatorListIdx = 0;
  271. this.indentLevel = 0;
  272. }
  273. init(operatorList) {
  274. const panel = this.panel;
  275. const content = c("div", "c=continue, s=step");
  276. const table = c("table");
  277. content.appendChild(table);
  278. table.cellSpacing = 0;
  279. const headerRow = c("tr");
  280. table.appendChild(headerRow);
  281. headerRow.appendChild(c("th", "Break"));
  282. headerRow.appendChild(c("th", "Idx"));
  283. headerRow.appendChild(c("th", "fn"));
  284. headerRow.appendChild(c("th", "args"));
  285. panel.appendChild(content);
  286. this.table = table;
  287. this.updateOperatorList(operatorList);
  288. }
  289. updateOperatorList(operatorList) {
  290. const self = this;
  291. function cboxOnClick() {
  292. const x = +this.dataset.idx;
  293. if (this.checked) {
  294. self.breakPoints.push(x);
  295. } else {
  296. self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
  297. }
  298. StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
  299. }
  300. const MAX_OPERATORS_COUNT = 15000;
  301. if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
  302. return;
  303. }
  304. const chunk = document.createDocumentFragment();
  305. const operatorsToDisplay = Math.min(MAX_OPERATORS_COUNT, operatorList.fnArray.length);
  306. for (let i = this.operatorListIdx; i < operatorsToDisplay; i++) {
  307. const line = c("tr");
  308. line.className = "line";
  309. line.dataset.idx = i;
  310. chunk.appendChild(line);
  311. const checked = this.breakPoints.includes(i);
  312. const args = operatorList.argsArray[i] || [];
  313. const breakCell = c("td");
  314. const cbox = c("input");
  315. cbox.type = "checkbox";
  316. cbox.className = "points";
  317. cbox.checked = checked;
  318. cbox.dataset.idx = i;
  319. cbox.onclick = cboxOnClick;
  320. breakCell.appendChild(cbox);
  321. line.appendChild(breakCell);
  322. line.appendChild(c("td", i.toString()));
  323. const fn = opMap[operatorList.fnArray[i]];
  324. let decArgs = args;
  325. if (fn === "showText") {
  326. const glyphs = args[0];
  327. const charCodeRow = c("tr");
  328. const fontCharRow = c("tr");
  329. const unicodeRow = c("tr");
  330. for (const glyph of glyphs) {
  331. if (typeof glyph === "object" && glyph !== null) {
  332. charCodeRow.appendChild(c("td", glyph.originalCharCode));
  333. fontCharRow.appendChild(c("td", glyph.fontChar));
  334. unicodeRow.appendChild(c("td", glyph.unicode));
  335. } else {
  336. const advanceEl = c("td", glyph);
  337. advanceEl.classList.add("advance");
  338. charCodeRow.appendChild(advanceEl);
  339. fontCharRow.appendChild(c("td"));
  340. unicodeRow.appendChild(c("td"));
  341. }
  342. }
  343. decArgs = c("td");
  344. const table = c("table");
  345. table.classList.add("showText");
  346. decArgs.appendChild(table);
  347. table.appendChild(charCodeRow);
  348. table.appendChild(fontCharRow);
  349. table.appendChild(unicodeRow);
  350. } else if (fn === "restore") {
  351. this.indentLevel--;
  352. }
  353. line.appendChild(c("td", " ".repeat(this.indentLevel * 2) + fn));
  354. if (fn === "save") {
  355. this.indentLevel++;
  356. }
  357. if (decArgs instanceof HTMLElement) {
  358. line.appendChild(decArgs);
  359. } else {
  360. line.appendChild(c("td", JSON.stringify(simplifyArgs(decArgs))));
  361. }
  362. }
  363. if (operatorsToDisplay < operatorList.fnArray.length) {
  364. const lastCell = c("td", "...");
  365. lastCell.colspan = 4;
  366. chunk.appendChild(lastCell);
  367. }
  368. this.operatorListIdx = operatorList.fnArray.length;
  369. this.table.appendChild(chunk);
  370. }
  371. getNextBreakPoint() {
  372. this.breakPoints.sort(function (a, b) {
  373. return a - b;
  374. });
  375. for (const breakPoint of this.breakPoints) {
  376. if (breakPoint > this.currentIdx) {
  377. return breakPoint;
  378. }
  379. }
  380. return null;
  381. }
  382. breakIt(idx, callback) {
  383. StepperManager.selectStepper(this.pageIndex, true);
  384. this.currentIdx = idx;
  385. const listener = evt => {
  386. switch (evt.keyCode) {
  387. case 83:
  388. document.removeEventListener("keydown", listener);
  389. this.nextBreakPoint = this.currentIdx + 1;
  390. this.goTo(-1);
  391. callback();
  392. break;
  393. case 67:
  394. document.removeEventListener("keydown", listener);
  395. this.nextBreakPoint = this.getNextBreakPoint();
  396. this.goTo(-1);
  397. callback();
  398. break;
  399. }
  400. };
  401. document.addEventListener("keydown", listener);
  402. this.goTo(idx);
  403. }
  404. goTo(idx) {
  405. const allRows = this.panel.getElementsByClassName("line");
  406. for (const row of allRows) {
  407. if ((row.dataset.idx | 0) === idx) {
  408. row.style.backgroundColor = "rgb(251,250,207)";
  409. row.scrollIntoView();
  410. } else {
  411. row.style.backgroundColor = null;
  412. }
  413. }
  414. }
  415. }
  416. return Stepper;
  417. }();
  418. const Stats = function Stats() {
  419. let stats = [];
  420. function clear(node) {
  421. node.textContent = "";
  422. }
  423. function getStatIndex(pageNumber) {
  424. for (const [i, stat] of stats.entries()) {
  425. if (stat.pageNumber === pageNumber) {
  426. return i;
  427. }
  428. }
  429. return false;
  430. }
  431. return {
  432. id: "Stats",
  433. name: "Stats",
  434. panel: null,
  435. manager: null,
  436. init(pdfjsLib) {},
  437. enabled: false,
  438. active: false,
  439. add(pageNumber, stat) {
  440. if (!stat) {
  441. return;
  442. }
  443. const statsIndex = getStatIndex(pageNumber);
  444. if (statsIndex !== false) {
  445. stats[statsIndex].div.remove();
  446. stats.splice(statsIndex, 1);
  447. }
  448. const wrapper = document.createElement("div");
  449. wrapper.className = "stats";
  450. const title = document.createElement("div");
  451. title.className = "title";
  452. title.textContent = "Page: " + pageNumber;
  453. const statsDiv = document.createElement("div");
  454. statsDiv.textContent = stat.toString();
  455. wrapper.appendChild(title);
  456. wrapper.appendChild(statsDiv);
  457. stats.push({
  458. pageNumber,
  459. div: wrapper
  460. });
  461. stats.sort(function (a, b) {
  462. return a.pageNumber - b.pageNumber;
  463. });
  464. clear(this.panel);
  465. for (const entry of stats) {
  466. this.panel.appendChild(entry.div);
  467. }
  468. },
  469. cleanup() {
  470. stats = [];
  471. clear(this.panel);
  472. }
  473. };
  474. }();
  475. const PDFBug = function PDFBugClosure() {
  476. const panelWidth = 300;
  477. const buttons = [];
  478. let activePanel = null;
  479. return {
  480. tools: [FontInspector, StepperManager, Stats],
  481. enable(ids) {
  482. const all = ids.length === 1 && ids[0] === "all";
  483. const tools = this.tools;
  484. for (const tool of tools) {
  485. if (all || ids.includes(tool.id)) {
  486. tool.enabled = true;
  487. }
  488. }
  489. if (!all) {
  490. tools.sort(function (a, b) {
  491. let indexA = ids.indexOf(a.id);
  492. indexA = indexA < 0 ? tools.length : indexA;
  493. let indexB = ids.indexOf(b.id);
  494. indexB = indexB < 0 ? tools.length : indexB;
  495. return indexA - indexB;
  496. });
  497. }
  498. },
  499. init(pdfjsLib, container, ids) {
  500. this.loadCSS();
  501. this.enable(ids);
  502. const ui = document.createElement("div");
  503. ui.id = "PDFBug";
  504. const controls = document.createElement("div");
  505. controls.setAttribute("class", "controls");
  506. ui.appendChild(controls);
  507. const panels = document.createElement("div");
  508. panels.setAttribute("class", "panels");
  509. ui.appendChild(panels);
  510. container.appendChild(ui);
  511. container.style.right = panelWidth + "px";
  512. for (const tool of this.tools) {
  513. const panel = document.createElement("div");
  514. const panelButton = document.createElement("button");
  515. panelButton.textContent = tool.name;
  516. panelButton.addEventListener("click", event => {
  517. event.preventDefault();
  518. this.selectPanel(tool);
  519. });
  520. controls.appendChild(panelButton);
  521. panels.appendChild(panel);
  522. tool.panel = panel;
  523. tool.manager = this;
  524. if (tool.enabled) {
  525. tool.init(pdfjsLib);
  526. } else {
  527. panel.textContent = `${tool.name} is disabled. To enable add "${tool.id}" to ` + "the pdfBug parameter and refresh (separate multiple by commas).";
  528. }
  529. buttons.push(panelButton);
  530. }
  531. this.selectPanel(0);
  532. },
  533. loadCSS() {
  534. const {
  535. url
  536. } = import.meta;
  537. const link = document.createElement("link");
  538. link.rel = "stylesheet";
  539. link.href = url.replace(/.js$/, ".css");
  540. document.head.appendChild(link);
  541. },
  542. cleanup() {
  543. for (const tool of this.tools) {
  544. if (tool.enabled) {
  545. tool.cleanup();
  546. }
  547. }
  548. },
  549. selectPanel(index) {
  550. if (typeof index !== "number") {
  551. index = this.tools.indexOf(index);
  552. }
  553. if (index === activePanel) {
  554. return;
  555. }
  556. activePanel = index;
  557. for (const [j, tool] of this.tools.entries()) {
  558. const isActive = j === index;
  559. buttons[j].classList.toggle("active", isActive);
  560. tool.active = isActive;
  561. tool.panel.hidden = !isActive;
  562. }
  563. }
  564. };
  565. }();
  566. exports.PDFBug = PDFBug;
  567. globalThis.FontInspector = FontInspector;
  568. globalThis.StepperManager = StepperManager;
  569. globalThis.Stats = Stats;