test_utils.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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.buildGetDocumentParams = buildGetDocumentParams;
  27. exports.TEST_PDFS_PATH = exports.XRefMock = exports.NodeCMapReaderFactory = exports.NodeCanvasFactory = exports.NodeFileReaderFactory = void 0;
  28. var _util = require("../../shared/util");
  29. var _is_node = _interopRequireDefault(require("../../shared/is_node"));
  30. var _primitives = require("../../core/primitives");
  31. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  32. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  33. 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); } }
  34. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  35. var NodeFileReaderFactory =
  36. /*#__PURE__*/
  37. function () {
  38. function NodeFileReaderFactory() {
  39. _classCallCheck(this, NodeFileReaderFactory);
  40. }
  41. _createClass(NodeFileReaderFactory, null, [{
  42. key: "fetch",
  43. value: function fetch(params) {
  44. var fs = require('fs');
  45. var file = fs.readFileSync(params.path);
  46. return new Uint8Array(file);
  47. }
  48. }]);
  49. return NodeFileReaderFactory;
  50. }();
  51. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  52. var TEST_PDFS_PATH = {
  53. dom: '../pdfs/',
  54. node: './test/pdfs/'
  55. };
  56. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;
  57. function buildGetDocumentParams(filename, options) {
  58. var params = Object.create(null);
  59. if ((0, _is_node.default)()) {
  60. params.url = TEST_PDFS_PATH.node + filename;
  61. } else {
  62. params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href;
  63. }
  64. for (var option in options) {
  65. params[option] = options[option];
  66. }
  67. return params;
  68. }
  69. var NodeCanvasFactory =
  70. /*#__PURE__*/
  71. function () {
  72. function NodeCanvasFactory() {
  73. _classCallCheck(this, NodeCanvasFactory);
  74. }
  75. _createClass(NodeCanvasFactory, [{
  76. key: "create",
  77. value: function create(width, height) {
  78. (0, _util.assert)(width > 0 && height > 0, 'Invalid canvas size');
  79. var Canvas = require('canvas');
  80. var canvas = Canvas.createCanvas(width, height);
  81. return {
  82. canvas: canvas,
  83. context: canvas.getContext('2d')
  84. };
  85. }
  86. }, {
  87. key: "reset",
  88. value: function reset(canvasAndContext, width, height) {
  89. (0, _util.assert)(canvasAndContext.canvas, 'Canvas is not specified');
  90. (0, _util.assert)(width > 0 && height > 0, 'Invalid canvas size');
  91. canvasAndContext.canvas.width = width;
  92. canvasAndContext.canvas.height = height;
  93. }
  94. }, {
  95. key: "destroy",
  96. value: function destroy(canvasAndContext) {
  97. (0, _util.assert)(canvasAndContext.canvas, 'Canvas is not specified');
  98. canvasAndContext.canvas.width = 0;
  99. canvasAndContext.canvas.height = 0;
  100. canvasAndContext.canvas = null;
  101. canvasAndContext.context = null;
  102. }
  103. }]);
  104. return NodeCanvasFactory;
  105. }();
  106. exports.NodeCanvasFactory = NodeCanvasFactory;
  107. var NodeCMapReaderFactory =
  108. /*#__PURE__*/
  109. function () {
  110. function NodeCMapReaderFactory(_ref) {
  111. var _ref$baseUrl = _ref.baseUrl,
  112. baseUrl = _ref$baseUrl === void 0 ? null : _ref$baseUrl,
  113. _ref$isCompressed = _ref.isCompressed,
  114. isCompressed = _ref$isCompressed === void 0 ? false : _ref$isCompressed;
  115. _classCallCheck(this, NodeCMapReaderFactory);
  116. this.baseUrl = baseUrl;
  117. this.isCompressed = isCompressed;
  118. }
  119. _createClass(NodeCMapReaderFactory, [{
  120. key: "fetch",
  121. value: function fetch(_ref2) {
  122. var _this = this;
  123. var name = _ref2.name;
  124. if (!this.baseUrl) {
  125. return Promise.reject(new Error('The CMap "baseUrl" parameter must be specified, ensure that ' + 'the "cMapUrl" and "cMapPacked" API parameters are provided.'));
  126. }
  127. if (!name) {
  128. return Promise.reject(new Error('CMap name must be specified.'));
  129. }
  130. return new Promise(function (resolve, reject) {
  131. var url = _this.baseUrl + name + (_this.isCompressed ? '.bcmap' : '');
  132. var fs = require('fs');
  133. fs.readFile(url, function (error, data) {
  134. if (error || !data) {
  135. reject(new Error('Unable to load ' + (_this.isCompressed ? 'binary ' : '') + 'CMap at: ' + url));
  136. return;
  137. }
  138. resolve({
  139. cMapData: new Uint8Array(data),
  140. compressionType: _this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE
  141. });
  142. });
  143. });
  144. }
  145. }]);
  146. return NodeCMapReaderFactory;
  147. }();
  148. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;
  149. var XRefMock =
  150. /*#__PURE__*/
  151. function () {
  152. function XRefMock(array) {
  153. _classCallCheck(this, XRefMock);
  154. this._map = Object.create(null);
  155. for (var key in array) {
  156. var obj = array[key];
  157. this._map[obj.ref.toString()] = obj.data;
  158. }
  159. }
  160. _createClass(XRefMock, [{
  161. key: "fetch",
  162. value: function fetch(ref) {
  163. return this._map[ref.toString()];
  164. }
  165. }, {
  166. key: "fetchAsync",
  167. value: function fetchAsync(ref) {
  168. return Promise.resolve(this.fetch(ref));
  169. }
  170. }, {
  171. key: "fetchIfRef",
  172. value: function fetchIfRef(obj) {
  173. if (!(0, _primitives.isRef)(obj)) {
  174. return obj;
  175. }
  176. return this.fetch(obj);
  177. }
  178. }, {
  179. key: "fetchIfRefAsync",
  180. value: function fetchIfRefAsync(obj) {
  181. return Promise.resolve(this.fetchIfRef(obj));
  182. }
  183. }]);
  184. return XRefMock;
  185. }();
  186. exports.XRefMock = XRefMock;