debugger.js 18 KB

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