debugger.js 16 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 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, [
  105. 'name',
  106. 'type'
  107. ]);
  108. var fontName = fontObj.loadedName;
  109. var font = document.createElement('div');
  110. var name = document.createElement('span');
  111. name.textContent = fontName;
  112. var download = document.createElement('a');
  113. if (url) {
  114. url = /url\(['"]?([^\)"']+)/.exec(url);
  115. download.href = url[1];
  116. } else if (fontObj.data) {
  117. url = URL.createObjectURL(new Blob([fontObj.data], { type: fontObj.mimeType }));
  118. download.href = url;
  119. }
  120. download.textContent = 'Download';
  121. var logIt = document.createElement('a');
  122. logIt.href = '';
  123. logIt.textContent = 'Log';
  124. logIt.addEventListener('click', function (event) {
  125. event.preventDefault();
  126. console.log(fontObj);
  127. });
  128. var select = document.createElement('input');
  129. select.setAttribute('type', 'checkbox');
  130. select.dataset.fontName = fontName;
  131. select.addEventListener('click', function (select, fontName) {
  132. return function () {
  133. selectFont(fontName, select.checked);
  134. };
  135. }(select, fontName));
  136. font.appendChild(select);
  137. font.appendChild(name);
  138. font.appendChild(document.createTextNode(' '));
  139. font.appendChild(download);
  140. font.appendChild(document.createTextNode(' '));
  141. font.appendChild(logIt);
  142. font.appendChild(moreInfo);
  143. fonts.appendChild(font);
  144. setTimeout(function () {
  145. if (this.active) {
  146. resetSelection();
  147. }
  148. }.bind(this), 2000);
  149. }
  150. };
  151. }();
  152. var opMap;
  153. var StepperManager = function StepperManagerClosure() {
  154. var steppers = [];
  155. var stepperDiv = null;
  156. var stepperControls = null;
  157. var stepperChooser = null;
  158. var breakPoints = Object.create(null);
  159. return {
  160. id: 'Stepper',
  161. name: 'Stepper',
  162. panel: null,
  163. manager: null,
  164. init: function init(pdfjsLib) {
  165. var self = this;
  166. this.panel.setAttribute('style', 'padding: 5px;');
  167. stepperControls = document.createElement('div');
  168. stepperChooser = document.createElement('select');
  169. stepperChooser.addEventListener('change', function (event) {
  170. self.selectStepper(this.value);
  171. });
  172. stepperControls.appendChild(stepperChooser);
  173. stepperDiv = document.createElement('div');
  174. this.panel.appendChild(stepperControls);
  175. this.panel.appendChild(stepperDiv);
  176. if (sessionStorage.getItem('pdfjsBreakPoints')) {
  177. breakPoints = JSON.parse(sessionStorage.getItem('pdfjsBreakPoints'));
  178. }
  179. opMap = Object.create(null);
  180. for (var key in pdfjsLib.OPS) {
  181. opMap[pdfjsLib.OPS[key]] = key;
  182. }
  183. },
  184. cleanup: function cleanup() {
  185. stepperChooser.textContent = '';
  186. stepperDiv.textContent = '';
  187. steppers = [];
  188. },
  189. enabled: false,
  190. active: false,
  191. create: function create(pageIndex) {
  192. var debug = document.createElement('div');
  193. debug.id = 'stepper' + pageIndex;
  194. debug.setAttribute('hidden', true);
  195. debug.className = 'stepper';
  196. stepperDiv.appendChild(debug);
  197. var b = document.createElement('option');
  198. b.textContent = 'Page ' + (pageIndex + 1);
  199. b.value = pageIndex;
  200. stepperChooser.appendChild(b);
  201. var initBreakPoints = breakPoints[pageIndex] || [];
  202. var stepper = new Stepper(debug, pageIndex, initBreakPoints);
  203. steppers.push(stepper);
  204. if (steppers.length === 1) {
  205. this.selectStepper(pageIndex, false);
  206. }
  207. return stepper;
  208. },
  209. selectStepper: function selectStepper(pageIndex, selectPanel) {
  210. var i;
  211. pageIndex = pageIndex | 0;
  212. if (selectPanel) {
  213. this.manager.selectPanel(this);
  214. }
  215. for (i = 0; i < steppers.length; ++i) {
  216. var stepper = steppers[i];
  217. if (stepper.pageIndex === pageIndex) {
  218. stepper.panel.removeAttribute('hidden');
  219. } else {
  220. stepper.panel.setAttribute('hidden', true);
  221. }
  222. }
  223. var options = stepperChooser.options;
  224. for (i = 0; i < options.length; ++i) {
  225. var option = options[i];
  226. option.selected = (option.value | 0) === pageIndex;
  227. }
  228. },
  229. saveBreakPoints: function saveBreakPoints(pageIndex, bps) {
  230. breakPoints[pageIndex] = bps;
  231. sessionStorage.setItem('pdfjsBreakPoints', JSON.stringify(breakPoints));
  232. }
  233. };
  234. }();
  235. var Stepper = function StepperClosure() {
  236. function c(tag, textContent) {
  237. var d = document.createElement(tag);
  238. if (textContent) {
  239. d.textContent = textContent;
  240. }
  241. return d;
  242. }
  243. function simplifyArgs(args) {
  244. if (typeof args === 'string') {
  245. var MAX_STRING_LENGTH = 75;
  246. return args.length <= MAX_STRING_LENGTH ? args : args.substr(0, MAX_STRING_LENGTH) + '...';
  247. }
  248. if (typeof args !== 'object' || args === null) {
  249. return args;
  250. }
  251. if ('length' in args) {
  252. var simpleArgs = [], i, 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 === '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 (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 (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 () {
  472. stats = [];
  473. clear(this.panel);
  474. }
  475. };
  476. }();
  477. var PDFBug = function PDFBugClosure() {
  478. var panelWidth = 300;
  479. var buttons = [];
  480. var activePanel = null;
  481. return {
  482. tools: [
  483. FontInspector,
  484. StepperManager,
  485. Stats
  486. ],
  487. enable: function (ids) {
  488. var all = false, tools = this.tools;
  489. if (ids.length === 1 && ids[0] === 'all') {
  490. all = true;
  491. }
  492. for (var i = 0; i < tools.length; ++i) {
  493. var tool = tools[i];
  494. if (all || ids.indexOf(tool.id) !== -1) {
  495. tool.enabled = true;
  496. }
  497. }
  498. if (!all) {
  499. tools.sort(function (a, b) {
  500. var indexA = ids.indexOf(a.id);
  501. indexA = indexA < 0 ? tools.length : indexA;
  502. var indexB = ids.indexOf(b.id);
  503. indexB = indexB < 0 ? tools.length : indexB;
  504. return indexA - indexB;
  505. });
  506. }
  507. },
  508. init: function init(pdfjsLib, container) {
  509. var ui = document.createElement('div');
  510. ui.id = 'PDFBug';
  511. var controls = document.createElement('div');
  512. controls.setAttribute('class', 'controls');
  513. ui.appendChild(controls);
  514. var panels = document.createElement('div');
  515. panels.setAttribute('class', 'panels');
  516. ui.appendChild(panels);
  517. container.appendChild(ui);
  518. container.style.right = panelWidth + 'px';
  519. var tools = this.tools;
  520. var self = this;
  521. for (var i = 0; i < tools.length; ++i) {
  522. var tool = tools[i];
  523. var panel = document.createElement('div');
  524. var panelButton = document.createElement('button');
  525. panelButton.textContent = tool.name;
  526. panelButton.addEventListener('click', function (selected) {
  527. return function (event) {
  528. event.preventDefault();
  529. self.selectPanel(selected);
  530. };
  531. }(i));
  532. controls.appendChild(panelButton);
  533. panels.appendChild(panel);
  534. tool.panel = panel;
  535. tool.manager = this;
  536. if (tool.enabled) {
  537. tool.init(pdfjsLib);
  538. } else {
  539. panel.textContent = tool.name + ' is disabled. To enable add ' + ' "' + tool.id + '" to the pdfBug parameter ' + 'and refresh (separate multiple by commas).';
  540. }
  541. buttons.push(panelButton);
  542. }
  543. this.selectPanel(0);
  544. },
  545. cleanup: function cleanup() {
  546. for (var i = 0, ii = this.tools.length; i < ii; i++) {
  547. if (this.tools[i].enabled) {
  548. this.tools[i].cleanup();
  549. }
  550. }
  551. },
  552. selectPanel: function selectPanel(index) {
  553. if (typeof index !== 'number') {
  554. index = this.tools.indexOf(index);
  555. }
  556. if (index === activePanel) {
  557. return;
  558. }
  559. activePanel = index;
  560. var tools = this.tools;
  561. for (var j = 0; j < tools.length; ++j) {
  562. if (j === index) {
  563. buttons[j].setAttribute('class', 'active');
  564. tools[j].active = true;
  565. tools[j].panel.removeAttribute('hidden');
  566. } else {
  567. buttons[j].setAttribute('class', '');
  568. tools[j].active = false;
  569. tools[j].panel.setAttribute('hidden', 'true');
  570. }
  571. }
  572. }
  573. };
  574. }();