pdf_page_view.js 23 KB

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