pdf_page_view.js 26 KB

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