debugger.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* Copyright 2017 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 + '=' + fontName + ']');
  36. for (var i = 0, ii = divs.length; i < ii; ++i) {
  37. var div = divs[i];
  38. div.className = show ? 'debuggerShowText' : 'debuggerHideText';
  39. }
  40. }
  41. function textLayerClick(e) {
  42. if (!e.target.dataset.fontName || e.target.tagName.toUpperCase() !== 'DIV') {
  43. return;
  44. }
  45. var fontName = e.target.dataset.fontName;
  46. var selects = document.getElementsByTagName('input');
  47. for (var i = 0; i < selects.length; ++i) {
  48. var select = selects[i];
  49. if (select.dataset.fontName !== fontName) {
  50. continue;
  51. }
  52. select.checked = !select.checked;
  53. selectFont(fontName, select.checked);
  54. select.scrollIntoView();
  55. }
  56. }
  57. return {
  58. id: 'FontInspector',
  59. name: 'Font Inspector',
  60. panel: null,
  61. manager: null,
  62. init: function init(pdfjsLib) {
  63. var panel = this.panel;
  64. panel.setAttribute('style', 'padding: 5px;');
  65. var tmp = document.createElement('button');
  66. tmp.addEventListener('click', resetSelection);
  67. tmp.textContent = 'Refresh';
  68. panel.appendChild(tmp);
  69. fonts = document.createElement('div');
  70. panel.appendChild(fonts);
  71. },
  72. cleanup: function cleanup() {
  73. fonts.textContent = '';
  74. },
  75. enabled: false,
  76. get active() {
  77. return active;
  78. },
  79. set active(value) {
  80. active = value;
  81. if (active) {
  82. document.body.addEventListener('click', textLayerClick, true);
  83. resetSelection();
  84. } else {
  85. document.body.removeEventListener('click', textLayerClick, true);
  86. removeSelection();
  87. }
  88. },
  89. fontAdded: function fontAdded(fontObj, url) {
  90. function properties(obj, list) {
  91. var moreInfo = document.createElement('table');
  92. for (var i = 0; i < list.length; i++) {
  93. var tr = document.createElement('tr');
  94. var td1 = document.createElement('td');
  95. td1.textContent = list[i];
  96. tr.appendChild(td1);
  97. var td2 = document.createElement('td');
  98. td2.textContent = obj[list[i]].toString();
  99. tr.appendChild(td2);
  100. moreInfo.appendChild(tr);
  101. }
  102. return moreInfo;
  103. }
  104. var moreInfo = properties(fontObj, ['name', 'type']);
  105. var fontName = fontObj.loadedName;
  106. var font = document.createElement('div');
  107. var name = document.createElement('span');
  108. name.textContent = fontName;
  109. var download = document.createElement('a');
  110. if (url) {
  111. url = /url\(['"]?([^\)"']+)/.exec(url);
  112. download.href = url[1];
  113. } else if (fontObj.data) {
  114. url = URL.createObjectURL(new Blob([fontObj.data], { type: fontObj.mimeType }));
  115. download.href = url;
  116. }
  117. download.textContent = 'Download';
  118. var logIt = document.createElement('a');
  119. logIt.href = '';
  120. logIt.textContent = 'Log';
  121. logIt.addEventListener('click', function (event) {
  122. event.preventDefault();
  123. console.log(fontObj);
  124. });
  125. var select = document.createElement('input');
  126. select.setAttribute('type', 'checkbox');
  127. select.dataset.fontName = fontName;
  128. select.addEventListener('click', function (select, fontName) {
  129. return function () {
  130. selectFont(fontName, select.checked);
  131. };
  132. }(select, fontName));
  133. font.appendChild(select);
  134. font.appendChild(name);
  135. font.appendChild(document.createTextNode(' '));
  136. font.appendChild(download);
  137. font.appendChild(document.createTextNode(' '));
  138. font.appendChild(logIt);
  139. font.appendChild(moreInfo);
  140. fonts.appendChild(font);
  141. setTimeout(function () {
  142. if (this.active) {
  143. resetSelection();
  144. }
  145. }.bind(this), 2000);
  146. }
  147. };
  148. }();
  149. var opMap;
  150. var StepperManager = function StepperManagerClosure() {
  151. var steppers = [];
  152. var stepperDiv = null;
  153. var stepperControls = null;
  154. var stepperChooser = null;
  155. var breakPoints = Object.create(null);
  156. return {
  157. id: 'Stepper',
  158. name: 'Stepper',
  159. panel: null,
  160. manager: null,
  161. init: function init(pdfjsLib) {
  162. var self = this;
  163. this.panel.setAttribute('style', 'padding: 5px;');
  164. stepperControls = document.createElement('div');
  165. stepperChooser = document.createElement('select');
  166. stepperChooser.addEventListener('change', function (event) {
  167. self.selectStepper(this.value);
  168. });
  169. stepperControls.appendChild(stepperChooser);
  170. stepperDiv = document.createElement('div');
  171. this.panel.appendChild(stepperControls);
  172. this.panel.appendChild(stepperDiv);
  173. if (sessionStorage.getItem('pdfjsBreakPoints')) {
  174. breakPoints = JSON.parse(sessionStorage.getItem('pdfjsBreakPoints'));
  175. }
  176. opMap = Object.create(null);
  177. for (var key in pdfjsLib.OPS) {
  178. opMap[pdfjsLib.OPS[key]] = key;
  179. }
  180. },
  181. cleanup: function cleanup() {
  182. stepperChooser.textContent = '';
  183. stepperDiv.textContent = '';
  184. steppers = [];
  185. },
  186. enabled: false,
  187. active: false,
  188. create: function create(pageIndex) {
  189. var debug = document.createElement('div');
  190. debug.id = 'stepper' + pageIndex;
  191. debug.setAttribute('hidden', true);
  192. debug.className = 'stepper';
  193. stepperDiv.appendChild(debug);
  194. var b = document.createElement('option');
  195. b.textContent = 'Page ' + (pageIndex + 1);
  196. b.value = pageIndex;
  197. stepperChooser.appendChild(b);
  198. var initBreakPoints = breakPoints[pageIndex] || [];
  199. var stepper = new Stepper(debug, pageIndex, initBreakPoints);
  200. steppers.push(stepper);
  201. if (steppers.length === 1) {
  202. this.selectStepper(pageIndex, false);
  203. }
  204. return stepper;
  205. },
  206. selectStepper: function selectStepper(pageIndex, selectPanel) {
  207. var i;
  208. pageIndex = pageIndex | 0;
  209. if (selectPanel) {
  210. this.manager.selectPanel(this);
  211. }
  212. for (i = 0; i < steppers.length; ++i) {
  213. var stepper = steppers[i];
  214. if (stepper.pageIndex === pageIndex) {
  215. stepper.panel.removeAttribute('hidden');
  216. } else {
  217. stepper.panel.setAttribute('hidden', true);
  218. }
  219. }
  220. var options = stepperChooser.options;
  221. for (i = 0; i < options.length; ++i) {
  222. var option = options[i];
  223. option.selected = (option.value | 0) === pageIndex;
  224. }
  225. },
  226. saveBreakPoints: function saveBreakPoints(pageIndex, bps) {
  227. breakPoints[pageIndex] = bps;
  228. sessionStorage.setItem('pdfjsBreakPoints', JSON.stringify(breakPoints));
  229. }
  230. };
  231. }();
  232. var Stepper = function StepperClosure() {
  233. function c(tag, textContent) {
  234. var d = document.createElement(tag);
  235. if (textContent) {
  236. d.textContent = textContent;
  237. }
  238. return d;
  239. }
  240. function simplifyArgs(args) {
  241. if (typeof args === 'string') {
  242. var MAX_STRING_LENGTH = 75;
  243. return args.length <= MAX_STRING_LENGTH ? args : args.substr(0, MAX_STRING_LENGTH) + '...';
  244. }
  245. if (typeof args !== 'object' || args === null) {
  246. return args;
  247. }
  248. if ('length' in args) {
  249. var simpleArgs = [],
  250. i,
  251. ii;
  252. var MAX_ITEMS = 10;
  253. for (i = 0, ii = Math.min(MAX_ITEMS, args.length); i < ii; i++) {
  254. simpleArgs.push(simplifyArgs(args[i]));
  255. }
  256. if (i < args.length) {
  257. simpleArgs.push('...');
  258. }
  259. return simpleArgs;
  260. }
  261. var simpleObj = {};
  262. for (var key in args) {
  263. simpleObj[key] = simplifyArgs(args[key]);
  264. }
  265. return simpleObj;
  266. }
  267. function Stepper(panel, pageIndex, initialBreakPoints) {
  268. this.panel = panel;
  269. this.breakPoint = 0;
  270. this.nextBreakPoint = null;
  271. this.pageIndex = pageIndex;
  272. this.breakPoints = initialBreakPoints;
  273. this.currentIdx = -1;
  274. this.operatorListIdx = 0;
  275. }
  276. Stepper.prototype = {
  277. init: function init(operatorList) {
  278. var panel = this.panel;
  279. var content = c('div', 'c=continue, s=step');
  280. var table = c('table');
  281. content.appendChild(table);
  282. table.cellSpacing = 0;
  283. var headerRow = c('tr');
  284. table.appendChild(headerRow);
  285. headerRow.appendChild(c('th', 'Break'));
  286. headerRow.appendChild(c('th', 'Idx'));
  287. headerRow.appendChild(c('th', 'fn'));
  288. headerRow.appendChild(c('th', 'args'));
  289. panel.appendChild(content);
  290. this.table = table;
  291. this.updateOperatorList(operatorList);
  292. },
  293. updateOperatorList: function updateOperatorList(operatorList) {
  294. var self = this;
  295. function cboxOnClick() {
  296. var x = +this.dataset.idx;
  297. if (this.checked) {
  298. self.breakPoints.push(x);
  299. } else {
  300. self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
  301. }
  302. StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
  303. }
  304. var MAX_OPERATORS_COUNT = 15000;
  305. if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
  306. return;
  307. }
  308. var chunk = document.createDocumentFragment();
  309. var operatorsToDisplay = Math.min(MAX_OPERATORS_COUNT, operatorList.fnArray.length);
  310. for (var i = this.operatorListIdx; i < operatorsToDisplay; i++) {
  311. var line = c('tr');
  312. line.className = 'line';
  313. line.dataset.idx = i;
  314. chunk.appendChild(line);
  315. var checked = this.breakPoints.indexOf(i) !== -1;
  316. var args = operatorList.argsArray[i] || [];
  317. var breakCell = c('td');
  318. var cbox = c('input');
  319. cbox.type = 'checkbox';
  320. cbox.className = 'points';
  321. cbox.checked = checked;
  322. cbox.dataset.idx = i;
  323. cbox.onclick = cboxOnClick;
  324. breakCell.appendChild(cbox);
  325. line.appendChild(breakCell);
  326. line.appendChild(c('td', i.toString()));
  327. var fn = opMap[operatorList.fnArray[i]];
  328. var decArgs = args;
  329. if (fn === 'showText') {
  330. var glyphs = args[0];
  331. var newArgs = [];
  332. var str = [];
  333. for (var j = 0; j < glyphs.length; j++) {
  334. var glyph = glyphs[j];
  335. if (typeof glyph === 'object' && glyph !== null) {
  336. str.push(glyph.fontChar);
  337. } else {
  338. if (str.length > 0) {
  339. newArgs.push(str.join(''));
  340. str = [];
  341. }
  342. newArgs.push(glyph);
  343. }
  344. }
  345. if (str.length > 0) {
  346. newArgs.push(str.join(''));
  347. }
  348. decArgs = [newArgs];
  349. }
  350. line.appendChild(c('td', fn));
  351. line.appendChild(c('td', JSON.stringify(simplifyArgs(decArgs))));
  352. }
  353. if (operatorsToDisplay < operatorList.fnArray.length) {
  354. line = c('tr');
  355. var lastCell = c('td', '...');
  356. lastCell.colspan = 4;
  357. chunk.appendChild(lastCell);
  358. }
  359. this.operatorListIdx = operatorList.fnArray.length;
  360. this.table.appendChild(chunk);
  361. },
  362. getNextBreakPoint: function getNextBreakPoint() {
  363. this.breakPoints.sort(function (a, b) {
  364. return a - b;
  365. });
  366. for (var i = 0; i < this.breakPoints.length; i++) {
  367. if (this.breakPoints[i] > this.currentIdx) {
  368. return this.breakPoints[i];
  369. }
  370. }
  371. return null;
  372. },
  373. breakIt: function breakIt(idx, callback) {
  374. StepperManager.selectStepper(this.pageIndex, true);
  375. var self = this;
  376. var dom = document;
  377. self.currentIdx = idx;
  378. var listener = function (e) {
  379. switch (e.keyCode) {
  380. case 83:
  381. dom.removeEventListener('keydown', listener);
  382. self.nextBreakPoint = self.currentIdx + 1;
  383. self.goTo(-1);
  384. callback();
  385. break;
  386. case 67:
  387. dom.removeEventListener('keydown', listener);
  388. var breakPoint = self.getNextBreakPoint();
  389. self.nextBreakPoint = breakPoint;
  390. self.goTo(-1);
  391. callback();
  392. break;
  393. }
  394. };
  395. dom.addEventListener('keydown', listener);
  396. self.goTo(idx);
  397. },
  398. goTo: function goTo(idx) {
  399. var allRows = this.panel.getElementsByClassName('line');
  400. for (var x = 0, xx = allRows.length; x < xx; ++x) {
  401. var row = allRows[x];
  402. if ((row.dataset.idx | 0) === idx) {
  403. row.style.backgroundColor = 'rgb(251,250,207)';
  404. row.scrollIntoView();
  405. } else {
  406. row.style.backgroundColor = null;
  407. }
  408. }
  409. }
  410. };
  411. return Stepper;
  412. }();
  413. var Stats = function Stats() {
  414. var stats = [];
  415. function clear(node) {
  416. while (node.hasChildNodes()) {
  417. node.removeChild(node.lastChild);
  418. }
  419. }
  420. function getStatIndex(pageNumber) {
  421. for (var i = 0, ii = stats.length; i < ii; ++i) {
  422. if (stats[i].pageNumber === pageNumber) {
  423. return i;
  424. }
  425. }
  426. return false;
  427. }
  428. return {
  429. id: 'Stats',
  430. name: 'Stats',
  431. panel: null,
  432. manager: null,
  433. init: function init(pdfjsLib) {
  434. this.panel.setAttribute('style', 'padding: 5px;');
  435. pdfjsLib.PDFJS.enableStats = true;
  436. },
  437. enabled: false,
  438. active: false,
  439. add: function (pageNumber, stat) {
  440. if (!stat) {
  441. return;
  442. }
  443. var statsIndex = getStatIndex(pageNumber);
  444. if (statsIndex !== false) {
  445. var b = stats[statsIndex];
  446. this.panel.removeChild(b.div);
  447. stats.splice(statsIndex, 1);
  448. }
  449. var wrapper = document.createElement('div');
  450. wrapper.className = 'stats';
  451. var title = document.createElement('div');
  452. title.className = 'title';
  453. title.textContent = 'Page: ' + pageNumber;
  454. var statsDiv = document.createElement('div');
  455. statsDiv.textContent = stat.toString();
  456. wrapper.appendChild(title);
  457. wrapper.appendChild(statsDiv);
  458. stats.push({
  459. pageNumber: pageNumber,
  460. div: wrapper
  461. });
  462. stats.sort(function (a, b) {
  463. return a.pageNumber - b.pageNumber;
  464. });
  465. clear(this.panel);
  466. for (var i = 0, ii = stats.length; i < ii; ++i) {
  467. this.panel.appendChild(stats[i].div);
  468. }
  469. },
  470. cleanup: function () {
  471. stats = [];
  472. clear(this.panel);
  473. }
  474. };
  475. }();
  476. window.PDFBug = function PDFBugClosure() {
  477. var panelWidth = 300;
  478. var buttons = [];
  479. var activePanel = null;
  480. return {
  481. tools: [FontInspector, StepperManager, Stats],
  482. enable: function (ids) {
  483. var all = false,
  484. tools = this.tools;
  485. if (ids.length === 1 && ids[0] === 'all') {
  486. all = true;
  487. }
  488. for (var i = 0; i < tools.length; ++i) {
  489. var tool = tools[i];
  490. if (all || ids.indexOf(tool.id) !== -1) {
  491. tool.enabled = true;
  492. }
  493. }
  494. if (!all) {
  495. tools.sort(function (a, b) {
  496. var indexA = ids.indexOf(a.id);
  497. indexA = indexA < 0 ? tools.length : indexA;
  498. var indexB = ids.indexOf(b.id);
  499. indexB = indexB < 0 ? tools.length : indexB;
  500. return indexA - indexB;
  501. });
  502. }
  503. },
  504. init: function init(pdfjsLib, container) {
  505. var ui = document.createElement('div');
  506. ui.id = 'PDFBug';
  507. var controls = document.createElement('div');
  508. controls.setAttribute('class', 'controls');
  509. ui.appendChild(controls);
  510. var panels = document.createElement('div');
  511. panels.setAttribute('class', 'panels');
  512. ui.appendChild(panels);
  513. container.appendChild(ui);
  514. container.style.right = panelWidth + 'px';
  515. var tools = this.tools;
  516. var self = this;
  517. for (var i = 0; i < tools.length; ++i) {
  518. var tool = tools[i];
  519. var panel = document.createElement('div');
  520. var panelButton = document.createElement('button');
  521. panelButton.textContent = tool.name;
  522. panelButton.addEventListener('click', function (selected) {
  523. return function (event) {
  524. event.preventDefault();
  525. self.selectPanel(selected);
  526. };
  527. }(i));
  528. controls.appendChild(panelButton);
  529. panels.appendChild(panel);
  530. tool.panel = panel;
  531. tool.manager = this;
  532. if (tool.enabled) {
  533. tool.init(pdfjsLib);
  534. } else {
  535. panel.textContent = tool.name + ' is disabled. To enable add ' + ' "' + tool.id + '" to the pdfBug parameter ' + 'and refresh (separate multiple by commas).';
  536. }
  537. buttons.push(panelButton);
  538. }
  539. this.selectPanel(0);
  540. },
  541. cleanup: function cleanup() {
  542. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  543. if (this.tools[i].enabled) {
  544. this.tools[i].cleanup();
  545. }
  546. }
  547. },
  548. selectPanel: function selectPanel(index) {
  549. if (typeof index !== 'number') {
  550. index = this.tools.indexOf(index);
  551. }
  552. if (index === activePanel) {
  553. return;
  554. }
  555. activePanel = index;
  556. var tools = this.tools;
  557. for (var j = 0; j < tools.length; ++j) {
  558. if (j === index) {
  559. buttons[j].setAttribute('class', 'active');
  560. tools[j].active = true;
  561. tools[j].panel.removeAttribute('hidden');
  562. } else {
  563. buttons[j].setAttribute('class', '');
  564. tools[j].active = false;
  565. tools[j].panel.setAttribute('hidden', 'true');
  566. }
  567. }
  568. }
  569. };
  570. }();