primitives_spec.js 19 KB

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