debugger.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. function Stepper(panel, pageIndex, initialBreakPoints) {
  280. this.panel = panel;
  281. this.breakPoint = 0;
  282. this.nextBreakPoint = null;
  283. this.pageIndex = pageIndex;
  284. this.breakPoints = initialBreakPoints;
  285. this.currentIdx = -1;
  286. this.operatorListIdx = 0;
  287. }
  288. Stepper.prototype = {
  289. init: function init(operatorList) {
  290. var panel = this.panel;
  291. var content = c("div", "c=continue, s=step");
  292. var table = c("table");
  293. content.appendChild(table);
  294. table.cellSpacing = 0;
  295. var headerRow = c("tr");
  296. table.appendChild(headerRow);
  297. headerRow.appendChild(c("th", "Break"));
  298. headerRow.appendChild(c("th", "Idx"));
  299. headerRow.appendChild(c("th", "fn"));
  300. headerRow.appendChild(c("th", "args"));
  301. panel.appendChild(content);
  302. this.table = table;
  303. this.updateOperatorList(operatorList);
  304. },
  305. updateOperatorList: function updateOperatorList(operatorList) {
  306. var self = this;
  307. function cboxOnClick() {
  308. var x = +this.dataset.idx;
  309. if (this.checked) {
  310. self.breakPoints.push(x);
  311. } else {
  312. self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
  313. }
  314. StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
  315. }
  316. var MAX_OPERATORS_COUNT = 15000;
  317. if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
  318. return;
  319. }
  320. var chunk = document.createDocumentFragment();
  321. var operatorsToDisplay = Math.min(
  322. MAX_OPERATORS_COUNT,
  323. operatorList.fnArray.length
  324. );
  325. for (var i = this.operatorListIdx; i < operatorsToDisplay; i++) {
  326. var line = c("tr");
  327. line.className = "line";
  328. line.dataset.idx = i;
  329. chunk.appendChild(line);
  330. var checked = this.breakPoints.includes(i);
  331. var args = operatorList.argsArray[i] || [];
  332. var breakCell = c("td");
  333. var cbox = c("input");
  334. cbox.type = "checkbox";
  335. cbox.className = "points";
  336. cbox.checked = checked;
  337. cbox.dataset.idx = i;
  338. cbox.onclick = cboxOnClick;
  339. breakCell.appendChild(cbox);
  340. line.appendChild(breakCell);
  341. line.appendChild(c("td", i.toString()));
  342. var fn = opMap[operatorList.fnArray[i]];
  343. var decArgs = args;
  344. if (fn === "showText") {
  345. var glyphs = args[0];
  346. var newArgs = [];
  347. var str = [];
  348. for (var j = 0; j < glyphs.length; j++) {
  349. var glyph = glyphs[j];
  350. if (typeof glyph === "object" && glyph !== null) {
  351. str.push(glyph.fontChar);
  352. } else {
  353. if (str.length > 0) {
  354. newArgs.push(str.join(""));
  355. str = [];
  356. }
  357. newArgs.push(glyph); // null or number
  358. }
  359. }
  360. if (str.length > 0) {
  361. newArgs.push(str.join(""));
  362. }
  363. decArgs = [newArgs];
  364. }
  365. line.appendChild(c("td", fn));
  366. line.appendChild(c("td", JSON.stringify(simplifyArgs(decArgs))));
  367. }
  368. if (operatorsToDisplay < operatorList.fnArray.length) {
  369. line = c("tr");
  370. var lastCell = c("td", "...");
  371. lastCell.colspan = 4;
  372. chunk.appendChild(lastCell);
  373. }
  374. this.operatorListIdx = operatorList.fnArray.length;
  375. this.table.appendChild(chunk);
  376. },
  377. getNextBreakPoint: function getNextBreakPoint() {
  378. this.breakPoints.sort(function(a, b) {
  379. return a - b;
  380. });
  381. for (var i = 0; i < this.breakPoints.length; i++) {
  382. if (this.breakPoints[i] > this.currentIdx) {
  383. return this.breakPoints[i];
  384. }
  385. }
  386. return null;
  387. },
  388. breakIt: function breakIt(idx, callback) {
  389. StepperManager.selectStepper(this.pageIndex, true);
  390. var self = this;
  391. var dom = document;
  392. self.currentIdx = idx;
  393. var listener = function(e) {
  394. switch (e.keyCode) {
  395. case 83: // step
  396. dom.removeEventListener("keydown", listener);
  397. self.nextBreakPoint = self.currentIdx + 1;
  398. self.goTo(-1);
  399. callback();
  400. break;
  401. case 67: // continue
  402. dom.removeEventListener("keydown", listener);
  403. var breakPoint = self.getNextBreakPoint();
  404. self.nextBreakPoint = breakPoint;
  405. self.goTo(-1);
  406. callback();
  407. break;
  408. }
  409. };
  410. dom.addEventListener("keydown", listener);
  411. self.goTo(idx);
  412. },
  413. goTo: function goTo(idx) {
  414. var allRows = this.panel.getElementsByClassName("line");
  415. for (var x = 0, xx = allRows.length; x < xx; ++x) {
  416. var row = allRows[x];
  417. if ((row.dataset.idx | 0) === idx) {
  418. row.style.backgroundColor = "rgb(251,250,207)";
  419. row.scrollIntoView();
  420. } else {
  421. row.style.backgroundColor = null;
  422. }
  423. }
  424. },
  425. };
  426. return Stepper;
  427. })();
  428. var Stats = (function Stats() {
  429. var stats = [];
  430. function clear(node) {
  431. while (node.hasChildNodes()) {
  432. node.removeChild(node.lastChild);
  433. }
  434. }
  435. function getStatIndex(pageNumber) {
  436. for (var i = 0, ii = stats.length; i < ii; ++i) {
  437. if (stats[i].pageNumber === pageNumber) {
  438. return i;
  439. }
  440. }
  441. return false;
  442. }
  443. return {
  444. // Properties/functions needed by PDFBug.
  445. id: "Stats",
  446. name: "Stats",
  447. panel: null,
  448. manager: null,
  449. init(pdfjsLib) {},
  450. enabled: false,
  451. active: false,
  452. // Stats specific functions.
  453. add(pageNumber, stat) {
  454. if (!stat) {
  455. return;
  456. }
  457. var statsIndex = getStatIndex(pageNumber);
  458. if (statsIndex !== false) {
  459. const b = stats[statsIndex];
  460. this.panel.removeChild(b.div);
  461. stats.splice(statsIndex, 1);
  462. }
  463. var wrapper = document.createElement("div");
  464. wrapper.className = "stats";
  465. var title = document.createElement("div");
  466. title.className = "title";
  467. title.textContent = "Page: " + pageNumber;
  468. var statsDiv = document.createElement("div");
  469. statsDiv.textContent = stat.toString();
  470. wrapper.appendChild(title);
  471. wrapper.appendChild(statsDiv);
  472. stats.push({ pageNumber, div: wrapper });
  473. stats.sort(function(a, b) {
  474. return a.pageNumber - b.pageNumber;
  475. });
  476. clear(this.panel);
  477. for (var i = 0, ii = stats.length; i < ii; ++i) {
  478. this.panel.appendChild(stats[i].div);
  479. }
  480. },
  481. cleanup() {
  482. stats = [];
  483. clear(this.panel);
  484. },
  485. };
  486. })();
  487. // Manages all the debugging tools.
  488. window.PDFBug = (function PDFBugClosure() {
  489. var panelWidth = 300;
  490. var buttons = [];
  491. var activePanel = null;
  492. return {
  493. tools: [FontInspector, StepperManager, Stats],
  494. enable(ids) {
  495. var all = false,
  496. tools = this.tools;
  497. if (ids.length === 1 && ids[0] === "all") {
  498. all = true;
  499. }
  500. for (var i = 0; i < tools.length; ++i) {
  501. var tool = tools[i];
  502. if (all || ids.includes(tool.id)) {
  503. tool.enabled = true;
  504. }
  505. }
  506. if (!all) {
  507. // Sort the tools by the order they are enabled.
  508. tools.sort(function(a, b) {
  509. var indexA = ids.indexOf(a.id);
  510. indexA = indexA < 0 ? tools.length : indexA;
  511. var indexB = ids.indexOf(b.id);
  512. indexB = indexB < 0 ? tools.length : indexB;
  513. return indexA - indexB;
  514. });
  515. }
  516. },
  517. init(pdfjsLib, container) {
  518. /*
  519. * Basic Layout:
  520. * PDFBug
  521. * Controls
  522. * Panels
  523. * Panel
  524. * Panel
  525. * ...
  526. */
  527. var ui = document.createElement("div");
  528. ui.id = "PDFBug";
  529. var controls = document.createElement("div");
  530. controls.setAttribute("class", "controls");
  531. ui.appendChild(controls);
  532. var panels = document.createElement("div");
  533. panels.setAttribute("class", "panels");
  534. ui.appendChild(panels);
  535. container.appendChild(ui);
  536. container.style.right = panelWidth + "px";
  537. // Initialize all the debugging tools.
  538. var tools = this.tools;
  539. var self = this;
  540. for (var i = 0; i < tools.length; ++i) {
  541. var tool = tools[i];
  542. var panel = document.createElement("div");
  543. var panelButton = document.createElement("button");
  544. panelButton.textContent = tool.name;
  545. panelButton.addEventListener(
  546. "click",
  547. (function(selected) {
  548. return function(event) {
  549. event.preventDefault();
  550. self.selectPanel(selected);
  551. };
  552. })(i)
  553. );
  554. controls.appendChild(panelButton);
  555. panels.appendChild(panel);
  556. tool.panel = panel;
  557. tool.manager = this;
  558. if (tool.enabled) {
  559. tool.init(pdfjsLib);
  560. } else {
  561. panel.textContent =
  562. tool.name +
  563. " is disabled. To enable add " +
  564. ' "' +
  565. tool.id +
  566. '" to the pdfBug parameter ' +
  567. "and refresh (separate multiple by commas).";
  568. }
  569. buttons.push(panelButton);
  570. }
  571. this.selectPanel(0);
  572. },
  573. cleanup() {
  574. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  575. if (this.tools[i].enabled) {
  576. this.tools[i].cleanup();
  577. }
  578. }
  579. },
  580. selectPanel(index) {
  581. if (typeof index !== "number") {
  582. index = this.tools.indexOf(index);
  583. }
  584. if (index === activePanel) {
  585. return;
  586. }
  587. activePanel = index;
  588. var tools = this.tools;
  589. for (var j = 0; j < tools.length; ++j) {
  590. if (j === index) {
  591. buttons[j].setAttribute("class", "active");
  592. tools[j].active = true;
  593. tools[j].panel.removeAttribute("hidden");
  594. } else {
  595. buttons[j].setAttribute("class", "");
  596. tools[j].active = false;
  597. tools[j].panel.setAttribute("hidden", "true");
  598. }
  599. }
  600. },
  601. };
  602. })();