primitives.js 7.9 KB

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