2
0

pdf_thumbnail_view.js 13 KB

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