debugger.js 19 KB

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