pdf_thumbnail_view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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.TempImageFactory = exports.PDFThumbnailView = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. var _pdf = require("../pdf");
  29. var _pdf_rendering_queue = require("./pdf_rendering_queue.js");
  30. const DRAW_UPSCALE_FACTOR = 2;
  31. const MAX_NUM_SCALING_STEPS = 3;
  32. const THUMBNAIL_CANVAS_BORDER_WIDTH = 1;
  33. const THUMBNAIL_WIDTH = 98;
  34. const TempImageFactory = function TempImageFactoryClosure() {
  35. let tempCanvasCache = null;
  36. return {
  37. getCanvas(width, height) {
  38. let tempCanvas = tempCanvasCache;
  39. if (!tempCanvas) {
  40. tempCanvas = document.createElement("canvas");
  41. tempCanvasCache = tempCanvas;
  42. }
  43. tempCanvas.width = width;
  44. tempCanvas.height = height;
  45. tempCanvas.mozOpaque = true;
  46. const ctx = tempCanvas.getContext("2d", {
  47. alpha: false
  48. });
  49. ctx.save();
  50. ctx.fillStyle = "rgb(255, 255, 255)";
  51. ctx.fillRect(0, 0, width, height);
  52. ctx.restore();
  53. return [tempCanvas, tempCanvas.getContext("2d")];
  54. },
  55. destroyCanvas() {
  56. const tempCanvas = tempCanvasCache;
  57. if (tempCanvas) {
  58. tempCanvas.width = 0;
  59. tempCanvas.height = 0;
  60. }
  61. tempCanvasCache = null;
  62. }
  63. };
  64. }();
  65. exports.TempImageFactory = TempImageFactory;
  66. class PDFThumbnailView {
  67. constructor({
  68. container,
  69. id,
  70. defaultViewport,
  71. optionalContentConfigPromise,
  72. linkService,
  73. renderingQueue,
  74. checkSetImageDisabled,
  75. l10n
  76. }) {
  77. this.id = id;
  78. this.renderingId = "thumbnail" + id;
  79. this.pageLabel = null;
  80. this.pdfPage = null;
  81. this.rotation = 0;
  82. this.viewport = defaultViewport;
  83. this.pdfPageRotate = defaultViewport.rotation;
  84. this._optionalContentConfigPromise = optionalContentConfigPromise || null;
  85. this.linkService = linkService;
  86. this.renderingQueue = renderingQueue;
  87. this.renderTask = null;
  88. this.renderingState = _pdf_rendering_queue.RenderingStates.INITIAL;
  89. this.resume = null;
  90. this._checkSetImageDisabled = checkSetImageDisabled || function () {
  91. return false;
  92. };
  93. const pageWidth = this.viewport.width,
  94. pageHeight = this.viewport.height,
  95. pageRatio = pageWidth / pageHeight;
  96. this.canvasWidth = THUMBNAIL_WIDTH;
  97. this.canvasHeight = this.canvasWidth / pageRatio | 0;
  98. this.scale = this.canvasWidth / pageWidth;
  99. this.l10n = l10n;
  100. const anchor = document.createElement("a");
  101. anchor.href = linkService.getAnchorUrl("#page=" + id);
  102. this._thumbPageTitle.then(msg => {
  103. anchor.title = msg;
  104. });
  105. anchor.onclick = function () {
  106. linkService.goToPage(id);
  107. return false;
  108. };
  109. this.anchor = anchor;
  110. const div = document.createElement("div");
  111. div.className = "thumbnail";
  112. div.setAttribute("data-page-number", this.id);
  113. this.div = div;
  114. const ring = document.createElement("div");
  115. ring.className = "thumbnailSelectionRing";
  116. const borderAdjustment = 2 * THUMBNAIL_CANVAS_BORDER_WIDTH;
  117. ring.style.width = this.canvasWidth + borderAdjustment + "px";
  118. ring.style.height = this.canvasHeight + borderAdjustment + "px";
  119. this.ring = ring;
  120. div.appendChild(ring);
  121. anchor.appendChild(div);
  122. container.appendChild(anchor);
  123. }
  124. setPdfPage(pdfPage) {
  125. this.pdfPage = pdfPage;
  126. this.pdfPageRotate = pdfPage.rotate;
  127. const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  128. this.viewport = pdfPage.getViewport({
  129. scale: 1,
  130. rotation: totalRotation
  131. });
  132. this.reset();
  133. }
  134. reset() {
  135. this.cancelRendering();
  136. this.renderingState = _pdf_rendering_queue.RenderingStates.INITIAL;
  137. const pageWidth = this.viewport.width,
  138. pageHeight = this.viewport.height,
  139. pageRatio = pageWidth / pageHeight;
  140. this.canvasHeight = this.canvasWidth / pageRatio | 0;
  141. this.scale = this.canvasWidth / pageWidth;
  142. this.div.removeAttribute("data-loaded");
  143. const ring = this.ring;
  144. ring.textContent = "";
  145. const borderAdjustment = 2 * THUMBNAIL_CANVAS_BORDER_WIDTH;
  146. ring.style.width = this.canvasWidth + borderAdjustment + "px";
  147. ring.style.height = this.canvasHeight + borderAdjustment + "px";
  148. if (this.canvas) {
  149. this.canvas.width = 0;
  150. this.canvas.height = 0;
  151. delete this.canvas;
  152. }
  153. if (this.image) {
  154. this.image.removeAttribute("src");
  155. delete this.image;
  156. }
  157. }
  158. update(rotation) {
  159. if (typeof rotation !== "undefined") {
  160. this.rotation = rotation;
  161. }
  162. const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  163. this.viewport = this.viewport.clone({
  164. scale: 1,
  165. rotation: totalRotation
  166. });
  167. this.reset();
  168. }
  169. cancelRendering() {
  170. if (this.renderTask) {
  171. this.renderTask.cancel();
  172. this.renderTask = null;
  173. }
  174. this.resume = null;
  175. }
  176. _getPageDrawContext(upscaleFactor = 1) {
  177. const canvas = document.createElement("canvas");
  178. canvas.mozOpaque = true;
  179. const ctx = canvas.getContext("2d", {
  180. alpha: false
  181. });
  182. const outputScale = (0, _ui_utils.getOutputScale)(ctx);
  183. canvas.width = upscaleFactor * this.canvasWidth * outputScale.sx | 0;
  184. canvas.height = upscaleFactor * this.canvasHeight * outputScale.sy | 0;
  185. const transform = outputScale.scaled ? [outputScale.sx, 0, 0, outputScale.sy, 0, 0] : null;
  186. return {
  187. ctx,
  188. canvas,
  189. transform
  190. };
  191. }
  192. _convertCanvasToImage(canvas) {
  193. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  194. throw new Error("_convertCanvasToImage: Rendering has not finished.");
  195. }
  196. const reducedCanvas = this._reduceImage(canvas);
  197. const image = document.createElement("img");
  198. image.className = "thumbnailImage";
  199. this._thumbPageCanvas.then(msg => {
  200. image.setAttribute("aria-label", msg);
  201. });
  202. image.style.width = this.canvasWidth + "px";
  203. image.style.height = this.canvasHeight + "px";
  204. image.src = reducedCanvas.toDataURL();
  205. this.image = image;
  206. this.div.setAttribute("data-loaded", true);
  207. this.ring.appendChild(image);
  208. reducedCanvas.width = 0;
  209. reducedCanvas.height = 0;
  210. }
  211. draw() {
  212. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  213. console.error("Must be in new state before drawing");
  214. return Promise.resolve(undefined);
  215. }
  216. const {
  217. pdfPage
  218. } = this;
  219. if (!pdfPage) {
  220. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  221. return Promise.reject(new Error("pdfPage is not loaded"));
  222. }
  223. this.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  224. const finishRenderTask = async (error = null) => {
  225. if (renderTask === this.renderTask) {
  226. this.renderTask = null;
  227. }
  228. if (error instanceof _pdf.RenderingCancelledException) {
  229. return;
  230. }
  231. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  232. this._convertCanvasToImage(canvas);
  233. if (error) {
  234. throw error;
  235. }
  236. };
  237. const {
  238. ctx,
  239. canvas,
  240. transform
  241. } = this._getPageDrawContext(DRAW_UPSCALE_FACTOR);
  242. const drawViewport = this.viewport.clone({
  243. scale: DRAW_UPSCALE_FACTOR * this.scale
  244. });
  245. const renderContinueCallback = cont => {
  246. if (!this.renderingQueue.isHighestPriority(this)) {
  247. this.renderingState = _pdf_rendering_queue.RenderingStates.PAUSED;
  248. this.resume = () => {
  249. this.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  250. cont();
  251. };
  252. return;
  253. }
  254. cont();
  255. };
  256. const renderContext = {
  257. canvasContext: ctx,
  258. transform,
  259. viewport: drawViewport,
  260. optionalContentConfigPromise: this._optionalContentConfigPromise
  261. };
  262. const renderTask = this.renderTask = pdfPage.render(renderContext);
  263. renderTask.onContinue = renderContinueCallback;
  264. const resultPromise = renderTask.promise.then(function () {
  265. return finishRenderTask(null);
  266. }, function (error) {
  267. return finishRenderTask(error);
  268. });
  269. resultPromise.finally(() => {
  270. canvas.width = 0;
  271. canvas.height = 0;
  272. const pageCached = this.linkService.isPageCached(this.id);
  273. if (!pageCached) {
  274. this.pdfPage?.cleanup();
  275. }
  276. });
  277. return resultPromise;
  278. }
  279. setImage(pageView) {
  280. if (this._checkSetImageDisabled()) {
  281. return;
  282. }
  283. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  284. return;
  285. }
  286. const {
  287. canvas,
  288. pdfPage
  289. } = pageView;
  290. if (!canvas) {
  291. return;
  292. }
  293. if (!this.pdfPage) {
  294. this.setPdfPage(pdfPage);
  295. }
  296. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  297. this._convertCanvasToImage(canvas);
  298. }
  299. _reduceImage(img) {
  300. const {
  301. ctx,
  302. canvas
  303. } = this._getPageDrawContext();
  304. if (img.width <= 2 * canvas.width) {
  305. ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
  306. return canvas;
  307. }
  308. let reducedWidth = canvas.width << MAX_NUM_SCALING_STEPS;
  309. let reducedHeight = canvas.height << MAX_NUM_SCALING_STEPS;
  310. const [reducedImage, reducedImageCtx] = TempImageFactory.getCanvas(reducedWidth, reducedHeight);
  311. while (reducedWidth > img.width || reducedHeight > img.height) {
  312. reducedWidth >>= 1;
  313. reducedHeight >>= 1;
  314. }
  315. reducedImageCtx.drawImage(img, 0, 0, img.width, img.height, 0, 0, reducedWidth, reducedHeight);
  316. while (reducedWidth > 2 * canvas.width) {
  317. reducedImageCtx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, reducedWidth >> 1, reducedHeight >> 1);
  318. reducedWidth >>= 1;
  319. reducedHeight >>= 1;
  320. }
  321. ctx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, canvas.width, canvas.height);
  322. return canvas;
  323. }
  324. get _thumbPageTitle() {
  325. return this.l10n.get("thumb_page_title", {
  326. page: this.pageLabel ?? this.id
  327. });
  328. }
  329. get _thumbPageCanvas() {
  330. return this.l10n.get("thumb_page_canvas", {
  331. page: this.pageLabel ?? this.id
  332. });
  333. }
  334. setPageLabel(label) {
  335. this.pageLabel = typeof label === "string" ? label : null;
  336. this._thumbPageTitle.then(msg => {
  337. this.anchor.title = msg;
  338. });
  339. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  340. return;
  341. }
  342. this._thumbPageCanvas.then(msg => {
  343. this.image?.setAttribute("aria-label", msg);
  344. });
  345. }
  346. }
  347. exports.PDFThumbnailView = PDFThumbnailView;