2
0

core_utils.js 14 KB

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