debugger.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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, createObjectURL;
  19. var active = false;
  20. var fontAttribute = 'data-font-name';
  21. function removeSelection() {
  22. var divs = document.querySelectorAll('div[' + fontAttribute + ']');
  23. for (var i = 0, ii = divs.length; i < ii; ++i) {
  24. var div = divs[i];
  25. div.className = '';
  26. }
  27. }
  28. function resetSelection() {
  29. var divs = document.querySelectorAll('div[' + fontAttribute + ']');
  30. for (var i = 0, ii = divs.length; i < ii; ++i) {
  31. var div = divs[i];
  32. div.className = 'debuggerHideText';
  33. }
  34. }
  35. function selectFont(fontName, show) {
  36. var divs = document.querySelectorAll('div[' + fontAttribute + '=' +
  37. fontName + ']');
  38. for (var i = 0, ii = divs.length; i < ii; ++i) {
  39. var div = divs[i];
  40. div.className = show ? 'debuggerShowText' : 'debuggerHideText';
  41. }
  42. }
  43. function textLayerClick(e) {
  44. if (!e.target.dataset.fontName ||
  45. e.target.tagName.toUpperCase() !== 'DIV') {
  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. panel.setAttribute('style', 'padding: 5px;');
  69. var tmp = document.createElement('button');
  70. tmp.addEventListener('click', resetSelection);
  71. tmp.textContent = 'Refresh';
  72. panel.appendChild(tmp);
  73. fonts = document.createElement('div');
  74. panel.appendChild(fonts);
  75. createObjectURL = pdfjsLib.createObjectURL;
  76. },
  77. cleanup: function cleanup() {
  78. fonts.textContent = '';
  79. },
  80. enabled: false,
  81. get active() {
  82. return active;
  83. },
  84. set active(value) {
  85. active = value;
  86. if (active) {
  87. document.body.addEventListener('click', textLayerClick, true);
  88. resetSelection();
  89. } else {
  90. document.body.removeEventListener('click', textLayerClick, true);
  91. removeSelection();
  92. }
  93. },
  94. // FontInspector specific functions.
  95. fontAdded: function fontAdded(fontObj, url) {
  96. function properties(obj, list) {
  97. var moreInfo = document.createElement('table');
  98. for (var i = 0; i < list.length; i++) {
  99. var tr = document.createElement('tr');
  100. var td1 = document.createElement('td');
  101. td1.textContent = list[i];
  102. tr.appendChild(td1);
  103. var td2 = document.createElement('td');
  104. td2.textContent = obj[list[i]].toString();
  105. tr.appendChild(td2);
  106. moreInfo.appendChild(tr);
  107. }
  108. return moreInfo;
  109. }
  110. var moreInfo = properties(fontObj, ['name', 'type']);
  111. var fontName = fontObj.loadedName;
  112. var font = document.createElement('div');
  113. var name = document.createElement('span');
  114. name.textContent = fontName;
  115. var download = document.createElement('a');
  116. if (url) {
  117. url = /url\(['"]?([^\)"']+)/.exec(url);
  118. download.href = url[1];
  119. } else if (fontObj.data) {
  120. download.href = createObjectURL(fontObj.data, fontObj.mimeType);
  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. var select = document.createElement('input');
  131. select.setAttribute('type', 'checkbox');
  132. select.dataset.fontName = fontName;
  133. select.addEventListener('click', (function(select, fontName) {
  134. return (function() {
  135. selectFont(fontName, select.checked);
  136. });
  137. })(select, fontName));
  138. font.appendChild(select);
  139. font.appendChild(name);
  140. font.appendChild(document.createTextNode(' '));
  141. font.appendChild(download);
  142. font.appendChild(document.createTextNode(' '));
  143. font.appendChild(logIt);
  144. font.appendChild(moreInfo);
  145. fonts.appendChild(font);
  146. // Somewhat of a hack, should probably add a hook for when the text layer
  147. // is done rendering.
  148. setTimeout(() => {
  149. if (this.active) {
  150. resetSelection();
  151. }
  152. }, 2000);
  153. },
  154. };
  155. })();
  156. var opMap;
  157. // Manages all the page steppers.
  158. var StepperManager = (function StepperManagerClosure() {
  159. var steppers = [];
  160. var stepperDiv = null;
  161. var stepperControls = null;
  162. var stepperChooser = null;
  163. var breakPoints = Object.create(null);
  164. return {
  165. // Properties/functions needed by PDFBug.
  166. id: 'Stepper',
  167. name: 'Stepper',
  168. panel: null,
  169. manager: null,
  170. init: function init(pdfjsLib) {
  171. var self = this;
  172. this.panel.setAttribute('style', 'padding: 5px;');
  173. stepperControls = document.createElement('div');
  174. stepperChooser = document.createElement('select');
  175. stepperChooser.addEventListener('change', function(event) {
  176. self.selectStepper(this.value);
  177. });
  178. stepperControls.appendChild(stepperChooser);
  179. stepperDiv = document.createElement('div');
  180. this.panel.appendChild(stepperControls);
  181. this.panel.appendChild(stepperDiv);
  182. if (sessionStorage.getItem('pdfjsBreakPoints')) {
  183. breakPoints = JSON.parse(sessionStorage.getItem('pdfjsBreakPoints'));
  184. }
  185. opMap = Object.create(null);
  186. for (var key in pdfjsLib.OPS) {
  187. opMap[pdfjsLib.OPS[key]] = key;
  188. }
  189. },
  190. cleanup: function cleanup() {
  191. stepperChooser.textContent = '';
  192. stepperDiv.textContent = '';
  193. steppers = [];
  194. },
  195. enabled: false,
  196. active: false,
  197. // Stepper specific functions.
  198. create: function create(pageIndex) {
  199. var debug = document.createElement('div');
  200. debug.id = 'stepper' + pageIndex;
  201. debug.setAttribute('hidden', true);
  202. debug.className = 'stepper';
  203. stepperDiv.appendChild(debug);
  204. var b = document.createElement('option');
  205. b.textContent = 'Page ' + (pageIndex + 1);
  206. b.value = pageIndex;
  207. stepperChooser.appendChild(b);
  208. var initBreakPoints = breakPoints[pageIndex] || [];
  209. var stepper = new Stepper(debug, pageIndex, initBreakPoints);
  210. steppers.push(stepper);
  211. if (steppers.length === 1) {
  212. this.selectStepper(pageIndex, false);
  213. }
  214. return stepper;
  215. },
  216. selectStepper: function selectStepper(pageIndex, selectPanel) {
  217. var i;
  218. pageIndex = pageIndex | 0;
  219. if (selectPanel) {
  220. this.manager.selectPanel(this);
  221. }
  222. for (i = 0; i < steppers.length; ++i) {
  223. var stepper = steppers[i];
  224. if (stepper.pageIndex === pageIndex) {
  225. stepper.panel.removeAttribute('hidden');
  226. } else {
  227. stepper.panel.setAttribute('hidden', true);
  228. }
  229. }
  230. var options = stepperChooser.options;
  231. for (i = 0; i < options.length; ++i) {
  232. var option = options[i];
  233. option.selected = (option.value | 0) === pageIndex;
  234. }
  235. },
  236. saveBreakPoints: function saveBreakPoints(pageIndex, bps) {
  237. breakPoints[pageIndex] = bps;
  238. sessionStorage.setItem('pdfjsBreakPoints', JSON.stringify(breakPoints));
  239. },
  240. };
  241. })();
  242. // The stepper for each page's IRQueue.
  243. var Stepper = (function StepperClosure() {
  244. // Shorter way to create element and optionally set textContent.
  245. function c(tag, textContent) {
  246. var d = document.createElement(tag);
  247. if (textContent) {
  248. d.textContent = textContent;
  249. }
  250. return d;
  251. }
  252. function simplifyArgs(args) {
  253. if (typeof args === 'string') {
  254. var MAX_STRING_LENGTH = 75;
  255. return args.length <= MAX_STRING_LENGTH ? args :
  256. args.substring(0, MAX_STRING_LENGTH) + '...';
  257. }
  258. if (typeof args !== 'object' || args === null) {
  259. return args;
  260. }
  261. if ('length' in args) { // array
  262. var simpleArgs = [], i, ii;
  263. var MAX_ITEMS = 10;
  264. for (i = 0, ii = Math.min(MAX_ITEMS, args.length); i < ii; i++) {
  265. simpleArgs.push(simplifyArgs(args[i]));
  266. }
  267. if (i < args.length) {
  268. simpleArgs.push('...');
  269. }
  270. return simpleArgs;
  271. }
  272. var simpleObj = {};
  273. for (var key in args) {
  274. simpleObj[key] = simplifyArgs(args[key]);
  275. }
  276. return simpleObj;
  277. }
  278. function Stepper(panel, pageIndex, initialBreakPoints) {
  279. this.panel = panel;
  280. this.breakPoint = 0;
  281. this.nextBreakPoint = null;
  282. this.pageIndex = pageIndex;
  283. this.breakPoints = initialBreakPoints;
  284. this.currentIdx = -1;
  285. this.operatorListIdx = 0;
  286. }
  287. Stepper.prototype = {
  288. init: function init(operatorList) {
  289. var panel = this.panel;
  290. var content = c('div', 'c=continue, s=step');
  291. var table = c('table');
  292. content.appendChild(table);
  293. table.cellSpacing = 0;
  294. var headerRow = c('tr');
  295. table.appendChild(headerRow);
  296. headerRow.appendChild(c('th', 'Break'));
  297. headerRow.appendChild(c('th', 'Idx'));
  298. headerRow.appendChild(c('th', 'fn'));
  299. headerRow.appendChild(c('th', 'args'));
  300. panel.appendChild(content);
  301. this.table = table;
  302. this.updateOperatorList(operatorList);
  303. },
  304. updateOperatorList: function updateOperatorList(operatorList) {
  305. var self = this;
  306. function cboxOnClick() {
  307. var x = +this.dataset.idx;
  308. if (this.checked) {
  309. self.breakPoints.push(x);
  310. } else {
  311. self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
  312. }
  313. StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
  314. }
  315. var MAX_OPERATORS_COUNT = 15000;
  316. if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
  317. return;
  318. }
  319. var chunk = document.createDocumentFragment();
  320. var operatorsToDisplay = Math.min(MAX_OPERATORS_COUNT,
  321. operatorList.fnArray.length);
  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. line = c('tr');
  367. var lastCell = c('td', '...');
  368. lastCell.colspan = 4;
  369. chunk.appendChild(lastCell);
  370. }
  371. this.operatorListIdx = operatorList.fnArray.length;
  372. this.table.appendChild(chunk);
  373. },
  374. getNextBreakPoint: function getNextBreakPoint() {
  375. this.breakPoints.sort(function(a, b) {
  376. return a - b;
  377. });
  378. for (var i = 0; i < this.breakPoints.length; i++) {
  379. if (this.breakPoints[i] > this.currentIdx) {
  380. return this.breakPoints[i];
  381. }
  382. }
  383. return null;
  384. },
  385. breakIt: function breakIt(idx, callback) {
  386. StepperManager.selectStepper(this.pageIndex, true);
  387. var self = this;
  388. var dom = document;
  389. self.currentIdx = idx;
  390. var listener = function(e) {
  391. switch (e.keyCode) {
  392. case 83: // step
  393. dom.removeEventListener('keydown', listener);
  394. self.nextBreakPoint = self.currentIdx + 1;
  395. self.goTo(-1);
  396. callback();
  397. break;
  398. case 67: // continue
  399. dom.removeEventListener('keydown', listener);
  400. var breakPoint = self.getNextBreakPoint();
  401. self.nextBreakPoint = breakPoint;
  402. self.goTo(-1);
  403. callback();
  404. break;
  405. }
  406. };
  407. dom.addEventListener('keydown', listener);
  408. self.goTo(idx);
  409. },
  410. goTo: function goTo(idx) {
  411. var allRows = this.panel.getElementsByClassName('line');
  412. for (var x = 0, xx = allRows.length; x < xx; ++x) {
  413. var row = allRows[x];
  414. if ((row.dataset.idx | 0) === idx) {
  415. row.style.backgroundColor = 'rgb(251,250,207)';
  416. row.scrollIntoView();
  417. } else {
  418. row.style.backgroundColor = null;
  419. }
  420. }
  421. },
  422. };
  423. return Stepper;
  424. })();
  425. var Stats = (function Stats() {
  426. var stats = [];
  427. function clear(node) {
  428. while (node.hasChildNodes()) {
  429. node.removeChild(node.lastChild);
  430. }
  431. }
  432. function getStatIndex(pageNumber) {
  433. for (var i = 0, ii = stats.length; i < ii; ++i) {
  434. if (stats[i].pageNumber === pageNumber) {
  435. return i;
  436. }
  437. }
  438. return false;
  439. }
  440. return {
  441. // Properties/functions needed by PDFBug.
  442. id: 'Stats',
  443. name: 'Stats',
  444. panel: null,
  445. manager: null,
  446. init(pdfjsLib) {
  447. this.panel.setAttribute('style', 'padding: 5px;');
  448. },
  449. enabled: false,
  450. active: false,
  451. // Stats specific functions.
  452. add(pageNumber, stat) {
  453. if (!stat) {
  454. return;
  455. }
  456. var statsIndex = getStatIndex(pageNumber);
  457. if (statsIndex !== false) {
  458. var b = stats[statsIndex];
  459. this.panel.removeChild(b.div);
  460. stats.splice(statsIndex, 1);
  461. }
  462. var wrapper = document.createElement('div');
  463. wrapper.className = 'stats';
  464. var title = document.createElement('div');
  465. title.className = 'title';
  466. title.textContent = 'Page: ' + pageNumber;
  467. var statsDiv = document.createElement('div');
  468. statsDiv.textContent = stat.toString();
  469. wrapper.appendChild(title);
  470. wrapper.appendChild(statsDiv);
  471. stats.push({ pageNumber, div: wrapper, });
  472. stats.sort(function(a, b) {
  473. return a.pageNumber - b.pageNumber;
  474. });
  475. clear(this.panel);
  476. for (var i = 0, ii = stats.length; i < ii; ++i) {
  477. this.panel.appendChild(stats[i].div);
  478. }
  479. },
  480. cleanup() {
  481. stats = [];
  482. clear(this.panel);
  483. },
  484. };
  485. })();
  486. // Manages all the debugging tools.
  487. window.PDFBug = (function PDFBugClosure() {
  488. var panelWidth = 300;
  489. var buttons = [];
  490. var activePanel = null;
  491. return {
  492. tools: [
  493. FontInspector,
  494. StepperManager,
  495. Stats
  496. ],
  497. enable(ids) {
  498. var all = false, tools = this.tools;
  499. if (ids.length === 1 && ids[0] === 'all') {
  500. all = true;
  501. }
  502. for (var i = 0; i < tools.length; ++i) {
  503. var tool = tools[i];
  504. if (all || ids.includes(tool.id)) {
  505. tool.enabled = true;
  506. }
  507. }
  508. if (!all) {
  509. // Sort the tools by the order they are enabled.
  510. tools.sort(function(a, b) {
  511. var indexA = ids.indexOf(a.id);
  512. indexA = indexA < 0 ? tools.length : indexA;
  513. var indexB = ids.indexOf(b.id);
  514. indexB = indexB < 0 ? tools.length : indexB;
  515. return indexA - indexB;
  516. });
  517. }
  518. },
  519. init(pdfjsLib, container) {
  520. /*
  521. * Basic Layout:
  522. * PDFBug
  523. * Controls
  524. * Panels
  525. * Panel
  526. * Panel
  527. * ...
  528. */
  529. var ui = document.createElement('div');
  530. ui.id = 'PDFBug';
  531. var controls = document.createElement('div');
  532. controls.setAttribute('class', 'controls');
  533. ui.appendChild(controls);
  534. var panels = document.createElement('div');
  535. panels.setAttribute('class', 'panels');
  536. ui.appendChild(panels);
  537. container.appendChild(ui);
  538. container.style.right = panelWidth + 'px';
  539. // Initialize all the debugging tools.
  540. var tools = this.tools;
  541. var self = this;
  542. for (var i = 0; i < tools.length; ++i) {
  543. var tool = tools[i];
  544. var panel = document.createElement('div');
  545. var panelButton = document.createElement('button');
  546. panelButton.textContent = tool.name;
  547. panelButton.addEventListener('click', (function(selected) {
  548. return function(event) {
  549. event.preventDefault();
  550. self.selectPanel(selected);
  551. };
  552. })(i));
  553. controls.appendChild(panelButton);
  554. panels.appendChild(panel);
  555. tool.panel = panel;
  556. tool.manager = this;
  557. if (tool.enabled) {
  558. tool.init(pdfjsLib);
  559. } else {
  560. panel.textContent = tool.name + ' is disabled. To enable add ' +
  561. ' "' + tool.id + '" to the pdfBug parameter ' +
  562. 'and refresh (separate multiple by commas).';
  563. }
  564. buttons.push(panelButton);
  565. }
  566. this.selectPanel(0);
  567. },
  568. cleanup() {
  569. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  570. if (this.tools[i].enabled) {
  571. this.tools[i].cleanup();
  572. }
  573. }
  574. },
  575. selectPanel(index) {
  576. if (typeof index !== 'number') {
  577. index = this.tools.indexOf(index);
  578. }
  579. if (index === activePanel) {
  580. return;
  581. }
  582. activePanel = index;
  583. var tools = this.tools;
  584. for (var j = 0; j < tools.length; ++j) {
  585. if (j === index) {
  586. buttons[j].setAttribute('class', 'active');
  587. tools[j].active = true;
  588. tools[j].panel.removeAttribute('hidden');
  589. } else {
  590. buttons[j].setAttribute('class', '');
  591. tools[j].active = false;
  592. tools[j].panel.setAttribute('hidden', 'true');
  593. }
  594. }
  595. },
  596. };
  597. })();