@syzlm/ofdparser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/.babelrc +14 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.json +18 -0
  4. package/.prettierrc.json +9 -0
  5. package/dist/OFD.d.ts +8 -0
  6. package/dist/Pages.d.ts +7 -0
  7. package/dist/RootDocumnet.d.ts +1 -0
  8. package/dist/bundle.cjs.js +64123 -0
  9. package/dist/bundle.esm.js +64115 -0
  10. package/dist/bundle.iife.js +14743 -0
  11. package/dist/bundle.umd.js +64045 -0
  12. package/dist/constant/index.d.ts +7 -0
  13. package/dist/constant.d.ts +6 -0
  14. package/dist/core/ofd-core/Annotations/AnnotationsXml.d.ts +16 -0
  15. package/dist/core/ofd-core/Asn.1Utils.d.ts +47 -0
  16. package/dist/core/ofd-core/Documents/DocumentResXml.d.ts +15 -0
  17. package/dist/core/ofd-core/Documents/DocumnetXml.d.ts +25 -0
  18. package/dist/core/ofd-core/Documents/PublicResXml.d.ts +15 -0
  19. package/dist/core/ofd-core/OFDElement.d.ts +232 -0
  20. package/dist/core/ofd-core/Ofd/OFDXml.d.ts +19 -0
  21. package/dist/core/ofd-core/Pages/PageXml.d.ts +20 -0
  22. package/dist/core/ofd-core/Signatures/Signatures.d.ts +20 -0
  23. package/dist/core/ofd-core/TemplatePages/TemplatePages.d.ts +20 -0
  24. package/dist/core/ofd-core/constant.d.ts +29 -0
  25. package/dist/core/ofd-core/utils.d.ts +21 -0
  26. package/dist/dicom/dicom.d.ts +15 -0
  27. package/dist/dicom/index.d.ts +1 -0
  28. package/dist/errors/ErrorHandle.d.ts +6 -0
  29. package/dist/errors/ErrorMsg.d.ts +16 -0
  30. package/dist/errors/FetchErrors.d.ts +4 -0
  31. package/dist/errors/MimeError.d.ts +4 -0
  32. package/dist/errors/OFDErrors.d.ts +4 -0
  33. package/dist/errors/UnzipErrors.d.ts +4 -0
  34. package/dist/errors/XmlErrors.d.ts +4 -0
  35. package/dist/errors/error-msg.d.ts +9 -0
  36. package/dist/es.d.ts +2 -0
  37. package/dist/index.d.ts +6 -0
  38. package/dist/lib/ParseFile.d.ts +7 -0
  39. package/dist/lib/UnitCoversion.d.ts +31 -0
  40. package/dist/lib/asn1/Asn.1.d.ts +34 -0
  41. package/dist/lib/asn1/Stream.d.ts +27 -0
  42. package/dist/lib/asn1/asn1.d.ts +4 -0
  43. package/dist/lib/asn1/base64.d.ts +4 -0
  44. package/dist/lib/asn1/hex.d.ts +5 -0
  45. package/dist/lib/asn1/index.d.ts +5 -0
  46. package/dist/lib/asn1/int10.d.ts +34 -0
  47. package/dist/lib/asn1/oids.d.ts +2 -0
  48. package/dist/lib/asn1/string-cut.d.ts +2 -0
  49. package/dist/lib/decode-html.d.ts +7 -0
  50. package/dist/lib/fetch.d.ts +17 -0
  51. package/dist/lib/is-element.d.ts +5 -0
  52. package/dist/lib/load-files.d.ts +3 -0
  53. package/dist/lib/load-opentype.d.ts +34 -0
  54. package/dist/lib/load-script.d.ts +1 -0
  55. package/dist/lib/mime.d.ts +8 -0
  56. package/dist/lib/read-buffer.d.ts +2 -0
  57. package/dist/lib/toFixeds.d.ts +7 -0
  58. package/dist/libs/Asn.1Utils.d.ts +47 -0
  59. package/dist/libs/asn1/Asn.1.d.ts +34 -0
  60. package/dist/libs/asn1/Stream.d.ts +27 -0
  61. package/dist/libs/asn1/asn1.d.ts +4 -0
  62. package/dist/libs/asn1/base64.d.ts +4 -0
  63. package/dist/libs/asn1/hex.d.ts +5 -0
  64. package/dist/libs/asn1/index.d.ts +5 -0
  65. package/dist/libs/asn1/int10.d.ts +34 -0
  66. package/dist/libs/asn1/oids.d.ts +2 -0
  67. package/dist/libs/asn1/string-cut.d.ts +2 -0
  68. package/dist/libs/decode-html.d.ts +7 -0
  69. package/dist/libs/fetch.d.ts +17 -0
  70. package/dist/libs/mime.d.ts +8 -0
  71. package/dist/libs/ofdArray.d.ts +10 -0
  72. package/dist/libs/ofdImageObject.d.ts +9 -0
  73. package/dist/libs/ofdLayer.d.ts +3 -0
  74. package/dist/libs/ofdName.d.ts +17 -0
  75. package/dist/libs/ofdPathObject.d.ts +11 -0
  76. package/dist/libs/ofdTextObject.d.ts +6 -0
  77. package/dist/libs/parseAnnotation.d.ts +5 -0
  78. package/dist/libs/parseColor.d.ts +5 -0
  79. package/dist/libs/parsePageArea.d.ts +17 -0
  80. package/dist/libs/parser-elements.d.ts +8 -0
  81. package/dist/libs/parserElements.d.ts +8 -0
  82. package/dist/libs/pipeExtensions.d.ts +4 -0
  83. package/dist/libs/read-buffer.d.ts +2 -0
  84. package/dist/main.d.ts +15 -0
  85. package/dist/ofd/ConverterDpi.d.ts +16 -0
  86. package/dist/ofd/index.d.ts +1 -0
  87. package/dist/ofd/ofd-utils.d.ts +137 -0
  88. package/dist/ofd/ofd.d.ts +35 -0
  89. package/dist/ofd/parse-ofd.d.ts +18 -0
  90. package/dist/ofd/render.d.ts +18 -0
  91. package/dist/ofd/svg-image.d.ts +12 -0
  92. package/dist/ofd/svg-path.d.ts +5 -0
  93. package/dist/ofd/svg-text.d.ts +8 -0
  94. package/dist/ofd.parse.d.ts +6 -0
  95. package/dist/ofd.xml.d.ts +1 -0
  96. package/dist/ofdParse.d.ts +6 -0
  97. package/dist/pipe/annotations.d.ts +13 -0
  98. package/dist/pipe/attachments.d.ts +13 -0
  99. package/dist/pipe/commonData.d.ts +18 -0
  100. package/dist/pipe/customTags.d.ts +14 -0
  101. package/dist/pipe/extensions.d.ts +14 -0
  102. package/dist/pipe/output.d.ts +7 -0
  103. package/dist/pipe/pages.d.ts +13 -0
  104. package/dist/pipe/permissions.d.ts +14 -0
  105. package/dist/pipe/pipeline.d.ts +63 -0
  106. package/dist/pipe/startPipe.d.ts +11 -0
  107. package/dist/xml/index.d.ts +2 -0
  108. package/dist/xml/render.d.ts +6 -0
  109. package/dist/xml/xml.d.ts +18 -0
  110. package/package.json +47 -0
  111. package/public/index.html +75 -0
  112. package/public/opentype.min.js +2 -0
  113. package/rollup.config.dev.mjs +47 -0
  114. package/rollup.config.mjs +70 -0
  115. package/src/constant/index.ts +13 -0
  116. package/src/core/ofd-core/Annotations/Annotation.km +206 -0
  117. package/src/core/ofd-core/Annotations/Annotations.km +59 -0
  118. package/src/core/ofd-core/Annotations/Annotations.md +13 -0
  119. package/src/core/ofd-core/Annotations/AnnotationsXml.ts +114 -0
  120. package/src/core/ofd-core/Asn.1Utils.ts +225 -0
  121. package/src/core/ofd-core/Documents/DocumentResXml.ts +55 -0
  122. package/src/core/ofd-core/Documents/Documnet.km +862 -0
  123. package/src/core/ofd-core/Documents/DocumnetXml.ts +118 -0
  124. package/src/core/ofd-core/Documents/Documnets.md +296 -0
  125. package/src/core/ofd-core/Documents/PublicResXml.ts +63 -0
  126. package/src/core/ofd-core/Documents//345/233/276/345/261/202/346/270/262/346/237/223/351/241/272/345/272/217.png +0 -0
  127. package/src/core/ofd-core/Documents//350/241/25022/347/272/277/346/235/241/350/277/236/346/216/245/346/240/267/345/274/217.png +0 -0
  128. package/src/core/ofd-core/Documents//350/241/25023/347/272/277/346/235/241/350/231/232/347/272/277/346/240/267/345/274/217.png +0 -0
  129. package/src/core/ofd-core/Documents//350/241/25023/347/273/223/345/220/210/347/202/271/351/225/277/345/272/246.png +0 -0
  130. package/src/core/ofd-core/Documents//350/241/25024/347/272/277/346/235/241/347/253/257/347/202/271/346/240/267/345/274/217.png +0 -0
  131. package/src/core/ofd-core/Documents//351/241/265/350/276/271/347/225/214/345/261/202/346/254/241/347/273/223/346/236/204.png +0 -0
  132. package/src/core/ofd-core/OFDElement.ts +724 -0
  133. package/src/core/ofd-core/Ofd/CT_DocInfo.km +197 -0
  134. package/src/core/ofd-core/Ofd/OFD.km +114 -0
  135. package/src/core/ofd-core/Ofd/OFD.md +99 -0
  136. package/src/core/ofd-core/Ofd/OFDXml.ts +69 -0
  137. package/src/core/ofd-core/Pages/PAGE.md +40 -0
  138. package/src/core/ofd-core/Pages/PageXml.ts +59 -0
  139. package/src/core/ofd-core/Signatures/Signature.km +308 -0
  140. package/src/core/ofd-core/Signatures/Signature.md +21 -0
  141. package/src/core/ofd-core/Signatures/Signatures.km +315 -0
  142. package/src/core/ofd-core/Signatures/Signatures.md +24 -0
  143. package/src/core/ofd-core/Signatures/Signatures.ts +247 -0
  144. package/src/core/ofd-core/TemplatePages/TemplatePages.ts +56 -0
  145. package/src/core/ofd-core/asn.md +60 -0
  146. package/src/core/ofd-core/constant.ts +35 -0
  147. package/src/core/ofd-core/index.d.ts +867 -0
  148. package/src/core/ofd-core/utils.ts +86 -0
  149. package/src/dicom/dicom.ts +42 -0
  150. package/src/dicom/index.ts +1 -0
  151. package/src/errors/ErrorHandle.ts +14 -0
  152. package/src/errors/FetchErrors.ts +8 -0
  153. package/src/errors/MimeError.ts +9 -0
  154. package/src/errors/OFDErrors.ts +11 -0
  155. package/src/errors/UnzipErrors.ts +9 -0
  156. package/src/errors/XmlErrors.ts +11 -0
  157. package/src/errors/error-msg.ts +17 -0
  158. package/src/es.ts +3 -0
  159. package/src/global.d.ts +7 -0
  160. package/src/index.ts +7 -0
  161. package/src/lib/ParseFile.ts +12 -0
  162. package/src/lib/README.md +69 -0
  163. package/src/lib/UnitCoversion.ts +55 -0
  164. package/src/lib/asn1/Asn.1.ts +284 -0
  165. package/src/lib/asn1/README.md +1 -0
  166. package/src/lib/asn1/Stream.ts +314 -0
  167. package/src/lib/asn1/asn1.ts +120 -0
  168. package/src/lib/asn1/base64.ts +97 -0
  169. package/src/lib/asn1/hex.ts +60 -0
  170. package/src/lib/asn1/index.ts +5 -0
  171. package/src/lib/asn1/int10.ts +103 -0
  172. package/src/lib/asn1/oids.ts +6164 -0
  173. package/src/lib/asn1/string-cut.ts +8 -0
  174. package/src/lib/decode-html.ts +37 -0
  175. package/src/lib/fetch.ts +70 -0
  176. package/src/lib/is-element.ts +13 -0
  177. package/src/lib/jbig/README.md +1 -0
  178. package/src/lib/jbig/arithmetic_decoder.js +184 -0
  179. package/src/lib/jbig/base_stream.js +117 -0
  180. package/src/lib/jbig/ccitt.js +1068 -0
  181. package/src/lib/jbig/ccitt_stream.js +60 -0
  182. package/src/lib/jbig/compatibility.js +108 -0
  183. package/src/lib/jbig/core_utils.js +591 -0
  184. package/src/lib/jbig/decode_stream.js +184 -0
  185. package/src/lib/jbig/is_node.js +28 -0
  186. package/src/lib/jbig/jbig2.js +2582 -0
  187. package/src/lib/jbig/jbig2_stream.js +78 -0
  188. package/src/lib/jbig/primitives.js +441 -0
  189. package/src/lib/jbig/stream.js +101 -0
  190. package/src/lib/jbig/util.js +1231 -0
  191. package/src/lib/load-files.ts +26 -0
  192. package/src/lib/load-opentype.ts +161 -0
  193. package/src/lib/load-script.ts +33 -0
  194. package/src/lib/mime.ts +97 -0
  195. package/src/lib/read-buffer.ts +19 -0
  196. package/src/lib/toFixeds.ts +56 -0
  197. package/src/ofd/ConverterDpi.ts +35 -0
  198. package/src/ofd/OFD.md +38 -0
  199. package/src/ofd/TODO.md +10 -0
  200. package/src/ofd/index.ts +1 -0
  201. package/src/ofd/ofd-utils.ts +627 -0
  202. package/src/ofd/ofd.demo.md +12 -0
  203. package/src/ofd/ofd.ts +200 -0
  204. package/src/ofd/ofdxml.km +162 -0
  205. package/src/ofd/parse-ofd.ts +103 -0
  206. package/src/ofd/render.ts +393 -0
  207. package/src/ofd/svg-image.ts +318 -0
  208. package/src/ofd/svg-path.ts +283 -0
  209. package/src/ofd/svg-text.ts +272 -0
  210. package/src/xml/index.ts +3 -0
  211. package/src/xml/render.ts +171 -0
  212. package/src/xml/xml.ts +136 -0
  213. package/tsconfig.json +21 -0
