primitives_spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 {
  16. Cmd,
  17. Dict,
  18. isCmd,
  19. isDict,
  20. isName,
  21. isRefsEqual,
  22. Name,
  23. Ref,
  24. RefSet,
  25. RefSetCache,
  26. } from "../../src/core/primitives.js";
  27. import { StringStream } from "../../src/core/stream.js";
  28. import { XRefMock } from "./test_utils.js";
  29. describe("primitives", function () {
  30. describe("Name", function () {
  31. it("should retain the given name", function () {
  32. const givenName = "Font";
  33. const name = Name.get(givenName);
  34. expect(name.name).toEqual(givenName);
  35. });
  36. it("should create only one object for a name and cache it", function () {
  37. const firstFont = Name.get("Font");
  38. const secondFont = Name.get("Font");
  39. const firstSubtype = Name.get("Subtype");
  40. const secondSubtype = Name.get("Subtype");
  41. expect(firstFont).toBe(secondFont);
  42. expect(firstSubtype).toBe(secondSubtype);
  43. expect(firstFont).not.toBe(firstSubtype);
  44. });
  45. it("should create only one object for *empty* names and cache it", function () {
  46. const firstEmpty = Name.get("");
  47. const secondEmpty = Name.get("");
  48. const normalName = Name.get("string");
  49. expect(firstEmpty).toBe(secondEmpty);
  50. expect(firstEmpty).not.toBe(normalName);
  51. });
  52. it("should not accept to create a non-string name", function () {
  53. expect(function () {
  54. Name.get(123);
  55. }).toThrow(new Error('Name: The "name" must be a string.'));
  56. });
  57. });
  58. describe("Cmd", function () {
  59. it("should retain the given cmd name", function () {
  60. const givenCmd = "BT";
  61. const cmd = Cmd.get(givenCmd);
  62. expect(cmd.cmd).toEqual(givenCmd);
  63. });
  64. it("should create only one object for a command and cache it", function () {
  65. const firstBT = Cmd.get("BT");
  66. const secondBT = Cmd.get("BT");
  67. const firstET = Cmd.get("ET");
  68. const secondET = Cmd.get("ET");
  69. expect(firstBT).toBe(secondBT);
  70. expect(firstET).toBe(secondET);
  71. expect(firstBT).not.toBe(firstET);
  72. });
  73. it("should not accept to create a non-string cmd", function () {
  74. expect(function () {
  75. Cmd.get(123);
  76. }).toThrow(new Error('Cmd: The "cmd" must be a string.'));
  77. });
  78. });
  79. describe("Dict", function () {
  80. const checkInvalidHasValues = function (dict) {
  81. expect(dict.has()).toBeFalsy();
  82. expect(dict.has("Prev")).toBeFalsy();
  83. };
  84. const checkInvalidKeyValues = function (dict) {
  85. expect(dict.get()).toBeUndefined();
  86. expect(dict.get("Prev")).toBeUndefined();
  87. expect(dict.get("D", "Decode")).toBeUndefined();
  88. expect(dict.get("FontFile", "FontFile2", "FontFile3")).toBeUndefined();
  89. };
  90. let emptyDict, dictWithSizeKey, dictWithManyKeys;
  91. const storedSize = 42;
  92. const testFontFile = "file1";
  93. const testFontFile2 = "file2";
  94. const testFontFile3 = "file3";
  95. beforeAll(function () {
  96. emptyDict = new Dict();
  97. dictWithSizeKey = new Dict();
  98. dictWithSizeKey.set("Size", storedSize);
  99. dictWithManyKeys = new Dict();
  100. dictWithManyKeys.set("FontFile", testFontFile);
  101. dictWithManyKeys.set("FontFile2", testFontFile2);
  102. dictWithManyKeys.set("FontFile3", testFontFile3);
  103. });
  104. afterAll(function () {
  105. emptyDict = dictWithSizeKey = dictWithManyKeys = null;
  106. });
  107. it("should allow assigning an XRef table after creation", function () {
  108. const dict = new Dict(null);
  109. expect(dict.xref).toEqual(null);
  110. const xref = new XRefMock([]);
  111. dict.assignXref(xref);
  112. expect(dict.xref).toEqual(xref);
  113. });
  114. it("should return correct size", function () {
  115. const dict = new Dict(null);
  116. expect(dict.size).toEqual(0);
  117. dict.set("Type", Name.get("Page"));
  118. expect(dict.size).toEqual(1);
  119. dict.set("Contents", Ref.get(10, 0));
  120. expect(dict.size).toEqual(2);
  121. });
  122. it("should return invalid values for unknown keys", function () {
  123. checkInvalidHasValues(emptyDict);
  124. checkInvalidKeyValues(emptyDict);
  125. });
  126. it("should return correct value for stored Size key", function () {
  127. expect(dictWithSizeKey.has("Size")).toBeTruthy();
  128. expect(dictWithSizeKey.get("Size")).toEqual(storedSize);
  129. expect(dictWithSizeKey.get("Prev", "Size")).toEqual(storedSize);
  130. expect(dictWithSizeKey.get("Prev", "Root", "Size")).toEqual(storedSize);
  131. });
  132. it("should return invalid values for unknown keys when Size key is stored", function () {
  133. checkInvalidHasValues(dictWithSizeKey);
  134. checkInvalidKeyValues(dictWithSizeKey);
  135. });
  136. it("should not accept to set a non-string key", function () {
  137. const dict = new Dict();
  138. expect(function () {
  139. dict.set(123, "val");
  140. }).toThrow(new Error('Dict.set: The "key" must be a string.'));
  141. expect(dict.has(123)).toBeFalsy();
  142. checkInvalidKeyValues(dict);
  143. });
  144. it("should not accept to set a key with an undefined value", function () {
  145. const dict = new Dict();
  146. expect(function () {
  147. dict.set("Size");
  148. }).toThrow(new Error('Dict.set: The "value" cannot be undefined.'));
  149. expect(dict.has("Size")).toBeFalsy();
  150. checkInvalidKeyValues(dict);
  151. });
  152. it("should return correct values for multiple stored keys", function () {
  153. expect(dictWithManyKeys.has("FontFile")).toBeTruthy();
  154. expect(dictWithManyKeys.has("FontFile2")).toBeTruthy();
  155. expect(dictWithManyKeys.has("FontFile3")).toBeTruthy();
  156. expect(dictWithManyKeys.get("FontFile3")).toEqual(testFontFile3);
  157. expect(dictWithManyKeys.get("FontFile2", "FontFile3")).toEqual(
  158. testFontFile2
  159. );
  160. expect(
  161. dictWithManyKeys.get("FontFile", "FontFile2", "FontFile3")
  162. ).toEqual(testFontFile);
  163. });
  164. it("should asynchronously fetch unknown keys", async function () {
  165. const keyPromises = [
  166. dictWithManyKeys.getAsync("Size"),
  167. dictWithSizeKey.getAsync("FontFile", "FontFile2", "FontFile3"),
  168. ];
  169. const values = await Promise.all(keyPromises);
  170. expect(values[0]).toBeUndefined();
  171. expect(values[1]).toBeUndefined();
  172. });
  173. it("should asynchronously fetch correct values for multiple stored keys", async function () {
  174. const keyPromises = [
  175. dictWithManyKeys.getAsync("FontFile3"),
  176. dictWithManyKeys.getAsync("FontFile2", "FontFile3"),
  177. dictWithManyKeys.getAsync("FontFile", "FontFile2", "FontFile3"),
  178. ];
  179. const values = await Promise.all(keyPromises);
  180. expect(values[0]).toEqual(testFontFile3);
  181. expect(values[1]).toEqual(testFontFile2);
  182. expect(values[2]).toEqual(testFontFile);
  183. });
  184. it("should callback for each stored key", function () {
  185. const callbackSpy = jasmine.createSpy("spy on callback in dictionary");
  186. dictWithManyKeys.forEach(callbackSpy);
  187. expect(callbackSpy).toHaveBeenCalled();
  188. const callbackSpyCalls = callbackSpy.calls;
  189. expect(callbackSpyCalls.argsFor(0)).toEqual(["FontFile", testFontFile]);
  190. expect(callbackSpyCalls.argsFor(1)).toEqual(["FontFile2", testFontFile2]);
  191. expect(callbackSpyCalls.argsFor(2)).toEqual(["FontFile3", testFontFile3]);
  192. expect(callbackSpyCalls.count()).toEqual(3);
  193. });
  194. it("should handle keys pointing to indirect objects, both sync and async", async function () {
  195. const fontRef = Ref.get(1, 0);
  196. const xref = new XRefMock([{ ref: fontRef, data: testFontFile }]);
  197. const fontDict = new Dict(xref);
  198. fontDict.set("FontFile", fontRef);
  199. expect(fontDict.getRaw("FontFile")).toEqual(fontRef);
  200. expect(fontDict.get("FontFile", "FontFile2", "FontFile3")).toEqual(
  201. testFontFile
  202. );
  203. const value = await fontDict.getAsync(
  204. "FontFile",
  205. "FontFile2",
  206. "FontFile3"
  207. );
  208. expect(value).toEqual(testFontFile);
  209. });
  210. it("should handle arrays containing indirect objects", function () {
  211. const minCoordRef = Ref.get(1, 0);
  212. const maxCoordRef = Ref.get(2, 0);
  213. const minCoord = 0;
  214. const maxCoord = 1;
  215. const xref = new XRefMock([
  216. { ref: minCoordRef, data: minCoord },
  217. { ref: maxCoordRef, data: maxCoord },
  218. ]);
  219. const xObjectDict = new Dict(xref);
  220. xObjectDict.set("BBox", [minCoord, maxCoord, minCoordRef, maxCoordRef]);
  221. expect(xObjectDict.get("BBox")).toEqual([
  222. minCoord,
  223. maxCoord,
  224. minCoordRef,
  225. maxCoordRef,
  226. ]);
  227. expect(xObjectDict.getArray("BBox")).toEqual([
  228. minCoord,
  229. maxCoord,
  230. minCoord,
  231. maxCoord,
  232. ]);
  233. });
  234. it("should get all key names", function () {
  235. const expectedKeys = ["FontFile", "FontFile2", "FontFile3"];
  236. const keys = dictWithManyKeys.getKeys();
  237. expect(keys.sort()).toEqual(expectedKeys);
  238. });
  239. it("should get all raw values", function () {
  240. // Test direct objects:
  241. const expectedRawValues1 = [testFontFile, testFontFile2, testFontFile3];
  242. const rawValues1 = dictWithManyKeys.getRawValues();
  243. expect(rawValues1.sort()).toEqual(expectedRawValues1);
  244. // Test indirect objects:
  245. const typeName = Name.get("Page");
  246. const resources = new Dict(null),
  247. resourcesRef = Ref.get(5, 0);
  248. const contents = new StringStream("data"),
  249. contentsRef = Ref.get(10, 0);
  250. const xref = new XRefMock([
  251. { ref: resourcesRef, data: resources },
  252. { ref: contentsRef, data: contents },
  253. ]);
  254. const dict = new Dict(xref);
  255. dict.set("Type", typeName);
  256. dict.set("Resources", resourcesRef);
  257. dict.set("Contents", contentsRef);
  258. const expectedRawValues2 = [contentsRef, resourcesRef, typeName];
  259. const rawValues2 = dict.getRawValues();
  260. expect(rawValues2.sort()).toEqual(expectedRawValues2);
  261. });
  262. it("should create only one object for Dict.empty", function () {
  263. const firstDictEmpty = Dict.empty;
  264. const secondDictEmpty = Dict.empty;
  265. expect(firstDictEmpty).toBe(secondDictEmpty);
  266. expect(firstDictEmpty).not.toBe(emptyDict);
  267. });
  268. it("should correctly merge dictionaries", function () {
  269. const expectedKeys = ["FontFile", "FontFile2", "FontFile3", "Size"];
  270. const fontFileDict = new Dict();
  271. fontFileDict.set("FontFile", "Type1 font file");
  272. const mergedDict = Dict.merge({
  273. xref: null,
  274. dictArray: [dictWithManyKeys, dictWithSizeKey, fontFileDict],
  275. });
  276. const mergedKeys = mergedDict.getKeys();
  277. expect(mergedKeys.sort()).toEqual(expectedKeys);
  278. expect(mergedDict.get("FontFile")).toEqual(testFontFile);
  279. });
  280. it("should correctly merge sub-dictionaries", function () {
  281. const localFontDict = new Dict();
  282. localFontDict.set("F1", "Local font one");
  283. const globalFontDict = new Dict();
  284. globalFontDict.set("F1", "Global font one");
  285. globalFontDict.set("F2", "Global font two");
  286. globalFontDict.set("F3", "Global font three");
  287. const localDict = new Dict();
  288. localDict.set("Font", localFontDict);
  289. const globalDict = new Dict();
  290. globalDict.set("Font", globalFontDict);
  291. const mergedDict = Dict.merge({
  292. xref: null,
  293. dictArray: [localDict, globalDict],
  294. });
  295. const mergedSubDict = Dict.merge({
  296. xref: null,
  297. dictArray: [localDict, globalDict],
  298. mergeSubDicts: true,
  299. });
  300. const mergedFontDict = mergedDict.get("Font");
  301. const mergedSubFontDict = mergedSubDict.get("Font");
  302. expect(mergedFontDict instanceof Dict).toEqual(true);
  303. expect(mergedSubFontDict instanceof Dict).toEqual(true);
  304. const mergedFontDictKeys = mergedFontDict.getKeys();
  305. const mergedSubFontDictKeys = mergedSubFontDict.getKeys();
  306. expect(mergedFontDictKeys).toEqual(["F1"]);
  307. expect(mergedSubFontDictKeys).toEqual(["F1", "F2", "F3"]);
  308. const mergedFontDictValues = mergedFontDict.getRawValues();
  309. const mergedSubFontDictValues = mergedSubFontDict.getRawValues();
  310. expect(mergedFontDictValues).toEqual(["Local font one"]);
  311. expect(mergedSubFontDictValues).toEqual([
  312. "Local font one",
  313. "Global font two",
  314. "Global font three",
  315. ]);
  316. });
  317. });
  318. describe("Ref", function () {
  319. it("should get a string representation", function () {
  320. const nonZeroRef = Ref.get(4, 2);
  321. expect(nonZeroRef.toString()).toEqual("4R2");
  322. // If the generation number is 0, a shorter representation is used.
  323. const zeroRef = Ref.get(4, 0);
  324. expect(zeroRef.toString()).toEqual("4R");
  325. });
  326. it("should retain the stored values", function () {
  327. const storedNum = 4;
  328. const storedGen = 2;
  329. const ref = Ref.get(storedNum, storedGen);
  330. expect(ref.num).toEqual(storedNum);
  331. expect(ref.gen).toEqual(storedGen);
  332. });
  333. it("should create only one object for a reference and cache it", function () {
  334. const firstRef = Ref.get(4, 2);
  335. const secondRef = Ref.get(4, 2);
  336. const firstOtherRef = Ref.get(5, 2);
  337. const secondOtherRef = Ref.get(5, 2);
  338. expect(firstRef).toBe(secondRef);
  339. expect(firstOtherRef).toBe(secondOtherRef);
  340. expect(firstRef).not.toBe(firstOtherRef);
  341. });
  342. });
  343. describe("RefSet", function () {
  344. const ref1 = Ref.get(4, 2),
  345. ref2 = Ref.get(5, 2);
  346. let refSet;
  347. beforeEach(function () {
  348. refSet = new RefSet();
  349. });
  350. afterEach(function () {
  351. refSet = null;
  352. });
  353. it("should have a stored value", function () {
  354. refSet.put(ref1);
  355. expect(refSet.has(ref1)).toBeTruthy();
  356. });
  357. it("should not have an unknown value", function () {
  358. expect(refSet.has(ref1)).toBeFalsy();
  359. refSet.put(ref1);
  360. expect(refSet.has(ref2)).toBeFalsy();
  361. });
  362. it("should support iteration", function () {
  363. refSet.put(ref1);
  364. refSet.put(ref2);
  365. expect([...refSet]).toEqual([ref1.toString(), ref2.toString()]);
  366. });
  367. });
  368. describe("RefSetCache", function () {
  369. const ref1 = Ref.get(4, 2),
  370. ref2 = Ref.get(5, 2),
  371. obj1 = Name.get("foo"),
  372. obj2 = Name.get("bar");
  373. let cache;
  374. beforeEach(function () {
  375. cache = new RefSetCache();
  376. });
  377. afterEach(function () {
  378. cache = null;
  379. });
  380. it("should put, have and get a value", function () {
  381. cache.put(ref1, obj1);
  382. expect(cache.has(ref1)).toBeTruthy();
  383. expect(cache.has(ref2)).toBeFalsy();
  384. expect(cache.get(ref1)).toBe(obj1);
  385. });
  386. it("should put, have and get a value by alias", function () {
  387. cache.put(ref1, obj1);
  388. cache.putAlias(ref2, ref1);
  389. expect(cache.has(ref1)).toBeTruthy();
  390. expect(cache.has(ref2)).toBeTruthy();
  391. expect(cache.get(ref1)).toBe(obj1);
  392. expect(cache.get(ref2)).toBe(obj1);
  393. });
  394. it("should report the size of the cache", function () {
  395. cache.put(ref1, obj1);
  396. expect(cache.size).toEqual(1);
  397. cache.put(ref2, obj2);
  398. expect(cache.size).toEqual(2);
  399. });
  400. it("should clear the cache", function () {
  401. cache.put(ref1, obj1);
  402. expect(cache.size).toEqual(1);
  403. cache.clear();
  404. expect(cache.size).toEqual(0);
  405. });
  406. it("should support iteration", function () {
  407. cache.put(ref1, obj1);
  408. cache.put(ref2, obj2);
  409. expect([...cache]).toEqual([obj1, obj2]);
  410. });
  411. });
  412. describe("isName", function () {
  413. /* eslint-disable no-restricted-syntax */
  414. it("handles non-names", function () {
  415. const nonName = {};
  416. expect(isName(nonName)).toEqual(false);
  417. });
  418. it("handles names", function () {
  419. const name = Name.get("Font");
  420. expect(isName(name)).toEqual(true);
  421. });
  422. it("handles names with name check", function () {
  423. const name = Name.get("Font");
  424. expect(isName(name, "Font")).toEqual(true);
  425. expect(isName(name, "Subtype")).toEqual(false);
  426. });
  427. it("handles *empty* names, with name check", function () {
  428. const emptyName = Name.get("");
  429. expect(isName(emptyName)).toEqual(true);
  430. expect(isName(emptyName, "")).toEqual(true);
  431. expect(isName(emptyName, "string")).toEqual(false);
  432. });
  433. /* eslint-enable no-restricted-syntax */
  434. });
  435. describe("isCmd", function () {
  436. /* eslint-disable no-restricted-syntax */
  437. it("handles non-commands", function () {
  438. const nonCmd = {};
  439. expect(isCmd(nonCmd)).toEqual(false);
  440. });
  441. it("handles commands", function () {
  442. const cmd = Cmd.get("BT");
  443. expect(isCmd(cmd)).toEqual(true);
  444. });
  445. it("handles commands with cmd check", function () {
  446. const cmd = Cmd.get("BT");
  447. expect(isCmd(cmd, "BT")).toEqual(true);
  448. expect(isCmd(cmd, "ET")).toEqual(false);
  449. });
  450. /* eslint-enable no-restricted-syntax */
  451. });
  452. describe("isDict", function () {
  453. /* eslint-disable no-restricted-syntax */
  454. it("handles non-dictionaries", function () {
  455. const nonDict = {};
  456. expect(isDict(nonDict)).toEqual(false);
  457. });
  458. it("handles empty dictionaries with type check", function () {
  459. const dict = Dict.empty;
  460. expect(isDict(dict)).toEqual(true);
  461. expect(isDict(dict, "Page")).toEqual(false);
  462. });
  463. it("handles dictionaries with type check", function () {
  464. const dict = new Dict();
  465. dict.set("Type", Name.get("Page"));
  466. expect(isDict(dict, "Page")).toEqual(true);
  467. expect(isDict(dict, "Contents")).toEqual(false);
  468. });
  469. /* eslint-enable no-restricted-syntax */
  470. });
  471. describe("isRefsEqual", function () {
  472. it("should handle Refs pointing to the same object", function () {
  473. const ref1 = Ref.get(1, 0);
  474. const ref2 = Ref.get(1, 0);
  475. expect(isRefsEqual(ref1, ref2)).toEqual(true);
  476. });
  477. it("should handle Refs pointing to different objects", function () {
  478. const ref1 = Ref.get(1, 0);
  479. const ref2 = Ref.get(2, 0);
  480. expect(isRefsEqual(ref1, ref2)).toEqual(false);
  481. });
  482. });
  483. });