primitives.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /* uses XRef */
  16. import { assert, unreachable } from "./util.js";
  17. var EOF = {};
  18. var Name = (function NameClosure() {
  19. let nameCache = Object.create(null);
  20. // eslint-disable-next-line no-shadow
  21. function Name(name) {
  22. this.name = name;
  23. }
  24. Name.prototype = {};
  25. Name.get = function Name_get(name) {
  26. var nameValue = nameCache[name];
  27. // eslint-disable-next-line no-restricted-syntax
  28. return nameValue ? nameValue : (nameCache[name] = new Name(name));
  29. };
  30. Name._clearCache = function () {
  31. nameCache = Object.create(null);
  32. };
  33. return Name;
  34. })();
  35. var Cmd = (function CmdClosure() {
  36. let cmdCache = Object.create(null);
  37. // eslint-disable-next-line no-shadow
  38. function Cmd(cmd) {
  39. this.cmd = cmd;
  40. }
  41. Cmd.prototype = {};
  42. Cmd.get = function Cmd_get(cmd) {
  43. var cmdValue = cmdCache[cmd];
  44. // eslint-disable-next-line no-restricted-syntax
  45. return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd));
  46. };
  47. Cmd._clearCache = function () {
  48. cmdCache = Object.create(null);
  49. };
  50. return Cmd;
  51. })();
  52. var Dict = (function DictClosure() {
  53. var nonSerializable = function nonSerializableClosure() {
  54. return nonSerializable; // creating closure on some variable
  55. };
  56. // xref is optional
  57. // eslint-disable-next-line no-shadow
  58. function Dict(xref) {
  59. // Map should only be used internally, use functions below to access.
  60. this._map = Object.create(null);
  61. this.xref = xref;
  62. this.objId = null;
  63. this.suppressEncryption = false;
  64. this.__nonSerializable__ = nonSerializable; // disable cloning of the Dict
  65. }
  66. Dict.prototype = {
  67. assignXref: function Dict_assignXref(newXref) {
  68. this.xref = newXref;
  69. },
  70. get size() {
  71. return Object.keys(this._map).length;
  72. },
  73. // automatically dereferences Ref objects
  74. get(key1, key2, key3) {
  75. let value = this._map[key1];
  76. if (value === undefined && key2 !== undefined) {
  77. value = this._map[key2];
  78. if (value === undefined && key3 !== undefined) {
  79. value = this._map[key3];
  80. }
  81. }
  82. if (value instanceof Ref && this.xref) {
  83. return this.xref.fetch(value, this.suppressEncryption);
  84. }
  85. return value;
  86. },
  87. // Same as get(), but returns a promise and uses fetchIfRefAsync().
  88. async getAsync(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.fetchAsync(value, this.suppressEncryption);
  98. }
  99. return value;
  100. },
  101. // Same as get(), but dereferences all elements if the result is an Array.
  102. getArray(key1, key2, key3) {
  103. let value = this.get(key1, key2, key3);
  104. if (!Array.isArray(value) || !this.xref) {
  105. return value;
  106. }
  107. value = value.slice(); // Ensure that we don't modify the Dict data.
  108. for (let i = 0, ii = value.length; i < ii; i++) {
  109. if (!(value[i] instanceof Ref)) {
  110. continue;
  111. }
  112. value[i] = this.xref.fetch(value[i], this.suppressEncryption);
  113. }
  114. return value;
  115. },
  116. // no dereferencing
  117. getRaw: function Dict_getRaw(key) {
  118. return this._map[key];
  119. },
  120. getKeys: function Dict_getKeys() {
  121. return Object.keys(this._map);
  122. },
  123. // no dereferencing
  124. getRawValues: function Dict_getRawValues() {
  125. return Object.values(this._map);
  126. },
  127. set: function Dict_set(key, value) {
  128. if (
  129. (typeof PDFJSDev === "undefined" ||
  130. PDFJSDev.test("!PRODUCTION || TESTING")) &&
  131. value === undefined
  132. ) {
  133. unreachable('Dict.set: The "value" cannot be undefined.');
  134. }
  135. this._map[key] = value;
  136. },
  137. has: function Dict_has(key) {
  138. return this._map[key] !== undefined;
  139. },
  140. forEach: function Dict_forEach(callback) {
  141. for (var key in this._map) {
  142. callback(key, this.get(key));
  143. }
  144. },
  145. };
  146. Dict.empty = new Dict(null);
  147. Dict.merge = function ({ xref, dictArray, mergeSubDicts = false }) {
  148. const mergedDict = new Dict(xref);
  149. if (!mergeSubDicts) {
  150. for (const dict of dictArray) {
  151. if (!(dict instanceof Dict)) {
  152. continue;
  153. }
  154. for (const [key, value] of Object.entries(dict._map)) {
  155. if (mergedDict._map[key] === undefined) {
  156. mergedDict._map[key] = value;
  157. }
  158. }
  159. }
  160. return mergedDict.size > 0 ? mergedDict : Dict.empty;
  161. }
  162. const properties = new Map();
  163. for (const dict of dictArray) {
  164. if (!(dict instanceof Dict)) {
  165. continue;
  166. }
  167. for (const [key, value] of Object.entries(dict._map)) {
  168. let property = properties.get(key);
  169. if (property === undefined) {
  170. property = [];
  171. properties.set(key, property);
  172. }
  173. property.push(value);
  174. }
  175. }
  176. for (const [name, values] of properties) {
  177. if (values.length === 1 || !(values[0] instanceof Dict)) {
  178. mergedDict._map[name] = values[0];
  179. continue;
  180. }
  181. const subDict = new Dict(xref);
  182. for (const dict of values) {
  183. if (!(dict instanceof Dict)) {
  184. continue;
  185. }
  186. for (const [key, value] of Object.entries(dict._map)) {
  187. if (subDict._map[key] === undefined) {
  188. subDict._map[key] = value;
  189. }
  190. }
  191. }
  192. if (subDict.size > 0) {
  193. mergedDict._map[name] = subDict;
  194. }
  195. }
  196. properties.clear();
  197. return mergedDict.size > 0 ? mergedDict : Dict.empty;
  198. };
  199. return Dict;
  200. })();
  201. var Ref = (function RefClosure() {
  202. let refCache = Object.create(null);
  203. // eslint-disable-next-line no-shadow
  204. function Ref(num, gen) {
  205. this.num = num;
  206. this.gen = gen;
  207. }
  208. Ref.prototype = {
  209. toString: function Ref_toString() {
  210. // This function is hot, so we make the string as compact as possible.
  211. // |this.gen| is almost always zero, so we treat that case specially.
  212. if (this.gen === 0) {
  213. return `${this.num}R`;
  214. }
  215. return `${this.num}R${this.gen}`;
  216. },
  217. };
  218. Ref.get = function (num, gen) {
  219. const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
  220. const refValue = refCache[key];
  221. // eslint-disable-next-line no-restricted-syntax
  222. return refValue ? refValue : (refCache[key] = new Ref(num, gen));
  223. };
  224. Ref._clearCache = function () {
  225. refCache = Object.create(null);
  226. };
  227. return Ref;
  228. })();
  229. // The reference is identified by number and generation.
  230. // This structure stores only one instance of the reference.
  231. class RefSet {
  232. constructor() {
  233. this._set = new 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. }
  245. class RefSetCache {
  246. constructor() {
  247. this._map = new Map();
  248. }
  249. get size() {
  250. return this._map.size;
  251. }
  252. get(ref) {
  253. return this._map.get(ref.toString());
  254. }
  255. has(ref) {
  256. return this._map.has(ref.toString());
  257. }
  258. put(ref, obj) {
  259. this._map.set(ref.toString(), obj);
  260. }
  261. putAlias(ref, aliasRef) {
  262. this._map.set(ref.toString(), this.get(aliasRef));
  263. }
  264. forEach(callback) {
  265. for (const value of this._map.values()) {
  266. callback(value);
  267. }
  268. }
  269. clear() {
  270. this._map.clear();
  271. }
  272. }
  273. function isEOF(v) {
  274. return v === EOF;
  275. }
  276. function isName(v, name) {
  277. return v instanceof Name && (name === undefined || v.name === name);
  278. }
  279. function isCmd(v, cmd) {
  280. return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
  281. }
  282. function isDict(v, type) {
  283. return (
  284. v instanceof Dict && (type === undefined || isName(v.get("Type"), type))
  285. );
  286. }
  287. function isRef(v) {
  288. return v instanceof Ref;
  289. }
  290. function isRefsEqual(v1, v2) {
  291. if (
  292. typeof PDFJSDev === "undefined" ||
  293. PDFJSDev.test("!PRODUCTION || TESTING")
  294. ) {
  295. assert(
  296. v1 instanceof Ref && v2 instanceof Ref,
  297. "isRefsEqual: Both parameters should be `Ref`s."
  298. );
  299. }
  300. return v1.num === v2.num && v1.gen === v2.gen;
  301. }
  302. function isStream(v) {
  303. return typeof v === "object" && v !== null && v.getBytes !== undefined;
  304. }
  305. function clearPrimitiveCaches() {
  306. Cmd._clearCache();
  307. Name._clearCache();
  308. Ref._clearCache();
  309. }
  310. export {
  311. EOF,
  312. clearPrimitiveCaches,
  313. Cmd,
  314. Dict,
  315. Name,
  316. Ref,
  317. RefSet,
  318. RefSetCache,
  319. isEOF,
  320. isCmd,
  321. isDict,
  322. isName,
  323. isRef,
  324. isRefsEqual,
  325. isStream,
  326. };