primitives.js 7.8 KB

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