pdf_thumbnail_view.js 11 KB

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