2
0

core_utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.XRefParseException = exports.XRefEntryException = exports.ParserEOFException = exports.MissingDataException = exports.DocStats = void 0;
  27. exports.collectActions = collectActions;
  28. exports.encodeToXmlString = encodeToXmlString;
  29. exports.escapePDFName = escapePDFName;
  30. exports.getArrayLookupTableFactory = getArrayLookupTableFactory;
  31. exports.getInheritableProperty = getInheritableProperty;
  32. exports.getLookupTableFactory = getLookupTableFactory;
  33. exports.getNewAnnotationsMap = getNewAnnotationsMap;
  34. exports.isWhiteSpace = isWhiteSpace;
  35. exports.log2 = log2;
  36. exports.numberToString = numberToString;
  37. exports.parseXFAPath = parseXFAPath;
  38. exports.readInt8 = readInt8;
  39. exports.readUint16 = readUint16;
  40. exports.readUint32 = readUint32;
  41. exports.recoverJsURL = recoverJsURL;
  42. exports.toRomanNumerals = toRomanNumerals;
  43. exports.validateCSSFont = validateCSSFont;
  44. var _util = require("../shared/util.js");
  45. var _primitives = require("./primitives.js");
  46. var _base_stream = require("./base_stream.js");
  47. function getLookupTableFactory(initializer) {
  48. let lookup;
  49. return function () {
  50. if (initializer) {
  51. lookup = Object.create(null);
  52. initializer(lookup);
  53. initializer = null;
  54. }
  55. return lookup;
  56. };
  57. }
  58. function getArrayLookupTableFactory(initializer) {
  59. let lookup;
  60. return function () {
  61. if (initializer) {
  62. let arr = initializer();
  63. initializer = null;
  64. lookup = Object.create(null);
  65. for (let i = 0, ii = arr.length; i < ii; i += 2) {
  66. lookup[arr[i]] = arr[i + 1];
  67. }
  68. arr = null;
  69. }
  70. return lookup;
  71. };
  72. }
  73. class MissingDataException extends _util.BaseException {
  74. constructor(begin, end) {
  75. super(`Missing data [${begin}, ${end})`, "MissingDataException");
  76. this.begin = begin;
  77. this.end = end;
  78. }
  79. }
  80. exports.MissingDataException = MissingDataException;
  81. class ParserEOFException extends _util.BaseException {
  82. constructor(msg) {
  83. super(msg, "ParserEOFException");
  84. }
  85. }
  86. exports.ParserEOFException = ParserEOFException;
  87. class XRefEntryException extends _util.BaseException {
  88. constructor(msg) {
  89. super(msg, "XRefEntryException");
  90. }
  91. }
  92. exports.XRefEntryException = XRefEntryException;
  93. class XRefParseException extends _util.BaseException {
  94. constructor(msg) {
  95. super(msg, "XRefParseException");
  96. }
  97. }
  98. exports.XRefParseException = XRefParseException;
  99. class DocStats {
  100. constructor(handler) {
  101. this._handler = handler;
  102. this._streamTypes = new Set();
  103. this._fontTypes = new Set();
  104. }
  105. _send() {
  106. const streamTypes = Object.create(null),
  107. fontTypes = Object.create(null);
  108. for (const type of this._streamTypes) {
  109. streamTypes[type] = true;
  110. }
  111. for (const type of this._fontTypes) {
  112. fontTypes[type] = true;
  113. }
  114. this._handler.send("DocStats", {
  115. streamTypes,
  116. fontTypes
  117. });
  118. }
  119. addStreamType(type) {
  120. if (this._streamTypes.has(type)) {
  121. return;
  122. }
  123. this._streamTypes.add(type);
  124. this._send();
  125. }
  126. addFontType(type) {
  127. if (this._fontTypes.has(type)) {
  128. return;
  129. }
  130. this._fontTypes.add(type);
  131. this._send();
  132. }
  133. }
  134. exports.DocStats = DocStats;
  135. function getInheritableProperty({
  136. dict,
  137. key,
  138. getArray = false,
  139. stopWhenFound = true
  140. }) {
  141. let values;
  142. const visited = new _primitives.RefSet();
  143. while (dict instanceof _primitives.Dict && !(dict.objId && visited.has(dict.objId))) {
  144. if (dict.objId) {
  145. visited.put(dict.objId);
  146. }
  147. const value = getArray ? dict.getArray(key) : dict.get(key);
  148. if (value !== undefined) {
  149. if (stopWhenFound) {
  150. return value;
  151. }
  152. if (!values) {
  153. values = [];
  154. }
  155. values.push(value);
  156. }
  157. dict = dict.get("Parent");
  158. }
  159. return values;
  160. }
  161. 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"];
  162. function toRomanNumerals(number, lowerCase = false) {
  163. (0, _util.assert)(Number.isInteger(number) && number > 0, "The number should be a positive integer.");
  164. const romanBuf = [];
  165. let pos;
  166. while (number >= 1000) {
  167. number -= 1000;
  168. romanBuf.push("M");
  169. }
  170. pos = number / 100 | 0;
  171. number %= 100;
  172. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  173. pos = number / 10 | 0;
  174. number %= 10;
  175. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  176. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  177. const romanStr = romanBuf.join("");
  178. return lowerCase ? romanStr.toLowerCase() : romanStr;
  179. }
  180. function log2(x) {
  181. if (x <= 0) {
  182. return 0;
  183. }
  184. return Math.ceil(Math.log2(x));
  185. }
  186. function readInt8(data, offset) {
  187. return data[offset] << 24 >> 24;
  188. }
  189. function readUint16(data, offset) {
  190. return data[offset] << 8 | data[offset + 1];
  191. }
  192. function readUint32(data, offset) {
  193. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  194. }
  195. function isWhiteSpace(ch) {
  196. return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
  197. }
  198. function parseXFAPath(path) {
  199. const positionPattern = /(.+)\[(\d+)\]$/;
  200. return path.split(".").map(component => {
  201. const m = component.match(positionPattern);
  202. if (m) {
  203. return {
  204. name: m[1],
  205. pos: parseInt(m[2], 10)
  206. };
  207. }
  208. return {
  209. name: component,
  210. pos: 0
  211. };
  212. });
  213. }
  214. function escapePDFName(str) {
  215. const buffer = [];
  216. let start = 0;
  217. for (let i = 0, ii = str.length; i < ii; i++) {
  218. const char = str.charCodeAt(i);
  219. 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) {
  220. if (start < i) {
  221. buffer.push(str.substring(start, i));
  222. }
  223. buffer.push(`#${char.toString(16)}`);
  224. start = i + 1;
  225. }
  226. }
  227. if (buffer.length === 0) {
  228. return str;
  229. }
  230. if (start < str.length) {
  231. buffer.push(str.substring(start, str.length));
  232. }
  233. return buffer.join("");
  234. }
  235. function _collectJS(entry, xref, list, parents) {
  236. if (!entry) {
  237. return;
  238. }
  239. let parent = null;
  240. if (entry instanceof _primitives.Ref) {
  241. if (parents.has(entry)) {
  242. return;
  243. }
  244. parent = entry;
  245. parents.put(parent);
  246. entry = xref.fetch(entry);
  247. }
  248. if (Array.isArray(entry)) {
  249. for (const element of entry) {
  250. _collectJS(element, xref, list, parents);
  251. }
  252. } else if (entry instanceof _primitives.Dict) {
  253. if ((0, _primitives.isName)(entry.get("S"), "JavaScript")) {
  254. const js = entry.get("JS");
  255. let code;
  256. if (js instanceof _base_stream.BaseStream) {
  257. code = js.getString();
  258. } else if (typeof js === "string") {
  259. code = js;
  260. }
  261. code = code && (0, _util.stringToPDFString)(code).replace(/\u0000/g, "");
  262. if (code) {
  263. list.push(code);
  264. }
  265. }
  266. _collectJS(entry.getRaw("Next"), xref, list, parents);
  267. }
  268. if (parent) {
  269. parents.remove(parent);
  270. }
  271. }
  272. function collectActions(xref, dict, eventType) {
  273. const actions = Object.create(null);
  274. const additionalActionsDicts = getInheritableProperty({
  275. dict,
  276. key: "AA",
  277. stopWhenFound: false
  278. });
  279. if (additionalActionsDicts) {
  280. for (let i = additionalActionsDicts.length - 1; i >= 0; i--) {
  281. const additionalActions = additionalActionsDicts[i];
  282. if (!(additionalActions instanceof _primitives.Dict)) {
  283. continue;
  284. }
  285. for (const key of additionalActions.getKeys()) {
  286. const action = eventType[key];
  287. if (!action) {
  288. continue;
  289. }
  290. const actionDict = additionalActions.getRaw(key);
  291. const parents = new _primitives.RefSet();
  292. const list = [];
  293. _collectJS(actionDict, xref, list, parents);
  294. if (list.length > 0) {
  295. actions[action] = list;
  296. }
  297. }
  298. }
  299. }
  300. if (dict.has("A")) {
  301. const actionDict = dict.get("A");
  302. const parents = new _primitives.RefSet();
  303. const list = [];
  304. _collectJS(actionDict, xref, list, parents);
  305. if (list.length > 0) {
  306. actions.Action = list;
  307. }
  308. }
  309. return (0, _util.objectSize)(actions) > 0 ? actions : null;
  310. }
  311. const XMLEntities = {
  312. 0x3c: "&lt;",
  313. 0x3e: "&gt;",
  314. 0x26: "&amp;",
  315. 0x22: "&quot;",
  316. 0x27: "&apos;"
  317. };
  318. function encodeToXmlString(str) {
  319. const buffer = [];
  320. let start = 0;
  321. for (let i = 0, ii = str.length; i < ii; i++) {
  322. const char = str.codePointAt(i);
  323. if (0x20 <= char && char <= 0x7e) {
  324. const entity = XMLEntities[char];
  325. if (entity) {
  326. if (start < i) {
  327. buffer.push(str.substring(start, i));
  328. }
  329. buffer.push(entity);
  330. start = i + 1;
  331. }
  332. } else {
  333. if (start < i) {
  334. buffer.push(str.substring(start, i));
  335. }
  336. buffer.push(`&#x${char.toString(16).toUpperCase()};`);
  337. if (char > 0xd7ff && (char < 0xe000 || char > 0xfffd)) {
  338. i++;
  339. }
  340. start = i + 1;
  341. }
  342. }
  343. if (buffer.length === 0) {
  344. return str;
  345. }
  346. if (start < str.length) {
  347. buffer.push(str.substring(start, str.length));
  348. }
  349. return buffer.join("");
  350. }
  351. function validateCSSFont(cssFontInfo) {
  352. const DEFAULT_CSS_FONT_OBLIQUE = "14";
  353. const DEFAULT_CSS_FONT_WEIGHT = "400";
  354. const CSS_FONT_WEIGHT_VALUES = new Set(["100", "200", "300", "400", "500", "600", "700", "800", "900", "1000", "normal", "bold", "bolder", "lighter"]);
  355. const {
  356. fontFamily,
  357. fontWeight,
  358. italicAngle
  359. } = cssFontInfo;
  360. if (/^".*"$/.test(fontFamily)) {
  361. if (/[^\\]"/.test(fontFamily.slice(1, fontFamily.length - 1))) {
  362. (0, _util.warn)(`XFA - FontFamily contains some unescaped ": ${fontFamily}.`);
  363. return false;
  364. }
  365. } else if (/^'.*'$/.test(fontFamily)) {
  366. if (/[^\\]'/.test(fontFamily.slice(1, fontFamily.length - 1))) {
  367. (0, _util.warn)(`XFA - FontFamily contains some unescaped ': ${fontFamily}.`);
  368. return false;
  369. }
  370. } else {
  371. for (const ident of fontFamily.split(/[ \t]+/)) {
  372. if (/^(\d|(-(\d|-)))/.test(ident) || !/^[\w-\\]+$/.test(ident)) {
  373. (0, _util.warn)(`XFA - FontFamily contains some invalid <custom-ident>: ${fontFamily}.`);
  374. return false;
  375. }
  376. }
  377. }
  378. const weight = fontWeight ? fontWeight.toString() : "";
  379. cssFontInfo.fontWeight = CSS_FONT_WEIGHT_VALUES.has(weight) ? weight : DEFAULT_CSS_FONT_WEIGHT;
  380. const angle = parseFloat(italicAngle);
  381. cssFontInfo.italicAngle = isNaN(angle) || angle < -90 || angle > 90 ? DEFAULT_CSS_FONT_OBLIQUE : italicAngle.toString();
  382. return true;
  383. }
  384. function recoverJsURL(str) {
  385. const URL_OPEN_METHODS = ["app.launchURL", "window.open", "xfa.host.gotoURL"];
  386. const regex = new RegExp("^\\s*(" + URL_OPEN_METHODS.join("|").split(".").join("\\.") + ")\\((?:'|\")([^'\"]*)(?:'|\")(?:,\\s*(\\w+)\\)|\\))", "i");
  387. const jsUrl = regex.exec(str);
  388. if (jsUrl && jsUrl[2]) {
  389. const url = jsUrl[2];
  390. let newWindow = false;
  391. if (jsUrl[3] === "true" && jsUrl[1] === "app.launchURL") {
  392. newWindow = true;
  393. }
  394. return {
  395. url,
  396. newWindow
  397. };
  398. }
  399. return null;
  400. }
  401. function numberToString(value) {
  402. if (Number.isInteger(value)) {
  403. return value.toString();
  404. }
  405. const roundedValue = Math.round(value * 100);
  406. if (roundedValue % 100 === 0) {
  407. return (roundedValue / 100).toString();
  408. }
  409. if (roundedValue % 10 === 0) {
  410. return value.toFixed(1);
  411. }
  412. return value.toFixed(2);
  413. }
  414. function getNewAnnotationsMap(annotationStorage) {
  415. if (!annotationStorage) {
  416. return null;
  417. }
  418. const newAnnotationsByPage = new Map();
  419. for (const [key, value] of annotationStorage) {
  420. if (!key.startsWith(_util.AnnotationEditorPrefix)) {
  421. continue;
  422. }
  423. let annotations = newAnnotationsByPage.get(value.pageIndex);
  424. if (!annotations) {
  425. annotations = [];
  426. newAnnotationsByPage.set(value.pageIndex, annotations);
  427. }
  428. annotations.push(value);
  429. }
  430. return newAnnotationsByPage.size > 0 ? newAnnotationsByPage : null;
  431. }