primitives.js 7.7 KB

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