core_utils.js 13 KB

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