2
0

primitives.js 7.3 KB

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