2
0

debugger.js 18 KB

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