pdf_page_view.js 18 KB

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