pdf_page_view.js 22 KB

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