debugger.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. /* globals PDFJS */
  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. 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() {
  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. },
  76. cleanup: function cleanup() {
  77. fonts.textContent = '';
  78. },
  79. enabled: false,
  80. get active() {
  81. return active;
  82. },
  83. set active(value) {
  84. active = value;
  85. if (active) {
  86. document.body.addEventListener('click', textLayerClick, true);
  87. resetSelection();
  88. } else {
  89. document.body.removeEventListener('click', textLayerClick, true);
  90. removeSelection();
  91. }
  92. },
  93. // FontInspector specific functions.
  94. fontAdded: function fontAdded(fontObj, url) {
  95. function properties(obj, list) {
  96. var moreInfo = document.createElement('table');
  97. for (var i = 0; i < list.length; i++) {
  98. var tr = document.createElement('tr');
  99. var td1 = document.createElement('td');
  100. td1.textContent = list[i];
  101. tr.appendChild(td1);
  102. var td2 = document.createElement('td');
  103. td2.textContent = obj[list[i]].toString();
  104. tr.appendChild(td2);
  105. moreInfo.appendChild(tr);
  106. }
  107. return moreInfo;
  108. }
  109. var moreInfo = properties(fontObj, ['name', 'type']);
  110. var fontName = fontObj.loadedName;
  111. var font = document.createElement('div');
  112. var name = document.createElement('span');
  113. name.textContent = fontName;
  114. var download = document.createElement('a');
  115. if (url) {
  116. url = /url\(['"]?([^\)"']+)/.exec(url);
  117. download.href = url[1];
  118. } else if (fontObj.data) {
  119. url = URL.createObjectURL(new Blob([fontObj.data], {
  120. type: fontObj.mimeType
  121. }));
  122. download.href = url;
  123. }
  124. download.textContent = 'Download';
  125. var logIt = document.createElement('a');
  126. logIt.href = '';
  127. logIt.textContent = 'Log';
  128. logIt.addEventListener('click', function(event) {
  129. event.preventDefault();
  130. console.log(fontObj);
  131. });
  132. var select = document.createElement('input');
  133. select.setAttribute('type', 'checkbox');
  134. select.dataset.fontName = fontName;
  135. select.addEventListener('click', (function(select, fontName) {
  136. return (function() {
  137. selectFont(fontName, select.checked);
  138. });
  139. })(select, fontName));
  140. font.appendChild(select);
  141. font.appendChild(name);
  142. font.appendChild(document.createTextNode(' '));
  143. font.appendChild(download);
  144. font.appendChild(document.createTextNode(' '));
  145. font.appendChild(logIt);
  146. font.appendChild(moreInfo);
  147. fonts.appendChild(font);
  148. // Somewhat of a hack, should probably add a hook for when the text layer
  149. // is done rendering.
  150. setTimeout(function() {
  151. if (this.active) {
  152. resetSelection();
  153. }
  154. }.bind(this), 2000);
  155. }
  156. };
  157. })();
  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 = {};
  165. return {
  166. // Properties/functions needed by PDFBug.
  167. id: 'Stepper',
  168. name: 'Stepper',
  169. panel: null,
  170. manager: null,
  171. init: function init() {
  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. },
  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.setAttribute('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. if (stepper.pageIndex === pageIndex) {
  222. stepper.panel.removeAttribute('hidden');
  223. } else {
  224. stepper.panel.setAttribute('hidden', true);
  225. }
  226. }
  227. var options = stepperChooser.options;
  228. for (i = 0; i < options.length; ++i) {
  229. var option = options[i];
  230. option.selected = (option.value | 0) === pageIndex;
  231. }
  232. },
  233. saveBreakPoints: function saveBreakPoints(pageIndex, bps) {
  234. breakPoints[pageIndex] = bps;
  235. sessionStorage.setItem('pdfjsBreakPoints', JSON.stringify(breakPoints));
  236. }
  237. };
  238. })();
  239. // The stepper for each page's IRQueue.
  240. var Stepper = (function StepperClosure() {
  241. // Shorter way to create element and optionally set textContent.
  242. function c(tag, textContent) {
  243. var d = document.createElement(tag);
  244. if (textContent) {
  245. d.textContent = textContent;
  246. }
  247. return d;
  248. }
  249. var opMap = null;
  250. function simplifyArgs(args) {
  251. if (typeof args === 'string') {
  252. var MAX_STRING_LENGTH = 75;
  253. return args.length <= MAX_STRING_LENGTH ? args :
  254. args.substr(0, MAX_STRING_LENGTH) + '...';
  255. }
  256. if (typeof args !== 'object' || args === null) {
  257. return args;
  258. }
  259. if ('length' in args) { // array
  260. var simpleArgs = [], i, ii;
  261. var MAX_ITEMS = 10;
  262. for (i = 0, ii = Math.min(MAX_ITEMS, args.length); i < ii; i++) {
  263. simpleArgs.push(simplifyArgs(args[i]));
  264. }
  265. if (i < args.length) {
  266. simpleArgs.push('...');
  267. }
  268. return simpleArgs;
  269. }
  270. var simpleObj = {};
  271. for (var key in args) {
  272. simpleObj[key] = simplifyArgs(args[key]);
  273. }
  274. return simpleObj;
  275. }
  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() {
  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. if (!opMap) {
  301. opMap = Object.create(null);
  302. for (var key in PDFJS.OPS) {
  303. opMap[PDFJS.OPS[key]] = key;
  304. }
  305. }
  306. },
  307. updateOperatorList: function updateOperatorList(operatorList) {
  308. var self = this;
  309. function cboxOnClick() {
  310. var x = +this.dataset.idx;
  311. if (this.checked) {
  312. self.breakPoints.push(x);
  313. } else {
  314. self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
  315. }
  316. StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
  317. }
  318. var MAX_OPERATORS_COUNT = 15000;
  319. if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
  320. return;
  321. }
  322. var chunk = document.createDocumentFragment();
  323. var operatorsToDisplay = Math.min(MAX_OPERATORS_COUNT,
  324. operatorList.fnArray.length);
  325. for (var i = this.operatorListIdx; i < operatorsToDisplay; i++) {
  326. var line = c('tr');
  327. line.className = 'line';
  328. line.dataset.idx = i;
  329. chunk.appendChild(line);
  330. var checked = this.breakPoints.indexOf(i) !== -1;
  331. var args = operatorList.argsArray[i] || [];
  332. var breakCell = c('td');
  333. var cbox = c('input');
  334. cbox.type = 'checkbox';
  335. cbox.className = 'points';
  336. cbox.checked = checked;
  337. cbox.dataset.idx = i;
  338. cbox.onclick = cboxOnClick;
  339. breakCell.appendChild(cbox);
  340. line.appendChild(breakCell);
  341. line.appendChild(c('td', i.toString()));
  342. var fn = opMap[operatorList.fnArray[i]];
  343. var decArgs = args;
  344. if (fn === 'showText') {
  345. var glyphs = args[0];
  346. var newArgs = [];
  347. var str = [];
  348. for (var j = 0; j < glyphs.length; j++) {
  349. var glyph = glyphs[j];
  350. if (typeof glyph === 'object' && glyph !== null) {
  351. str.push(glyph.fontChar);
  352. } else {
  353. if (str.length > 0) {
  354. newArgs.push(str.join(''));
  355. str = [];
  356. }
  357. newArgs.push(glyph); // null or number
  358. }
  359. }
  360. if (str.length > 0) {
  361. newArgs.push(str.join(''));
  362. }
  363. decArgs = [newArgs];
  364. }
  365. line.appendChild(c('td', fn));
  366. line.appendChild(c('td', JSON.stringify(simplifyArgs(decArgs))));
  367. }
  368. if (operatorsToDisplay < operatorList.fnArray.length) {
  369. line = c('tr');
  370. var lastCell = c('td', '...');
  371. lastCell.colspan = 4;
  372. chunk.appendChild(lastCell);
  373. }
  374. this.operatorListIdx = operatorList.fnArray.length;
  375. this.table.appendChild(chunk);
  376. },
  377. getNextBreakPoint: function getNextBreakPoint() {
  378. this.breakPoints.sort(function(a, b) { return a - b; });
  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, false);
  395. self.nextBreakPoint = self.currentIdx + 1;
  396. self.goTo(-1);
  397. callback();
  398. break;
  399. case 67: // continue
  400. dom.removeEventListener('keydown', listener, false);
  401. var breakPoint = self.getNextBreakPoint();
  402. self.nextBreakPoint = breakPoint;
  403. self.goTo(-1);
  404. callback();
  405. break;
  406. }
  407. };
  408. dom.addEventListener('keydown', listener, false);
  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: function init() {
  448. this.panel.setAttribute('style', 'padding: 5px;');
  449. PDFJS.enableStats = true;
  450. },
  451. enabled: false,
  452. active: false,
  453. // Stats specific functions.
  454. add: function(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: pageNumber, div: wrapper });
  474. stats.sort(function(a, b) { return a.pageNumber - b.pageNumber; });
  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: function () {
  481. stats = [];
  482. clear(this.panel);
  483. }
  484. };
  485. })();
  486. // Manages all the debugging tools.
  487. var 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: function(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.indexOf(tool.id) !== -1) {
  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: function init() {
  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. var container = document.getElementById('viewerContainer');
  538. container.appendChild(ui);
  539. container.style.right = panelWidth + 'px';
  540. // Initialize all the debugging tools.
  541. var tools = this.tools;
  542. var self = this;
  543. for (var i = 0; i < tools.length; ++i) {
  544. var tool = tools[i];
  545. var panel = document.createElement('div');
  546. var panelButton = document.createElement('button');
  547. panelButton.textContent = tool.name;
  548. panelButton.addEventListener('click', (function(selected) {
  549. return function(event) {
  550. event.preventDefault();
  551. self.selectPanel(selected);
  552. };
  553. })(i));
  554. controls.appendChild(panelButton);
  555. panels.appendChild(panel);
  556. tool.panel = panel;
  557. tool.manager = this;
  558. if (tool.enabled) {
  559. tool.init();
  560. } else {
  561. panel.textContent = tool.name + ' is disabled. To enable add ' +
  562. ' "' + tool.id + '" to the pdfBug parameter ' +
  563. 'and refresh (seperate multiple by commas).';
  564. }
  565. buttons.push(panelButton);
  566. }
  567. this.selectPanel(0);
  568. },
  569. cleanup: function 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: function 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. if (j === index) {
  587. buttons[j].setAttribute('class', 'active');
  588. tools[j].active = true;
  589. tools[j].panel.removeAttribute('hidden');
  590. } else {
  591. buttons[j].setAttribute('class', '');
  592. tools[j].active = false;
  593. tools[j].panel.setAttribute('hidden', 'true');
  594. }
  595. }
  596. }
  597. };
  598. })();