debugger.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  24. var FontInspector = function FontInspectorClosure() {
  25. var fonts;
  26. var active = false;
  27. var fontAttribute = 'data-font-name';
  28. function removeSelection() {
  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 = '';
  33. }
  34. }
  35. function resetSelection() {
  36. var divs = document.querySelectorAll('div[' + fontAttribute + ']');
  37. for (var i = 0, ii = divs.length; i < ii; ++i) {
  38. var div = divs[i];
  39. div.className = 'debuggerHideText';
  40. }
  41. }
  42. function selectFont(fontName, show) {
  43. var divs = document.querySelectorAll('div[' + fontAttribute + '=' + fontName + ']');
  44. for (var i = 0, ii = divs.length; i < ii; ++i) {
  45. var div = divs[i];
  46. div.className = show ? 'debuggerShowText' : 'debuggerHideText';
  47. }
  48. }
  49. function textLayerClick(e) {
  50. if (!e.target.dataset.fontName || e.target.tagName.toUpperCase() !== 'DIV') {
  51. return;
  52. }
  53. var fontName = e.target.dataset.fontName;
  54. var selects = document.getElementsByTagName('input');
  55. for (var i = 0; i < selects.length; ++i) {
  56. var select = selects[i];
  57. if (select.dataset.fontName !== fontName) {
  58. continue;
  59. }
  60. select.checked = !select.checked;
  61. selectFont(fontName, select.checked);
  62. select.scrollIntoView();
  63. }
  64. }
  65. return {
  66. id: 'FontInspector',
  67. name: 'Font Inspector',
  68. panel: null,
  69. manager: null,
  70. init: function init(pdfjsLib) {
  71. var panel = this.panel;
  72. panel.setAttribute('style', 'padding: 5px;');
  73. var tmp = document.createElement('button');
  74. tmp.addEventListener('click', resetSelection);
  75. tmp.textContent = 'Refresh';
  76. panel.appendChild(tmp);
  77. fonts = document.createElement('div');
  78. panel.appendChild(fonts);
  79. },
  80. cleanup: function cleanup() {
  81. fonts.textContent = '';
  82. },
  83. enabled: false,
  84. get active() {
  85. return active;
  86. },
  87. set active(value) {
  88. active = value;
  89. if (active) {
  90. document.body.addEventListener('click', textLayerClick, true);
  91. resetSelection();
  92. } else {
  93. document.body.removeEventListener('click', textLayerClick, true);
  94. removeSelection();
  95. }
  96. },
  97. fontAdded: function fontAdded(fontObj, url) {
  98. var _this = this;
  99. function properties(obj, list) {
  100. var moreInfo = document.createElement('table');
  101. for (var i = 0; i < list.length; i++) {
  102. var tr = document.createElement('tr');
  103. var td1 = document.createElement('td');
  104. td1.textContent = list[i];
  105. tr.appendChild(td1);
  106. var td2 = document.createElement('td');
  107. td2.textContent = obj[list[i]].toString();
  108. tr.appendChild(td2);
  109. moreInfo.appendChild(tr);
  110. }
  111. return moreInfo;
  112. }
  113. var moreInfo = properties(fontObj, ['name', 'type']);
  114. var fontName = fontObj.loadedName;
  115. var font = document.createElement('div');
  116. var name = document.createElement('span');
  117. name.textContent = fontName;
  118. var download = document.createElement('a');
  119. if (url) {
  120. url = /url\(['"]?([^\)"']+)/.exec(url);
  121. download.href = url[1];
  122. } else if (fontObj.data) {
  123. url = URL.createObjectURL(new Blob([fontObj.data], { type: fontObj.mimeType }));
  124. download.href = url;
  125. }
  126. download.textContent = 'Download';
  127. var logIt = document.createElement('a');
  128. logIt.href = '';
  129. logIt.textContent = 'Log';
  130. logIt.addEventListener('click', function (event) {
  131. event.preventDefault();
  132. console.log(fontObj);
  133. });
  134. var select = document.createElement('input');
  135. select.setAttribute('type', 'checkbox');
  136. select.dataset.fontName = fontName;
  137. select.addEventListener('click', function (select, fontName) {
  138. return function () {
  139. selectFont(fontName, select.checked);
  140. };
  141. }(select, fontName));
  142. font.appendChild(select);
  143. font.appendChild(name);
  144. font.appendChild(document.createTextNode(' '));
  145. font.appendChild(download);
  146. font.appendChild(document.createTextNode(' '));
  147. font.appendChild(logIt);
  148. font.appendChild(moreInfo);
  149. fonts.appendChild(font);
  150. setTimeout(function () {
  151. if (_this.active) {
  152. resetSelection();
  153. }
  154. }, 2000);
  155. }
  156. };
  157. }();
  158. var opMap;
  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. id: 'Stepper',
  167. name: 'Stepper',
  168. panel: null,
  169. manager: null,
  170. init: function init(pdfjsLib) {
  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. opMap = Object.create(null);
  186. for (var key in pdfjsLib.OPS) {
  187. opMap[pdfjsLib.OPS[key]] = key;
  188. }
  189. },
  190. cleanup: function cleanup() {
  191. stepperChooser.textContent = '';
  192. stepperDiv.textContent = '';
  193. steppers = [];
  194. },
  195. enabled: false,
  196. active: false,
  197. create: function create(pageIndex) {
  198. var debug = document.createElement('div');
  199. debug.id = 'stepper' + pageIndex;
  200. debug.setAttribute('hidden', true);
  201. debug.className = 'stepper';
  202. stepperDiv.appendChild(debug);
  203. var b = document.createElement('option');
  204. b.textContent = 'Page ' + (pageIndex + 1);
  205. b.value = pageIndex;
  206. stepperChooser.appendChild(b);
  207. var initBreakPoints = breakPoints[pageIndex] || [];
  208. var stepper = new Stepper(debug, pageIndex, initBreakPoints);
  209. steppers.push(stepper);
  210. if (steppers.length === 1) {
  211. this.selectStepper(pageIndex, false);
  212. }
  213. return stepper;
  214. },
  215. selectStepper: function selectStepper(pageIndex, selectPanel) {
  216. var i;
  217. pageIndex = pageIndex | 0;
  218. if (selectPanel) {
  219. this.manager.selectPanel(this);
  220. }
  221. for (i = 0; i < steppers.length; ++i) {
  222. var stepper = steppers[i];
  223. if (stepper.pageIndex === pageIndex) {
  224. stepper.panel.removeAttribute('hidden');
  225. } else {
  226. stepper.panel.setAttribute('hidden', true);
  227. }
  228. }
  229. var options = stepperChooser.options;
  230. for (i = 0; i < options.length; ++i) {
  231. var option = options[i];
  232. option.selected = (option.value | 0) === pageIndex;
  233. }
  234. },
  235. saveBreakPoints: function saveBreakPoints(pageIndex, bps) {
  236. breakPoints[pageIndex] = bps;
  237. sessionStorage.setItem('pdfjsBreakPoints', JSON.stringify(breakPoints));
  238. }
  239. };
  240. }();
  241. var Stepper = function StepperClosure() {
  242. function c(tag, textContent) {
  243. var d = document.createElement(tag);
  244. if (textContent) {
  245. d.textContent = textContent;
  246. }
  247. return d;
  248. }
  249. function simplifyArgs(args) {
  250. if (typeof args === 'string') {
  251. var MAX_STRING_LENGTH = 75;
  252. return args.length <= MAX_STRING_LENGTH ? args : args.substr(0, MAX_STRING_LENGTH) + '...';
  253. }
  254. if ((typeof args === 'undefined' ? 'undefined' : _typeof(args)) !== 'object' || args === null) {
  255. return args;
  256. }
  257. if ('length' in args) {
  258. var simpleArgs = [],
  259. i,
  260. 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(operatorList) {
  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. this.updateOperatorList(operatorList);
  301. },
  302. updateOperatorList: function updateOperatorList(operatorList) {
  303. var self = this;
  304. function cboxOnClick() {
  305. var x = +this.dataset.idx;
  306. if (this.checked) {
  307. self.breakPoints.push(x);
  308. } else {
  309. self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
  310. }
  311. StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
  312. }
  313. var MAX_OPERATORS_COUNT = 15000;
  314. if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
  315. return;
  316. }
  317. var chunk = document.createDocumentFragment();
  318. var operatorsToDisplay = Math.min(MAX_OPERATORS_COUNT, operatorList.fnArray.length);
  319. for (var i = this.operatorListIdx; i < operatorsToDisplay; i++) {
  320. var line = c('tr');
  321. line.className = 'line';
  322. line.dataset.idx = i;
  323. chunk.appendChild(line);
  324. var checked = this.breakPoints.includes(i);
  325. var args = operatorList.argsArray[i] || [];
  326. var breakCell = c('td');
  327. var cbox = c('input');
  328. cbox.type = 'checkbox';
  329. cbox.className = 'points';
  330. cbox.checked = checked;
  331. cbox.dataset.idx = i;
  332. cbox.onclick = cboxOnClick;
  333. breakCell.appendChild(cbox);
  334. line.appendChild(breakCell);
  335. line.appendChild(c('td', i.toString()));
  336. var fn = opMap[operatorList.fnArray[i]];
  337. var decArgs = args;
  338. if (fn === 'showText') {
  339. var glyphs = args[0];
  340. var newArgs = [];
  341. var str = [];
  342. for (var j = 0; j < glyphs.length; j++) {
  343. var glyph = glyphs[j];
  344. if ((typeof glyph === 'undefined' ? 'undefined' : _typeof(glyph)) === 'object' && glyph !== null) {
  345. str.push(glyph.fontChar);
  346. } else {
  347. if (str.length > 0) {
  348. newArgs.push(str.join(''));
  349. str = [];
  350. }
  351. newArgs.push(glyph);
  352. }
  353. }
  354. if (str.length > 0) {
  355. newArgs.push(str.join(''));
  356. }
  357. decArgs = [newArgs];
  358. }
  359. line.appendChild(c('td', fn));
  360. line.appendChild(c('td', JSON.stringify(simplifyArgs(decArgs))));
  361. }
  362. if (operatorsToDisplay < operatorList.fnArray.length) {
  363. line = c('tr');
  364. var lastCell = c('td', '...');
  365. lastCell.colspan = 4;
  366. chunk.appendChild(lastCell);
  367. }
  368. this.operatorListIdx = operatorList.fnArray.length;
  369. this.table.appendChild(chunk);
  370. },
  371. getNextBreakPoint: function getNextBreakPoint() {
  372. this.breakPoints.sort(function (a, b) {
  373. return a - b;
  374. });
  375. for (var i = 0; i < this.breakPoints.length; i++) {
  376. if (this.breakPoints[i] > this.currentIdx) {
  377. return this.breakPoints[i];
  378. }
  379. }
  380. return null;
  381. },
  382. breakIt: function breakIt(idx, callback) {
  383. StepperManager.selectStepper(this.pageIndex, true);
  384. var self = this;
  385. var dom = document;
  386. self.currentIdx = idx;
  387. var listener = function listener(e) {
  388. switch (e.keyCode) {
  389. case 83:
  390. dom.removeEventListener('keydown', listener);
  391. self.nextBreakPoint = self.currentIdx + 1;
  392. self.goTo(-1);
  393. callback();
  394. break;
  395. case 67:
  396. dom.removeEventListener('keydown', listener);
  397. var breakPoint = self.getNextBreakPoint();
  398. self.nextBreakPoint = breakPoint;
  399. self.goTo(-1);
  400. callback();
  401. break;
  402. }
  403. };
  404. dom.addEventListener('keydown', listener);
  405. self.goTo(idx);
  406. },
  407. goTo: function goTo(idx) {
  408. var allRows = this.panel.getElementsByClassName('line');
  409. for (var x = 0, xx = allRows.length; x < xx; ++x) {
  410. var row = allRows[x];
  411. if ((row.dataset.idx | 0) === idx) {
  412. row.style.backgroundColor = 'rgb(251,250,207)';
  413. row.scrollIntoView();
  414. } else {
  415. row.style.backgroundColor = null;
  416. }
  417. }
  418. }
  419. };
  420. return Stepper;
  421. }();
  422. var Stats = function Stats() {
  423. var stats = [];
  424. function clear(node) {
  425. while (node.hasChildNodes()) {
  426. node.removeChild(node.lastChild);
  427. }
  428. }
  429. function getStatIndex(pageNumber) {
  430. for (var i = 0, ii = stats.length; i < ii; ++i) {
  431. if (stats[i].pageNumber === pageNumber) {
  432. return i;
  433. }
  434. }
  435. return false;
  436. }
  437. return {
  438. id: 'Stats',
  439. name: 'Stats',
  440. panel: null,
  441. manager: null,
  442. init: function init(pdfjsLib) {
  443. this.panel.setAttribute('style', 'padding: 5px;');
  444. },
  445. enabled: false,
  446. active: false,
  447. add: function add(pageNumber, stat) {
  448. if (!stat) {
  449. return;
  450. }
  451. var statsIndex = getStatIndex(pageNumber);
  452. if (statsIndex !== false) {
  453. var b = stats[statsIndex];
  454. this.panel.removeChild(b.div);
  455. stats.splice(statsIndex, 1);
  456. }
  457. var wrapper = document.createElement('div');
  458. wrapper.className = 'stats';
  459. var title = document.createElement('div');
  460. title.className = 'title';
  461. title.textContent = 'Page: ' + pageNumber;
  462. var statsDiv = document.createElement('div');
  463. statsDiv.textContent = stat.toString();
  464. wrapper.appendChild(title);
  465. wrapper.appendChild(statsDiv);
  466. stats.push({
  467. pageNumber: pageNumber,
  468. div: wrapper
  469. });
  470. stats.sort(function (a, b) {
  471. return a.pageNumber - b.pageNumber;
  472. });
  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 cleanup() {
  479. stats = [];
  480. clear(this.panel);
  481. }
  482. };
  483. }();
  484. window.PDFBug = function PDFBugClosure() {
  485. var panelWidth = 300;
  486. var buttons = [];
  487. var activePanel = null;
  488. return {
  489. tools: [FontInspector, StepperManager, Stats],
  490. enable: function enable(ids) {
  491. var all = false,
  492. tools = this.tools;
  493. if (ids.length === 1 && ids[0] === 'all') {
  494. all = true;
  495. }
  496. for (var i = 0; i < tools.length; ++i) {
  497. var tool = tools[i];
  498. if (all || ids.includes(tool.id)) {
  499. tool.enabled = true;
  500. }
  501. }
  502. if (!all) {
  503. tools.sort(function (a, b) {
  504. var indexA = ids.indexOf(a.id);
  505. indexA = indexA < 0 ? tools.length : indexA;
  506. var indexB = ids.indexOf(b.id);
  507. indexB = indexB < 0 ? tools.length : indexB;
  508. return indexA - indexB;
  509. });
  510. }
  511. },
  512. init: function init(pdfjsLib, container) {
  513. var ui = document.createElement('div');
  514. ui.id = 'PDFBug';
  515. var controls = document.createElement('div');
  516. controls.setAttribute('class', 'controls');
  517. ui.appendChild(controls);
  518. var panels = document.createElement('div');
  519. panels.setAttribute('class', 'panels');
  520. ui.appendChild(panels);
  521. container.appendChild(ui);
  522. container.style.right = panelWidth + 'px';
  523. var tools = this.tools;
  524. var self = this;
  525. for (var i = 0; i < tools.length; ++i) {
  526. var tool = tools[i];
  527. var panel = document.createElement('div');
  528. var panelButton = document.createElement('button');
  529. panelButton.textContent = tool.name;
  530. panelButton.addEventListener('click', function (selected) {
  531. return function (event) {
  532. event.preventDefault();
  533. self.selectPanel(selected);
  534. };
  535. }(i));
  536. controls.appendChild(panelButton);
  537. panels.appendChild(panel);
  538. tool.panel = panel;
  539. tool.manager = this;
  540. if (tool.enabled) {
  541. tool.init(pdfjsLib);
  542. } else {
  543. panel.textContent = tool.name + ' is disabled. To enable add ' + ' "' + tool.id + '" to the pdfBug parameter ' + 'and refresh (separate multiple by commas).';
  544. }
  545. buttons.push(panelButton);
  546. }
  547. this.selectPanel(0);
  548. },
  549. cleanup: function cleanup() {
  550. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  551. if (this.tools[i].enabled) {
  552. this.tools[i].cleanup();
  553. }
  554. }
  555. },
  556. selectPanel: function selectPanel(index) {
  557. if (typeof index !== 'number') {
  558. index = this.tools.indexOf(index);
  559. }
  560. if (index === activePanel) {
  561. return;
  562. }
  563. activePanel = index;
  564. var tools = this.tools;
  565. for (var j = 0; j < tools.length; ++j) {
  566. if (j === index) {
  567. buttons[j].setAttribute('class', 'active');
  568. tools[j].active = true;
  569. tools[j].panel.removeAttribute('hidden');
  570. } else {
  571. buttons[j].setAttribute('class', '');
  572. tools[j].active = false;
  573. tools[j].panel.setAttribute('hidden', 'true');
  574. }
  575. }
  576. }
  577. };
  578. }();