primitives.js 7.3 KB

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