2
0

pdf_page_view.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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 _pdf = require("../pdf");
  28. var _ui_utils = require("./ui_utils.js");
  29. var _app_options = require("./app_options.js");
  30. var _l10n_utils = require("./l10n_utils.js");
  31. var _text_accessibility = require("./text_accessibility.js");
  32. const MAX_CANVAS_PIXELS = _app_options.compatibilityParams.maxCanvasPixels || 16777216;
  33. class PDFPageView {
  34. #annotationMode = _pdf.AnnotationMode.ENABLE_FORMS;
  35. #useThumbnailCanvas = {
  36. initialOptionalContent: true,
  37. regularAnnotations: true
  38. };
  39. constructor(options) {
  40. const container = options.container;
  41. const defaultViewport = options.defaultViewport;
  42. this.id = options.id;
  43. this.renderingId = "page" + this.id;
  44. this.pdfPage = null;
  45. this.pageLabel = null;
  46. this.rotation = 0;
  47. this.scale = options.scale || _ui_utils.DEFAULT_SCALE;
  48. this.viewport = defaultViewport;
  49. this.pdfPageRotate = defaultViewport.rotation;
  50. this._optionalContentConfigPromise = options.optionalContentConfigPromise || null;
  51. this.hasRestrictedScaling = false;
  52. this.textLayerMode = options.textLayerMode ?? _ui_utils.TextLayerMode.ENABLE;
  53. this.#annotationMode = options.annotationMode ?? _pdf.AnnotationMode.ENABLE_FORMS;
  54. this.imageResourcesPath = options.imageResourcesPath || "";
  55. this.useOnlyCssZoom = options.useOnlyCssZoom || false;
  56. this.maxCanvasPixels = options.maxCanvasPixels || MAX_CANVAS_PIXELS;
  57. this.pageColors = options.pageColors || null;
  58. this.eventBus = options.eventBus;
  59. this.renderingQueue = options.renderingQueue;
  60. this.textLayerFactory = options.textLayerFactory;
  61. this.annotationLayerFactory = options.annotationLayerFactory;
  62. this.annotationEditorLayerFactory = options.annotationEditorLayerFactory;
  63. this.xfaLayerFactory = options.xfaLayerFactory;
  64. this.textHighlighter = options.textHighlighterFactory?.createTextHighlighter({
  65. pageIndex: this.id - 1,
  66. eventBus: this.eventBus
  67. });
  68. this.structTreeLayerFactory = options.structTreeLayerFactory;
  69. this.renderer = options.renderer || _ui_utils.RendererType.CANVAS;
  70. this.l10n = options.l10n || _l10n_utils.NullL10n;
  71. this.paintTask = null;
  72. this.paintedViewportMap = new WeakMap();
  73. this.renderingState = _ui_utils.RenderingStates.INITIAL;
  74. this.resume = null;
  75. this._renderError = null;
  76. this._isStandalone = !this.renderingQueue?.hasViewer();
  77. this._annotationCanvasMap = null;
  78. this.annotationLayer = null;
  79. this.annotationEditorLayer = null;
  80. this.textLayer = null;
  81. this.zoomLayer = null;
  82. this.xfaLayer = null;
  83. this.structTreeLayer = null;
  84. const div = document.createElement("div");
  85. div.className = "page";
  86. div.style.width = Math.floor(this.viewport.width) + "px";
  87. div.style.height = Math.floor(this.viewport.height) + "px";
  88. div.setAttribute("data-page-number", this.id);
  89. div.setAttribute("role", "region");
  90. this.l10n.get("page_landmark", {
  91. page: this.id
  92. }).then(msg => {
  93. div.setAttribute("aria-label", msg);
  94. });
  95. this.div = div;
  96. container?.append(div);
  97. if (this._isStandalone) {
  98. const {
  99. optionalContentConfigPromise
  100. } = options;
  101. if (optionalContentConfigPromise) {
  102. optionalContentConfigPromise.then(optionalContentConfig => {
  103. if (optionalContentConfigPromise !== this._optionalContentConfigPromise) {
  104. return;
  105. }
  106. this.#useThumbnailCanvas.initialOptionalContent = optionalContentConfig.hasInitialVisibility;
  107. });
  108. }
  109. }
  110. }
  111. setPdfPage(pdfPage) {
  112. this.pdfPage = pdfPage;
  113. this.pdfPageRotate = pdfPage.rotate;
  114. const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  115. this.viewport = pdfPage.getViewport({
  116. scale: this.scale * _pdf.PixelsPerInch.PDF_TO_CSS_UNITS,
  117. rotation: totalRotation
  118. });
  119. this.reset();
  120. }
  121. destroy() {
  122. this.reset();
  123. this.pdfPage?.cleanup();
  124. }
  125. async _renderAnnotationLayer() {
  126. let error = null;
  127. try {
  128. await this.annotationLayer.render(this.viewport, "display");
  129. } catch (ex) {
  130. console.error(`_renderAnnotationLayer: "${ex}".`);
  131. error = ex;
  132. } finally {
  133. this.eventBus.dispatch("annotationlayerrendered", {
  134. source: this,
  135. pageNumber: this.id,
  136. error
  137. });
  138. }
  139. }
  140. async _renderAnnotationEditorLayer() {
  141. let error = null;
  142. try {
  143. await this.annotationEditorLayer.render(this.viewport, "display");
  144. } catch (ex) {
  145. console.error(`_renderAnnotationEditorLayer: "${ex}".`);
  146. error = ex;
  147. } finally {
  148. this.eventBus.dispatch("annotationeditorlayerrendered", {
  149. source: this,
  150. pageNumber: this.id,
  151. error
  152. });
  153. }
  154. }
  155. async _renderXfaLayer() {
  156. let error = null;
  157. try {
  158. const result = await this.xfaLayer.render(this.viewport, "display");
  159. if (result?.textDivs && this.textHighlighter) {
  160. this._buildXfaTextContentItems(result.textDivs);
  161. }
  162. } catch (ex) {
  163. console.error(`_renderXfaLayer: "${ex}".`);
  164. error = ex;
  165. } finally {
  166. this.eventBus.dispatch("xfalayerrendered", {
  167. source: this,
  168. pageNumber: this.id,
  169. error
  170. });
  171. }
  172. }
  173. async _buildXfaTextContentItems(textDivs) {
  174. const text = await this.pdfPage.getTextContent();
  175. const items = [];
  176. for (const item of text.items) {
  177. items.push(item.str);
  178. }
  179. this.textHighlighter.setTextMapping(textDivs, items);
  180. this.textHighlighter.enable();
  181. }
  182. _resetZoomLayer(removeFromDOM = false) {
  183. if (!this.zoomLayer) {
  184. return;
  185. }
  186. const zoomLayerCanvas = this.zoomLayer.firstChild;
  187. this.paintedViewportMap.delete(zoomLayerCanvas);
  188. zoomLayerCanvas.width = 0;
  189. zoomLayerCanvas.height = 0;
  190. if (removeFromDOM) {
  191. this.zoomLayer.remove();
  192. }
  193. this.zoomLayer = null;
  194. }
  195. reset({
  196. keepZoomLayer = false,
  197. keepAnnotationLayer = false,
  198. keepAnnotationEditorLayer = false,
  199. keepXfaLayer = false
  200. } = {}) {
  201. this.cancelRendering({
  202. keepAnnotationLayer,
  203. keepAnnotationEditorLayer,
  204. keepXfaLayer
  205. });
  206. this.renderingState = _ui_utils.RenderingStates.INITIAL;
  207. const div = this.div;
  208. div.style.width = Math.floor(this.viewport.width) + "px";
  209. div.style.height = Math.floor(this.viewport.height) + "px";
  210. const childNodes = div.childNodes,
  211. zoomLayerNode = keepZoomLayer && this.zoomLayer || null,
  212. annotationLayerNode = keepAnnotationLayer && this.annotationLayer?.div || null,
  213. annotationEditorLayerNode = keepAnnotationEditorLayer && this.annotationEditorLayer?.div || null,
  214. xfaLayerNode = keepXfaLayer && this.xfaLayer?.div || null;
  215. for (let i = childNodes.length - 1; i >= 0; i--) {
  216. const node = childNodes[i];
  217. switch (node) {
  218. case zoomLayerNode:
  219. case annotationLayerNode:
  220. case annotationEditorLayerNode:
  221. case xfaLayerNode:
  222. continue;
  223. }
  224. node.remove();
  225. }
  226. div.removeAttribute("data-loaded");
  227. if (annotationLayerNode) {
  228. this.annotationLayer.hide();
  229. }
  230. if (annotationEditorLayerNode) {
  231. this.annotationEditorLayer.hide();
  232. } else {
  233. this.annotationEditorLayer?.destroy();
  234. }
  235. if (xfaLayerNode) {
  236. this.xfaLayer.hide();
  237. }
  238. if (!zoomLayerNode) {
  239. if (this.canvas) {
  240. this.paintedViewportMap.delete(this.canvas);
  241. this.canvas.width = 0;
  242. this.canvas.height = 0;
  243. delete this.canvas;
  244. }
  245. this._resetZoomLayer();
  246. }
  247. if (this.svg) {
  248. this.paintedViewportMap.delete(this.svg);
  249. delete this.svg;
  250. }
  251. this.loadingIconDiv = document.createElement("div");
  252. this.loadingIconDiv.className = "loadingIcon notVisible";
  253. if (this._isStandalone) {
  254. this.toggleLoadingIconSpinner(true);
  255. }
  256. this.loadingIconDiv.setAttribute("role", "img");
  257. this.l10n.get("loading").then(msg => {
  258. this.loadingIconDiv?.setAttribute("aria-label", msg);
  259. });
  260. div.append(this.loadingIconDiv);
  261. }
  262. update({
  263. scale = 0,
  264. rotation = null,
  265. optionalContentConfigPromise = null
  266. }) {
  267. this.scale = scale || this.scale;
  268. if (typeof rotation === "number") {
  269. this.rotation = rotation;
  270. }
  271. if (optionalContentConfigPromise instanceof Promise) {
  272. this._optionalContentConfigPromise = optionalContentConfigPromise;
  273. optionalContentConfigPromise.then(optionalContentConfig => {
  274. if (optionalContentConfigPromise !== this._optionalContentConfigPromise) {
  275. return;
  276. }
  277. this.#useThumbnailCanvas.initialOptionalContent = optionalContentConfig.hasInitialVisibility;
  278. });
  279. }
  280. const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  281. this.viewport = this.viewport.clone({
  282. scale: this.scale * _pdf.PixelsPerInch.PDF_TO_CSS_UNITS,
  283. rotation: totalRotation
  284. });
  285. if (this._isStandalone) {
  286. _ui_utils.docStyle.setProperty("--scale-factor", this.viewport.scale);
  287. }
  288. if (this.svg) {
  289. this.cssTransform({
  290. target: this.svg,
  291. redrawAnnotationLayer: true,
  292. redrawAnnotationEditorLayer: true,
  293. redrawXfaLayer: true
  294. });
  295. this.eventBus.dispatch("pagerendered", {
  296. source: this,
  297. pageNumber: this.id,
  298. cssTransform: true,
  299. timestamp: performance.now(),
  300. error: this._renderError
  301. });
  302. return;
  303. }
  304. let isScalingRestricted = false;
  305. if (this.canvas && this.maxCanvasPixels > 0) {
  306. const outputScale = this.outputScale;
  307. if ((Math.floor(this.viewport.width) * outputScale.sx | 0) * (Math.floor(this.viewport.height) * outputScale.sy | 0) > this.maxCanvasPixels) {
  308. isScalingRestricted = true;
  309. }
  310. }
  311. if (this.canvas) {
  312. if (this.useOnlyCssZoom || this.hasRestrictedScaling && isScalingRestricted) {
  313. this.cssTransform({
  314. target: this.canvas,
  315. redrawAnnotationLayer: true,
  316. redrawAnnotationEditorLayer: true,
  317. redrawXfaLayer: true
  318. });
  319. this.eventBus.dispatch("pagerendered", {
  320. source: this,
  321. pageNumber: this.id,
  322. cssTransform: true,
  323. timestamp: performance.now(),
  324. error: this._renderError
  325. });
  326. return;
  327. }
  328. if (!this.zoomLayer && !this.canvas.hidden) {
  329. this.zoomLayer = this.canvas.parentNode;
  330. this.zoomLayer.style.position = "absolute";
  331. }
  332. }
  333. if (this.zoomLayer) {
  334. this.cssTransform({
  335. target: this.zoomLayer.firstChild
  336. });
  337. }
  338. this.reset({
  339. keepZoomLayer: true,
  340. keepAnnotationLayer: true,
  341. keepAnnotationEditorLayer: true,
  342. keepXfaLayer: true
  343. });
  344. }
  345. cancelRendering({
  346. keepAnnotationLayer = false,
  347. keepAnnotationEditorLayer = false,
  348. keepXfaLayer = false
  349. } = {}) {
  350. if (this.paintTask) {
  351. this.paintTask.cancel();
  352. this.paintTask = null;
  353. }
  354. this.resume = null;
  355. if (this.textLayer) {
  356. this.textLayer.cancel();
  357. this.textLayer = null;
  358. }
  359. if (this.annotationLayer && (!keepAnnotationLayer || !this.annotationLayer.div)) {
  360. this.annotationLayer.cancel();
  361. this.annotationLayer = null;
  362. this._annotationCanvasMap = null;
  363. }
  364. if (this.annotationEditorLayer && (!keepAnnotationEditorLayer || !this.annotationEditorLayer.div)) {
  365. this.annotationEditorLayer.cancel();
  366. this.annotationEditorLayer = null;
  367. }
  368. if (this.xfaLayer && (!keepXfaLayer || !this.xfaLayer.div)) {
  369. this.xfaLayer.cancel();
  370. this.xfaLayer = null;
  371. this.textHighlighter?.disable();
  372. }
  373. if (this._onTextLayerRendered) {
  374. this.eventBus._off("textlayerrendered", this._onTextLayerRendered);
  375. this._onTextLayerRendered = null;
  376. }
  377. }
  378. cssTransform({
  379. target,
  380. redrawAnnotationLayer = false,
  381. redrawAnnotationEditorLayer = false,
  382. redrawXfaLayer = false
  383. }) {
  384. const width = this.viewport.width;
  385. const height = this.viewport.height;
  386. const div = this.div;
  387. target.style.width = target.parentNode.style.width = div.style.width = Math.floor(width) + "px";
  388. target.style.height = target.parentNode.style.height = div.style.height = Math.floor(height) + "px";
  389. const relativeRotation = this.viewport.rotation - this.paintedViewportMap.get(target).rotation;
  390. const absRotation = Math.abs(relativeRotation);
  391. let scaleX = 1,
  392. scaleY = 1;
  393. if (absRotation === 90 || absRotation === 270) {
  394. scaleX = height / width;
  395. scaleY = width / height;
  396. }
  397. target.style.transform = `rotate(${relativeRotation}deg) scale(${scaleX}, ${scaleY})`;
  398. if (this.textLayer) {
  399. const textLayerViewport = this.textLayer.viewport;
  400. const textRelativeRotation = this.viewport.rotation - textLayerViewport.rotation;
  401. const textAbsRotation = Math.abs(textRelativeRotation);
  402. let scale = width / textLayerViewport.width;
  403. if (textAbsRotation === 90 || textAbsRotation === 270) {
  404. scale = width / textLayerViewport.height;
  405. }
  406. const textLayerDiv = this.textLayer.textLayerDiv;
  407. let transX, transY;
  408. switch (textAbsRotation) {
  409. case 0:
  410. transX = transY = 0;
  411. break;
  412. case 90:
  413. transX = 0;
  414. transY = "-" + textLayerDiv.style.height;
  415. break;
  416. case 180:
  417. transX = "-" + textLayerDiv.style.width;
  418. transY = "-" + textLayerDiv.style.height;
  419. break;
  420. case 270:
  421. transX = "-" + textLayerDiv.style.width;
  422. transY = 0;
  423. break;
  424. default:
  425. console.error("Bad rotation value.");
  426. break;
  427. }
  428. textLayerDiv.style.transform = `rotate(${textAbsRotation}deg) ` + `scale(${scale}) ` + `translate(${transX}, ${transY})`;
  429. textLayerDiv.style.transformOrigin = "0% 0%";
  430. }
  431. if (redrawAnnotationLayer && this.annotationLayer) {
  432. this._renderAnnotationLayer();
  433. }
  434. if (redrawAnnotationEditorLayer && this.annotationEditorLayer) {
  435. this._renderAnnotationEditorLayer();
  436. }
  437. if (redrawXfaLayer && this.xfaLayer) {
  438. this._renderXfaLayer();
  439. }
  440. }
  441. get width() {
  442. return this.viewport.width;
  443. }
  444. get height() {
  445. return this.viewport.height;
  446. }
  447. getPagePoint(x, y) {
  448. return this.viewport.convertToPdfPoint(x, y);
  449. }
  450. toggleLoadingIconSpinner(viewVisible = false) {
  451. this.loadingIconDiv?.classList.toggle("notVisible", !viewVisible);
  452. }
  453. draw() {
  454. if (this.renderingState !== _ui_utils.RenderingStates.INITIAL) {
  455. console.error("Must be in new state before drawing");
  456. this.reset();
  457. }
  458. const {
  459. div,
  460. pdfPage
  461. } = this;
  462. if (!pdfPage) {
  463. this.renderingState = _ui_utils.RenderingStates.FINISHED;
  464. if (this.loadingIconDiv) {
  465. this.loadingIconDiv.remove();
  466. delete this.loadingIconDiv;
  467. }
  468. return Promise.reject(new Error("pdfPage is not loaded"));
  469. }
  470. this.renderingState = _ui_utils.RenderingStates.RUNNING;
  471. const canvasWrapper = document.createElement("div");
  472. canvasWrapper.style.width = div.style.width;
  473. canvasWrapper.style.height = div.style.height;
  474. canvasWrapper.classList.add("canvasWrapper");
  475. const lastDivBeforeTextDiv = this.annotationLayer?.div || this.annotationEditorLayer?.div;
  476. if (lastDivBeforeTextDiv) {
  477. lastDivBeforeTextDiv.before(canvasWrapper);
  478. } else {
  479. div.append(canvasWrapper);
  480. }
  481. let textLayer = null;
  482. if (this.textLayerMode !== _ui_utils.TextLayerMode.DISABLE && this.textLayerFactory) {
  483. this._accessibilityManager ||= new _text_accessibility.TextAccessibilityManager();
  484. const textLayerDiv = document.createElement("div");
  485. textLayerDiv.className = "textLayer";
  486. textLayerDiv.style.width = canvasWrapper.style.width;
  487. textLayerDiv.style.height = canvasWrapper.style.height;
  488. if (lastDivBeforeTextDiv) {
  489. lastDivBeforeTextDiv.before(textLayerDiv);
  490. } else {
  491. div.append(textLayerDiv);
  492. }
  493. textLayer = this.textLayerFactory.createTextLayerBuilder({
  494. textLayerDiv,
  495. pageIndex: this.id - 1,
  496. viewport: this.viewport,
  497. eventBus: this.eventBus,
  498. highlighter: this.textHighlighter,
  499. accessibilityManager: this._accessibilityManager
  500. });
  501. }
  502. this.textLayer = textLayer;
  503. if (this.#annotationMode !== _pdf.AnnotationMode.DISABLE && this.annotationLayerFactory) {
  504. this._annotationCanvasMap ||= new Map();
  505. this.annotationLayer ||= this.annotationLayerFactory.createAnnotationLayerBuilder({
  506. pageDiv: div,
  507. pdfPage,
  508. imageResourcesPath: this.imageResourcesPath,
  509. renderForms: this.#annotationMode === _pdf.AnnotationMode.ENABLE_FORMS,
  510. l10n: this.l10n,
  511. annotationCanvasMap: this._annotationCanvasMap,
  512. accessibilityManager: this._accessibilityManager
  513. });
  514. }
  515. if (this.xfaLayer?.div) {
  516. div.append(this.xfaLayer.div);
  517. }
  518. let renderContinueCallback = null;
  519. if (this.renderingQueue) {
  520. renderContinueCallback = cont => {
  521. if (!this.renderingQueue.isHighestPriority(this)) {
  522. this.renderingState = _ui_utils.RenderingStates.PAUSED;
  523. this.resume = () => {
  524. this.renderingState = _ui_utils.RenderingStates.RUNNING;
  525. cont();
  526. };
  527. return;
  528. }
  529. cont();
  530. };
  531. }
  532. const finishPaintTask = async (error = null) => {
  533. if (paintTask === this.paintTask) {
  534. this.paintTask = null;
  535. }
  536. if (error instanceof _pdf.RenderingCancelledException) {
  537. this._renderError = null;
  538. return;
  539. }
  540. this._renderError = error;
  541. this.renderingState = _ui_utils.RenderingStates.FINISHED;
  542. if (this.loadingIconDiv) {
  543. this.loadingIconDiv.remove();
  544. delete this.loadingIconDiv;
  545. }
  546. this._resetZoomLayer(true);
  547. this.#useThumbnailCanvas.regularAnnotations = !paintTask.separateAnnots;
  548. this.eventBus.dispatch("pagerendered", {
  549. source: this,
  550. pageNumber: this.id,
  551. cssTransform: false,
  552. timestamp: performance.now(),
  553. error: this._renderError
  554. });
  555. if (error) {
  556. throw error;
  557. }
  558. };
  559. const paintTask = this.renderer === _ui_utils.RendererType.SVG ? this.paintOnSvg(canvasWrapper) : this.paintOnCanvas(canvasWrapper);
  560. paintTask.onRenderContinue = renderContinueCallback;
  561. this.paintTask = paintTask;
  562. const resultPromise = paintTask.promise.then(() => {
  563. return finishPaintTask(null).then(() => {
  564. if (textLayer) {
  565. const readableStream = pdfPage.streamTextContent({
  566. includeMarkedContent: true
  567. });
  568. textLayer.setTextContentStream(readableStream);
  569. textLayer.render();
  570. }
  571. if (this.annotationLayer) {
  572. this._renderAnnotationLayer().then(() => {
  573. if (this.annotationEditorLayerFactory) {
  574. this.annotationEditorLayer ||= this.annotationEditorLayerFactory.createAnnotationEditorLayerBuilder({
  575. pageDiv: div,
  576. pdfPage,
  577. l10n: this.l10n,
  578. accessibilityManager: this._accessibilityManager
  579. });
  580. this._renderAnnotationEditorLayer();
  581. }
  582. });
  583. }
  584. });
  585. }, function (reason) {
  586. return finishPaintTask(reason);
  587. });
  588. if (this.xfaLayerFactory) {
  589. this.xfaLayer ||= this.xfaLayerFactory.createXfaLayerBuilder({
  590. pageDiv: div,
  591. pdfPage
  592. });
  593. this._renderXfaLayer();
  594. }
  595. if (this.structTreeLayerFactory && this.textLayer && this.canvas) {
  596. this._onTextLayerRendered = event => {
  597. if (event.pageNumber !== this.id) {
  598. return;
  599. }
  600. this.eventBus._off("textlayerrendered", this._onTextLayerRendered);
  601. this._onTextLayerRendered = null;
  602. if (!this.canvas) {
  603. return;
  604. }
  605. this.pdfPage.getStructTree().then(tree => {
  606. if (!tree) {
  607. return;
  608. }
  609. if (!this.canvas) {
  610. return;
  611. }
  612. const treeDom = this.structTreeLayer.render(tree);
  613. treeDom.classList.add("structTree");
  614. this.canvas.append(treeDom);
  615. });
  616. };
  617. this.eventBus._on("textlayerrendered", this._onTextLayerRendered);
  618. this.structTreeLayer = this.structTreeLayerFactory.createStructTreeLayerBuilder({
  619. pdfPage
  620. });
  621. }
  622. div.setAttribute("data-loaded", true);
  623. this.eventBus.dispatch("pagerender", {
  624. source: this,
  625. pageNumber: this.id
  626. });
  627. return resultPromise;
  628. }
  629. paintOnCanvas(canvasWrapper) {
  630. const renderCapability = (0, _pdf.createPromiseCapability)();
  631. const result = {
  632. promise: renderCapability.promise,
  633. onRenderContinue(cont) {
  634. cont();
  635. },
  636. cancel() {
  637. renderTask.cancel();
  638. },
  639. get separateAnnots() {
  640. return renderTask.separateAnnots;
  641. }
  642. };
  643. const viewport = this.viewport;
  644. const canvas = document.createElement("canvas");
  645. canvas.setAttribute("role", "presentation");
  646. canvas.hidden = true;
  647. let isCanvasHidden = true;
  648. const showCanvas = function () {
  649. if (isCanvasHidden) {
  650. canvas.hidden = false;
  651. isCanvasHidden = false;
  652. }
  653. };
  654. canvasWrapper.append(canvas);
  655. this.canvas = canvas;
  656. const ctx = canvas.getContext("2d", {
  657. alpha: false
  658. });
  659. const outputScale = this.outputScale = new _ui_utils.OutputScale();
  660. if (this.useOnlyCssZoom) {
  661. const actualSizeViewport = viewport.clone({
  662. scale: _pdf.PixelsPerInch.PDF_TO_CSS_UNITS
  663. });
  664. outputScale.sx *= actualSizeViewport.width / viewport.width;
  665. outputScale.sy *= actualSizeViewport.height / viewport.height;
  666. }
  667. if (this.maxCanvasPixels > 0) {
  668. const pixelsInViewport = viewport.width * viewport.height;
  669. const maxScale = Math.sqrt(this.maxCanvasPixels / pixelsInViewport);
  670. if (outputScale.sx > maxScale || outputScale.sy > maxScale) {
  671. outputScale.sx = maxScale;
  672. outputScale.sy = maxScale;
  673. this.hasRestrictedScaling = true;
  674. } else {
  675. this.hasRestrictedScaling = false;
  676. }
  677. }
  678. const sfx = (0, _ui_utils.approximateFraction)(outputScale.sx);
  679. const sfy = (0, _ui_utils.approximateFraction)(outputScale.sy);
  680. canvas.width = (0, _ui_utils.roundToDivide)(viewport.width * outputScale.sx, sfx[0]);
  681. canvas.height = (0, _ui_utils.roundToDivide)(viewport.height * outputScale.sy, sfy[0]);
  682. canvas.style.width = (0, _ui_utils.roundToDivide)(viewport.width, sfx[1]) + "px";
  683. canvas.style.height = (0, _ui_utils.roundToDivide)(viewport.height, sfy[1]) + "px";
  684. this.paintedViewportMap.set(canvas, viewport);
  685. const transform = outputScale.scaled ? [outputScale.sx, 0, 0, outputScale.sy, 0, 0] : null;
  686. const renderContext = {
  687. canvasContext: ctx,
  688. transform,
  689. viewport: this.viewport,
  690. annotationMode: this.#annotationMode,
  691. optionalContentConfigPromise: this._optionalContentConfigPromise,
  692. annotationCanvasMap: this._annotationCanvasMap,
  693. pageColors: this.pageColors
  694. };
  695. const renderTask = this.pdfPage.render(renderContext);
  696. renderTask.onContinue = function (cont) {
  697. showCanvas();
  698. if (result.onRenderContinue) {
  699. result.onRenderContinue(cont);
  700. } else {
  701. cont();
  702. }
  703. };
  704. renderTask.promise.then(function () {
  705. showCanvas();
  706. renderCapability.resolve();
  707. }, function (error) {
  708. showCanvas();
  709. renderCapability.reject(error);
  710. });
  711. return result;
  712. }
  713. paintOnSvg(wrapper) {
  714. let cancelled = false;
  715. const ensureNotCancelled = () => {
  716. if (cancelled) {
  717. throw new _pdf.RenderingCancelledException(`Rendering cancelled, page ${this.id}`, "svg");
  718. }
  719. };
  720. const pdfPage = this.pdfPage;
  721. const actualSizeViewport = this.viewport.clone({
  722. scale: _pdf.PixelsPerInch.PDF_TO_CSS_UNITS
  723. });
  724. const promise = pdfPage.getOperatorList({
  725. annotationMode: this.#annotationMode
  726. }).then(opList => {
  727. ensureNotCancelled();
  728. const svgGfx = new _pdf.SVGGraphics(pdfPage.commonObjs, pdfPage.objs);
  729. return svgGfx.getSVG(opList, actualSizeViewport).then(svg => {
  730. ensureNotCancelled();
  731. this.svg = svg;
  732. this.paintedViewportMap.set(svg, actualSizeViewport);
  733. svg.style.width = wrapper.style.width;
  734. svg.style.height = wrapper.style.height;
  735. this.renderingState = _ui_utils.RenderingStates.FINISHED;
  736. wrapper.append(svg);
  737. });
  738. });
  739. return {
  740. promise,
  741. onRenderContinue(cont) {
  742. cont();
  743. },
  744. cancel() {
  745. cancelled = true;
  746. },
  747. get separateAnnots() {
  748. return false;
  749. }
  750. };
  751. }
  752. setPageLabel(label) {
  753. this.pageLabel = typeof label === "string" ? label : null;
  754. if (this.pageLabel !== null) {
  755. this.div.setAttribute("data-page-label", this.pageLabel);
  756. } else {
  757. this.div.removeAttribute("data-page-label");
  758. }
  759. }
  760. get thumbnailCanvas() {
  761. const {
  762. initialOptionalContent,
  763. regularAnnotations
  764. } = this.#useThumbnailCanvas;
  765. return initialOptionalContent && regularAnnotations ? this.canvas : null;
  766. }
  767. }
  768. exports.PDFPageView = PDFPageView;