2
0

primitives.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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");
  36. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  37. var EOF = {};
  38. exports.EOF = EOF;
  39. var Name = function NameClosure() {
  40. var nameCache = Object.create(null);
  41. function Name(name) {
  42. this.name = name;
  43. }
  44. Name.prototype = {};
  45. Name.get = function Name_get(name) {
  46. var nameValue = nameCache[name];
  47. return nameValue ? nameValue : nameCache[name] = new Name(name);
  48. };
  49. Name._clearCache = function () {
  50. nameCache = Object.create(null);
  51. };
  52. return Name;
  53. }();
  54. exports.Name = Name;
  55. var Cmd = function CmdClosure() {
  56. var cmdCache = Object.create(null);
  57. function Cmd(cmd) {
  58. this.cmd = cmd;
  59. }
  60. Cmd.prototype = {};
  61. Cmd.get = function Cmd_get(cmd) {
  62. var cmdValue = cmdCache[cmd];
  63. return cmdValue ? cmdValue : cmdCache[cmd] = new Cmd(cmd);
  64. };
  65. Cmd._clearCache = function () {
  66. cmdCache = Object.create(null);
  67. };
  68. return Cmd;
  69. }();
  70. exports.Cmd = Cmd;
  71. var Dict = function DictClosure() {
  72. var nonSerializable = function nonSerializableClosure() {
  73. return nonSerializable;
  74. };
  75. function Dict(xref) {
  76. this._map = Object.create(null);
  77. this.xref = xref;
  78. this.objId = null;
  79. this.suppressEncryption = false;
  80. this.__nonSerializable__ = nonSerializable;
  81. }
  82. Dict.prototype = {
  83. assignXref: function Dict_assignXref(newXref) {
  84. this.xref = newXref;
  85. },
  86. get: function Dict_get(key1, key2, key3) {
  87. var value;
  88. var xref = this.xref,
  89. suppressEncryption = this.suppressEncryption;
  90. if (typeof (value = this._map[key1]) !== 'undefined' || key1 in this._map || typeof key2 === 'undefined') {
  91. return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
  92. }
  93. if (typeof (value = this._map[key2]) !== 'undefined' || key2 in this._map || typeof key3 === 'undefined') {
  94. return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
  95. }
  96. value = this._map[key3] || null;
  97. return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
  98. },
  99. getAsync: function Dict_getAsync(key1, key2, key3) {
  100. var value;
  101. var xref = this.xref,
  102. suppressEncryption = this.suppressEncryption;
  103. if (typeof (value = this._map[key1]) !== 'undefined' || key1 in this._map || typeof key2 === 'undefined') {
  104. if (xref) {
  105. return xref.fetchIfRefAsync(value, suppressEncryption);
  106. }
  107. return Promise.resolve(value);
  108. }
  109. if (typeof (value = this._map[key2]) !== 'undefined' || key2 in this._map || typeof key3 === 'undefined') {
  110. if (xref) {
  111. return xref.fetchIfRefAsync(value, suppressEncryption);
  112. }
  113. return Promise.resolve(value);
  114. }
  115. value = this._map[key3] || null;
  116. if (xref) {
  117. return xref.fetchIfRefAsync(value, suppressEncryption);
  118. }
  119. return Promise.resolve(value);
  120. },
  121. getArray: function Dict_getArray(key1, key2, key3) {
  122. var value = this.get(key1, key2, key3);
  123. var xref = this.xref,
  124. suppressEncryption = this.suppressEncryption;
  125. if (!Array.isArray(value) || !xref) {
  126. return value;
  127. }
  128. value = value.slice();
  129. for (var i = 0, ii = value.length; i < ii; i++) {
  130. if (!isRef(value[i])) {
  131. continue;
  132. }
  133. value[i] = xref.fetch(value[i], suppressEncryption);
  134. }
  135. return value;
  136. },
  137. getRaw: function Dict_getRaw(key) {
  138. return this._map[key];
  139. },
  140. getKeys: function Dict_getKeys() {
  141. return Object.keys(this._map);
  142. },
  143. set: function Dict_set(key, value) {
  144. this._map[key] = value;
  145. },
  146. has: function Dict_has(key) {
  147. return key in this._map;
  148. },
  149. forEach: function Dict_forEach(callback) {
  150. for (var key in this._map) {
  151. callback(key, this.get(key));
  152. }
  153. }
  154. };
  155. Dict.empty = new Dict(null);
  156. Dict.merge = function (xref, dictArray) {
  157. var mergedDict = new Dict(xref);
  158. for (var i = 0, ii = dictArray.length; i < ii; i++) {
  159. var dict = dictArray[i];
  160. if (!isDict(dict)) {
  161. continue;
  162. }
  163. for (var keyName in dict._map) {
  164. if (mergedDict._map[keyName] !== undefined) {
  165. continue;
  166. }
  167. mergedDict._map[keyName] = dict._map[keyName];
  168. }
  169. }
  170. return mergedDict;
  171. };
  172. return Dict;
  173. }();
  174. exports.Dict = Dict;
  175. var Ref = function RefClosure() {
  176. var refCache = Object.create(null);
  177. function Ref(num, gen) {
  178. this.num = num;
  179. this.gen = gen;
  180. }
  181. Ref.prototype = {
  182. toString: function Ref_toString() {
  183. if (this.gen === 0) {
  184. return "".concat(this.num, "R");
  185. }
  186. return "".concat(this.num, "R").concat(this.gen);
  187. }
  188. };
  189. Ref.get = function (num, gen) {
  190. var key = gen === 0 ? "".concat(num, "R") : "".concat(num, "R").concat(gen);
  191. var refValue = refCache[key];
  192. return refValue ? refValue : refCache[key] = new Ref(num, gen);
  193. };
  194. Ref._clearCache = function () {
  195. refCache = Object.create(null);
  196. };
  197. return Ref;
  198. }();
  199. exports.Ref = Ref;
  200. var RefSet = function RefSetClosure() {
  201. function RefSet() {
  202. this.dict = Object.create(null);
  203. }
  204. RefSet.prototype = {
  205. has: function RefSet_has(ref) {
  206. return ref.toString() in this.dict;
  207. },
  208. put: function RefSet_put(ref) {
  209. this.dict[ref.toString()] = true;
  210. },
  211. remove: function RefSet_remove(ref) {
  212. delete this.dict[ref.toString()];
  213. }
  214. };
  215. return RefSet;
  216. }();
  217. exports.RefSet = RefSet;
  218. var RefSetCache = function RefSetCacheClosure() {
  219. function RefSetCache() {
  220. this.dict = Object.create(null);
  221. }
  222. RefSetCache.prototype = {
  223. get: function RefSetCache_get(ref) {
  224. return this.dict[ref.toString()];
  225. },
  226. has: function RefSetCache_has(ref) {
  227. return ref.toString() in this.dict;
  228. },
  229. put: function RefSetCache_put(ref, obj) {
  230. this.dict[ref.toString()] = obj;
  231. },
  232. putAlias: function RefSetCache_putAlias(ref, aliasRef) {
  233. this.dict[ref.toString()] = this.get(aliasRef);
  234. },
  235. forEach: function RefSetCache_forEach(fn, thisArg) {
  236. for (var i in this.dict) {
  237. fn.call(thisArg, this.dict[i]);
  238. }
  239. },
  240. clear: function RefSetCache_clear() {
  241. this.dict = Object.create(null);
  242. }
  243. };
  244. return RefSetCache;
  245. }();
  246. exports.RefSetCache = RefSetCache;
  247. function isEOF(v) {
  248. return v === EOF;
  249. }
  250. function isName(v, name) {
  251. return v instanceof Name && (name === undefined || v.name === name);
  252. }
  253. function isCmd(v, cmd) {
  254. return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
  255. }
  256. function isDict(v, type) {
  257. return v instanceof Dict && (type === undefined || isName(v.get('Type'), type));
  258. }
  259. function isRef(v) {
  260. return v instanceof Ref;
  261. }
  262. function isRefsEqual(v1, v2) {
  263. return v1.num === v2.num && v1.gen === v2.gen;
  264. }
  265. function isStream(v) {
  266. return _typeof(v) === 'object' && v !== null && v.getBytes !== undefined;
  267. }
  268. function clearPrimitiveCaches() {
  269. Cmd._clearCache();
  270. Name._clearCache();
  271. Ref._clearCache();
  272. }