pdf_page_view.js 25 KB

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