test_utils.js 4.8 KB

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