primitives.js 7.5 KB

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