pdf_page_view.js 22 KB

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