test_utils.js 4.2 KB

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