primitives.js 8.0 KB

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