core_utils.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. exports.collectActions = collectActions;
  27. exports.encodeToXmlString = encodeToXmlString;
  28. exports.escapePDFName = escapePDFName;
  29. exports.getArrayLookupTableFactory = getArrayLookupTableFactory;
  30. exports.getInheritableProperty = getInheritableProperty;
  31. exports.getLookupTableFactory = getLookupTableFactory;
  32. exports.isWhiteSpace = isWhiteSpace;
  33. exports.log2 = log2;
  34. exports.parseXFAPath = parseXFAPath;
  35. exports.readInt8 = readInt8;
  36. exports.readUint16 = readUint16;
  37. exports.readUint32 = readUint32;
  38. exports.toRomanNumerals = toRomanNumerals;
  39. exports.XRefParseException = exports.XRefEntryException = exports.MissingDataException = void 0;
  40. var _util = require("../shared/util.js");
  41. var _primitives = require("./primitives.js");
  42. function getLookupTableFactory(initializer) {
  43. let lookup;
  44. return function () {
  45. if (initializer) {
  46. lookup = Object.create(null);
  47. initializer(lookup);
  48. initializer = null;
  49. }
  50. return lookup;
  51. };
  52. }
  53. function getArrayLookupTableFactory(initializer) {
  54. let lookup;
  55. return function () {
  56. if (initializer) {
  57. let arr = initializer();
  58. initializer = null;
  59. lookup = Object.create(null);
  60. for (let i = 0, ii = arr.length; i < ii; i += 2) {
  61. lookup[arr[i]] = arr[i + 1];
  62. }
  63. arr = null;
  64. }
  65. return lookup;
  66. };
  67. }
  68. class MissingDataException extends _util.BaseException {
  69. constructor(begin, end) {
  70. super(`Missing data [${begin}, ${end})`);
  71. this.begin = begin;
  72. this.end = end;
  73. }
  74. }
  75. exports.MissingDataException = MissingDataException;
  76. class XRefEntryException extends _util.BaseException {}
  77. exports.XRefEntryException = XRefEntryException;
  78. class XRefParseException extends _util.BaseException {}
  79. exports.XRefParseException = XRefParseException;
  80. function getInheritableProperty({
  81. dict,
  82. key,
  83. getArray = false,
  84. stopWhenFound = true
  85. }) {
  86. let values;
  87. const visited = new _primitives.RefSet();
  88. while (dict instanceof _primitives.Dict && !(dict.objId && visited.has(dict.objId))) {
  89. if (dict.objId) {
  90. visited.put(dict.objId);
  91. }
  92. const value = getArray ? dict.getArray(key) : dict.get(key);
  93. if (value !== undefined) {
  94. if (stopWhenFound) {
  95. return value;
  96. }
  97. if (!values) {
  98. values = [];
  99. }
  100. values.push(value);
  101. }
  102. dict = dict.get("Parent");
  103. }
  104. return values;
  105. }
  106. const ROMAN_NUMBER_MAP = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
  107. function toRomanNumerals(number, lowerCase = false) {
  108. (0, _util.assert)(Number.isInteger(number) && number > 0, "The number should be a positive integer.");
  109. const romanBuf = [];
  110. let pos;
  111. while (number >= 1000) {
  112. number -= 1000;
  113. romanBuf.push("M");
  114. }
  115. pos = number / 100 | 0;
  116. number %= 100;
  117. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  118. pos = number / 10 | 0;
  119. number %= 10;
  120. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  121. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  122. const romanStr = romanBuf.join("");
  123. return lowerCase ? romanStr.toLowerCase() : romanStr;
  124. }
  125. function log2(x) {
  126. if (x <= 0) {
  127. return 0;
  128. }
  129. return Math.ceil(Math.log2(x));
  130. }
  131. function readInt8(data, offset) {
  132. return data[offset] << 24 >> 24;
  133. }
  134. function readUint16(data, offset) {
  135. return data[offset] << 8 | data[offset + 1];
  136. }
  137. function readUint32(data, offset) {
  138. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  139. }
  140. function isWhiteSpace(ch) {
  141. return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
  142. }
  143. function parseXFAPath(path) {
  144. const positionPattern = /(.+)\[([0-9]+)\]$/;
  145. return path.split(".").map(component => {
  146. const m = component.match(positionPattern);
  147. if (m) {
  148. return {
  149. name: m[1],
  150. pos: parseInt(m[2], 10)
  151. };
  152. }
  153. return {
  154. name: component,
  155. pos: 0
  156. };
  157. });
  158. }
  159. function escapePDFName(str) {
  160. const buffer = [];
  161. let start = 0;
  162. for (let i = 0, ii = str.length; i < ii; i++) {
  163. const char = str.charCodeAt(i);
  164. if (char < 0x21 || char > 0x7e || char === 0x23 || char === 0x28 || char === 0x29 || char === 0x3c || char === 0x3e || char === 0x5b || char === 0x5d || char === 0x7b || char === 0x7d || char === 0x2f || char === 0x25) {
  165. if (start < i) {
  166. buffer.push(str.substring(start, i));
  167. }
  168. buffer.push(`#${char.toString(16)}`);
  169. start = i + 1;
  170. }
  171. }
  172. if (buffer.length === 0) {
  173. return str;
  174. }
  175. if (start < str.length) {
  176. buffer.push(str.substring(start, str.length));
  177. }
  178. return buffer.join("");
  179. }
  180. function _collectJS(entry, xref, list, parents) {
  181. if (!entry) {
  182. return;
  183. }
  184. let parent = null;
  185. if ((0, _primitives.isRef)(entry)) {
  186. if (parents.has(entry)) {
  187. return;
  188. }
  189. parent = entry;
  190. parents.put(parent);
  191. entry = xref.fetch(entry);
  192. }
  193. if (Array.isArray(entry)) {
  194. for (const element of entry) {
  195. _collectJS(element, xref, list, parents);
  196. }
  197. } else if (entry instanceof _primitives.Dict) {
  198. if ((0, _primitives.isName)(entry.get("S"), "JavaScript") && entry.has("JS")) {
  199. const js = entry.get("JS");
  200. let code;
  201. if ((0, _primitives.isStream)(js)) {
  202. code = (0, _util.bytesToString)(js.getBytes());
  203. } else {
  204. code = js;
  205. }
  206. code = (0, _util.stringToPDFString)(code);
  207. if (code) {
  208. list.push(code);
  209. }
  210. }
  211. _collectJS(entry.getRaw("Next"), xref, list, parents);
  212. }
  213. if (parent) {
  214. parents.remove(parent);
  215. }
  216. }
  217. function collectActions(xref, dict, eventType) {
  218. const actions = Object.create(null);
  219. const additionalActionsDicts = getInheritableProperty({
  220. dict,
  221. key: "AA",
  222. stopWhenFound: false
  223. });
  224. if (additionalActionsDicts) {
  225. for (let i = additionalActionsDicts.length - 1; i >= 0; i--) {
  226. const additionalActions = additionalActionsDicts[i];
  227. if (!(additionalActions instanceof _primitives.Dict)) {
  228. continue;
  229. }
  230. for (const key of additionalActions.getKeys()) {
  231. const action = eventType[key];
  232. if (!action) {
  233. continue;
  234. }
  235. const actionDict = additionalActions.getRaw(key);
  236. const parents = new _primitives.RefSet();
  237. const list = [];
  238. _collectJS(actionDict, xref, list, parents);
  239. if (list.length > 0) {
  240. actions[action] = list;
  241. }
  242. }
  243. }
  244. }
  245. if (dict.has("A")) {
  246. const actionDict = dict.get("A");
  247. const parents = new _primitives.RefSet();
  248. const list = [];
  249. _collectJS(actionDict, xref, list, parents);
  250. if (list.length > 0) {
  251. actions.Action = list;
  252. }
  253. }
  254. return (0, _util.objectSize)(actions) > 0 ? actions : null;
  255. }
  256. const XMLEntities = {
  257. 0x3c: "&lt;",
  258. 0x3e: "&gt;",
  259. 0x26: "&amp;",
  260. 0x22: "&quot;",
  261. 0x27: "&apos;"
  262. };
  263. function encodeToXmlString(str) {
  264. const buffer = [];
  265. let start = 0;
  266. for (let i = 0, ii = str.length; i < ii; i++) {
  267. const char = str.codePointAt(i);
  268. if (0x20 <= char && char <= 0x7e) {
  269. const entity = XMLEntities[char];
  270. if (entity) {
  271. if (start < i) {
  272. buffer.push(str.substring(start, i));
  273. }
  274. buffer.push(entity);
  275. start = i + 1;
  276. }
  277. } else {
  278. if (start < i) {
  279. buffer.push(str.substring(start, i));
  280. }
  281. buffer.push(`&#x${char.toString(16).toUpperCase()};`);
  282. if (char > 0xd7ff && (char < 0xe000 || char > 0xfffd)) {
  283. i++;
  284. }
  285. start = i + 1;
  286. }
  287. }
  288. if (buffer.length === 0) {
  289. return str;
  290. }
  291. if (start < str.length) {
  292. buffer.push(str.substring(start, str.length));
  293. }
  294. return buffer.join("");
  295. }