2
0

pdf_thumbnail_view.js 11 KB

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