primitives.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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(key1, key2, key3) {
  86. let value = this._map[key1];
  87. if (value === undefined && key2 !== undefined) {
  88. value = this._map[key2];
  89. if (value === undefined && key3 !== undefined) {
  90. value = this._map[key3];
  91. }
  92. }
  93. if (value instanceof Ref && this.xref) {
  94. return this.xref.fetch(value, this.suppressEncryption);
  95. }
  96. return value;
  97. },
  98. async getAsync(key1, key2, key3) {
  99. let value = this._map[key1];
  100. if (value === undefined && key2 !== undefined) {
  101. value = this._map[key2];
  102. if (value === undefined && key3 !== undefined) {
  103. value = this._map[key3];
  104. }
  105. }
  106. if (value instanceof Ref && this.xref) {
  107. return this.xref.fetchAsync(value, this.suppressEncryption);
  108. }
  109. return value;
  110. },
  111. getArray(key1, key2, key3) {
  112. let value = this.get(key1, key2, key3);
  113. if (!Array.isArray(value) || !this.xref) {
  114. return value;
  115. }
  116. value = value.slice();
  117. for (let i = 0, ii = value.length; i < ii; i++) {
  118. if (!(value[i] instanceof Ref)) {
  119. continue;
  120. }
  121. value[i] = this.xref.fetch(value[i], this.suppressEncryption);
  122. }
  123. return value;
  124. },
  125. getRaw: function Dict_getRaw(key) {
  126. return this._map[key];
  127. },
  128. getKeys: function Dict_getKeys() {
  129. return Object.keys(this._map);
  130. },
  131. set: function Dict_set(key, value) {
  132. this._map[key] = value;
  133. },
  134. has: function Dict_has(key) {
  135. return this._map[key] !== undefined;
  136. },
  137. forEach: function Dict_forEach(callback) {
  138. for (var key in this._map) {
  139. callback(key, this.get(key));
  140. }
  141. }
  142. };
  143. Dict.empty = new Dict(null);
  144. Dict.merge = function (xref, dictArray) {
  145. const mergedDict = new Dict(xref);
  146. for (let i = 0, ii = dictArray.length; i < ii; i++) {
  147. const dict = dictArray[i];
  148. if (!isDict(dict)) {
  149. continue;
  150. }
  151. for (const keyName in dict._map) {
  152. if (mergedDict._map[keyName] !== undefined) {
  153. continue;
  154. }
  155. mergedDict._map[keyName] = dict._map[keyName];
  156. }
  157. }
  158. return mergedDict;
  159. };
  160. return Dict;
  161. }();
  162. exports.Dict = Dict;
  163. var Ref = function RefClosure() {
  164. let refCache = Object.create(null);
  165. function Ref(num, gen) {
  166. this.num = num;
  167. this.gen = gen;
  168. }
  169. Ref.prototype = {
  170. toString: function Ref_toString() {
  171. if (this.gen === 0) {
  172. return `${this.num}R`;
  173. }
  174. return `${this.num}R${this.gen}`;
  175. }
  176. };
  177. Ref.get = function (num, gen) {
  178. const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
  179. const refValue = refCache[key];
  180. return refValue ? refValue : refCache[key] = new Ref(num, gen);
  181. };
  182. Ref._clearCache = function () {
  183. refCache = Object.create(null);
  184. };
  185. return Ref;
  186. }();
  187. exports.Ref = Ref;
  188. var RefSet = function RefSetClosure() {
  189. function RefSet() {
  190. this.dict = Object.create(null);
  191. }
  192. RefSet.prototype = {
  193. has: function RefSet_has(ref) {
  194. return ref.toString() in this.dict;
  195. },
  196. put: function RefSet_put(ref) {
  197. this.dict[ref.toString()] = true;
  198. },
  199. remove: function RefSet_remove(ref) {
  200. delete this.dict[ref.toString()];
  201. }
  202. };
  203. return RefSet;
  204. }();
  205. exports.RefSet = RefSet;
  206. var RefSetCache = function RefSetCacheClosure() {
  207. function RefSetCache() {
  208. this.dict = Object.create(null);
  209. }
  210. RefSetCache.prototype = {
  211. get size() {
  212. return Object.keys(this.dict).length;
  213. },
  214. get: function RefSetCache_get(ref) {
  215. return this.dict[ref.toString()];
  216. },
  217. has: function RefSetCache_has(ref) {
  218. return ref.toString() in this.dict;
  219. },
  220. put: function RefSetCache_put(ref, obj) {
  221. this.dict[ref.toString()] = obj;
  222. },
  223. putAlias: function RefSetCache_putAlias(ref, aliasRef) {
  224. this.dict[ref.toString()] = this.get(aliasRef);
  225. },
  226. forEach: function RefSetCache_forEach(callback) {
  227. for (const i in this.dict) {
  228. callback(this.dict[i]);
  229. }
  230. },
  231. clear: function RefSetCache_clear() {
  232. this.dict = Object.create(null);
  233. }
  234. };
  235. return RefSetCache;
  236. }();
  237. exports.RefSetCache = RefSetCache;
  238. function isEOF(v) {
  239. return v === EOF;
  240. }
  241. function isName(v, name) {
  242. return v instanceof Name && (name === undefined || v.name === name);
  243. }
  244. function isCmd(v, cmd) {
  245. return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
  246. }
  247. function isDict(v, type) {
  248. return v instanceof Dict && (type === undefined || isName(v.get("Type"), type));
  249. }
  250. function isRef(v) {
  251. return v instanceof Ref;
  252. }
  253. function isRefsEqual(v1, v2) {
  254. return v1.num === v2.num && v1.gen === v2.gen;
  255. }
  256. function isStream(v) {
  257. return typeof v === "object" && v !== null && v.getBytes !== undefined;
  258. }
  259. function clearPrimitiveCaches() {
  260. Cmd._clearCache();
  261. Name._clearCache();
  262. Ref._clearCache();
  263. }