test_utils.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.createIdFactory = createIdFactory;
  28. exports.isEmptyObj = isEmptyObj;
  29. exports.XRefMock = exports.TEST_PDFS_PATH = exports.STANDARD_FONT_DATA_URL = exports.DefaultFileReaderFactory = exports.CMAP_PARAMS = void 0;
  30. var _primitives = require("../../core/primitives.js");
  31. var _document = require("../../core/document.js");
  32. var _util = require("../../shared/util.js");
  33. var _is_node = require("../../shared/is_node.js");
  34. var _stream = require("../../core/stream.js");
  35. const TEST_PDFS_PATH = _is_node.isNodeJS ? "./test/pdfs/" : "../pdfs/";
  36. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;
  37. const CMAP_PARAMS = {
  38. cMapUrl: _is_node.isNodeJS ? "./external/bcmaps/" : "../../external/bcmaps/",
  39. cMapPacked: true
  40. };
  41. exports.CMAP_PARAMS = CMAP_PARAMS;
  42. const STANDARD_FONT_DATA_URL = _is_node.isNodeJS ? "./external/standard_fonts/" : "../../external/standard_fonts/";
  43. exports.STANDARD_FONT_DATA_URL = STANDARD_FONT_DATA_URL;
  44. class DOMFileReaderFactory {
  45. static async fetch(params) {
  46. const response = await fetch(params.path);
  47. if (!response.ok) {
  48. throw new Error(response.statusText);
  49. }
  50. return new Uint8Array(await response.arrayBuffer());
  51. }
  52. }
  53. class NodeFileReaderFactory {
  54. static async fetch(params) {
  55. const fs = require("fs");
  56. return new Promise((resolve, reject) => {
  57. fs.readFile(params.path, (error, data) => {
  58. if (error || !data) {
  59. reject(error || new Error(`Empty file for: ${params.path}`));
  60. return;
  61. }
  62. resolve(new Uint8Array(data));
  63. });
  64. });
  65. }
  66. }
  67. const DefaultFileReaderFactory = _is_node.isNodeJS ? NodeFileReaderFactory : DOMFileReaderFactory;
  68. exports.DefaultFileReaderFactory = DefaultFileReaderFactory;
  69. function buildGetDocumentParams(filename, options) {
  70. const params = Object.create(null);
  71. params.url = _is_node.isNodeJS ? TEST_PDFS_PATH + filename : new URL(TEST_PDFS_PATH + filename, window.location).href;
  72. params.standardFontDataUrl = STANDARD_FONT_DATA_URL;
  73. for (const option in options) {
  74. params[option] = options[option];
  75. }
  76. return params;
  77. }
  78. class XRefMock {
  79. constructor(array) {
  80. this._map = Object.create(null);
  81. this.stats = {
  82. streamTypes: Object.create(null),
  83. fontTypes: Object.create(null)
  84. };
  85. this._newRefNum = null;
  86. for (const key in array) {
  87. const obj = array[key];
  88. this._map[obj.ref.toString()] = obj.data;
  89. }
  90. }
  91. getNewRef() {
  92. if (this._newRefNum === null) {
  93. this._newRefNum = Object.keys(this._map).length;
  94. }
  95. return _primitives.Ref.get(this._newRefNum++, 0);
  96. }
  97. resetNewRef() {
  98. this.newRef = null;
  99. }
  100. fetch(ref) {
  101. return this._map[ref.toString()];
  102. }
  103. async fetchAsync(ref) {
  104. return this.fetch(ref);
  105. }
  106. fetchIfRef(obj) {
  107. if (!(0, _primitives.isRef)(obj)) {
  108. return obj;
  109. }
  110. return this.fetch(obj);
  111. }
  112. async fetchIfRefAsync(obj) {
  113. return this.fetchIfRef(obj);
  114. }
  115. }
  116. exports.XRefMock = XRefMock;
  117. function createIdFactory(pageIndex) {
  118. const pdfManager = {
  119. get docId() {
  120. return "d0";
  121. }
  122. };
  123. const stream = new _stream.StringStream("Dummy_PDF_data");
  124. const pdfDocument = new _document.PDFDocument(pdfManager, stream);
  125. const page = new _document.Page({
  126. pdfManager: pdfDocument.pdfManager,
  127. xref: pdfDocument.xref,
  128. pageIndex,
  129. globalIdFactory: pdfDocument._globalIdFactory
  130. });
  131. return page._localIdFactory;
  132. }
  133. function isEmptyObj(obj) {
  134. (0, _util.assert)(typeof obj === "object" && obj !== null, "isEmptyObj - invalid argument.");
  135. return Object.keys(obj).length === 0;
  136. }