@@ -0,0 +1,591 @@
1
+ /* Copyright 2019 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
+
16
+ import {
17
+ AnnotationEditorPrefix,
18
+ assert,
19
+ BaseException,
20
+ FontType,
21
+ objectSize,
22
+ StreamType,
23
+ stringToPDFString,
24
+ warn
25
+ } from './util.js';
26
+ import { Dict, isName, Ref, RefSet } from './primitives.js';
27
+ import { BaseStream } from './base_stream.js';
28
+
29
+ function getLookupTableFactory(initializer) {
30
+ let lookup;
31
+ return function () {
32
+ if (initializer) {
33
+ lookup = Object.create(null);
34
+ initializer(lookup);
35
+ initializer = null;
36
+ }
37
+ return lookup;
38
+ };
39
+ }
40
+
41
+ function getArrayLookupTableFactory(initializer) {
42
+ let lookup;
43
+ return function () {
44
+ if (initializer) {
45
+ let arr = initializer();
46
+ initializer = null;
47
+ lookup = Object.create(null);
48
+ for (let i = 0, ii = arr.length; i < ii; i += 2) {
49
+ lookup[arr[i]] = arr[i + 1];
50
+ }
51
+ arr = null;
52
+ }
53
+ return lookup;
54
+ };
55
+ }
56
+
57
+ class MissingDataException extends BaseException {
58
+ constructor(begin, end) {
59
+ super(`Missing data [${begin}, ${end})`, 'MissingDataException');
60
+ this.begin = begin;
61
+ this.end = end;
62
+ }
63
+ }
64
+
65
+ class ParserEOFException extends BaseException {
66
+ constructor(msg) {
67
+ super(msg, 'ParserEOFException');
68
+ }
69
+ }
70
+
71
+ class XRefEntryException extends BaseException {
72
+ constructor(msg) {
73
+ super(msg, 'XRefEntryException');
74
+ }
75
+ }
76
+
77
+ class XRefParseException extends BaseException {
78
+ constructor(msg) {
79
+ super(msg, 'XRefParseException');
80
+ }
81
+ }
82
+
83
+ class DocStats {
84
+ constructor(handler) {
85
+ this._handler = handler;
86
+
87
+ this._streamTypes = new Set();
88
+ this._fontTypes = new Set();
89
+ }
90
+
91
+ _send() {
92
+ const streamTypes = Object.create(null),
93
+ fontTypes = Object.create(null);
94
+ for (const type of this._streamTypes) {
95
+ streamTypes[type] = true;
96
+ }
97
+ for (const type of this._fontTypes) {
98
+ fontTypes[type] = true;
99
+ }
100
+ this._handler.send('DocStats', { streamTypes, fontTypes });
101
+ }
102
+
103
+ addStreamType(type) {
104
+ if (this._streamTypes.has(type)) {
105
+ return;
106
+ }
107
+ this._streamTypes.add(type);
108
+ this._send();
109
+ }
110
+
111
+ addFontType(type) {
112
+ if (this._fontTypes.has(type)) {
113
+ return;
114
+ }
115
+ this._fontTypes.add(type);
116
+ this._send();
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Get the value of an inheritable property.
122
+ *
123
+ * If the PDF specification explicitly lists a property in a dictionary as
124
+ * inheritable, then the value of the property may be present in the dictionary
125
+ * itself or in one or more parents of the dictionary.
126
+ *
127
+ * If the key is not found in the tree, `undefined` is returned. Otherwise,
128
+ * the value for the key is returned or, if `stopWhenFound` is `false`, a list
129
+ * of values is returned.
130
+ *
131
+ * @param {Object} obj
132
+ * @param {Dict} obj.dict - Dictionary from where to start the traversal.
133
+ * @param {string} obj.key - The key of the property to find the value for.
134
+ * @param {boolean} obj.getArray - Whether or not the value should be fetched as an
135
+ * array. The default value is `false`.
136
+ * @param {boolean} obj.stopWhenFound - Whether or not to stop the traversal when
137
+ * the key is found. If set to `false`, we always walk up the entire parent
138
+ * chain, for example to be able to find `\Resources` placed on multiple
139
+ * levels of the tree. The default value is `true`.
140
+ */
141
+ function getInheritableProperty({
142
+ // @ts-ignore
143
+ dict,
144
+ // @ts-ignore
145
+ key,
146
+ // @ts-ignore
147
+ getArray = false,
148
+ // @ts-ignore
149
+ stopWhenFound = true
150
+ }) {
151
+ let values;
152
+ const visited = new RefSet();
153
+
154
+ while (dict instanceof Dict && !(dict.objId && visited.has(dict.objId))) {
155
+ if (dict.objId) {
156
+ visited.put(dict.objId);
157
+ }
158
+ // @ts-ignore
159
+ const value = getArray ? dict.getArray(key) : dict.get(key);
160
+ if (value !== undefined) {
161
+ if (stopWhenFound) {
162
+ return value;
163
+ }
164
+ if (!values) {
165
+ values = [];
166
+ }
167
+ values.push(value);
168
+ }
169
+ dict = dict.get('Parent');
170
+ }
171
+ return values;
172
+ }
173
+
174
+ // prettier-ignore
175
+ const ROMAN_NUMBER_MAP = [
176
+ "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
177
+ "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
178
+ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"
179
+ ];
180
+
181
+ /**
182
+ * Converts positive integers to (upper case) Roman numerals.
183
+ * @param {number} number - The number that should be converted.
184
+ * @param {boolean} lowerCase - Indicates if the result should be converted
185
+ * to lower case letters. The default value is `false`.
186
+ * @returns {string} The resulting Roman number.
187
+ */
188
+ function toRomanNumerals(number, lowerCase = false) {
189
+ assert(
190
+ Number.isInteger(number) && number > 0,
191
+ 'The number should be a positive integer.'
192
+ );
193
+ const romanBuf = [];
194
+ let pos;
195
+ // Thousands
196
+ while (number >= 1000) {
197
+ number -= 1000;
198
+ romanBuf.push('M');
199
+ }
200
+ // Hundreds
201
+ pos = (number / 100) | 0;
202
+ number %= 100;
203
+ romanBuf.push(ROMAN_NUMBER_MAP[pos]);
204
+ // Tens
205
+ pos = (number / 10) | 0;
206
+ number %= 10;
207
+ romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
208
+ // Ones
209
+ romanBuf.push(ROMAN_NUMBER_MAP[20 + number]); // eslint-disable-line unicorn/no-array-push-push
210
+
211
+ const romanStr = romanBuf.join('');
212
+ return lowerCase ? romanStr.toLowerCase() : romanStr;
213
+ }
214
+
215
+ // Calculate the base 2 logarithm of the number `x`. This differs from the
216
+ // native function in the sense that it returns the ceiling value and that it
217
+ // returns 0 instead of `Infinity`/`NaN` for `x` values smaller than/equal to 0.
218
+ function log2(x) {
219
+ if (x <= 0) {
220
+ return 0;
221
+ }
222
+ return Math.ceil(Math.log2(x));
223
+ }
224
+
225
+ function readInt8(data, offset) {
226
+ return (data[offset] << 24) >> 24;
227
+ }
228
+
229
+ function readUint16(data, offset) {
230
+ return (data[offset] << 8) | data[offset + 1];
231
+ }
232
+
233
+ function readUint32(data, offset) {
234
+ return (
235
+ ((data[offset] << 24) |
236
+ (data[offset + 1] << 16) |
237
+ (data[offset + 2] << 8) |
238
+ data[offset + 3]) >>>
239
+ 0
240
+ );
241
+ }
242
+
243
+ // Checks if ch is one of the following characters: SPACE, TAB, CR or LF.
244
+ function isWhiteSpace(ch) {
245
+ return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
246
+ }
247
+
248
+ /**
249
+ * AcroForm field names use an array like notation to refer to
250
+ * repeated XFA elements e.g. foo.bar[nnn].
251
+ * see: XFA Spec Chapter 3 - Repeated Elements
252
+ *
253
+ * @param {string} path - XFA path name.
254
+ * @returns {Array} - Array of Objects with the name and pos of
255
+ * each part of the path.
256
+ */
257
+ function parseXFAPath(path) {
258
+ const positionPattern = /(.+)\[(\d+)\]$/;
259
+ return path.split('.').map(component => {
260
+ const m = component.match(positionPattern);
261
+ if (m) {
262
+ return { name: m[1], pos: parseInt(m[2], 10) };
263
+ }
264
+ return { name: component, pos: 0 };
265
+ });
266
+ }
267
+
268
+ function escapePDFName(str) {
269
+ const buffer = [];
270
+ let start = 0;
271
+ for (let i = 0, ii = str.length; i < ii; i++) {
272
+ const char = str.charCodeAt(i);
273
+ // Whitespace or delimiters aren't regular chars, so escape them.
274
+ if (
275
+ char < 0x21 ||
276
+ char > 0x7e ||
277
+ char === 0x23 /* # */ ||
278
+ char === 0x28 /* ( */ ||
279
+ char === 0x29 /* ) */ ||
280
+ char === 0x3c /* < */ ||
281
+ char === 0x3e /* > */ ||
282
+ char === 0x5b /* [ */ ||
283
+ char === 0x5d /* ] */ ||
284
+ char === 0x7b /* { */ ||
285
+ char === 0x7d /* } */ ||
286
+ char === 0x2f /* / */ ||
287
+ char === 0x25 /* % */
288
+ ) {
289
+ if (start < i) {
290
+ buffer.push(str.substring(start, i));
291
+ }
292
+ buffer.push(`#${char.toString(16)}`);
293
+ start = i + 1;
294
+ }
295
+ }
296
+
297
+ if (buffer.length === 0) {
298
+ return str;
299
+ }
300
+
301
+ if (start < str.length) {
302
+ buffer.push(str.substring(start, str.length));
303
+ }
304
+
305
+ return buffer.join('');
306
+ }
307
+
308
+ function _collectJS(entry, xref, list, parents) {
309
+ if (!entry) {
310
+ return;
311
+ }
312
+
313
+ let parent = null;
314
+ if (entry instanceof Ref) {
315
+ if (parents.has(entry)) {
316
+ // If we've already found entry then we've a cycle.
317
+ return;
318
+ }
319
+ parent = entry;
320
+ parents.put(parent);
321
+ entry = xref.fetch(entry);
322
+ }
323
+ if (Array.isArray(entry)) {
324
+ for (const element of entry) {
325
+ _collectJS(element, xref, list, parents);
326
+ }
327
+ } else if (entry instanceof Dict) {
328
+ if (isName(entry.get('S'), 'JavaScript')) {
329
+ const js = entry.get('JS');
330
+ let code;
331
+ if (js instanceof BaseStream) {
332
+ code = js.getString();
333
+ } else if (typeof js === 'string') {
334
+ code = js;
335
+ }
336
+ code = code && stringToPDFString(code).replace(/\u0000/g, '');
337
+ if (code) {
338
+ list.push(code);
339
+ }
340
+ }
341
+ _collectJS(entry.getRaw('Next'), xref, list, parents);
342
+ }
343
+
344
+ if (parent) {
345
+ parents.remove(parent);
346
+ }
347
+ }
348
+
349
+ function collectActions(xref, dict, eventType) {
350
+ const actions = Object.create(null);
351
+ // @ts-ignore
352
+ const additionalActionsDicts = getInheritableProperty({
353
+ dict,
354
+ key: 'AA',
355
+ stopWhenFound: false
356
+ });
357
+ if (additionalActionsDicts) {
358
+ // additionalActionsDicts contains dicts from ancestors
359
+ // as they're found in the tree from bottom to top.
360
+ // So the dicts are visited in reverse order to guarantee
361
+ // that actions from elder ancestors will be overwritten
362
+ // by ones from younger ancestors.
363
+ for (let i = additionalActionsDicts.length - 1; i >= 0; i--) {
364
+ const additionalActions = additionalActionsDicts[i];
365
+ if (!(additionalActions instanceof Dict)) {
366
+ continue;
367
+ }
368
+ for (const key of additionalActions.getKeys()) {
369
+ const action = eventType[key];
370
+ if (!action) {
371
+ continue;
372
+ }
373
+ const actionDict = additionalActions.getRaw(key);
374
+ const parents = new RefSet();
375
+ const list = [];
376
+ _collectJS(actionDict, xref, list, parents);
377
+ if (list.length > 0) {
378
+ actions[action] = list;
379
+ }
380
+ }
381
+ }
382
+ }
383
+ // Collect the Action if any (we may have one on pushbutton).
384
+ if (dict.has('A')) {
385
+ const actionDict = dict.get('A');
386
+ const parents = new RefSet();
387
+ const list = [];
388
+ _collectJS(actionDict, xref, list, parents);
389
+ if (list.length > 0) {
390
+ actions.Action = list;
391
+ }
392
+ }
393
+ return objectSize(actions) > 0 ? actions : null;
394
+ }
395
+
396
+ const XMLEntities = {
397
+ /* < */ 0x3c: '&lt;',
398
+ /* > */ 0x3e: '&gt;',
399
+ /* & */ 0x26: '&amp;',
400
+ /* " */ 0x22: '&quot;',
401
+ /* ' */ 0x27: '&apos;'
402
+ };
403
+
404
+ function encodeToXmlString(str) {
405
+ const buffer = [];
406
+ let start = 0;
407
+ for (let i = 0, ii = str.length; i < ii; i++) {
408
+ const char = str.codePointAt(i);
409
+ if (0x20 <= char && char <= 0x7e) {
410
+ // ascii
411
+ const entity = XMLEntities[char];
412
+ if (entity) {
413
+ if (start < i) {
414
+ buffer.push(str.substring(start, i));
415
+ }
416
+ buffer.push(entity);
417
+ start = i + 1;
418
+ }
419
+ } else {
420
+ if (start < i) {
421
+ buffer.push(str.substring(start, i));
422
+ }
423
+ buffer.push(`&#x${char.toString(16).toUpperCase()};`);
424
+ if (char > 0xd7ff && (char < 0xe000 || char > 0xfffd)) {
425
+ // char is represented by two u16
426
+ i++;
427
+ }
428
+ start = i + 1;
429
+ }
430
+ }
431
+
432
+ if (buffer.length === 0) {
433
+ return str;
434
+ }
435
+ if (start < str.length) {
436
+ buffer.push(str.substring(start, str.length));
437
+ }
438
+ return buffer.join('');
439
+ }
440
+
441
+ function validateCSSFont(cssFontInfo) {
442
+ // See https://developer.mozilla.org/en-US/docs/Web/CSS/font-style.
443
+ const DEFAULT_CSS_FONT_OBLIQUE = '14';
444
+ // See https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight.
445
+ const DEFAULT_CSS_FONT_WEIGHT = '400';
446
+ const CSS_FONT_WEIGHT_VALUES = new Set([
447
+ '100',
448
+ '200',
449
+ '300',
450
+ '400',
451
+ '500',
452
+ '600',
453
+ '700',
454
+ '800',
455
+ '900',
456
+ '1000',
457
+ 'normal',
458
+ 'bold',
459
+ 'bolder',
460
+ 'lighter'
461
+ ]);
462
+
463
+ const { fontFamily, fontWeight, italicAngle } = cssFontInfo;
464
+
465
+ // See https://developer.mozilla.org/en-US/docs/Web/CSS/string.
466
+ if (/^".*"$/.test(fontFamily)) {
467
+ if (/[^\\]"/.test(fontFamily.slice(1, fontFamily.length - 1))) {
468
+ warn(`XFA - FontFamily contains some unescaped ": ${fontFamily}.`);
469
+ return false;
470
+ }
471
+ } else if (/^'.*'$/.test(fontFamily)) {
472
+ if (/[^\\]'/.test(fontFamily.slice(1, fontFamily.length - 1))) {
473
+ warn(`XFA - FontFamily contains some unescaped ': ${fontFamily}.`);
474
+ return false;
475
+ }
476
+ } else {
477
+ // See https://developer.mozilla.org/en-US/docs/Web/CSS/custom-ident.
478
+ for (const ident of fontFamily.split(/[ \t]+/)) {
479
+ if (/^(\d|(-(\d|-)))/.test(ident) || !/^[\w-\\]+$/.test(ident)) {
480
+ warn(
481
+ `XFA - FontFamily contains some invalid <custom-ident>: ${fontFamily}.`
482
+ );
483
+ return false;
484
+ }
485
+ }
486
+ }
487
+
488
+ const weight = fontWeight ? fontWeight.toString() : '';
489
+ cssFontInfo.fontWeight = CSS_FONT_WEIGHT_VALUES.has(weight)
490
+ ? weight
491
+ : DEFAULT_CSS_FONT_WEIGHT;
492
+
493
+ const angle = parseFloat(italicAngle);
494
+ cssFontInfo.italicAngle =
495
+ isNaN(angle) || angle < -90 || angle > 90
496
+ ? DEFAULT_CSS_FONT_OBLIQUE
497
+ : italicAngle.toString();
498
+
499
+ return true;
500
+ }
501
+
502
+ function recoverJsURL(str) {
503
+ // Attempt to recover valid URLs from `JS` entries with certain
504
+ // white-listed formats:
505
+ // - window.open('http://example.com')
506
+ // - app.launchURL('http://example.com', true)
507
+ // - xfa.host.gotoURL('http://example.com')
508
+ const URL_OPEN_METHODS = ['app.launchURL', 'window.open', 'xfa.host.gotoURL'];
509
+ const regex = new RegExp(
510
+ '^\\s*(' +
511
+ URL_OPEN_METHODS.join('|').split('.').join('\\.') +
512
+ ')\\((?:\'|")([^\'"]*)(?:\'|")(?:,\\s*(\\w+)\\)|\\))',
513
+ 'i'
514
+ );
515
+
516
+ const jsUrl = regex.exec(str);
517
+ if (jsUrl && jsUrl[2]) {
518
+ const url = jsUrl[2];
519
+ let newWindow = false;
520
+
521
+ if (jsUrl[3] === 'true' && jsUrl[1] === 'app.launchURL') {
522
+ newWindow = true;
523
+ }
524
+ return { url, newWindow };
525
+ }
526
+
527
+ return null;
528
+ }
529
+
530
+ function numberToString(value) {
531
+ if (Number.isInteger(value)) {
532
+ return value.toString();
533
+ }
534
+
535
+ const roundedValue = Math.round(value * 100);
536
+ if (roundedValue % 100 === 0) {
537
+ return (roundedValue / 100).toString();
538
+ }
539
+
540
+ if (roundedValue % 10 === 0) {
541
+ return value.toFixed(1);
542
+ }
543
+
544
+ return value.toFixed(2);
545
+ }
546
+
547
+ function getNewAnnotationsMap(annotationStorage) {
548
+ if (!annotationStorage) {
549
+ return null;
550
+ }
551
+ const newAnnotationsByPage = new Map();
552
+ // The concept of page in a XFA is very different, so
553
+ // editing is just not implemented.
554
+ for (const [key, value] of annotationStorage) {
555
+ if (!key.startsWith(AnnotationEditorPrefix)) {
556
+ continue;
557
+ }
558
+ let annotations = newAnnotationsByPage.get(value.pageIndex);
559
+ if (!annotations) {
560
+ annotations = [];
561
+ newAnnotationsByPage.set(value.pageIndex, annotations);
562
+ }
563
+ annotations.push(value);
564
+ }
565
+ return newAnnotationsByPage.size > 0 ? newAnnotationsByPage : null;
566
+ }
567
+
568
+ export {
569
+ collectActions,
570
+ DocStats,
571
+ encodeToXmlString,
572
+ escapePDFName,
573
+ getArrayLookupTableFactory,
574
+ getInheritableProperty,
575
+ getLookupTableFactory,
576
+ getNewAnnotationsMap,
577
+ isWhiteSpace,
578
+ log2,
579
+ MissingDataException,
580
+ numberToString,
581
+ ParserEOFException,
582
+ parseXFAPath,
583
+ readInt8,
584
+ readUint16,
585
+ readUint32,
586
+ recoverJsURL,
587
+ toRomanNumerals,
588
+ validateCSSFont,
589
+ XRefEntryException,
590
+ XRefParseException
591
+ };