debugger.js 18 KB

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