primitives_spec.js 18 KB

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