debugger.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. 'use strict';
  16. var FontInspector = (function FontInspectorClosure() {
  17. var fonts;
  18. var active = false;
  19. var fontAttribute = 'data-font-name';
  20. function removeSelection() {
  21. var divs = document.querySelectorAll('div[' + fontAttribute + ']');
  22. for (var i = 0, ii = divs.length; i < ii; ++i) {
  23. var div = divs[i];
  24. div.className = '';
  25. }
  26. }
  27. function resetSelection() {
  28. var divs = document.querySelectorAll('div[' + fontAttribute + ']');
  29. for (var i = 0, ii = divs.length; i < ii; ++i) {
  30. var div = divs[i];
  31. div.className = 'debuggerHideText';
  32. }
  33. }
  34. function selectFont(fontName, show) {
  35. var divs = document.querySelectorAll('div[' + fontAttribute + '=' +
  36. fontName + ']');
  37. for (var i = 0, ii = divs.length; i < ii; ++i) {
  38. var div = divs[i];
  39. div.className = show ? 'debuggerShowText' : 'debuggerHideText';
  40. }
  41. }
  42. function textLayerClick(e) {
  43. if (!e.target.dataset.fontName ||
  44. e.target.tagName.toUpperCase() !== 'DIV') {
  45. return;
  46. }
  47. var fontName = e.target.dataset.fontName;
  48. var selects = document.getElementsByTagName('input');
  49. for (var i = 0; i < selects.length; ++i) {
  50. var select = selects[i];
  51. if (select.dataset.fontName !== fontName) {
  52. continue;
  53. }
  54. select.checked = !select.checked;
  55. selectFont(fontName, select.checked);
  56. select.scrollIntoView();
  57. }
  58. }
  59. return {
  60. // Properties/functions needed by PDFBug.
  61. id: 'FontInspector',
  62. name: 'Font Inspector',
  63. panel: null,
  64. manager: null,
  65. init: function init(pdfjsLib) {
  66. var panel = this.panel;
  67. panel.setAttribute('style', 'padding: 5px;');
  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. var 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. url = URL.createObjectURL(new Blob([fontObj.data], {
  119. type: fontObj.mimeType,
  120. }));
  121. download.href = url;
  122. }
  123. download.textContent = 'Download';
  124. var logIt = document.createElement('a');
  125. logIt.href = '';
  126. logIt.textContent = 'Log';
  127. logIt.addEventListener('click', function(event) {
  128. event.preventDefault();
  129. console.log(fontObj);
  130. });
  131. var select = document.createElement('input');
  132. select.setAttribute('type', 'checkbox');
  133. select.dataset.fontName = fontName;
  134. select.addEventListener('click', (function(select, fontName) {
  135. return (function() {
  136. selectFont(fontName, select.checked);
  137. });
  138. })(select, fontName));
  139. font.appendChild(select);
  140. font.appendChild(name);
  141. font.appendChild(document.createTextNode(' '));
  142. font.appendChild(download);
  143. font.appendChild(document.createTextNode(' '));
  144. font.appendChild(logIt);
  145. font.appendChild(moreInfo);
  146. fonts.appendChild(font);
  147. // Somewhat of a hack, should probably add a hook for when the text layer
  148. // is done rendering.
  149. setTimeout(() => {
  150. if (this.active) {
  151. resetSelection();
  152. }
  153. }, 2000);
  154. },
  155. };
  156. })();
  157. var opMap;
  158. // Manages all the page steppers.
  159. var StepperManager = (function StepperManagerClosure() {
  160. var steppers = [];
  161. var stepperDiv = null;
  162. var stepperControls = null;
  163. var stepperChooser = null;
  164. var breakPoints = Object.create(null);
  165. return {
  166. // Properties/functions needed by PDFBug.
  167. id: 'Stepper',
  168. name: 'Stepper',
  169. panel: null,
  170. manager: null,
  171. init: function init(pdfjsLib) {
  172. var self = this;
  173. this.panel.setAttribute('style', 'padding: 5px;');
  174. stepperControls = document.createElement('div');
  175. stepperChooser = document.createElement('select');
  176. stepperChooser.addEventListener('change', function(event) {
  177. self.selectStepper(this.value);
  178. });
  179. stepperControls.appendChild(stepperChooser);
  180. stepperDiv = document.createElement('div');
  181. this.panel.appendChild(stepperControls);
  182. this.panel.appendChild(stepperDiv);
  183. if (sessionStorage.getItem('pdfjsBreakPoints')) {
  184. breakPoints = JSON.parse(sessionStorage.getItem('pdfjsBreakPoints'));
  185. }
  186. opMap = Object.create(null);
  187. for (var key in pdfjsLib.OPS) {
  188. opMap[pdfjsLib.OPS[key]] = key;
  189. }
  190. },
  191. cleanup: function cleanup() {
  192. stepperChooser.textContent = '';
  193. stepperDiv.textContent = '';
  194. steppers = [];
  195. },
  196. enabled: false,
  197. active: false,
  198. // Stepper specific functions.
  199. create: function create(pageIndex) {
  200. var debug = document.createElement('div');
  201. debug.id = 'stepper' + pageIndex;
  202. debug.setAttribute('hidden', true);
  203. debug.className = 'stepper';
  204. stepperDiv.appendChild(debug);
  205. var b = document.createElement('option');
  206. b.textContent = 'Page ' + (pageIndex + 1);
  207. b.value = pageIndex;
  208. stepperChooser.appendChild(b);
  209. var initBreakPoints = breakPoints[pageIndex] || [];
  210. var stepper = new Stepper(debug, pageIndex, initBreakPoints);
  211. steppers.push(stepper);
  212. if (steppers.length === 1) {
  213. this.selectStepper(pageIndex, false);
  214. }
  215. return stepper;
  216. },
  217. selectStepper: function selectStepper(pageIndex, selectPanel) {
  218. var i;
  219. pageIndex = pageIndex | 0;
  220. if (selectPanel) {
  221. this.manager.selectPanel(this);
  222. }
  223. for (i = 0; i < steppers.length; ++i) {
  224. var stepper = steppers[i];
  225. if (stepper.pageIndex === pageIndex) {
  226. stepper.panel.removeAttribute('hidden');
  227. } else {
  228. stepper.panel.setAttribute('hidden', true);
  229. }
  230. }
  231. var options = stepperChooser.options;
  232. for (i = 0; i < options.length; ++i) {
  233. var option = options[i];
  234. option.selected = (option.value | 0) === pageIndex;
  235. }
  236. },
  237. saveBreakPoints: function saveBreakPoints(pageIndex, bps) {
  238. breakPoints[pageIndex] = bps;
  239. sessionStorage.setItem('pdfjsBreakPoints', JSON.stringify(breakPoints));
  240. },
  241. };
  242. })();
  243. // The stepper for each page's IRQueue.
  244. var Stepper = (function StepperClosure() {
  245. // Shorter way to create element and optionally set textContent.
  246. function c(tag, textContent) {
  247. var d = document.createElement(tag);
  248. if (textContent) {
  249. d.textContent = textContent;
  250. }
  251. return d;
  252. }
  253. function simplifyArgs(args) {
  254. if (typeof args === 'string') {
  255. var MAX_STRING_LENGTH = 75;
  256. return args.length <= MAX_STRING_LENGTH ? args :
  257. args.substr(0, MAX_STRING_LENGTH) + '...';
  258. }
  259. if (typeof args !== 'object' || args === null) {
  260. return args;
  261. }
  262. if ('length' in args) { // array
  263. var simpleArgs = [], i, 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(MAX_OPERATORS_COUNT,
  322. operatorList.fnArray.length);
  323. for (var i = this.operatorListIdx; i < operatorsToDisplay; i++) {
  324. var line = c('tr');
  325. line.className = 'line';
  326. line.dataset.idx = i;
  327. chunk.appendChild(line);
  328. var checked = this.breakPoints.indexOf(i) !== -1;
  329. var args = operatorList.argsArray[i] || [];
  330. var breakCell = c('td');
  331. var cbox = c('input');
  332. cbox.type = 'checkbox';
  333. cbox.className = 'points';
  334. cbox.checked = checked;
  335. cbox.dataset.idx = i;
  336. cbox.onclick = cboxOnClick;
  337. breakCell.appendChild(cbox);
  338. line.appendChild(breakCell);
  339. line.appendChild(c('td', i.toString()));
  340. var fn = opMap[operatorList.fnArray[i]];
  341. var decArgs = args;
  342. if (fn === 'showText') {
  343. var glyphs = args[0];
  344. var newArgs = [];
  345. var str = [];
  346. for (var j = 0; j < glyphs.length; j++) {
  347. var glyph = glyphs[j];
  348. if (typeof glyph === 'object' && glyph !== null) {
  349. str.push(glyph.fontChar);
  350. } else {
  351. if (str.length > 0) {
  352. newArgs.push(str.join(''));
  353. str = [];
  354. }
  355. newArgs.push(glyph); // null or number
  356. }
  357. }
  358. if (str.length > 0) {
  359. newArgs.push(str.join(''));
  360. }
  361. decArgs = [newArgs];
  362. }
  363. line.appendChild(c('td', fn));
  364. line.appendChild(c('td', JSON.stringify(simplifyArgs(decArgs))));
  365. }
  366. if (operatorsToDisplay < operatorList.fnArray.length) {
  367. line = c('tr');
  368. var lastCell = c('td', '...');
  369. lastCell.colspan = 4;
  370. chunk.appendChild(lastCell);
  371. }
  372. this.operatorListIdx = operatorList.fnArray.length;
  373. this.table.appendChild(chunk);
  374. },
  375. getNextBreakPoint: function getNextBreakPoint() {
  376. this.breakPoints.sort(function(a, b) {
  377. return a - b;
  378. });
  379. for (var i = 0; i < this.breakPoints.length; i++) {
  380. if (this.breakPoints[i] > this.currentIdx) {
  381. return this.breakPoints[i];
  382. }
  383. }
  384. return null;
  385. },
  386. breakIt: function breakIt(idx, callback) {
  387. StepperManager.selectStepper(this.pageIndex, true);
  388. var self = this;
  389. var dom = document;
  390. self.currentIdx = idx;
  391. var listener = function(e) {
  392. switch (e.keyCode) {
  393. case 83: // step
  394. dom.removeEventListener('keydown', listener);
  395. self.nextBreakPoint = self.currentIdx + 1;
  396. self.goTo(-1);
  397. callback();
  398. break;
  399. case 67: // continue
  400. dom.removeEventListener('keydown', listener);
  401. var breakPoint = self.getNextBreakPoint();
  402. self.nextBreakPoint = breakPoint;
  403. self.goTo(-1);
  404. callback();
  405. break;
  406. }
  407. };
  408. dom.addEventListener('keydown', listener);
  409. self.goTo(idx);
  410. },
  411. goTo: function goTo(idx) {
  412. var allRows = this.panel.getElementsByClassName('line');
  413. for (var x = 0, xx = allRows.length; x < xx; ++x) {
  414. var row = allRows[x];
  415. if ((row.dataset.idx | 0) === idx) {
  416. row.style.backgroundColor = 'rgb(251,250,207)';
  417. row.scrollIntoView();
  418. } else {
  419. row.style.backgroundColor = null;
  420. }
  421. }
  422. },
  423. };
  424. return Stepper;
  425. })();
  426. var Stats = (function Stats() {
  427. var stats = [];
  428. function clear(node) {
  429. while (node.hasChildNodes()) {
  430. node.removeChild(node.lastChild);
  431. }
  432. }
  433. function getStatIndex(pageNumber) {
  434. for (var i = 0, ii = stats.length; i < ii; ++i) {
  435. if (stats[i].pageNumber === pageNumber) {
  436. return i;
  437. }
  438. }
  439. return false;
  440. }
  441. return {
  442. // Properties/functions needed by PDFBug.
  443. id: 'Stats',
  444. name: 'Stats',
  445. panel: null,
  446. manager: null,
  447. init(pdfjsLib) {
  448. this.panel.setAttribute('style', 'padding: 5px;');
  449. pdfjsLib.PDFJS.enableStats = true;
  450. },
  451. enabled: false,
  452. active: false,
  453. // Stats specific functions.
  454. add(pageNumber, stat) {
  455. if (!stat) {
  456. return;
  457. }
  458. var statsIndex = getStatIndex(pageNumber);
  459. if (statsIndex !== false) {
  460. var b = stats[statsIndex];
  461. this.panel.removeChild(b.div);
  462. stats.splice(statsIndex, 1);
  463. }
  464. var wrapper = document.createElement('div');
  465. wrapper.className = 'stats';
  466. var title = document.createElement('div');
  467. title.className = 'title';
  468. title.textContent = 'Page: ' + pageNumber;
  469. var statsDiv = document.createElement('div');
  470. statsDiv.textContent = stat.toString();
  471. wrapper.appendChild(title);
  472. wrapper.appendChild(statsDiv);
  473. stats.push({ pageNumber, div: wrapper, });
  474. stats.sort(function(a, b) {
  475. return a.pageNumber - b.pageNumber;
  476. });
  477. clear(this.panel);
  478. for (var i = 0, ii = stats.length; i < ii; ++i) {
  479. this.panel.appendChild(stats[i].div);
  480. }
  481. },
  482. cleanup() {
  483. stats = [];
  484. clear(this.panel);
  485. },
  486. };
  487. })();
  488. // Manages all the debugging tools.
  489. window.PDFBug = (function PDFBugClosure() {
  490. var panelWidth = 300;
  491. var buttons = [];
  492. var activePanel = null;
  493. return {
  494. tools: [
  495. FontInspector,
  496. StepperManager,
  497. Stats
  498. ],
  499. enable(ids) {
  500. var all = false, tools = this.tools;
  501. if (ids.length === 1 && ids[0] === 'all') {
  502. all = true;
  503. }
  504. for (var i = 0; i < tools.length; ++i) {
  505. var tool = tools[i];
  506. if (all || ids.indexOf(tool.id) !== -1) {
  507. tool.enabled = true;
  508. }
  509. }
  510. if (!all) {
  511. // Sort the tools by the order they are enabled.
  512. tools.sort(function(a, b) {
  513. var indexA = ids.indexOf(a.id);
  514. indexA = indexA < 0 ? tools.length : indexA;
  515. var indexB = ids.indexOf(b.id);
  516. indexB = indexB < 0 ? tools.length : indexB;
  517. return indexA - indexB;
  518. });
  519. }
  520. },
  521. init(pdfjsLib, container) {
  522. /*
  523. * Basic Layout:
  524. * PDFBug
  525. * Controls
  526. * Panels
  527. * Panel
  528. * Panel
  529. * ...
  530. */
  531. var ui = document.createElement('div');
  532. ui.id = 'PDFBug';
  533. var controls = document.createElement('div');
  534. controls.setAttribute('class', 'controls');
  535. ui.appendChild(controls);
  536. var panels = document.createElement('div');
  537. panels.setAttribute('class', 'panels');
  538. ui.appendChild(panels);
  539. container.appendChild(ui);
  540. container.style.right = panelWidth + 'px';
  541. // Initialize all the debugging tools.
  542. var tools = this.tools;
  543. var self = this;
  544. for (var i = 0; i < tools.length; ++i) {
  545. var tool = tools[i];
  546. var panel = document.createElement('div');
  547. var panelButton = document.createElement('button');
  548. panelButton.textContent = tool.name;
  549. panelButton.addEventListener('click', (function(selected) {
  550. return function(event) {
  551. event.preventDefault();
  552. self.selectPanel(selected);
  553. };
  554. })(i));
  555. controls.appendChild(panelButton);
  556. panels.appendChild(panel);
  557. tool.panel = panel;
  558. tool.manager = this;
  559. if (tool.enabled) {
  560. tool.init(pdfjsLib);
  561. } else {
  562. panel.textContent = tool.name + ' is disabled. To enable add ' +
  563. ' "' + tool.id + '" to the pdfBug parameter ' +
  564. 'and refresh (separate multiple by commas).';
  565. }
  566. buttons.push(panelButton);
  567. }
  568. this.selectPanel(0);
  569. },
  570. cleanup() {
  571. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  572. if (this.tools[i].enabled) {
  573. this.tools[i].cleanup();
  574. }
  575. }
  576. },
  577. selectPanel(index) {
  578. if (typeof index !== 'number') {
  579. index = this.tools.indexOf(index);
  580. }
  581. if (index === activePanel) {
  582. return;
  583. }
  584. activePanel = index;
  585. var tools = this.tools;
  586. for (var j = 0; j < tools.length; ++j) {
  587. if (j === index) {
  588. buttons[j].setAttribute('class', 'active');
  589. tools[j].active = true;
  590. tools[j].panel.removeAttribute('hidden');
  591. } else {
  592. buttons[j].setAttribute('class', '');
  593. tools[j].active = false;
  594. tools[j].panel.setAttribute('hidden', 'true');
  595. }
  596. }
  597. },
  598. };
  599. })();