pdf_thumbnail_view.js 13 KB

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