primitives.js 7.5 KB

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