You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

616 lines
19 KiB

7 years ago
  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. 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) { return a - b; });
  377. for (var i = 0; i < this.breakPoints.length; i++) {
  378. if (this.breakPoints[i] > this.currentIdx) {
  379. return this.breakPoints[i];
  380. }
  381. }
  382. return null;
  383. },
  384. breakIt: function breakIt(idx, callback) {
  385. StepperManager.selectStepper(this.pageIndex, true);
  386. var self = this;
  387. var dom = document;
  388. self.currentIdx = idx;
  389. var listener = function(e) {
  390. switch (e.keyCode) {
  391. case 83: // step
  392. dom.removeEventListener('keydown', listener);
  393. self.nextBreakPoint = self.currentIdx + 1;
  394. self.goTo(-1);
  395. callback();
  396. break;
  397. case 67: // continue
  398. dom.removeEventListener('keydown', listener);
  399. var breakPoint = self.getNextBreakPoint();
  400. self.nextBreakPoint = breakPoint;
  401. self.goTo(-1);
  402. callback();
  403. break;
  404. }
  405. };
  406. dom.addEventListener('keydown', listener);
  407. self.goTo(idx);
  408. },
  409. goTo: function goTo(idx) {
  410. var allRows = this.panel.getElementsByClassName('line');
  411. for (var x = 0, xx = allRows.length; x < xx; ++x) {
  412. var row = allRows[x];
  413. if ((row.dataset.idx | 0) === idx) {
  414. row.style.backgroundColor = 'rgb(251,250,207)';
  415. row.scrollIntoView();
  416. } else {
  417. row.style.backgroundColor = null;
  418. }
  419. }
  420. }
  421. };
  422. return Stepper;
  423. })();
  424. var Stats = (function Stats() {
  425. var stats = [];
  426. function clear(node) {
  427. while (node.hasChildNodes()) {
  428. node.removeChild(node.lastChild);
  429. }
  430. }
  431. function getStatIndex(pageNumber) {
  432. for (var i = 0, ii = stats.length; i < ii; ++i) {
  433. if (stats[i].pageNumber === pageNumber) {
  434. return i;
  435. }
  436. }
  437. return false;
  438. }
  439. return {
  440. // Properties/functions needed by PDFBug.
  441. id: 'Stats',
  442. name: 'Stats',
  443. panel: null,
  444. manager: null,
  445. init: function init(pdfjsLib) {
  446. this.panel.setAttribute('style', 'padding: 5px;');
  447. pdfjsLib.PDFJS.enableStats = true;
  448. },
  449. enabled: false,
  450. active: false,
  451. // Stats specific functions.
  452. add: function(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: pageNumber, div: wrapper });
  472. stats.sort(function(a, b) { return a.pageNumber - b.pageNumber; });
  473. clear(this.panel);
  474. for (var i = 0, ii = stats.length; i < ii; ++i) {
  475. this.panel.appendChild(stats[i].div);
  476. }
  477. },
  478. cleanup: function () {
  479. stats = [];
  480. clear(this.panel);
  481. }
  482. };
  483. })();
  484. // Manages all the debugging tools.
  485. var PDFBug = (function PDFBugClosure() {
  486. var panelWidth = 300;
  487. var buttons = [];
  488. var activePanel = null;
  489. return {
  490. tools: [
  491. FontInspector,
  492. StepperManager,
  493. Stats
  494. ],
  495. enable: function(ids) {
  496. var all = false, tools = this.tools;
  497. if (ids.length === 1 && ids[0] === 'all') {
  498. all = true;
  499. }
  500. for (var i = 0; i < tools.length; ++i) {
  501. var tool = tools[i];
  502. if (all || ids.indexOf(tool.id) !== -1) {
  503. tool.enabled = true;
  504. }
  505. }
  506. if (!all) {
  507. // Sort the tools by the order they are enabled.
  508. tools.sort(function(a, b) {
  509. var indexA = ids.indexOf(a.id);
  510. indexA = indexA < 0 ? tools.length : indexA;
  511. var indexB = ids.indexOf(b.id);
  512. indexB = indexB < 0 ? tools.length : indexB;
  513. return indexA - indexB;
  514. });
  515. }
  516. },
  517. init: function init(pdfjsLib, container) {
  518. /*
  519. * Basic Layout:
  520. * PDFBug
  521. * Controls
  522. * Panels
  523. * Panel
  524. * Panel
  525. * ...
  526. */
  527. var ui = document.createElement('div');
  528. ui.id = 'PDFBug';
  529. var controls = document.createElement('div');
  530. controls.setAttribute('class', 'controls');
  531. ui.appendChild(controls);
  532. var panels = document.createElement('div');
  533. panels.setAttribute('class', 'panels');
  534. ui.appendChild(panels);
  535. container.appendChild(ui);
  536. container.style.right = panelWidth + 'px';
  537. // Initialize all the debugging tools.
  538. var tools = this.tools;
  539. var self = this;
  540. for (var i = 0; i < tools.length; ++i) {
  541. var tool = tools[i];
  542. var panel = document.createElement('div');
  543. var panelButton = document.createElement('button');
  544. panelButton.textContent = tool.name;
  545. panelButton.addEventListener('click', (function(selected) {
  546. return function(event) {
  547. event.preventDefault();
  548. self.selectPanel(selected);
  549. };
  550. })(i));
  551. controls.appendChild(panelButton);
  552. panels.appendChild(panel);
  553. tool.panel = panel;
  554. tool.manager = this;
  555. if (tool.enabled) {
  556. tool.init(pdfjsLib);
  557. } else {
  558. panel.textContent = tool.name + ' is disabled. To enable add ' +
  559. ' "' + tool.id + '" to the pdfBug parameter ' +
  560. 'and refresh (separate multiple by commas).';
  561. }
  562. buttons.push(panelButton);
  563. }
  564. this.selectPanel(0);
  565. },
  566. cleanup: function cleanup() {
  567. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  568. if (this.tools[i].enabled) {
  569. this.tools[i].cleanup();
  570. }
  571. }
  572. },
  573. selectPanel: function selectPanel(index) {
  574. if (typeof index !== 'number') {
  575. index = this.tools.indexOf(index);
  576. }
  577. if (index === activePanel) {
  578. return;
  579. }
  580. activePanel = index;
  581. var tools = this.tools;
  582. for (var j = 0; j < tools.length; ++j) {
  583. if (j === index) {
  584. buttons[j].setAttribute('class', 'active');
  585. tools[j].active = true;
  586. tools[j].panel.removeAttribute('hidden');
  587. } else {
  588. buttons[j].setAttribute('class', '');
  589. tools[j].active = false;
  590. tools[j].panel.setAttribute('hidden', 'true');
  591. }
  592. }
  593. }
  594. };
  595. })();