pdf_thumbnail_view.js 11 KB

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