pdf_thumbnail_view.js 12 KB

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