pdf_page_view.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFPageView = undefined;
  27. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  28. var _ui_utils = require('./ui_utils');
  29. var _pdf = require('../pdf');
  30. var _dom_events = require('./dom_events');
  31. var _pdf_rendering_queue = require('./pdf_rendering_queue');
  32. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  33. var PDFPageView = function () {
  34. function PDFPageView(options) {
  35. _classCallCheck(this, PDFPageView);
  36. var container = options.container;
  37. var defaultViewport = options.defaultViewport;
  38. this.id = options.id;
  39. this.renderingId = 'page' + this.id;
  40. this.pdfPage = null;
  41. this.pageLabel = null;
  42. this.rotation = 0;
  43. this.scale = options.scale || _ui_utils.DEFAULT_SCALE;
  44. this.viewport = defaultViewport;
  45. this.pdfPageRotate = defaultViewport.rotation;
  46. this.hasRestrictedScaling = false;
  47. this.enhanceTextSelection = options.enhanceTextSelection || false;
  48. this.renderInteractiveForms = options.renderInteractiveForms || false;
  49. this.eventBus = options.eventBus || (0, _dom_events.getGlobalEventBus)();
  50. this.renderingQueue = options.renderingQueue;
  51. this.textLayerFactory = options.textLayerFactory;
  52. this.annotationLayerFactory = options.annotationLayerFactory;
  53. this.renderer = options.renderer || _ui_utils.RendererType.CANVAS;
  54. this.l10n = options.l10n || _ui_utils.NullL10n;
  55. this.paintTask = null;
  56. this.paintedViewportMap = new WeakMap();
  57. this.renderingState = _pdf_rendering_queue.RenderingStates.INITIAL;
  58. this.resume = null;
  59. this.error = null;
  60. this.onBeforeDraw = null;
  61. this.onAfterDraw = null;
  62. this.annotationLayer = null;
  63. this.textLayer = null;
  64. this.zoomLayer = null;
  65. var div = document.createElement('div');
  66. div.className = 'page';
  67. div.style.width = Math.floor(this.viewport.width) + 'px';
  68. div.style.height = Math.floor(this.viewport.height) + 'px';
  69. div.setAttribute('data-page-number', this.id);
  70. this.div = div;
  71. container.appendChild(div);
  72. }
  73. _createClass(PDFPageView, [{
  74. key: 'setPdfPage',
  75. value: function setPdfPage(pdfPage) {
  76. this.pdfPage = pdfPage;
  77. this.pdfPageRotate = pdfPage.rotate;
  78. var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  79. this.viewport = pdfPage.getViewport(this.scale * _ui_utils.CSS_UNITS, totalRotation);
  80. this.stats = pdfPage.stats;
  81. this.reset();
  82. }
  83. }, {
  84. key: 'destroy',
  85. value: function destroy() {
  86. this.reset();
  87. if (this.pdfPage) {
  88. this.pdfPage.cleanup();
  89. }
  90. }
  91. }, {
  92. key: '_resetZoomLayer',
  93. value: function _resetZoomLayer() {
  94. var removeFromDOM = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  95. if (!this.zoomLayer) {
  96. return;
  97. }
  98. var zoomLayerCanvas = this.zoomLayer.firstChild;
  99. this.paintedViewportMap.delete(zoomLayerCanvas);
  100. zoomLayerCanvas.width = 0;
  101. zoomLayerCanvas.height = 0;
  102. if (removeFromDOM) {
  103. this.zoomLayer.remove();
  104. }
  105. this.zoomLayer = null;
  106. }
  107. }, {
  108. key: 'reset',
  109. value: function reset() {
  110. var keepZoomLayer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  111. var keepAnnotations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  112. this.cancelRendering(keepAnnotations);
  113. var div = this.div;
  114. div.style.width = Math.floor(this.viewport.width) + 'px';
  115. div.style.height = Math.floor(this.viewport.height) + 'px';
  116. var childNodes = div.childNodes;
  117. var currentZoomLayerNode = keepZoomLayer && this.zoomLayer || null;
  118. var currentAnnotationNode = keepAnnotations && this.annotationLayer && this.annotationLayer.div || null;
  119. for (var i = childNodes.length - 1; i >= 0; i--) {
  120. var node = childNodes[i];
  121. if (currentZoomLayerNode === node || currentAnnotationNode === node) {
  122. continue;
  123. }
  124. div.removeChild(node);
  125. }
  126. div.removeAttribute('data-loaded');
  127. if (currentAnnotationNode) {
  128. this.annotationLayer.hide();
  129. } else if (this.annotationLayer) {
  130. this.annotationLayer.cancel();
  131. this.annotationLayer = null;
  132. }
  133. if (!currentZoomLayerNode) {
  134. if (this.canvas) {
  135. this.paintedViewportMap.delete(this.canvas);
  136. this.canvas.width = 0;
  137. this.canvas.height = 0;
  138. delete this.canvas;
  139. }
  140. this._resetZoomLayer();
  141. }
  142. if (this.svg) {
  143. this.paintedViewportMap.delete(this.svg);
  144. delete this.svg;
  145. }
  146. this.loadingIconDiv = document.createElement('div');
  147. this.loadingIconDiv.className = 'loadingIcon';
  148. div.appendChild(this.loadingIconDiv);
  149. }
  150. }, {
  151. key: 'update',
  152. value: function update(scale, rotation) {
  153. this.scale = scale || this.scale;
  154. if (typeof rotation !== 'undefined') {
  155. this.rotation = rotation;
  156. }
  157. var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  158. this.viewport = this.viewport.clone({
  159. scale: this.scale * _ui_utils.CSS_UNITS,
  160. rotation: totalRotation
  161. });
  162. if (this.svg) {
  163. this.cssTransform(this.svg, true);
  164. this.eventBus.dispatch('pagerendered', {
  165. source: this,
  166. pageNumber: this.id,
  167. cssTransform: true
  168. });
  169. return;
  170. }
  171. var isScalingRestricted = false;
  172. if (this.canvas && _pdf.PDFJS.maxCanvasPixels > 0) {
  173. var outputScale = this.outputScale;
  174. if ((Math.floor(this.viewport.width) * outputScale.sx | 0) * (Math.floor(this.viewport.height) * outputScale.sy | 0) > _pdf.PDFJS.maxCanvasPixels) {
  175. isScalingRestricted = true;
  176. }
  177. }
  178. if (this.canvas) {
  179. if (_pdf.PDFJS.useOnlyCssZoom || this.hasRestrictedScaling && isScalingRestricted) {
  180. this.cssTransform(this.canvas, true);
  181. this.eventBus.dispatch('pagerendered', {
  182. source: this,
  183. pageNumber: this.id,
  184. cssTransform: true
  185. });
  186. return;
  187. }
  188. if (!this.zoomLayer && !this.canvas.hasAttribute('hidden')) {
  189. this.zoomLayer = this.canvas.parentNode;
  190. this.zoomLayer.style.position = 'absolute';
  191. }
  192. }
  193. if (this.zoomLayer) {
  194. this.cssTransform(this.zoomLayer.firstChild);
  195. }
  196. this.reset(true, true);
  197. }
  198. }, {
  199. key: 'cancelRendering',
  200. value: function cancelRendering() {
  201. var keepAnnotations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  202. if (this.paintTask) {
  203. this.paintTask.cancel();
  204. this.paintTask = null;
  205. }
  206. this.renderingState = _pdf_rendering_queue.RenderingStates.INITIAL;
  207. this.resume = null;
  208. if (this.textLayer) {
  209. this.textLayer.cancel();
  210. this.textLayer = null;
  211. }
  212. if (!keepAnnotations && this.annotationLayer) {
  213. this.annotationLayer.cancel();
  214. this.annotationLayer = null;
  215. }
  216. }
  217. }, {
  218. key: 'cssTransform',
  219. value: function cssTransform(target) {
  220. var redrawAnnotations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  221. var width = this.viewport.width;
  222. var height = this.viewport.height;
  223. var div = this.div;
  224. target.style.width = target.parentNode.style.width = div.style.width = Math.floor(width) + 'px';
  225. target.style.height = target.parentNode.style.height = div.style.height = Math.floor(height) + 'px';
  226. var relativeRotation = this.viewport.rotation - this.paintedViewportMap.get(target).rotation;
  227. var absRotation = Math.abs(relativeRotation);
  228. var scaleX = 1,
  229. scaleY = 1;
  230. if (absRotation === 90 || absRotation === 270) {
  231. scaleX = height / width;
  232. scaleY = width / height;
  233. }
  234. var cssTransform = 'rotate(' + relativeRotation + 'deg) ' + 'scale(' + scaleX + ',' + scaleY + ')';
  235. target.style.transform = cssTransform;
  236. if (this.textLayer) {
  237. var textLayerViewport = this.textLayer.viewport;
  238. var textRelativeRotation = this.viewport.rotation - textLayerViewport.rotation;
  239. var textAbsRotation = Math.abs(textRelativeRotation);
  240. var scale = width / textLayerViewport.width;
  241. if (textAbsRotation === 90 || textAbsRotation === 270) {
  242. scale = width / textLayerViewport.height;
  243. }
  244. var textLayerDiv = this.textLayer.textLayerDiv;
  245. var transX = void 0,
  246. transY = void 0;
  247. switch (textAbsRotation) {
  248. case 0:
  249. transX = transY = 0;
  250. break;
  251. case 90:
  252. transX = 0;
  253. transY = '-' + textLayerDiv.style.height;
  254. break;
  255. case 180:
  256. transX = '-' + textLayerDiv.style.width;
  257. transY = '-' + textLayerDiv.style.height;
  258. break;
  259. case 270:
  260. transX = '-' + textLayerDiv.style.width;
  261. transY = 0;
  262. break;
  263. default:
  264. console.error('Bad rotation value.');
  265. break;
  266. }
  267. textLayerDiv.style.transform = 'rotate(' + textAbsRotation + 'deg) ' + 'scale(' + scale + ', ' + scale + ') ' + 'translate(' + transX + ', ' + transY + ')';
  268. textLayerDiv.style.transformOrigin = '0% 0%';
  269. }
  270. if (redrawAnnotations && this.annotationLayer) {
  271. this.annotationLayer.render(this.viewport, 'display');
  272. }
  273. }
  274. }, {
  275. key: 'getPagePoint',
  276. value: function getPagePoint(x, y) {
  277. return this.viewport.convertToPdfPoint(x, y);
  278. }
  279. }, {
  280. key: 'draw',
  281. value: function draw() {
  282. var _this = this;
  283. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  284. console.error('Must be in new state before drawing');
  285. this.reset();
  286. }
  287. if (!this.pdfPage) {
  288. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  289. return Promise.reject(new Error('Page is not loaded'));
  290. }
  291. this.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  292. var pdfPage = this.pdfPage;
  293. var div = this.div;
  294. var canvasWrapper = document.createElement('div');
  295. canvasWrapper.style.width = div.style.width;
  296. canvasWrapper.style.height = div.style.height;
  297. canvasWrapper.classList.add('canvasWrapper');
  298. if (this.annotationLayer && this.annotationLayer.div) {
  299. div.insertBefore(canvasWrapper, this.annotationLayer.div);
  300. } else {
  301. div.appendChild(canvasWrapper);
  302. }
  303. var textLayer = null;
  304. if (this.textLayerFactory) {
  305. var textLayerDiv = document.createElement('div');
  306. textLayerDiv.className = 'textLayer';
  307. textLayerDiv.style.width = canvasWrapper.style.width;
  308. textLayerDiv.style.height = canvasWrapper.style.height;
  309. if (this.annotationLayer && this.annotationLayer.div) {
  310. div.insertBefore(textLayerDiv, this.annotationLayer.div);
  311. } else {
  312. div.appendChild(textLayerDiv);
  313. }
  314. textLayer = this.textLayerFactory.createTextLayerBuilder(textLayerDiv, this.id - 1, this.viewport, this.enhanceTextSelection);
  315. }
  316. this.textLayer = textLayer;
  317. var renderContinueCallback = null;
  318. if (this.renderingQueue) {
  319. renderContinueCallback = function renderContinueCallback(cont) {
  320. if (!_this.renderingQueue.isHighestPriority(_this)) {
  321. _this.renderingState = _pdf_rendering_queue.RenderingStates.PAUSED;
  322. _this.resume = function () {
  323. _this.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  324. cont();
  325. };
  326. return;
  327. }
  328. cont();
  329. };
  330. }
  331. var finishPaintTask = function finishPaintTask(error) {
  332. if (paintTask === _this.paintTask) {
  333. _this.paintTask = null;
  334. }
  335. if (error instanceof _pdf.RenderingCancelledException) {
  336. _this.error = null;
  337. return Promise.resolve(undefined);
  338. }
  339. _this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  340. if (_this.loadingIconDiv) {
  341. div.removeChild(_this.loadingIconDiv);
  342. delete _this.loadingIconDiv;
  343. }
  344. _this._resetZoomLayer(true);
  345. _this.error = error;
  346. _this.stats = pdfPage.stats;
  347. if (_this.onAfterDraw) {
  348. _this.onAfterDraw();
  349. }
  350. _this.eventBus.dispatch('pagerendered', {
  351. source: _this,
  352. pageNumber: _this.id,
  353. cssTransform: false
  354. });
  355. if (error) {
  356. return Promise.reject(error);
  357. }
  358. return Promise.resolve(undefined);
  359. };
  360. var paintTask = this.renderer === _ui_utils.RendererType.SVG ? this.paintOnSvg(canvasWrapper) : this.paintOnCanvas(canvasWrapper);
  361. paintTask.onRenderContinue = renderContinueCallback;
  362. this.paintTask = paintTask;
  363. var resultPromise = paintTask.promise.then(function () {
  364. return finishPaintTask(null).then(function () {
  365. if (textLayer) {
  366. var readableStream = pdfPage.streamTextContent({ normalizeWhitespace: true });
  367. textLayer.setTextContentStream(readableStream);
  368. textLayer.render();
  369. }
  370. });
  371. }, function (reason) {
  372. return finishPaintTask(reason);
  373. });
  374. if (this.annotationLayerFactory) {
  375. if (!this.annotationLayer) {
  376. this.annotationLayer = this.annotationLayerFactory.createAnnotationLayerBuilder(div, pdfPage, this.renderInteractiveForms, this.l10n);
  377. }
  378. this.annotationLayer.render(this.viewport, 'display');
  379. }
  380. div.setAttribute('data-loaded', true);
  381. if (this.onBeforeDraw) {
  382. this.onBeforeDraw();
  383. }
  384. return resultPromise;
  385. }
  386. }, {
  387. key: 'paintOnCanvas',
  388. value: function paintOnCanvas(canvasWrapper) {
  389. var renderCapability = (0, _pdf.createPromiseCapability)();
  390. var result = {
  391. promise: renderCapability.promise,
  392. onRenderContinue: function onRenderContinue(cont) {
  393. cont();
  394. },
  395. cancel: function cancel() {
  396. renderTask.cancel();
  397. }
  398. };
  399. var viewport = this.viewport;
  400. var canvas = document.createElement('canvas');
  401. canvas.id = this.renderingId;
  402. canvas.setAttribute('hidden', 'hidden');
  403. var isCanvasHidden = true;
  404. var showCanvas = function showCanvas() {
  405. if (isCanvasHidden) {
  406. canvas.removeAttribute('hidden');
  407. isCanvasHidden = false;
  408. }
  409. };
  410. canvasWrapper.appendChild(canvas);
  411. this.canvas = canvas;
  412. canvas.mozOpaque = true;
  413. var ctx = canvas.getContext('2d', { alpha: false });
  414. var outputScale = (0, _ui_utils.getOutputScale)(ctx);
  415. this.outputScale = outputScale;
  416. if (_pdf.PDFJS.useOnlyCssZoom) {
  417. var actualSizeViewport = viewport.clone({ scale: _ui_utils.CSS_UNITS });
  418. outputScale.sx *= actualSizeViewport.width / viewport.width;
  419. outputScale.sy *= actualSizeViewport.height / viewport.height;
  420. outputScale.scaled = true;
  421. }
  422. if (_pdf.PDFJS.maxCanvasPixels > 0) {
  423. var pixelsInViewport = viewport.width * viewport.height;
  424. var maxScale = Math.sqrt(_pdf.PDFJS.maxCanvasPixels / pixelsInViewport);
  425. if (outputScale.sx > maxScale || outputScale.sy > maxScale) {
  426. outputScale.sx = maxScale;
  427. outputScale.sy = maxScale;
  428. outputScale.scaled = true;
  429. this.hasRestrictedScaling = true;
  430. } else {
  431. this.hasRestrictedScaling = false;
  432. }
  433. }
  434. var sfx = (0, _ui_utils.approximateFraction)(outputScale.sx);
  435. var sfy = (0, _ui_utils.approximateFraction)(outputScale.sy);
  436. canvas.width = (0, _ui_utils.roundToDivide)(viewport.width * outputScale.sx, sfx[0]);
  437. canvas.height = (0, _ui_utils.roundToDivide)(viewport.height * outputScale.sy, sfy[0]);
  438. canvas.style.width = (0, _ui_utils.roundToDivide)(viewport.width, sfx[1]) + 'px';
  439. canvas.style.height = (0, _ui_utils.roundToDivide)(viewport.height, sfy[1]) + 'px';
  440. this.paintedViewportMap.set(canvas, viewport);
  441. var transform = !outputScale.scaled ? null : [outputScale.sx, 0, 0, outputScale.sy, 0, 0];
  442. var renderContext = {
  443. canvasContext: ctx,
  444. transform: transform,
  445. viewport: this.viewport,
  446. renderInteractiveForms: this.renderInteractiveForms
  447. };
  448. var renderTask = this.pdfPage.render(renderContext);
  449. renderTask.onContinue = function (cont) {
  450. showCanvas();
  451. if (result.onRenderContinue) {
  452. result.onRenderContinue(cont);
  453. } else {
  454. cont();
  455. }
  456. };
  457. renderTask.promise.then(function () {
  458. showCanvas();
  459. renderCapability.resolve(undefined);
  460. }, function (error) {
  461. showCanvas();
  462. renderCapability.reject(error);
  463. });
  464. return result;
  465. }
  466. }, {
  467. key: 'paintOnSvg',
  468. value: function paintOnSvg(wrapper) {
  469. var _this2 = this;
  470. var cancelled = false;
  471. var ensureNotCancelled = function ensureNotCancelled() {
  472. if (cancelled) {
  473. throw new _pdf.RenderingCancelledException('Rendering cancelled, page ' + _this2.id, 'svg');
  474. }
  475. };
  476. var pdfPage = this.pdfPage;
  477. var actualSizeViewport = this.viewport.clone({ scale: _ui_utils.CSS_UNITS });
  478. var promise = pdfPage.getOperatorList().then(function (opList) {
  479. ensureNotCancelled();
  480. var svgGfx = new _pdf.SVGGraphics(pdfPage.commonObjs, pdfPage.objs);
  481. return svgGfx.getSVG(opList, actualSizeViewport).then(function (svg) {
  482. ensureNotCancelled();
  483. _this2.svg = svg;
  484. _this2.paintedViewportMap.set(svg, actualSizeViewport);
  485. svg.style.width = wrapper.style.width;
  486. svg.style.height = wrapper.style.height;
  487. _this2.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  488. wrapper.appendChild(svg);
  489. });
  490. });
  491. return {
  492. promise: promise,
  493. onRenderContinue: function onRenderContinue(cont) {
  494. cont();
  495. },
  496. cancel: function cancel() {
  497. cancelled = true;
  498. }
  499. };
  500. }
  501. }, {
  502. key: 'setPageLabel',
  503. value: function setPageLabel(label) {
  504. this.pageLabel = typeof label === 'string' ? label : null;
  505. if (this.pageLabel !== null) {
  506. this.div.setAttribute('data-page-label', this.pageLabel);
  507. } else {
  508. this.div.removeAttribute('data-page-label');
  509. }
  510. }
  511. }, {
  512. key: 'width',
  513. get: function get() {
  514. return this.viewport.width;
  515. }
  516. }, {
  517. key: 'height',
  518. get: function get() {
  519. return this.viewport.height;
  520. }
  521. }]);
  522. return PDFPageView;
  523. }();
  524. exports.PDFPageView = PDFPageView;