pdf_page_view.js 20 KB

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