2
0

primitives_spec.js 19 KB

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