pdf_thumbnail_view.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. if (id === 1) {
  111. div.classList.add('selected');
  112. }
  113. var ring = document.createElement('div');
  114. ring.className = 'thumbnailSelectionRing';
  115. var borderAdjustment = 2 * THUMBNAIL_CANVAS_BORDER_WIDTH;
  116. ring.style.width = this.canvasWidth + borderAdjustment + 'px';
  117. ring.style.height = this.canvasHeight + borderAdjustment + 'px';
  118. this.ring = ring;
  119. div.appendChild(ring);
  120. anchor.appendChild(div);
  121. container.appendChild(anchor);
  122. }
  123. _createClass(PDFThumbnailView, [{
  124. key: 'setPdfPage',
  125. value: function setPdfPage(pdfPage) {
  126. this.pdfPage = pdfPage;
  127. this.pdfPageRotate = pdfPage.rotate;
  128. var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  129. this.viewport = pdfPage.getViewport(1, totalRotation);
  130. this.reset();
  131. }
  132. }, {
  133. key: 'reset',
  134. value: function reset() {
  135. this.cancelRendering();
  136. this.pageWidth = this.viewport.width;
  137. this.pageHeight = this.viewport.height;
  138. this.pageRatio = this.pageWidth / this.pageHeight;
  139. this.canvasHeight = this.canvasWidth / this.pageRatio | 0;
  140. this.scale = this.canvasWidth / this.pageWidth;
  141. this.div.removeAttribute('data-loaded');
  142. var ring = this.ring;
  143. var childNodes = ring.childNodes;
  144. for (var i = childNodes.length - 1; i >= 0; i--) {
  145. ring.removeChild(childNodes[i]);
  146. }
  147. var borderAdjustment = 2 * THUMBNAIL_CANVAS_BORDER_WIDTH;
  148. ring.style.width = this.canvasWidth + borderAdjustment + 'px';
  149. ring.style.height = this.canvasHeight + borderAdjustment + 'px';
  150. if (this.canvas) {
  151. this.canvas.width = 0;
  152. this.canvas.height = 0;
  153. delete this.canvas;
  154. }
  155. if (this.image) {
  156. this.image.removeAttribute('src');
  157. delete this.image;
  158. }
  159. }
  160. }, {
  161. key: 'update',
  162. value: function update(rotation) {
  163. if (typeof rotation !== 'undefined') {
  164. this.rotation = rotation;
  165. }
  166. var totalRotation = (this.rotation + this.pdfPageRotate) % 360;
  167. this.viewport = this.viewport.clone({
  168. scale: 1,
  169. rotation: totalRotation
  170. });
  171. this.reset();
  172. }
  173. }, {
  174. key: 'cancelRendering',
  175. value: function cancelRendering() {
  176. if (this.renderTask) {
  177. this.renderTask.cancel();
  178. this.renderTask = null;
  179. }
  180. this.renderingState = _pdf_rendering_queue.RenderingStates.INITIAL;
  181. this.resume = null;
  182. }
  183. }, {
  184. key: '_getPageDrawContext',
  185. value: function _getPageDrawContext() {
  186. var noCtxScale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  187. var canvas = document.createElement('canvas');
  188. this.canvas = canvas;
  189. canvas.mozOpaque = true;
  190. var ctx = canvas.getContext('2d', { alpha: false });
  191. var outputScale = (0, _ui_utils.getOutputScale)(ctx);
  192. canvas.width = this.canvasWidth * outputScale.sx | 0;
  193. canvas.height = this.canvasHeight * outputScale.sy | 0;
  194. canvas.style.width = this.canvasWidth + 'px';
  195. canvas.style.height = this.canvasHeight + 'px';
  196. if (!noCtxScale && outputScale.scaled) {
  197. ctx.scale(outputScale.sx, outputScale.sy);
  198. }
  199. return ctx;
  200. }
  201. }, {
  202. key: '_convertCanvasToImage',
  203. value: function _convertCanvasToImage() {
  204. var _this = this;
  205. if (!this.canvas) {
  206. return;
  207. }
  208. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  209. return;
  210. }
  211. var id = this.renderingId;
  212. var className = 'thumbnailImage';
  213. if (this.disableCanvasToImageConversion) {
  214. this.canvas.id = id;
  215. this.canvas.className = className;
  216. this.l10n.get('thumb_page_canvas', { page: this.pageId }, 'Thumbnail of Page {{page}}').then(function (msg) {
  217. _this.canvas.setAttribute('aria-label', msg);
  218. });
  219. this.div.setAttribute('data-loaded', true);
  220. this.ring.appendChild(this.canvas);
  221. return;
  222. }
  223. var image = document.createElement('img');
  224. image.id = id;
  225. image.className = className;
  226. this.l10n.get('thumb_page_canvas', { page: this.pageId }, 'Thumbnail of Page {{page}}').then(function (msg) {
  227. image.setAttribute('aria-label', msg);
  228. });
  229. image.style.width = this.canvasWidth + 'px';
  230. image.style.height = this.canvasHeight + 'px';
  231. image.src = this.canvas.toDataURL();
  232. this.image = image;
  233. this.div.setAttribute('data-loaded', true);
  234. this.ring.appendChild(image);
  235. this.canvas.width = 0;
  236. this.canvas.height = 0;
  237. delete this.canvas;
  238. }
  239. }, {
  240. key: 'draw',
  241. value: function draw() {
  242. var _this2 = this;
  243. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  244. console.error('Must be in new state before drawing');
  245. return Promise.resolve(undefined);
  246. }
  247. this.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  248. var renderCapability = (0, _pdf.createPromiseCapability)();
  249. var finishRenderTask = function finishRenderTask(error) {
  250. if (renderTask === _this2.renderTask) {
  251. _this2.renderTask = null;
  252. }
  253. if (error instanceof _pdf.RenderingCancelledException) {
  254. renderCapability.resolve(undefined);
  255. return;
  256. }
  257. _this2.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  258. _this2._convertCanvasToImage();
  259. if (!error) {
  260. renderCapability.resolve(undefined);
  261. } else {
  262. renderCapability.reject(error);
  263. }
  264. };
  265. var ctx = this._getPageDrawContext();
  266. var drawViewport = this.viewport.clone({ scale: this.scale });
  267. var renderContinueCallback = function renderContinueCallback(cont) {
  268. if (!_this2.renderingQueue.isHighestPriority(_this2)) {
  269. _this2.renderingState = _pdf_rendering_queue.RenderingStates.PAUSED;
  270. _this2.resume = function () {
  271. _this2.renderingState = _pdf_rendering_queue.RenderingStates.RUNNING;
  272. cont();
  273. };
  274. return;
  275. }
  276. cont();
  277. };
  278. var renderContext = {
  279. canvasContext: ctx,
  280. viewport: drawViewport
  281. };
  282. var renderTask = this.renderTask = this.pdfPage.render(renderContext);
  283. renderTask.onContinue = renderContinueCallback;
  284. renderTask.promise.then(function () {
  285. finishRenderTask(null);
  286. }, function (error) {
  287. finishRenderTask(error);
  288. });
  289. return renderCapability.promise;
  290. }
  291. }, {
  292. key: 'setImage',
  293. value: function setImage(pageView) {
  294. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.INITIAL) {
  295. return;
  296. }
  297. var img = pageView.canvas;
  298. if (!img) {
  299. return;
  300. }
  301. if (!this.pdfPage) {
  302. this.setPdfPage(pageView.pdfPage);
  303. }
  304. this.renderingState = _pdf_rendering_queue.RenderingStates.FINISHED;
  305. var ctx = this._getPageDrawContext(true);
  306. var canvas = ctx.canvas;
  307. if (img.width <= 2 * canvas.width) {
  308. ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
  309. this._convertCanvasToImage();
  310. return;
  311. }
  312. var reducedWidth = canvas.width << MAX_NUM_SCALING_STEPS;
  313. var reducedHeight = canvas.height << MAX_NUM_SCALING_STEPS;
  314. var reducedImage = TempImageFactory.getCanvas(reducedWidth, reducedHeight);
  315. var reducedImageCtx = reducedImage.getContext('2d');
  316. while (reducedWidth > img.width || reducedHeight > img.height) {
  317. reducedWidth >>= 1;
  318. reducedHeight >>= 1;
  319. }
  320. reducedImageCtx.drawImage(img, 0, 0, img.width, img.height, 0, 0, reducedWidth, reducedHeight);
  321. while (reducedWidth > 2 * canvas.width) {
  322. reducedImageCtx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, reducedWidth >> 1, reducedHeight >> 1);
  323. reducedWidth >>= 1;
  324. reducedHeight >>= 1;
  325. }
  326. ctx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, canvas.width, canvas.height);
  327. this._convertCanvasToImage();
  328. }
  329. }, {
  330. key: 'setPageLabel',
  331. value: function setPageLabel(label) {
  332. var _this3 = this;
  333. this.pageLabel = typeof label === 'string' ? label : null;
  334. this.l10n.get('thumb_page_title', { page: this.pageId }, 'Page {{page}}').then(function (msg) {
  335. _this3.anchor.title = msg;
  336. });
  337. if (this.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  338. return;
  339. }
  340. this.l10n.get('thumb_page_canvas', { page: this.pageId }, 'Thumbnail of Page {{page}}').then(function (ariaLabel) {
  341. if (_this3.image) {
  342. _this3.image.setAttribute('aria-label', ariaLabel);
  343. } else if (_this3.disableCanvasToImageConversion && _this3.canvas) {
  344. _this3.canvas.setAttribute('aria-label', ariaLabel);
  345. }
  346. });
  347. }
  348. }, {
  349. key: 'pageId',
  350. get: function get() {
  351. return this.pageLabel !== null ? this.pageLabel : this.id;
  352. }
  353. }], [{
  354. key: 'cleanup',
  355. value: function cleanup() {
  356. TempImageFactory.destroyCanvas();
  357. }
  358. }]);
  359. return PDFThumbnailView;
  360. }();
  361. exports.PDFThumbnailView = PDFThumbnailView;