2
0

primitives.js 8.0 KB

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