pdf_thumbnail_view.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFThumbnailView = undefined;
  20. var _ui_utils = require('pdfjs-web/ui_utils');
  21. var _pdf_rendering_queue = require('pdfjs-web/pdf_rendering_queue');
  22. var THUMBNAIL_WIDTH = 98;
  23. var THUMBNAIL_CANVAS_BORDER_WIDTH = 1;
  24. var PDFThumbnailView = function PDFThumbnailViewClosure() {
  25. function getTempCanvas(width, height) {
  26. var tempCanvas = PDFThumbnailView.tempImageCache;
  27. if (!tempCanvas) {
  28. tempCanvas = document.createElement('canvas');
  29. PDFThumbnailView.tempImageCache = tempCanvas;
  30. }
  31. tempCanvas.width = width;
  32. tempCanvas.height = height;
  33. tempCanvas.mozOpaque = true;
  34. var ctx = tempCanvas.getContext('2d', { alpha: false });
  35. ctx.save();
  36. ctx.fillStyle = 'rgb(255, 255, 255)';
  37. ctx.fillRect(0, 0, width, height);
  38. ctx.restore();
  39. return tempCanvas;
  40. }
  41. function PDFThumbnailView(options) {
  42. var container = options.container;
  43. var id = options.id;
  44. var defaultViewport = options.defaultViewport;
  45. var linkService = options.linkService;
  46. var renderingQueue = options.renderingQueue;
  47. var disableCanvasToImageConversion = options.disableCanvasToImageConversion || false;
  48. this.id = id;
  49. this.renderingId = 'thumbnail' + id;
  50. this.pageLabel = null;
  51. this.pdfPage = null;
  52. this.rotation = 0;
  53. this.viewport = defaultViewport;
  54. this.pdfPageRotate = defaultViewport.rotation;
  55. this.linkService = linkService;
  56. this.renderingQueue = renderingQueue;
  57. this.renderTask = null;
  58. this.renderingState = _pdf_rendering_queue.RenderingStates.INITIAL;
  59. this.resume = null;
  60. this.disableCanvasToImageConversion = disableCanvasToImageConversion;
  61. this.pageWidth = this.viewport.width;
  62. this.pageHeight = this.viewport.height;
  63. this.pageRatio = this.pageWidth / this.pageHeight;
  64. this.canvasWidth = THUMBNAIL_WIDTH;
  65. this.canvasHeight = this.canvasWidth / this.pageRatio | 0;
  66. this.scale = this.canvasWidth / this.pageWidth;
  67. var anchor = document.createElement('a');
  68. anchor.href = linkService.getAnchorUrl('#page=' + id);
  69. anchor.title = _ui_utils.mozL10n.get('thumb_page_title', { page: id }, 'Page {{page}}');
  70. anchor.onclick = function stopNavigation() {
  71. linkService.page = id;
  72. return false;
  73. };
  74. this.anchor = anchor;
  75. var div = document.createElement('div');
  76. div.className = 'thumbnail';
  77. div.setAttribute('data-page-number', this.id);
  78. this.div = div;
  79. if (id === 1) {
  80. div.classList.add('selected');
  81. }
  82. var ring = document.createElement('div');
  83. ring.className = 'thumbnailSelectionRing';
  84. var borderAdjustment = 2 * THUMBNAIL_CANVAS_BORDER_WIDTH;
  85. ring.style.width = this.canvasWidth + borderAdjustment + 'px';
  86. ring.style.height = this.canvasHeight + borderAdjustment + 'px';
  87. this.ring = ring;
  88. div.appendChild(ring);
  89. anchor.appendChild(div);
  90. container.appendChild(anchor);
  91. }
  92. PDFThumbnailView.prototype = {
  93. setPdfPage: function PDFThumbnailView_setPdfPage(pdfPage) {
  94. this.pdfPage = pdfPage;
  95. this.pdfPageRotate = pdfPage.rotate;
  96. var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  97. this.viewport = pdfPage.getViewport(1, totalRotation);
  98. this.reset();
  99. },
  100. reset: function PDFThumbnailView_reset() {
  101. this.cancelRendering();
  102. this.pageWidth = this.viewport.width;
  103. this.pageHeight = this.viewport.height;
  104. this.pageRatio = this.pageWidth / this.pageHeight;
  105. this.canvasHeight = this.canvasWidth / this.pageRatio | 0;
  106. this.scale = this.canvasWidth / this.pageWidth;
  107. this.div.removeAttribute('data-loaded');
  108. var ring = this.ring;
  109. var childNodes = ring.childNodes;
  110. for (var i = childNodes.length - 1; i >= 0; i--) {
  111. ring.removeChild(childNodes[i]);
  112. }
  113. var borderAdjustment = 2 * THUMBNAIL_CANVAS_BORDER_WIDTH;
  114. ring.style.width = this.canvasWidth + borderAdjustment + 'px';
  115. ring.style.height = this.canvasHeight + borderAdjustment + 'px';
  116. if (this.canvas) {
  117. this.canvas.width = 0;
  118. this.canvas.height = 0;
  119. delete this.canvas;
  120. }
  121. if (this.image) {
  122. this.image.removeAttribute('src');
  123. delete this.image;
  124. }
  125. },
  126. update: function PDFThumbnailView_update(rotation) {
  127. if (typeof rotation !== 'undefined') {
  128. this.rotation = rotation;
  129. }
  130. var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  131. this.viewport = this.viewport.clone({
  132. scale: 1,
  133. rotation: totalRotation
  134. });
  135. this.reset();
  136. },
  137. cancelRendering: function PDFThumbnailView_cancelRendering() {
  138. if (this.renderTask) {
  139. this.renderTask.cancel();
  140. this.renderTask = null;
  141. }
  142. this.renderingState = _pdf_rendering_queue.RenderingStates.INITIAL;
  143. this.resume = null;
  144. },
  145. _getPageDrawContext: function PDFThumbnailView_getPageDrawContext(noCtxScale) {
  146. var canvas = document.createElement('canvas');
  147. this.canvas = canvas;
  148. canvas.mozOpaque = true;
  149. var ctx = canvas.getContext('2d', { alpha: false });
  150. var outputScale = (0, _ui_utils.getOutputScale)(ctx);
  151. canvas.width = this.canvasWidth * outputScale.sx | 0;
  152. canvas.height = this.canvasHeight * outputScale.sy | 0;
  153. canvas.style.width = this.canvasWidth + 'px';
  154. canvas.style.height = this.canvasHeight + 'px';
  155. if (!noCtxScale && outputScale.scaled) {
  156. ctx.scale(outputScale.sx, outputScale.sy);
  157. }
  158. return ctx;
  159. },
  160. _convertCanvasToImage: function PDFThumbnailView_convertCanvasToImage() {
  161. if (!this.canvas) {
  162. return;
  163. }
  164. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  165. return;
  166. }
  167. var id = this.renderingId;
  168. var className = 'thumbnailImage';
  169. var ariaLabel = _ui_utils.mozL10n.get('thumb_page_canvas', { page: this.pageId }, 'Thumbnail of Page {{page}}');
  170. if (this.disableCanvasToImageConversion) {
  171. this.canvas.id = id;
  172. this.canvas.className = className;
  173. this.canvas.setAttribute('aria-label', ariaLabel);
  174. this.div.setAttribute('data-loaded', true);
  175. this.ring.appendChild(this.canvas);
  176. return;
  177. }
  178. var image = document.createElement('img');
  179. image.id = id;
  180. image.className = className;
  181. image.setAttribute('aria-label', ariaLabel);
  182. image.style.width = this.canvasWidth + 'px';
  183. image.style.height = this.canvasHeight + 'px';
  184. image.src = this.canvas.toDataURL();
  185. this.image = image;
  186. this.div.setAttribute('data-loaded', true);
  187. this.ring.appendChild(image);
  188. this.canvas.width = 0;
  189. this.canvas.height = 0;
  190. delete this.canvas;
  191. },
  192. draw: function PDFThumbnailView_draw() {
  193. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  194. console.error('Must be in new state before drawing');
  195. return Promise.resolve(undefined);
  196. }
  197. this.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  198. var resolveRenderPromise, rejectRenderPromise;
  199. var promise = new Promise(function (resolve, reject) {
  200. resolveRenderPromise = resolve;
  201. rejectRenderPromise = reject;
  202. });
  203. var self = this;
  204. function thumbnailDrawCallback(error) {
  205. if (renderTask === self.renderTask) {
  206. self.renderTask = null;
  207. }
  208. if (error === 'cancelled') {
  209. rejectRenderPromise(error);
  210. return;
  211. }
  212. self.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  213. self._convertCanvasToImage();
  214. if (!error) {
  215. resolveRenderPromise(undefined);
  216. } else {
  217. rejectRenderPromise(error);
  218. }
  219. }
  220. var ctx = this._getPageDrawContext();
  221. var drawViewport = this.viewport.clone({ scale: this.scale });
  222. var renderContinueCallback = function renderContinueCallback(cont) {
  223. if (!self.renderingQueue.isHighestPriority(self)) {
  224. self.renderingState = _pdf_rendering_queue.RenderingStates.PAUSED;
  225. self.resume = function resumeCallback() {
  226. self.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  227. cont();
  228. };
  229. return;
  230. }
  231. cont();
  232. };
  233. var renderContext = {
  234. canvasContext: ctx,
  235. viewport: drawViewport
  236. };
  237. var renderTask = this.renderTask = this.pdfPage.render(renderContext);
  238. renderTask.onContinue = renderContinueCallback;
  239. renderTask.promise.then(function pdfPageRenderCallback() {
  240. thumbnailDrawCallback(null);
  241. }, function pdfPageRenderError(error) {
  242. thumbnailDrawCallback(error);
  243. });
  244. return promise;
  245. },
  246. setImage: function PDFThumbnailView_setImage(pageView) {
  247. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  248. return;
  249. }
  250. var img = pageView.canvas;
  251. if (!img) {
  252. return;
  253. }
  254. if (!this.pdfPage) {
  255. this.setPdfPage(pageView.pdfPage);
  256. }
  257. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  258. var ctx = this._getPageDrawContext(true);
  259. var canvas = ctx.canvas;
  260. if (img.width <= 2 * canvas.width) {
  261. ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
  262. this._convertCanvasToImage();
  263. return;
  264. }
  265. var MAX_NUM_SCALING_STEPS = 3;
  266. var reducedWidth = canvas.width << MAX_NUM_SCALING_STEPS;
  267. var reducedHeight = canvas.height << MAX_NUM_SCALING_STEPS;
  268. var reducedImage = getTempCanvas(reducedWidth, reducedHeight);
  269. var reducedImageCtx = reducedImage.getContext('2d');
  270. while (reducedWidth > img.width || reducedHeight > img.height) {
  271. reducedWidth >>= 1;
  272. reducedHeight >>= 1;
  273. }
  274. reducedImageCtx.drawImage(img, 0, 0, img.width, img.height, 0, 0, reducedWidth, reducedHeight);
  275. while (reducedWidth > 2 * canvas.width) {
  276. reducedImageCtx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, reducedWidth >> 1, reducedHeight >> 1);
  277. reducedWidth >>= 1;
  278. reducedHeight >>= 1;
  279. }
  280. ctx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, canvas.width, canvas.height);
  281. this._convertCanvasToImage();
  282. },
  283. get pageId() {
  284. return this.pageLabel !== null ? this.pageLabel : this.id;
  285. },
  286. setPageLabel: function PDFThumbnailView_setPageLabel(label) {
  287. this.pageLabel = typeof label === 'string' ? label : null;
  288. this.anchor.title = _ui_utils.mozL10n.get('thumb_page_title', { page: this.pageId }, 'Page {{page}}');
  289. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  290. return;
  291. }
  292. var ariaLabel = _ui_utils.mozL10n.get('thumb_page_canvas', { page: this.pageId }, 'Thumbnail of Page {{page}}');
  293. if (this.image) {
  294. this.image.setAttribute('aria-label', ariaLabel);
  295. } else if (this.disableCanvasToImageConversion && this.canvas) {
  296. this.canvas.setAttribute('aria-label', ariaLabel);
  297. }
  298. }
  299. };
  300. return PDFThumbnailView;
  301. }();
  302. PDFThumbnailView.tempImageCache = null;
  303. exports.PDFThumbnailView = PDFThumbnailView;