pdf_thumbnail_view.js 12 KB

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