debugger.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. var fonts;
  25. var active = false;
  26. var 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. var fontName = e.target.dataset.fontName;
  50. var selects = document.getElementsByTagName("input");
  51. for (var i = 0; i < selects.length; ++i) {
  52. var 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. 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. fontAdded: function fontAdded(fontObj, url) {
  93. function properties(obj, list) {
  94. var moreInfo = document.createElement("table");
  95. for (var i = 0; i < list.length; i++) {
  96. var tr = document.createElement("tr");
  97. var td1 = document.createElement("td");
  98. td1.textContent = list[i];
  99. tr.appendChild(td1);
  100. var 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. var moreInfo = properties(fontObj, ["name", "type"]);
  108. const fontName = fontObj.loadedName;
  109. var font = document.createElement("div");
  110. var name = document.createElement("span");
  111. name.textContent = fontName;
  112. var 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. var 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. var opMap;
  152. var StepperManager = function StepperManagerClosure() {
  153. var steppers = [];
  154. var stepperDiv = null;
  155. var stepperControls = null;
  156. var stepperChooser = null;
  157. var breakPoints = Object.create(null);
  158. return {
  159. id: "Stepper",
  160. name: "Stepper",
  161. panel: null,
  162. manager: null,
  163. init: function init(pdfjsLib) {
  164. var 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 (var 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. var debug = document.createElement("div");
  191. debug.id = "stepper" + pageIndex;
  192. debug.hidden = true;
  193. debug.className = "stepper";
  194. stepperDiv.appendChild(debug);
  195. var b = document.createElement("option");
  196. b.textContent = "Page " + (pageIndex + 1);
  197. b.value = pageIndex;
  198. stepperChooser.appendChild(b);
  199. var initBreakPoints = breakPoints[pageIndex] || [];
  200. var 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. var i;
  209. pageIndex = pageIndex | 0;
  210. if (selectPanel) {
  211. this.manager.selectPanel(this);
  212. }
  213. for (i = 0; i < steppers.length; ++i) {
  214. var stepper = steppers[i];
  215. stepper.panel.hidden = stepper.pageIndex !== pageIndex;
  216. }
  217. var options = stepperChooser.options;
  218. for (i = 0; i < options.length; ++i) {
  219. var 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. var Stepper = function StepperClosure() {
  230. function c(tag, textContent) {
  231. var 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. var 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. var simpleArgs = [],
  247. i,
  248. ii;
  249. var MAX_ITEMS = 10;
  250. for (i = 0, ii = Math.min(MAX_ITEMS, args.length); i < ii; i++) {
  251. simpleArgs.push(simplifyArgs(args[i]));
  252. }
  253. if (i < args.length) {
  254. simpleArgs.push("...");
  255. }
  256. return simpleArgs;
  257. }
  258. var simpleObj = {};
  259. for (var key in args) {
  260. simpleObj[key] = simplifyArgs(args[key]);
  261. }
  262. return simpleObj;
  263. }
  264. function Stepper(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. Stepper.prototype = {
  274. init: function init(operatorList) {
  275. var panel = this.panel;
  276. var content = c("div", "c=continue, s=step");
  277. var table = c("table");
  278. content.appendChild(table);
  279. table.cellSpacing = 0;
  280. var 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: function updateOperatorList(operatorList) {
  291. var self = this;
  292. function cboxOnClick() {
  293. var 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. var MAX_OPERATORS_COUNT = 15000;
  302. if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
  303. return;
  304. }
  305. var chunk = document.createDocumentFragment();
  306. var operatorsToDisplay = Math.min(MAX_OPERATORS_COUNT, operatorList.fnArray.length);
  307. for (var i = this.operatorListIdx; i < operatorsToDisplay; i++) {
  308. var line = c("tr");
  309. line.className = "line";
  310. line.dataset.idx = i;
  311. chunk.appendChild(line);
  312. var checked = this.breakPoints.includes(i);
  313. var args = operatorList.argsArray[i] || [];
  314. var breakCell = c("td");
  315. var 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. var fn = opMap[operatorList.fnArray[i]];
  325. var decArgs = args;
  326. if (fn === "showText") {
  327. var glyphs = args[0];
  328. var newArgs = [];
  329. var str = [];
  330. for (var j = 0; j < glyphs.length; j++) {
  331. var glyph = glyphs[j];
  332. if (typeof glyph === "object" && glyph !== null) {
  333. str.push(glyph.fontChar);
  334. } else {
  335. if (str.length > 0) {
  336. newArgs.push(str.join(""));
  337. str = [];
  338. }
  339. newArgs.push(glyph);
  340. }
  341. }
  342. if (str.length > 0) {
  343. newArgs.push(str.join(""));
  344. }
  345. decArgs = [newArgs];
  346. }
  347. line.appendChild(c("td", fn));
  348. line.appendChild(c("td", JSON.stringify(simplifyArgs(decArgs))));
  349. }
  350. if (operatorsToDisplay < operatorList.fnArray.length) {
  351. var lastCell = c("td", "...");
  352. lastCell.colspan = 4;
  353. chunk.appendChild(lastCell);
  354. }
  355. this.operatorListIdx = operatorList.fnArray.length;
  356. this.table.appendChild(chunk);
  357. },
  358. getNextBreakPoint: function getNextBreakPoint() {
  359. this.breakPoints.sort(function (a, b) {
  360. return a - b;
  361. });
  362. for (var i = 0; i < this.breakPoints.length; i++) {
  363. if (this.breakPoints[i] > this.currentIdx) {
  364. return this.breakPoints[i];
  365. }
  366. }
  367. return null;
  368. },
  369. breakIt: function breakIt(idx, callback) {
  370. StepperManager.selectStepper(this.pageIndex, true);
  371. var self = this;
  372. var dom = document;
  373. self.currentIdx = idx;
  374. var listener = function (e) {
  375. switch (e.keyCode) {
  376. case 83:
  377. dom.removeEventListener("keydown", listener);
  378. self.nextBreakPoint = self.currentIdx + 1;
  379. self.goTo(-1);
  380. callback();
  381. break;
  382. case 67:
  383. dom.removeEventListener("keydown", listener);
  384. var breakPoint = self.getNextBreakPoint();
  385. self.nextBreakPoint = breakPoint;
  386. self.goTo(-1);
  387. callback();
  388. break;
  389. }
  390. };
  391. dom.addEventListener("keydown", listener);
  392. self.goTo(idx);
  393. },
  394. goTo: function goTo(idx) {
  395. var allRows = this.panel.getElementsByClassName("line");
  396. for (var x = 0, xx = allRows.length; x < xx; ++x) {
  397. var row = allRows[x];
  398. if ((row.dataset.idx | 0) === idx) {
  399. row.style.backgroundColor = "rgb(251,250,207)";
  400. row.scrollIntoView();
  401. } else {
  402. row.style.backgroundColor = null;
  403. }
  404. }
  405. }
  406. };
  407. return Stepper;
  408. }();
  409. var Stats = function Stats() {
  410. var stats = [];
  411. function clear(node) {
  412. while (node.hasChildNodes()) {
  413. node.removeChild(node.lastChild);
  414. }
  415. }
  416. function getStatIndex(pageNumber) {
  417. for (var i = 0, ii = stats.length; i < ii; ++i) {
  418. if (stats[i].pageNumber === pageNumber) {
  419. return i;
  420. }
  421. }
  422. return false;
  423. }
  424. return {
  425. id: "Stats",
  426. name: "Stats",
  427. panel: null,
  428. manager: null,
  429. init(pdfjsLib) {},
  430. enabled: false,
  431. active: false,
  432. add(pageNumber, stat) {
  433. if (!stat) {
  434. return;
  435. }
  436. var statsIndex = getStatIndex(pageNumber);
  437. if (statsIndex !== false) {
  438. const b = stats[statsIndex];
  439. this.panel.removeChild(b.div);
  440. stats.splice(statsIndex, 1);
  441. }
  442. var wrapper = document.createElement("div");
  443. wrapper.className = "stats";
  444. var title = document.createElement("div");
  445. title.className = "title";
  446. title.textContent = "Page: " + pageNumber;
  447. var statsDiv = document.createElement("div");
  448. statsDiv.textContent = stat.toString();
  449. wrapper.appendChild(title);
  450. wrapper.appendChild(statsDiv);
  451. stats.push({
  452. pageNumber,
  453. div: wrapper
  454. });
  455. stats.sort(function (a, b) {
  456. return a.pageNumber - b.pageNumber;
  457. });
  458. clear(this.panel);
  459. for (var i = 0, ii = stats.length; i < ii; ++i) {
  460. this.panel.appendChild(stats[i].div);
  461. }
  462. },
  463. cleanup() {
  464. stats = [];
  465. clear(this.panel);
  466. }
  467. };
  468. }();
  469. window.PDFBug = function PDFBugClosure() {
  470. var panelWidth = 300;
  471. var buttons = [];
  472. var activePanel = null;
  473. return {
  474. tools: [FontInspector, StepperManager, Stats],
  475. enable(ids) {
  476. var all = false,
  477. tools = this.tools;
  478. if (ids.length === 1 && ids[0] === "all") {
  479. all = true;
  480. }
  481. for (var i = 0; i < tools.length; ++i) {
  482. var tool = tools[i];
  483. if (all || ids.includes(tool.id)) {
  484. tool.enabled = true;
  485. }
  486. }
  487. if (!all) {
  488. tools.sort(function (a, b) {
  489. var indexA = ids.indexOf(a.id);
  490. indexA = indexA < 0 ? tools.length : indexA;
  491. var indexB = ids.indexOf(b.id);
  492. indexB = indexB < 0 ? tools.length : indexB;
  493. return indexA - indexB;
  494. });
  495. }
  496. },
  497. init(pdfjsLib, container) {
  498. var ui = document.createElement("div");
  499. ui.id = "PDFBug";
  500. var controls = document.createElement("div");
  501. controls.setAttribute("class", "controls");
  502. ui.appendChild(controls);
  503. var panels = document.createElement("div");
  504. panels.setAttribute("class", "panels");
  505. ui.appendChild(panels);
  506. container.appendChild(ui);
  507. container.style.right = panelWidth + "px";
  508. var tools = this.tools;
  509. var self = this;
  510. for (var i = 0; i < tools.length; ++i) {
  511. var tool = tools[i];
  512. var panel = document.createElement("div");
  513. var panelButton = document.createElement("button");
  514. panelButton.textContent = tool.name;
  515. panelButton.addEventListener("click", function (selected) {
  516. return function (event) {
  517. event.preventDefault();
  518. self.selectPanel(selected);
  519. };
  520. }(i));
  521. controls.appendChild(panelButton);
  522. panels.appendChild(panel);
  523. tool.panel = panel;
  524. tool.manager = this;
  525. if (tool.enabled) {
  526. tool.init(pdfjsLib);
  527. } else {
  528. panel.textContent = tool.name + " is disabled. To enable add " + ' "' + tool.id + '" to the pdfBug parameter ' + "and refresh (separate multiple by commas).";
  529. }
  530. buttons.push(panelButton);
  531. }
  532. this.selectPanel(0);
  533. },
  534. cleanup() {
  535. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  536. if (this.tools[i].enabled) {
  537. this.tools[i].cleanup();
  538. }
  539. }
  540. },
  541. selectPanel(index) {
  542. if (typeof index !== "number") {
  543. index = this.tools.indexOf(index);
  544. }
  545. if (index === activePanel) {
  546. return;
  547. }
  548. activePanel = index;
  549. var tools = this.tools;
  550. for (var j = 0; j < tools.length; ++j) {
  551. var isActive = j === index;
  552. buttons[j].classList.toggle("active", isActive);
  553. tools[j].active = isActive;
  554. tools[j].panel.hidden = !isActive;
  555. }
  556. }
  557. };
  558. }();