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