pdf_page_view.js 21 KB

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