pdf_thumbnail_view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. checkSetImageDisabled,
  67. l10n
  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.linkService = linkService;
  78. this.renderingQueue = renderingQueue;
  79. this.renderTask = null;
  80. this.renderingState = _ui_utils.RenderingStates.INITIAL;
  81. this.resume = null;
  82. this._checkSetImageDisabled = checkSetImageDisabled || function () {
  83. return false;
  84. };
  85. const pageWidth = this.viewport.width,
  86. pageHeight = this.viewport.height,
  87. pageRatio = pageWidth / pageHeight;
  88. this.canvasWidth = THUMBNAIL_WIDTH;
  89. this.canvasHeight = this.canvasWidth / pageRatio | 0;
  90. this.scale = this.canvasWidth / pageWidth;
  91. this.l10n = l10n;
  92. const anchor = document.createElement("a");
  93. anchor.href = linkService.getAnchorUrl("#page=" + id);
  94. this._thumbPageTitle.then(msg => {
  95. anchor.title = msg;
  96. });
  97. anchor.onclick = function () {
  98. linkService.goToPage(id);
  99. return false;
  100. };
  101. this.anchor = anchor;
  102. const div = document.createElement("div");
  103. div.className = "thumbnail";
  104. div.setAttribute("data-page-number", this.id);
  105. this.div = div;
  106. const ring = document.createElement("div");
  107. ring.className = "thumbnailSelectionRing";
  108. const borderAdjustment = 2 * THUMBNAIL_CANVAS_BORDER_WIDTH;
  109. ring.style.width = this.canvasWidth + borderAdjustment + "px";
  110. ring.style.height = this.canvasHeight + borderAdjustment + "px";
  111. this.ring = ring;
  112. div.appendChild(ring);
  113. anchor.appendChild(div);
  114. container.appendChild(anchor);
  115. }
  116. setPdfPage(pdfPage) {
  117. this.pdfPage = pdfPage;
  118. this.pdfPageRotate = pdfPage.rotate;
  119. const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  120. this.viewport = pdfPage.getViewport({
  121. scale: 1,
  122. rotation: totalRotation
  123. });
  124. this.reset();
  125. }
  126. reset() {
  127. this.cancelRendering();
  128. this.renderingState = _ui_utils.RenderingStates.INITIAL;
  129. const pageWidth = this.viewport.width,
  130. pageHeight = this.viewport.height,
  131. pageRatio = pageWidth / pageHeight;
  132. this.canvasHeight = this.canvasWidth / pageRatio | 0;
  133. this.scale = this.canvasWidth / pageWidth;
  134. this.div.removeAttribute("data-loaded");
  135. const ring = this.ring;
  136. ring.textContent = "";
  137. const borderAdjustment = 2 * THUMBNAIL_CANVAS_BORDER_WIDTH;
  138. ring.style.width = this.canvasWidth + borderAdjustment + "px";
  139. ring.style.height = this.canvasHeight + borderAdjustment + "px";
  140. if (this.canvas) {
  141. this.canvas.width = 0;
  142. this.canvas.height = 0;
  143. delete this.canvas;
  144. }
  145. if (this.image) {
  146. this.image.removeAttribute("src");
  147. delete this.image;
  148. }
  149. }
  150. update({
  151. rotation = null
  152. }) {
  153. if (typeof rotation === "number") {
  154. this.rotation = rotation;
  155. }
  156. const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  157. this.viewport = this.viewport.clone({
  158. scale: 1,
  159. rotation: totalRotation
  160. });
  161. this.reset();
  162. }
  163. cancelRendering() {
  164. if (this.renderTask) {
  165. this.renderTask.cancel();
  166. this.renderTask = null;
  167. }
  168. this.resume = null;
  169. }
  170. _getPageDrawContext(upscaleFactor = 1) {
  171. const canvas = document.createElement("canvas");
  172. const ctx = canvas.getContext("2d", {
  173. alpha: false
  174. });
  175. const outputScale = new _ui_utils.OutputScale();
  176. canvas.width = upscaleFactor * this.canvasWidth * outputScale.sx | 0;
  177. canvas.height = upscaleFactor * this.canvasHeight * outputScale.sy | 0;
  178. const transform = outputScale.scaled ? [outputScale.sx, 0, 0, outputScale.sy, 0, 0] : null;
  179. return {
  180. ctx,
  181. canvas,
  182. transform
  183. };
  184. }
  185. _convertCanvasToImage(canvas) {
  186. if (this.renderingState !== _ui_utils.RenderingStates.FINISHED) {
  187. throw new Error("_convertCanvasToImage: Rendering has not finished.");
  188. }
  189. const reducedCanvas = this._reduceImage(canvas);
  190. const image = document.createElement("img");
  191. image.className = "thumbnailImage";
  192. this._thumbPageCanvas.then(msg => {
  193. image.setAttribute("aria-label", msg);
  194. });
  195. image.style.width = this.canvasWidth + "px";
  196. image.style.height = this.canvasHeight + "px";
  197. image.src = reducedCanvas.toDataURL();
  198. this.image = image;
  199. this.div.setAttribute("data-loaded", true);
  200. this.ring.appendChild(image);
  201. reducedCanvas.width = 0;
  202. reducedCanvas.height = 0;
  203. }
  204. draw() {
  205. if (this.renderingState !== _ui_utils.RenderingStates.INITIAL) {
  206. console.error("Must be in new state before drawing");
  207. return Promise.resolve();
  208. }
  209. const {
  210. pdfPage
  211. } = this;
  212. if (!pdfPage) {
  213. this.renderingState = _ui_utils.RenderingStates.FINISHED;
  214. return Promise.reject(new Error("pdfPage is not loaded"));
  215. }
  216. this.renderingState = _ui_utils.RenderingStates.RUNNING;
  217. const finishRenderTask = async (error = null) => {
  218. if (renderTask === this.renderTask) {
  219. this.renderTask = null;
  220. }
  221. if (error instanceof _pdf.RenderingCancelledException) {
  222. return;
  223. }
  224. this.renderingState = _ui_utils.RenderingStates.FINISHED;
  225. this._convertCanvasToImage(canvas);
  226. if (error) {
  227. throw error;
  228. }
  229. };
  230. const {
  231. ctx,
  232. canvas,
  233. transform
  234. } = this._getPageDrawContext(DRAW_UPSCALE_FACTOR);
  235. const drawViewport = this.viewport.clone({
  236. scale: DRAW_UPSCALE_FACTOR * this.scale
  237. });
  238. const renderContinueCallback = cont => {
  239. if (!this.renderingQueue.isHighestPriority(this)) {
  240. this.renderingState = _ui_utils.RenderingStates.PAUSED;
  241. this.resume = () => {
  242. this.renderingState = _ui_utils.RenderingStates.RUNNING;
  243. cont();
  244. };
  245. return;
  246. }
  247. cont();
  248. };
  249. const renderContext = {
  250. canvasContext: ctx,
  251. transform,
  252. viewport: drawViewport,
  253. optionalContentConfigPromise: this._optionalContentConfigPromise
  254. };
  255. const renderTask = this.renderTask = pdfPage.render(renderContext);
  256. renderTask.onContinue = renderContinueCallback;
  257. const resultPromise = renderTask.promise.then(function () {
  258. return finishRenderTask(null);
  259. }, function (error) {
  260. return finishRenderTask(error);
  261. });
  262. resultPromise.finally(() => {
  263. canvas.width = 0;
  264. canvas.height = 0;
  265. const pageCached = this.linkService.isPageCached(this.id);
  266. if (!pageCached) {
  267. this.pdfPage?.cleanup();
  268. }
  269. });
  270. return resultPromise;
  271. }
  272. setImage(pageView) {
  273. if (this._checkSetImageDisabled()) {
  274. return;
  275. }
  276. if (this.renderingState !== _ui_utils.RenderingStates.INITIAL) {
  277. return;
  278. }
  279. const {
  280. canvas,
  281. pdfPage
  282. } = pageView;
  283. if (!canvas) {
  284. return;
  285. }
  286. if (!this.pdfPage) {
  287. this.setPdfPage(pdfPage);
  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;