primitives.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.isEOF = isEOF;
  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. exports.RefSetCache = exports.RefSet = exports.Ref = exports.Name = exports.Dict = exports.Cmd = exports.EOF = void 0;
  35. var _util = require("../shared/util.js");
  36. var EOF = {};
  37. exports.EOF = EOF;
  38. var Name = function NameClosure() {
  39. let nameCache = Object.create(null);
  40. function Name(name) {
  41. this.name = name;
  42. }
  43. Name.prototype = {};
  44. Name.get = function Name_get(name) {
  45. var nameValue = nameCache[name];
  46. return nameValue ? nameValue : nameCache[name] = new Name(name);
  47. };
  48. Name._clearCache = function () {
  49. nameCache = Object.create(null);
  50. };
  51. return Name;
  52. }();
  53. exports.Name = Name;
  54. var Cmd = function CmdClosure() {
  55. let cmdCache = Object.create(null);
  56. function Cmd(cmd) {
  57. this.cmd = cmd;
  58. }
  59. Cmd.prototype = {};
  60. Cmd.get = function Cmd_get(cmd) {
  61. var cmdValue = cmdCache[cmd];
  62. return cmdValue ? cmdValue : cmdCache[cmd] = new Cmd(cmd);
  63. };
  64. Cmd._clearCache = function () {
  65. cmdCache = Object.create(null);
  66. };
  67. return Cmd;
  68. }();
  69. exports.Cmd = Cmd;
  70. var Dict = function DictClosure() {
  71. var nonSerializable = function nonSerializableClosure() {
  72. return nonSerializable;
  73. };
  74. function Dict(xref) {
  75. this._map = Object.create(null);
  76. this.xref = xref;
  77. this.objId = null;
  78. this.suppressEncryption = false;
  79. this.__nonSerializable__ = nonSerializable;
  80. }
  81. Dict.prototype = {
  82. assignXref: function Dict_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.get(key1, key2, key3);
  116. if (!Array.isArray(value) || !this.xref) {
  117. return value;
  118. }
  119. value = value.slice();
  120. for (let i = 0, ii = value.length; i < ii; i++) {
  121. if (!(value[i] instanceof Ref)) {
  122. continue;
  123. }
  124. value[i] = this.xref.fetch(value[i], this.suppressEncryption);
  125. }
  126. return value;
  127. },
  128. getRaw: function Dict_getRaw(key) {
  129. return this._map[key];
  130. },
  131. getKeys: function Dict_getKeys() {
  132. return Object.keys(this._map);
  133. },
  134. getRawValues: function Dict_getRawValues() {
  135. return Object.values(this._map);
  136. },
  137. set: function Dict_set(key, value) {
  138. this._map[key] = value;
  139. },
  140. has: function Dict_has(key) {
  141. return this._map[key] !== undefined;
  142. },
  143. forEach: function Dict_forEach(callback) {
  144. for (var key in this._map) {
  145. callback(key, this.get(key));
  146. }
  147. }
  148. };
  149. Dict.empty = new Dict(null);
  150. Dict.merge = function ({
  151. xref,
  152. dictArray,
  153. mergeSubDicts = false
  154. }) {
  155. const mergedDict = new Dict(xref);
  156. if (!mergeSubDicts) {
  157. for (const dict of dictArray) {
  158. if (!(dict instanceof Dict)) {
  159. continue;
  160. }
  161. for (const [key, value] of Object.entries(dict._map)) {
  162. if (mergedDict._map[key] === undefined) {
  163. mergedDict._map[key] = value;
  164. }
  165. }
  166. }
  167. return mergedDict.size > 0 ? mergedDict : Dict.empty;
  168. }
  169. const 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. }
  180. property.push(value);
  181. }
  182. }
  183. for (const [name, values] of properties) {
  184. if (values.length === 1 || !(values[0] instanceof Dict)) {
  185. mergedDict._map[name] = values[0];
  186. continue;
  187. }
  188. const subDict = new Dict(xref);
  189. for (const dict of values) {
  190. if (!(dict instanceof Dict)) {
  191. continue;
  192. }
  193. for (const [key, value] of Object.entries(dict._map)) {
  194. if (subDict._map[key] === undefined) {
  195. subDict._map[key] = value;
  196. }
  197. }
  198. }
  199. if (subDict.size > 0) {
  200. mergedDict._map[name] = subDict;
  201. }
  202. }
  203. properties.clear();
  204. return mergedDict.size > 0 ? mergedDict : Dict.empty;
  205. };
  206. return Dict;
  207. }();
  208. exports.Dict = Dict;
  209. var Ref = function RefClosure() {
  210. let refCache = Object.create(null);
  211. function Ref(num, gen) {
  212. this.num = num;
  213. this.gen = gen;
  214. }
  215. Ref.prototype = {
  216. toString: function Ref_toString() {
  217. if (this.gen === 0) {
  218. return `${this.num}R`;
  219. }
  220. return `${this.num}R${this.gen}`;
  221. }
  222. };
  223. Ref.get = function (num, gen) {
  224. const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
  225. const refValue = refCache[key];
  226. return refValue ? refValue : refCache[key] = new Ref(num, gen);
  227. };
  228. Ref._clearCache = function () {
  229. refCache = Object.create(null);
  230. };
  231. return Ref;
  232. }();
  233. exports.Ref = Ref;
  234. class RefSet {
  235. constructor() {
  236. this._set = new 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. }
  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. forEach(callback) {
  269. for (const value of this._map.values()) {
  270. callback(value);
  271. }
  272. }
  273. clear() {
  274. this._map.clear();
  275. }
  276. }
  277. exports.RefSetCache = RefSetCache;
  278. function isEOF(v) {
  279. return v === EOF;
  280. }
  281. function isName(v, name) {
  282. return v instanceof Name && (name === undefined || v.name === name);
  283. }
  284. function isCmd(v, cmd) {
  285. return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
  286. }
  287. function isDict(v, type) {
  288. return v instanceof Dict && (type === undefined || isName(v.get("Type"), type));
  289. }
  290. function isRef(v) {
  291. return v instanceof Ref;
  292. }
  293. function isRefsEqual(v1, v2) {
  294. return v1.num === v2.num && v1.gen === v2.gen;
  295. }
  296. function isStream(v) {
  297. return typeof v === "object" && v !== null && v.getBytes !== undefined;
  298. }
  299. function clearPrimitiveCaches() {
  300. Cmd._clearCache();
  301. Name._clearCache();
  302. Ref._clearCache();
  303. }