pdf_thumbnail_view.js 11 KB

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