debugger.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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(function() {
  150. if (this.active) {
  151. resetSelection();
  152. }
  153. }.bind(this), 2000);
  154. }
  155. };
  156. })();
  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() {
  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. },
  186. cleanup: function cleanup() {
  187. stepperChooser.textContent = '';
  188. stepperDiv.textContent = '';
  189. steppers = [];
  190. },
  191. enabled: false,
  192. active: false,
  193. // Stepper specific functions.
  194. create: function create(pageIndex) {
  195. var debug = document.createElement('div');
  196. debug.id = 'stepper' + pageIndex;
  197. debug.setAttribute('hidden', true);
  198. debug.className = 'stepper';
  199. stepperDiv.appendChild(debug);
  200. var b = document.createElement('option');
  201. b.textContent = 'Page ' + (pageIndex + 1);
  202. b.value = pageIndex;
  203. stepperChooser.appendChild(b);
  204. var initBreakPoints = breakPoints[pageIndex] || [];
  205. var stepper = new Stepper(debug, pageIndex, initBreakPoints);
  206. steppers.push(stepper);
  207. if (steppers.length === 1) {
  208. this.selectStepper(pageIndex, false);
  209. }
  210. return stepper;
  211. },
  212. selectStepper: function selectStepper(pageIndex, selectPanel) {
  213. var i;
  214. pageIndex = pageIndex | 0;
  215. if (selectPanel) {
  216. this.manager.selectPanel(this);
  217. }
  218. for (i = 0; i < steppers.length; ++i) {
  219. var stepper = steppers[i];
  220. if (stepper.pageIndex === pageIndex) {
  221. stepper.panel.removeAttribute('hidden');
  222. } else {
  223. stepper.panel.setAttribute('hidden', true);
  224. }
  225. }
  226. var options = stepperChooser.options;
  227. for (i = 0; i < options.length; ++i) {
  228. var option = options[i];
  229. option.selected = (option.value | 0) === pageIndex;
  230. }
  231. },
  232. saveBreakPoints: function saveBreakPoints(pageIndex, bps) {
  233. breakPoints[pageIndex] = bps;
  234. sessionStorage.setItem('pdfjsBreakPoints', JSON.stringify(breakPoints));
  235. }
  236. };
  237. })();
  238. // The stepper for each page's IRQueue.
  239. var Stepper = (function StepperClosure() {
  240. // Shorter way to create element and optionally set textContent.
  241. function c(tag, textContent) {
  242. var d = document.createElement(tag);
  243. if (textContent) {
  244. d.textContent = textContent;
  245. }
  246. return d;
  247. }
  248. var opMap = null;
  249. function simplifyArgs(args) {
  250. if (typeof args === 'string') {
  251. var MAX_STRING_LENGTH = 75;
  252. return args.length <= MAX_STRING_LENGTH ? args :
  253. args.substr(0, MAX_STRING_LENGTH) + '...';
  254. }
  255. if (typeof args !== 'object' || args === null) {
  256. return args;
  257. }
  258. if ('length' in args) { // array
  259. var simpleArgs = [], i, ii;
  260. var MAX_ITEMS = 10;
  261. for (i = 0, ii = Math.min(MAX_ITEMS, args.length); i < ii; i++) {
  262. simpleArgs.push(simplifyArgs(args[i]));
  263. }
  264. if (i < args.length) {
  265. simpleArgs.push('...');
  266. }
  267. return simpleArgs;
  268. }
  269. var simpleObj = {};
  270. for (var key in args) {
  271. simpleObj[key] = simplifyArgs(args[key]);
  272. }
  273. return simpleObj;
  274. }
  275. function Stepper(panel, pageIndex, initialBreakPoints) {
  276. this.panel = panel;
  277. this.breakPoint = 0;
  278. this.nextBreakPoint = null;
  279. this.pageIndex = pageIndex;
  280. this.breakPoints = initialBreakPoints;
  281. this.currentIdx = -1;
  282. this.operatorListIdx = 0;
  283. }
  284. Stepper.prototype = {
  285. init: function init(pdfjsLib) {
  286. var panel = this.panel;
  287. var content = c('div', 'c=continue, s=step');
  288. var table = c('table');
  289. content.appendChild(table);
  290. table.cellSpacing = 0;
  291. var headerRow = c('tr');
  292. table.appendChild(headerRow);
  293. headerRow.appendChild(c('th', 'Break'));
  294. headerRow.appendChild(c('th', 'Idx'));
  295. headerRow.appendChild(c('th', 'fn'));
  296. headerRow.appendChild(c('th', 'args'));
  297. panel.appendChild(content);
  298. this.table = table;
  299. if (!opMap) {
  300. opMap = Object.create(null);
  301. for (var key in pdfjsLib.OPS) {
  302. opMap[pdfjsLib.OPS[key]] = key;
  303. }
  304. }
  305. },
  306. updateOperatorList: function updateOperatorList(operatorList) {
  307. var self = this;
  308. function cboxOnClick() {
  309. var x = +this.dataset.idx;
  310. if (this.checked) {
  311. self.breakPoints.push(x);
  312. } else {
  313. self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
  314. }
  315. StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
  316. }
  317. var MAX_OPERATORS_COUNT = 15000;
  318. if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
  319. return;
  320. }
  321. var chunk = document.createDocumentFragment();
  322. var operatorsToDisplay = Math.min(MAX_OPERATORS_COUNT,
  323. operatorList.fnArray.length);
  324. for (var i = this.operatorListIdx; i < operatorsToDisplay; i++) {
  325. var line = c('tr');
  326. line.className = 'line';
  327. line.dataset.idx = i;
  328. chunk.appendChild(line);
  329. var checked = this.breakPoints.indexOf(i) !== -1;
  330. var args = operatorList.argsArray[i] || [];
  331. var breakCell = c('td');
  332. var cbox = c('input');
  333. cbox.type = 'checkbox';
  334. cbox.className = 'points';
  335. cbox.checked = checked;
  336. cbox.dataset.idx = i;
  337. cbox.onclick = cboxOnClick;
  338. breakCell.appendChild(cbox);
  339. line.appendChild(breakCell);
  340. line.appendChild(c('td', i.toString()));
  341. var fn = opMap[operatorList.fnArray[i]];
  342. var decArgs = args;
  343. if (fn === 'showText') {
  344. var glyphs = args[0];
  345. var newArgs = [];
  346. var str = [];
  347. for (var j = 0; j < glyphs.length; j++) {
  348. var glyph = glyphs[j];
  349. if (typeof glyph === 'object' && glyph !== null) {
  350. str.push(glyph.fontChar);
  351. } else {
  352. if (str.length > 0) {
  353. newArgs.push(str.join(''));
  354. str = [];
  355. }
  356. newArgs.push(glyph); // null or number
  357. }
  358. }
  359. if (str.length > 0) {
  360. newArgs.push(str.join(''));
  361. }
  362. decArgs = [newArgs];
  363. }
  364. line.appendChild(c('td', fn));
  365. line.appendChild(c('td', JSON.stringify(simplifyArgs(decArgs))));
  366. }
  367. if (operatorsToDisplay < operatorList.fnArray.length) {
  368. line = c('tr');
  369. var lastCell = c('td', '...');
  370. lastCell.colspan = 4;
  371. chunk.appendChild(lastCell);
  372. }
  373. this.operatorListIdx = operatorList.fnArray.length;
  374. this.table.appendChild(chunk);
  375. },
  376. getNextBreakPoint: function getNextBreakPoint() {
  377. this.breakPoints.sort(function(a, b) { return a - b; });
  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, false);
  394. self.nextBreakPoint = self.currentIdx + 1;
  395. self.goTo(-1);
  396. callback();
  397. break;
  398. case 67: // continue
  399. dom.removeEventListener('keydown', listener, false);
  400. var breakPoint = self.getNextBreakPoint();
  401. self.nextBreakPoint = breakPoint;
  402. self.goTo(-1);
  403. callback();
  404. break;
  405. }
  406. };
  407. dom.addEventListener('keydown', listener, false);
  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: function init(pdfjsLib) {
  447. this.panel.setAttribute('style', 'padding: 5px;');
  448. pdfjsLib.PDFJS.enableStats = true;
  449. },
  450. enabled: false,
  451. active: false,
  452. // Stats specific functions.
  453. add: function(pageNumber, stat) {
  454. if (!stat) {
  455. return;
  456. }
  457. var statsIndex = getStatIndex(pageNumber);
  458. if (statsIndex !== false) {
  459. var b = stats[statsIndex];
  460. this.panel.removeChild(b.div);
  461. stats.splice(statsIndex, 1);
  462. }
  463. var wrapper = document.createElement('div');
  464. wrapper.className = 'stats';
  465. var title = document.createElement('div');
  466. title.className = 'title';
  467. title.textContent = 'Page: ' + pageNumber;
  468. var statsDiv = document.createElement('div');
  469. statsDiv.textContent = stat.toString();
  470. wrapper.appendChild(title);
  471. wrapper.appendChild(statsDiv);
  472. stats.push({ pageNumber: pageNumber, div: wrapper });
  473. stats.sort(function(a, b) { return a.pageNumber - b.pageNumber; });
  474. clear(this.panel);
  475. for (var i = 0, ii = stats.length; i < ii; ++i) {
  476. this.panel.appendChild(stats[i].div);
  477. }
  478. },
  479. cleanup: function () {
  480. stats = [];
  481. clear(this.panel);
  482. }
  483. };
  484. })();
  485. // Manages all the debugging tools.
  486. var PDFBug = (function PDFBugClosure() {
  487. var panelWidth = 300;
  488. var buttons = [];
  489. var activePanel = null;
  490. return {
  491. tools: [
  492. FontInspector,
  493. StepperManager,
  494. Stats
  495. ],
  496. enable: function(ids) {
  497. var all = false, tools = this.tools;
  498. if (ids.length === 1 && ids[0] === 'all') {
  499. all = true;
  500. }
  501. for (var i = 0; i < tools.length; ++i) {
  502. var tool = tools[i];
  503. if (all || ids.indexOf(tool.id) !== -1) {
  504. tool.enabled = true;
  505. }
  506. }
  507. if (!all) {
  508. // Sort the tools by the order they are enabled.
  509. tools.sort(function(a, b) {
  510. var indexA = ids.indexOf(a.id);
  511. indexA = indexA < 0 ? tools.length : indexA;
  512. var indexB = ids.indexOf(b.id);
  513. indexB = indexB < 0 ? tools.length : indexB;
  514. return indexA - indexB;
  515. });
  516. }
  517. },
  518. init: function init(pdfjsLib, container) {
  519. /*
  520. * Basic Layout:
  521. * PDFBug
  522. * Controls
  523. * Panels
  524. * Panel
  525. * Panel
  526. * ...
  527. */
  528. var ui = document.createElement('div');
  529. ui.id = 'PDFBug';
  530. var controls = document.createElement('div');
  531. controls.setAttribute('class', 'controls');
  532. ui.appendChild(controls);
  533. var panels = document.createElement('div');
  534. panels.setAttribute('class', 'panels');
  535. ui.appendChild(panels);
  536. container.appendChild(ui);
  537. container.style.right = panelWidth + 'px';
  538. // Initialize all the debugging tools.
  539. var tools = this.tools;
  540. var self = this;
  541. for (var i = 0; i < tools.length; ++i) {
  542. var tool = tools[i];
  543. var panel = document.createElement('div');
  544. var panelButton = document.createElement('button');
  545. panelButton.textContent = tool.name;
  546. panelButton.addEventListener('click', (function(selected) {
  547. return function(event) {
  548. event.preventDefault();
  549. self.selectPanel(selected);
  550. };
  551. })(i));
  552. controls.appendChild(panelButton);
  553. panels.appendChild(panel);
  554. tool.panel = panel;
  555. tool.manager = this;
  556. if (tool.enabled) {
  557. tool.init(pdfjsLib);
  558. } else {
  559. panel.textContent = tool.name + ' is disabled. To enable add ' +
  560. ' "' + tool.id + '" to the pdfBug parameter ' +
  561. 'and refresh (seperate multiple by commas).';
  562. }
  563. buttons.push(panelButton);
  564. }
  565. this.selectPanel(0);
  566. },
  567. cleanup: function cleanup() {
  568. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  569. if (this.tools[i].enabled) {
  570. this.tools[i].cleanup();
  571. }
  572. }
  573. },
  574. selectPanel: function selectPanel(index) {
  575. if (typeof index !== 'number') {
  576. index = this.tools.indexOf(index);
  577. }
  578. if (index === activePanel) {
  579. return;
  580. }
  581. activePanel = index;
  582. var tools = this.tools;
  583. for (var j = 0; j < tools.length; ++j) {
  584. if (j === index) {
  585. buttons[j].setAttribute('class', 'active');
  586. tools[j].active = true;
  587. tools[j].panel.removeAttribute('hidden');
  588. } else {
  589. buttons[j].setAttribute('class', '');
  590. tools[j].active = false;
  591. tools[j].panel.setAttribute('hidden', 'true');
  592. }
  593. }
  594. }
  595. };
  596. })();