debugger.js 18 KB

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