2
0

debugger.js 18 KB

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