2
0

primitives.js 9.4 KB

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