primitives.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.RefSetCache = exports.RefSet = exports.Ref = exports.Name = exports.EOF = exports.Dict = exports.Cmd = exports.CIRCULAR_REF = void 0;
  27. exports.clearPrimitiveCaches = clearPrimitiveCaches;
  28. exports.isCmd = isCmd;
  29. exports.isDict = isDict;
  30. exports.isName = isName;
  31. exports.isRefsEqual = isRefsEqual;
  32. var _util = require("../shared/util.js");
  33. const CIRCULAR_REF = Symbol("CIRCULAR_REF");
  34. exports.CIRCULAR_REF = CIRCULAR_REF;
  35. const EOF = Symbol("EOF");
  36. exports.EOF = EOF;
  37. const Name = function NameClosure() {
  38. let nameCache = Object.create(null);
  39. class Name {
  40. constructor(name) {
  41. this.name = name;
  42. }
  43. static get(name) {
  44. return nameCache[name] || (nameCache[name] = new Name(name));
  45. }
  46. static _clearCache() {
  47. nameCache = Object.create(null);
  48. }
  49. }
  50. return Name;
  51. }();
  52. exports.Name = Name;
  53. const Cmd = function CmdClosure() {
  54. let cmdCache = Object.create(null);
  55. class Cmd {
  56. constructor(cmd) {
  57. this.cmd = cmd;
  58. }
  59. static get(cmd) {
  60. return cmdCache[cmd] || (cmdCache[cmd] = new Cmd(cmd));
  61. }
  62. static _clearCache() {
  63. cmdCache = Object.create(null);
  64. }
  65. }
  66. return Cmd;
  67. }();
  68. exports.Cmd = Cmd;
  69. const nonSerializable = function nonSerializableClosure() {
  70. return nonSerializable;
  71. };
  72. class Dict {
  73. constructor(xref = null) {
  74. this._map = Object.create(null);
  75. this.xref = xref;
  76. this.objId = null;
  77. this.suppressEncryption = false;
  78. this.__nonSerializable__ = nonSerializable;
  79. }
  80. assignXref(newXref) {
  81. this.xref = newXref;
  82. }
  83. get size() {
  84. return Object.keys(this._map).length;
  85. }
  86. get(key1, key2, key3) {
  87. let value = this._map[key1];
  88. if (value === undefined && key2 !== undefined) {
  89. value = this._map[key2];
  90. if (value === undefined && key3 !== undefined) {
  91. value = this._map[key3];
  92. }
  93. }
  94. if (value instanceof Ref && this.xref) {
  95. return this.xref.fetch(value, this.suppressEncryption);
  96. }
  97. return value;
  98. }
  99. async getAsync(key1, key2, key3) {
  100. let value = this._map[key1];
  101. if (value === undefined && key2 !== undefined) {
  102. value = this._map[key2];
  103. if (value === undefined && key3 !== undefined) {
  104. value = this._map[key3];
  105. }
  106. }
  107. if (value instanceof Ref && this.xref) {
  108. return this.xref.fetchAsync(value, this.suppressEncryption);
  109. }
  110. return value;
  111. }
  112. getArray(key1, key2, key3) {
  113. let value = this._map[key1];
  114. if (value === undefined && key2 !== undefined) {
  115. value = this._map[key2];
  116. if (value === undefined && key3 !== undefined) {
  117. value = this._map[key3];
  118. }
  119. }
  120. if (value instanceof Ref && this.xref) {
  121. value = this.xref.fetch(value, this.suppressEncryption);
  122. }
  123. if (Array.isArray(value)) {
  124. value = value.slice();
  125. for (let i = 0, ii = value.length; i < ii; i++) {
  126. if (value[i] instanceof Ref && this.xref) {
  127. value[i] = this.xref.fetch(value[i], this.suppressEncryption);
  128. }
  129. }
  130. }
  131. return value;
  132. }
  133. getRaw(key) {
  134. return this._map[key];
  135. }
  136. getKeys() {
  137. return Object.keys(this._map);
  138. }
  139. getRawValues() {
  140. return Object.values(this._map);
  141. }
  142. set(key, value) {
  143. this._map[key] = value;
  144. }
  145. has(key) {
  146. return this._map[key] !== undefined;
  147. }
  148. forEach(callback) {
  149. for (const key in this._map) {
  150. callback(key, this.get(key));
  151. }
  152. }
  153. static get empty() {
  154. const emptyDict = new Dict(null);
  155. emptyDict.set = (key, value) => {
  156. (0, _util.unreachable)("Should not call `set` on the empty dictionary.");
  157. };
  158. return (0, _util.shadow)(this, "empty", emptyDict);
  159. }
  160. static merge({
  161. xref,
  162. dictArray,
  163. mergeSubDicts = false
  164. }) {
  165. const mergedDict = new Dict(xref),
  166. properties = new Map();
  167. for (const dict of dictArray) {
  168. if (!(dict instanceof Dict)) {
  169. continue;
  170. }
  171. for (const [key, value] of Object.entries(dict._map)) {
  172. let property = properties.get(key);
  173. if (property === undefined) {
  174. property = [];
  175. properties.set(key, property);
  176. } else if (!mergeSubDicts || !(value instanceof Dict)) {
  177. continue;
  178. }
  179. property.push(value);
  180. }
  181. }
  182. for (const [name, values] of properties) {
  183. if (values.length === 1 || !(values[0] instanceof Dict)) {
  184. mergedDict._map[name] = values[0];
  185. continue;
  186. }
  187. const subDict = new Dict(xref);
  188. for (const dict of values) {
  189. for (const [key, value] of Object.entries(dict._map)) {
  190. if (subDict._map[key] === undefined) {
  191. subDict._map[key] = value;
  192. }
  193. }
  194. }
  195. if (subDict.size > 0) {
  196. mergedDict._map[name] = subDict;
  197. }
  198. }
  199. properties.clear();
  200. return mergedDict.size > 0 ? mergedDict : Dict.empty;
  201. }
  202. }
  203. exports.Dict = Dict;
  204. const Ref = function RefClosure() {
  205. let refCache = Object.create(null);
  206. class Ref {
  207. constructor(num, gen) {
  208. this.num = num;
  209. this.gen = gen;
  210. }
  211. toString() {
  212. if (this.gen === 0) {
  213. return `${this.num}R`;
  214. }
  215. return `${this.num}R${this.gen}`;
  216. }
  217. static get(num, gen) {
  218. const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
  219. return refCache[key] || (refCache[key] = new Ref(num, gen));
  220. }
  221. static _clearCache() {
  222. refCache = Object.create(null);
  223. }
  224. }
  225. return Ref;
  226. }();
  227. exports.Ref = Ref;
  228. class RefSet {
  229. constructor(parent = null) {
  230. this._set = new Set(parent && parent._set);
  231. }
  232. has(ref) {
  233. return this._set.has(ref.toString());
  234. }
  235. put(ref) {
  236. this._set.add(ref.toString());
  237. }
  238. remove(ref) {
  239. this._set.delete(ref.toString());
  240. }
  241. [Symbol.iterator]() {
  242. return this._set.values();
  243. }
  244. clear() {
  245. this._set.clear();
  246. }
  247. }
  248. exports.RefSet = RefSet;
  249. class RefSetCache {
  250. constructor() {
  251. this._map = new Map();
  252. }
  253. get size() {
  254. return this._map.size;
  255. }
  256. get(ref) {
  257. return this._map.get(ref.toString());
  258. }
  259. has(ref) {
  260. return this._map.has(ref.toString());
  261. }
  262. put(ref, obj) {
  263. this._map.set(ref.toString(), obj);
  264. }
  265. putAlias(ref, aliasRef) {
  266. this._map.set(ref.toString(), this.get(aliasRef));
  267. }
  268. [Symbol.iterator]() {
  269. return this._map.values();
  270. }
  271. clear() {
  272. this._map.clear();
  273. }
  274. }
  275. exports.RefSetCache = RefSetCache;
  276. function isName(v, name) {
  277. return v instanceof Name && (name === undefined || v.name === name);
  278. }
  279. function isCmd(v, cmd) {
  280. return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
  281. }
  282. function isDict(v, type) {
  283. return v instanceof Dict && (type === undefined || isName(v.get("Type"), type));
  284. }
  285. function isRefsEqual(v1, v2) {
  286. return v1.num === v2.num && v1.gen === v2.gen;
  287. }
  288. function clearPrimitiveCaches() {
  289. Cmd._clearCache();
  290. Name._clearCache();
  291. Ref._clearCache();
  292. }