pdf_thumbnail_view.js 14 KB

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