primitives.js 6.9 KB

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