debugger.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. }
  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 (let j = 0; j < glyphs.length; j++) {
  331. const glyph = glyphs[j];
  332. if (typeof glyph === "object" && glyph !== null) {
  333. charCodeRow.appendChild(c("td", glyph.originalCharCode));
  334. fontCharRow.appendChild(c("td", glyph.fontChar));
  335. unicodeRow.appendChild(c("td", glyph.unicode));
  336. } else {
  337. const advanceEl = c("td", glyph);
  338. advanceEl.classList.add("advance");
  339. charCodeRow.appendChild(advanceEl);
  340. fontCharRow.appendChild(c("td"));
  341. unicodeRow.appendChild(c("td"));
  342. }
  343. }
  344. decArgs = c("td");
  345. const table = c("table");
  346. table.classList.add("showText");
  347. decArgs.appendChild(table);
  348. table.appendChild(charCodeRow);
  349. table.appendChild(fontCharRow);
  350. table.appendChild(unicodeRow);
  351. }
  352. line.appendChild(c("td", fn));
  353. if (decArgs instanceof HTMLElement) {
  354. line.appendChild(decArgs);
  355. } else {
  356. line.appendChild(c("td", JSON.stringify(simplifyArgs(decArgs))));
  357. }
  358. }
  359. if (operatorsToDisplay < operatorList.fnArray.length) {
  360. const lastCell = c("td", "...");
  361. lastCell.colspan = 4;
  362. chunk.appendChild(lastCell);
  363. }
  364. this.operatorListIdx = operatorList.fnArray.length;
  365. this.table.appendChild(chunk);
  366. }
  367. getNextBreakPoint() {
  368. this.breakPoints.sort(function (a, b) {
  369. return a - b;
  370. });
  371. for (let i = 0; i < this.breakPoints.length; i++) {
  372. if (this.breakPoints[i] > this.currentIdx) {
  373. return this.breakPoints[i];
  374. }
  375. }
  376. return null;
  377. }
  378. breakIt(idx, callback) {
  379. StepperManager.selectStepper(this.pageIndex, true);
  380. this.currentIdx = idx;
  381. const listener = evt => {
  382. switch (evt.keyCode) {
  383. case 83:
  384. document.removeEventListener("keydown", listener);
  385. this.nextBreakPoint = this.currentIdx + 1;
  386. this.goTo(-1);
  387. callback();
  388. break;
  389. case 67:
  390. document.removeEventListener("keydown", listener);
  391. this.nextBreakPoint = this.getNextBreakPoint();
  392. this.goTo(-1);
  393. callback();
  394. break;
  395. }
  396. };
  397. document.addEventListener("keydown", listener);
  398. this.goTo(idx);
  399. }
  400. goTo(idx) {
  401. const allRows = this.panel.getElementsByClassName("line");
  402. for (let x = 0, xx = allRows.length; x < xx; ++x) {
  403. const row = allRows[x];
  404. if ((row.dataset.idx | 0) === idx) {
  405. row.style.backgroundColor = "rgb(251,250,207)";
  406. row.scrollIntoView();
  407. } else {
  408. row.style.backgroundColor = null;
  409. }
  410. }
  411. }
  412. }
  413. return Stepper;
  414. }();
  415. var Stats = function Stats() {
  416. let stats = [];
  417. function clear(node) {
  418. while (node.hasChildNodes()) {
  419. node.removeChild(node.lastChild);
  420. }
  421. }
  422. function getStatIndex(pageNumber) {
  423. for (let i = 0, ii = stats.length; i < ii; ++i) {
  424. if (stats[i].pageNumber === pageNumber) {
  425. return i;
  426. }
  427. }
  428. return false;
  429. }
  430. return {
  431. id: "Stats",
  432. name: "Stats",
  433. panel: null,
  434. manager: null,
  435. init(pdfjsLib) {},
  436. enabled: false,
  437. active: false,
  438. add(pageNumber, stat) {
  439. if (!stat) {
  440. return;
  441. }
  442. const statsIndex = getStatIndex(pageNumber);
  443. if (statsIndex !== false) {
  444. const b = stats[statsIndex];
  445. this.panel.removeChild(b.div);
  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 (let i = 0, ii = stats.length; i < ii; ++i) {
  466. this.panel.appendChild(stats[i].div);
  467. }
  468. },
  469. cleanup() {
  470. stats = [];
  471. clear(this.panel);
  472. }
  473. };
  474. }();
  475. window.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 (let i = 0; i < tools.length; ++i) {
  485. const tool = tools[i];
  486. if (all || ids.includes(tool.id)) {
  487. tool.enabled = true;
  488. }
  489. }
  490. if (!all) {
  491. tools.sort(function (a, b) {
  492. let indexA = ids.indexOf(a.id);
  493. indexA = indexA < 0 ? tools.length : indexA;
  494. let indexB = ids.indexOf(b.id);
  495. indexB = indexB < 0 ? tools.length : indexB;
  496. return indexA - indexB;
  497. });
  498. }
  499. },
  500. init(pdfjsLib, container) {
  501. const ui = document.createElement("div");
  502. ui.id = "PDFBug";
  503. const controls = document.createElement("div");
  504. controls.setAttribute("class", "controls");
  505. ui.appendChild(controls);
  506. const panels = document.createElement("div");
  507. panels.setAttribute("class", "panels");
  508. ui.appendChild(panels);
  509. container.appendChild(ui);
  510. container.style.right = panelWidth + "px";
  511. const tools = this.tools;
  512. const self = this;
  513. for (let i = 0; i < tools.length; ++i) {
  514. const tool = tools[i];
  515. const panel = document.createElement("div");
  516. const panelButton = document.createElement("button");
  517. panelButton.textContent = tool.name;
  518. panelButton.addEventListener("click", function (selected) {
  519. return function (event) {
  520. event.preventDefault();
  521. self.selectPanel(selected);
  522. };
  523. }(i));
  524. controls.appendChild(panelButton);
  525. panels.appendChild(panel);
  526. tool.panel = panel;
  527. tool.manager = this;
  528. if (tool.enabled) {
  529. tool.init(pdfjsLib);
  530. } else {
  531. panel.textContent = tool.name + " is disabled. To enable add " + ' "' + tool.id + '" to the pdfBug parameter ' + "and refresh (separate multiple by commas).";
  532. }
  533. buttons.push(panelButton);
  534. }
  535. this.selectPanel(0);
  536. },
  537. cleanup() {
  538. for (let i = 0, ii = this.tools.length; i < ii; i++) {
  539. if (this.tools[i].enabled) {
  540. this.tools[i].cleanup();
  541. }
  542. }
  543. },
  544. selectPanel(index) {
  545. if (typeof index !== "number") {
  546. index = this.tools.indexOf(index);
  547. }
  548. if (index === activePanel) {
  549. return;
  550. }
  551. activePanel = index;
  552. const tools = this.tools;
  553. for (let j = 0; j < tools.length; ++j) {
  554. const isActive = j === index;
  555. buttons[j].classList.toggle("active", isActive);
  556. tools[j].active = isActive;
  557. tools[j].panel.hidden = !isActive;
  558. }
  559. }
  560. };
  561. }();