pdf_thumbnail_view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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({
  159. rotation = null
  160. }) {
  161. if (typeof rotation === "number") {
  162. this.rotation = rotation;
  163. }
  164. const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  165. this.viewport = this.viewport.clone({
  166. scale: 1,
  167. rotation: totalRotation
  168. });
  169. this.reset();
  170. }
  171. cancelRendering() {
  172. if (this.renderTask) {
  173. this.renderTask.cancel();
  174. this.renderTask = null;
  175. }
  176. this.resume = null;
  177. }
  178. _getPageDrawContext(upscaleFactor = 1) {
  179. const canvas = document.createElement("canvas");
  180. canvas.mozOpaque = true;
  181. const ctx = canvas.getContext("2d", {
  182. alpha: false
  183. });
  184. const outputScale = (0, _ui_utils.getOutputScale)(ctx);
  185. canvas.width = upscaleFactor * this.canvasWidth * outputScale.sx | 0;
  186. canvas.height = upscaleFactor * this.canvasHeight * outputScale.sy | 0;
  187. const transform = outputScale.scaled ? [outputScale.sx, 0, 0, outputScale.sy, 0, 0] : null;
  188. return {
  189. ctx,
  190. canvas,
  191. transform
  192. };
  193. }
  194. _convertCanvasToImage(canvas) {
  195. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  196. throw new Error("_convertCanvasToImage: Rendering has not finished.");
  197. }
  198. const reducedCanvas = this._reduceImage(canvas);
  199. const image = document.createElement("img");
  200. image.className = "thumbnailImage";
  201. this._thumbPageCanvas.then(msg => {
  202. image.setAttribute("aria-label", msg);
  203. });
  204. image.style.width = this.canvasWidth + "px";
  205. image.style.height = this.canvasHeight + "px";
  206. image.src = reducedCanvas.toDataURL();
  207. this.image = image;
  208. this.div.setAttribute("data-loaded", true);
  209. this.ring.appendChild(image);
  210. reducedCanvas.width = 0;
  211. reducedCanvas.height = 0;
  212. }
  213. draw() {
  214. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  215. console.error("Must be in new state before drawing");
  216. return Promise.resolve(undefined);
  217. }
  218. const {
  219. pdfPage
  220. } = this;
  221. if (!pdfPage) {
  222. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  223. return Promise.reject(new Error("pdfPage is not loaded"));
  224. }
  225. this.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  226. const finishRenderTask = async (error = null) => {
  227. if (renderTask === this.renderTask) {
  228. this.renderTask = null;
  229. }
  230. if (error instanceof _pdf.RenderingCancelledException) {
  231. return;
  232. }
  233. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  234. this._convertCanvasToImage(canvas);
  235. if (error) {
  236. throw error;
  237. }
  238. };
  239. const {
  240. ctx,
  241. canvas,
  242. transform
  243. } = this._getPageDrawContext(DRAW_UPSCALE_FACTOR);
  244. const drawViewport = this.viewport.clone({
  245. scale: DRAW_UPSCALE_FACTOR * this.scale
  246. });
  247. const renderContinueCallback = cont => {
  248. if (!this.renderingQueue.isHighestPriority(this)) {
  249. this.renderingState = _pdf_rendering_queue.RenderingStates.PAUSED;
  250. this.resume = () => {
  251. this.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  252. cont();
  253. };
  254. return;
  255. }
  256. cont();
  257. };
  258. const renderContext = {
  259. canvasContext: ctx,
  260. transform,
  261. viewport: drawViewport,
  262. optionalContentConfigPromise: this._optionalContentConfigPromise
  263. };
  264. const renderTask = this.renderTask = pdfPage.render(renderContext);
  265. renderTask.onContinue = renderContinueCallback;
  266. const resultPromise = renderTask.promise.then(function () {
  267. return finishRenderTask(null);
  268. }, function (error) {
  269. return finishRenderTask(error);
  270. });
  271. resultPromise.finally(() => {
  272. canvas.width = 0;
  273. canvas.height = 0;
  274. const pageCached = this.linkService.isPageCached(this.id);
  275. if (!pageCached) {
  276. this.pdfPage?.cleanup();
  277. }
  278. });
  279. return resultPromise;
  280. }
  281. setImage(pageView) {
  282. if (this._checkSetImageDisabled()) {
  283. return;
  284. }
  285. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  286. return;
  287. }
  288. const {
  289. canvas,
  290. pdfPage
  291. } = pageView;
  292. if (!canvas) {
  293. return;
  294. }
  295. if (!this.pdfPage) {
  296. this.setPdfPage(pdfPage);
  297. }
  298. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  299. this._convertCanvasToImage(canvas);
  300. }
  301. _reduceImage(img) {
  302. const {
  303. ctx,
  304. canvas
  305. } = this._getPageDrawContext();
  306. if (img.width <= 2 * canvas.width) {
  307. ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
  308. return canvas;
  309. }
  310. let reducedWidth = canvas.width << MAX_NUM_SCALING_STEPS;
  311. let reducedHeight = canvas.height << MAX_NUM_SCALING_STEPS;
  312. const [reducedImage, reducedImageCtx] = TempImageFactory.getCanvas(reducedWidth, reducedHeight);
  313. while (reducedWidth > img.width || reducedHeight > img.height) {
  314. reducedWidth >>= 1;
  315. reducedHeight >>= 1;
  316. }
  317. reducedImageCtx.drawImage(img, 0, 0, img.width, img.height, 0, 0, reducedWidth, reducedHeight);
  318. while (reducedWidth > 2 * canvas.width) {
  319. reducedImageCtx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, reducedWidth >> 1, reducedHeight >> 1);
  320. reducedWidth >>= 1;
  321. reducedHeight >>= 1;
  322. }
  323. ctx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, canvas.width, canvas.height);
  324. return canvas;
  325. }
  326. get _thumbPageTitle() {
  327. return this.l10n.get("thumb_page_title", {
  328. page: this.pageLabel ?? this.id
  329. });
  330. }
  331. get _thumbPageCanvas() {
  332. return this.l10n.get("thumb_page_canvas", {
  333. page: this.pageLabel ?? this.id
  334. });
  335. }
  336. setPageLabel(label) {
  337. this.pageLabel = typeof label === "string" ? label : null;
  338. this._thumbPageTitle.then(msg => {
  339. this.anchor.title = msg;
  340. });
  341. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  342. return;
  343. }
  344. this._thumbPageCanvas.then(msg => {
  345. this.image?.setAttribute("aria-label", msg);
  346. });
  347. }
  348. }
  349. exports.PDFThumbnailView = PDFThumbnailView;