@patrickdk77/xfa-flatten 0.0.0-stage → 0.1.0

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 (74) hide show
  1. package/LICENSE +21 -0
  2. package/LICENSE-APACHE-2.0 +202 -0
  3. package/NOTICE +14 -0
  4. package/README.md +112 -2
  5. package/dist/barcode-paint.d.ts +11 -0
  6. package/dist/barcode-paint.js +135 -0
  7. package/dist/barcode.d.ts +45 -0
  8. package/dist/barcode.js +750 -0
  9. package/dist/canvas.d.ts +59 -0
  10. package/dist/canvas.js +179 -0
  11. package/dist/cli.d.ts +2 -0
  12. package/dist/cli.js +34 -0
  13. package/dist/css.d.ts +48 -0
  14. package/dist/css.js +288 -0
  15. package/dist/deadline.d.ts +6 -0
  16. package/dist/deadline.js +18 -0
  17. package/dist/filters.d.ts +21 -0
  18. package/dist/filters.js +296 -0
  19. package/dist/flatten.d.ts +25 -0
  20. package/dist/flatten.js +319 -0
  21. package/dist/font-metrics.d.ts +9 -0
  22. package/dist/font-metrics.js +215 -0
  23. package/dist/fonts.d.ts +36 -0
  24. package/dist/fonts.js +144 -0
  25. package/dist/images.d.ts +23 -0
  26. package/dist/images.js +519 -0
  27. package/dist/index.d.ts +2 -0
  28. package/dist/index.js +5 -0
  29. package/dist/index.mjs +3 -0
  30. package/dist/layout.d.ts +64 -0
  31. package/dist/layout.js +779 -0
  32. package/dist/limits.d.ts +2 -0
  33. package/dist/limits.js +6 -0
  34. package/dist/locale.d.ts +29 -0
  35. package/dist/locale.js +97 -0
  36. package/dist/paint.d.ts +15 -0
  37. package/dist/paint.js +533 -0
  38. package/dist/pdf-crypt.d.ts +35 -0
  39. package/dist/pdf-crypt.js +261 -0
  40. package/dist/pdf-read.d.ts +140 -0
  41. package/dist/pdf-read.js +1026 -0
  42. package/dist/pdf417-table.d.ts +2 -0
  43. package/dist/pdf417-table.js +146 -0
  44. package/dist/pdfjs-repair.d.ts +13 -0
  45. package/dist/pdfjs-repair.js +257 -0
  46. package/dist/pdfjs.d.ts +16 -0
  47. package/dist/pdfjs.js +62 -0
  48. package/dist/picture.d.ts +40 -0
  49. package/dist/picture.js +678 -0
  50. package/dist/saslprep.d.ts +11 -0
  51. package/dist/saslprep.js +178 -0
  52. package/dist/style.d.ts +29 -0
  53. package/dist/style.js +178 -0
  54. package/dist/text.d.ts +44 -0
  55. package/dist/text.js +199 -0
  56. package/dist/tiff.d.ts +3 -0
  57. package/dist/tiff.js +270 -0
  58. package/dist/tree.d.ts +23 -0
  59. package/dist/tree.js +54 -0
  60. package/dist/types.d.ts +31 -0
  61. package/dist/types.js +2 -0
  62. package/dist/writer.d.ts +38 -0
  63. package/dist/writer.js +145 -0
  64. package/dist/xfa-apply.d.ts +49 -0
  65. package/dist/xfa-apply.js +617 -0
  66. package/dist/xfa-model.d.ts +42 -0
  67. package/dist/xfa-model.js +227 -0
  68. package/dist/xfa-paint.d.ts +24 -0
  69. package/dist/xfa-paint.js +275 -0
  70. package/dist/xfa-spec.d.ts +47 -0
  71. package/dist/xfa-spec.js +3 -0
  72. package/dist/xml.d.ts +30 -0
  73. package/dist/xml.js +211 -0
  74. package/package.json +83 -4
@@ -0,0 +1,678 @@
1
+ "use strict";
2
+ // XFA picture clauses for display: date, time, date-time, numeric and text pictures, with categories,
3
+ // predefined locale patterns (date.long{}, num.currency{}), alternatives (a|b), null{} and zero{}.
4
+ // formatPicture returns null when no alternative can format the value, and the caller shows the raw value.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.CATEGORIES = void 0;
7
+ exports.parseDate = parseDate;
8
+ exports.parseTime = parseTime;
9
+ exports.parseDecimal = parseDecimal;
10
+ exports.formatPicture = formatPicture;
11
+ exports.parseWithDatePicture = parseWithDatePicture;
12
+ /** Splits on a top-level separator, outside quotes and braces. */
13
+ function splitTop(s, sep) {
14
+ const out = [];
15
+ let depth = 0;
16
+ let quoted = false;
17
+ let start = 0;
18
+ for (let i = 0; i < s.length; i++) {
19
+ const c = s[i];
20
+ if (c === "'")
21
+ quoted = !quoted;
22
+ else if (!quoted && c === '{')
23
+ depth++;
24
+ else if (!quoted && c === '}')
25
+ depth = Math.max(0, depth - 1);
26
+ else if (!quoted && depth === 0 && c === sep) {
27
+ out.push(s.slice(start, i));
28
+ start = i + 1;
29
+ }
30
+ }
31
+ out.push(s.slice(start));
32
+ return out;
33
+ }
34
+ const CATEGORIES = ['datetime', 'date', 'time', 'num', 'text', 'null', 'zero'];
35
+ exports.CATEGORIES = CATEGORIES;
36
+ /** Parses one alternative into category segments and the literal text between them. */
37
+ function segments(alt) {
38
+ const out = [];
39
+ let i = 0;
40
+ let literal = '';
41
+ let anyCategory = false;
42
+ // Sticky, so each position is tried in place rather than on a fresh copy of the rest of the picture.
43
+ const re = /(datetime|date|time|num|text|null|zero)(?:\.([A-Za-z]+))?(?:\(([A-Za-z_-]*)\))?\{/y;
44
+ while (i < alt.length) {
45
+ if (alt[i] === "'") {
46
+ const e = alt.indexOf("'", i + 1);
47
+ literal += alt.slice(i, e < 0 ? alt.length : e + 1);
48
+ i = e < 0 ? alt.length : e + 1;
49
+ continue;
50
+ }
51
+ re.lastIndex = i;
52
+ const m = re.exec(alt);
53
+ if (m) {
54
+ let depth = 1;
55
+ let j = i + m[0].length;
56
+ let quoted = false;
57
+ for (; j < alt.length; j++) {
58
+ const c = alt[j];
59
+ if (c === "'")
60
+ quoted = !quoted;
61
+ else if (!quoted && c === '{')
62
+ depth++;
63
+ else if (!quoted && c === '}') {
64
+ depth--;
65
+ if (depth === 0)
66
+ break;
67
+ }
68
+ }
69
+ if (literal)
70
+ out.push({ category: null, sub: '', locale: '', body: literal });
71
+ literal = '';
72
+ out.push({ category: m[1], sub: m[2] ?? '', locale: m[3] ?? '', body: alt.slice(i + m[0].length, j) });
73
+ anyCategory = true;
74
+ i = j + 1;
75
+ continue;
76
+ }
77
+ literal += alt[i];
78
+ i++;
79
+ }
80
+ if (literal)
81
+ out.push({ category: null, sub: '', locale: '', body: literal });
82
+ if (!anyCategory)
83
+ return [{ category: null, sub: '', locale: '', body: alt }];
84
+ return out;
85
+ }
86
+ /** Splits a picture body into symbol runs (letters in `symbols`) and literal text. */
87
+ function tokens(body, symbols) {
88
+ const out = [];
89
+ let i = 0;
90
+ while (i < body.length) {
91
+ const c = body[i];
92
+ if (c === "'") {
93
+ let j = i + 1;
94
+ let text = '';
95
+ while (j < body.length) {
96
+ if (body[j] === "'") {
97
+ if (body[j + 1] === "'") {
98
+ text += "'";
99
+ j += 2;
100
+ continue;
101
+ }
102
+ break;
103
+ }
104
+ text += body[j++];
105
+ }
106
+ out.push({ sym: '', n: 0, text: j === i + 1 && body[j] === "'" && text === '' ? "'" : text });
107
+ i = j + 1;
108
+ continue;
109
+ }
110
+ if (symbols.includes(c)) {
111
+ let j = i;
112
+ while (j < body.length && body[j] === c)
113
+ j++;
114
+ out.push({ sym: c, n: j - i, text: '' });
115
+ i = j;
116
+ continue;
117
+ }
118
+ out.push({ sym: '', n: 0, text: c });
119
+ i++;
120
+ }
121
+ return out;
122
+ }
123
+ function parseDate(v) {
124
+ const s = v.trim();
125
+ let m = /^(\d{4})-?(\d{2})-?(\d{2})$/.exec(s);
126
+ if (m) {
127
+ const y = Number(m[1]);
128
+ const mo = Number(m[2]);
129
+ const d = Number(m[3]);
130
+ if (mo < 1 || mo > 12 || d < 1 || d > daysIn(y, mo))
131
+ return null;
132
+ return { y, m: mo, d };
133
+ }
134
+ m = /^(\d{4})-(\d{2})$/.exec(s);
135
+ if (m && Number(m[2]) >= 1 && Number(m[2]) <= 12)
136
+ return { y: Number(m[1]), m: Number(m[2]), d: 1 };
137
+ return null;
138
+ }
139
+ function parseTime(v) {
140
+ const m = /^(\d{2}):?(\d{2})(?::?(\d{2})(?:[.,](\d{1,3}))?)?(Z|[+-]\d{2}(?::?\d{2})?)?$/.exec(v.trim());
141
+ if (!m)
142
+ return null;
143
+ const h = Number(m[1]);
144
+ const mi = Number(m[2]);
145
+ const s = m[3] ? Number(m[3]) : 0;
146
+ // 24 is allowed only as 24:00:00, the end of the day.
147
+ if (h > 24 || mi > 59 || s > 60 || (h === 24 && (mi > 0 || s > 0 || Number(m[4] ?? 0) > 0)))
148
+ return null;
149
+ let zone = null;
150
+ if (m[5] === 'Z')
151
+ zone = 0;
152
+ else if (m[5]) {
153
+ const sign = m[5][0] === '-' ? -1 : 1;
154
+ const digits = m[5].slice(1).replace(':', '');
155
+ const zh = Number(digits.slice(0, 2));
156
+ const zm = Number(digits.slice(2, 4) || '0');
157
+ if (zh > 14 || zm > 59)
158
+ return null;
159
+ zone = sign * (zh * 60 + zm);
160
+ }
161
+ return { h, mi, s, ms: m[4] ? Number(m[4].padEnd(3, '0')) : 0, zone };
162
+ }
163
+ /** Date.UTC without its mapping of years 0 to 99 onto 1900 to 1999. */
164
+ function utc(y, m0, d) {
165
+ const t = new Date(0);
166
+ t.setUTCFullYear(y, m0, d);
167
+ return t.getTime();
168
+ }
169
+ function daysIn(y, m) {
170
+ return new Date(utc(y, m, 0)).getUTCDate();
171
+ }
172
+ function pad(n, w) {
173
+ return String(n).padStart(w, '0');
174
+ }
175
+ function dayOfYear(p) {
176
+ return Math.round((utc(p.y, p.m - 1, p.d) - utc(p.y, 0, 1)) / 86400000) + 1;
177
+ }
178
+ function weekday(p) {
179
+ return new Date(utc(p.y, p.m - 1, p.d)).getUTCDay();
180
+ }
181
+ function isoWeek(p) {
182
+ // The Thursday of the same ISO week decides the year; week 1 holds that year's first Thursday.
183
+ const t = new Date(utc(p.y, p.m - 1, p.d));
184
+ t.setUTCDate(t.getUTCDate() - ((t.getUTCDay() + 6) % 7) + 3);
185
+ const jan1 = utc(t.getUTCFullYear(), 0, 1);
186
+ return 1 + Math.floor((t.getTime() - jan1) / 86400000 / 7);
187
+ }
188
+ function formatDate(body, p, loc) {
189
+ let out = '';
190
+ for (const t of tokens(body, 'DJMEeGYwW')) {
191
+ if (!t.sym) {
192
+ out += t.text;
193
+ continue;
194
+ }
195
+ const { sym, n } = t;
196
+ let v = null;
197
+ if (sym === 'D')
198
+ v = n === 1 ? String(p.d) : n === 2 ? pad(p.d, 2) : null;
199
+ else if (sym === 'J')
200
+ v = n === 1 ? String(dayOfYear(p)) : n === 3 ? pad(dayOfYear(p), 3) : null;
201
+ else if (sym === 'M')
202
+ v = n === 1 ? String(p.m) : n === 2 ? pad(p.m, 2) : n === 3 ? loc.monthsAbbr[p.m - 1] : n === 4 ? loc.months[p.m - 1] : null;
203
+ else if (sym === 'E')
204
+ v = n === 1 ? String(weekday(p) + 1) : n === 3 ? loc.daysAbbr[weekday(p)] : n === 4 ? loc.days[weekday(p)] : null;
205
+ else if (sym === 'e')
206
+ v = n === 1 ? String(((weekday(p) + 6) % 7) + 1) : null;
207
+ else if (sym === 'G')
208
+ v = n === 1 ? loc.eras[p.y > 0 ? 1 : 0] : null;
209
+ else if (sym === 'Y')
210
+ v = n === 2 ? pad(p.y % 100, 2) : n === 4 ? pad(p.y, 4) : null;
211
+ else if (sym === 'w')
212
+ v = n === 1 ? String(Math.floor((p.d + ((weekday({ y: p.y, m: p.m, d: 1 }) + 6) % 7) - 1) / 7) + 1) : null;
213
+ else if (sym === 'W')
214
+ v = n === 2 ? pad(isoWeek(p), 2) : null;
215
+ if (v === null || v === undefined)
216
+ return null;
217
+ out += v;
218
+ }
219
+ return out;
220
+ }
221
+ function formatZone(zone, kind) {
222
+ if (zone === null)
223
+ return '';
224
+ if (zone === 0 && kind === 'Z')
225
+ return 'Z';
226
+ const sign = zone < 0 ? '-' : '+';
227
+ const a = Math.abs(zone);
228
+ const hh = pad(Math.floor(a / 60), 2);
229
+ const mm = pad(a % 60, 2);
230
+ if (kind === 'zz')
231
+ return `GMT${sign}${hh}:${mm}`;
232
+ return `${sign}${hh}:${mm}`;
233
+ }
234
+ function formatTime(body, p, loc) {
235
+ let out = '';
236
+ for (const t of tokens(body, 'hkHKMSFAZz')) {
237
+ if (!t.sym) {
238
+ out += t.text;
239
+ continue;
240
+ }
241
+ const { sym, n } = t;
242
+ let v = null;
243
+ const h12 = p.h % 12 === 0 ? 12 : p.h % 12;
244
+ if (sym === 'h' && n <= 2)
245
+ v = n === 1 ? String(h12) : pad(h12, 2);
246
+ else if (sym === 'k' && n <= 2)
247
+ v = n === 1 ? String(p.h % 12) : pad(p.h % 12, 2);
248
+ else if (sym === 'H' && n <= 2)
249
+ v = n === 1 ? String(p.h % 24) : pad(p.h % 24, 2);
250
+ else if (sym === 'K' && n <= 2)
251
+ v = n === 1 ? String(p.h === 0 ? 24 : p.h) : pad(p.h === 0 ? 24 : p.h, 2);
252
+ else if (sym === 'M' && n <= 2)
253
+ v = n === 1 ? String(p.mi) : pad(p.mi, 2);
254
+ else if (sym === 'S' && n <= 2)
255
+ v = n === 1 ? String(p.s) : pad(p.s, 2);
256
+ else if (sym === 'F' && n === 3)
257
+ v = pad(p.ms, 3);
258
+ else if (sym === 'A' && n === 1)
259
+ v = loc.meridiem[p.h >= 12 && p.h < 24 ? 1 : 0];
260
+ else if (sym === 'Z' && n === 1)
261
+ v = formatZone(p.zone, 'Z');
262
+ else if (sym === 'z' && n <= 2)
263
+ v = formatZone(p.zone, n === 1 ? 'z' : 'zz');
264
+ if (v === null)
265
+ return null;
266
+ out += v;
267
+ }
268
+ return out;
269
+ }
270
+ /** The largest exponent parseDecimal takes; doubles end near 1e308 and 5e-324. */
271
+ const MAX_EXPONENT = 400;
272
+ /** Parses a canonical number (pdf.js's toString, or a datasets value) into exact digits. */
273
+ function parseDecimal(v) {
274
+ const m = /^\s*([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?\s*$/.exec(v);
275
+ if (!m || (m[2] === '' && (m[3] === undefined || m[3] === '')))
276
+ return null;
277
+ // An exponent past any double's range would only build a huge digit string (or throw); such a value is not a number.
278
+ const exp = Number(m[4] ?? 0);
279
+ if (Math.abs(exp) > MAX_EXPONENT)
280
+ return null;
281
+ let digits = m[2] + (m[3] ?? '');
282
+ let point = m[2].length + exp;
283
+ if (point < 0) {
284
+ digits = '0'.repeat(-point) + digits;
285
+ point = 0;
286
+ }
287
+ if (point > digits.length)
288
+ digits = digits.padEnd(point, '0');
289
+ const int = digits.slice(0, point).replace(/^0+/, '');
290
+ const frac = digits.slice(point);
291
+ return { negative: m[1] === '-' && /[1-9]/.test(digits), int, frac };
292
+ }
293
+ /** Rounds to `places` fraction digits, half away from zero, on the digit strings. */
294
+ function roundDecimal(d, places) {
295
+ if (d.frac.length <= places)
296
+ return { ...d, frac: d.frac.padEnd(places, '0') };
297
+ const keep = d.int + d.frac.slice(0, places);
298
+ const up = d.frac.charCodeAt(places) >= 0x35;
299
+ let digits = keep;
300
+ if (up) {
301
+ const arr = `0${keep}`.split('').map(Number);
302
+ let k = arr.length - 1;
303
+ while (k >= 0) {
304
+ arr[k]++;
305
+ if (arr[k] < 10)
306
+ break;
307
+ arr[k] = 0;
308
+ k--;
309
+ }
310
+ digits = arr.join('');
311
+ }
312
+ else
313
+ digits = `0${keep}`;
314
+ const int = digits.slice(0, digits.length - places).replace(/^0+/, '');
315
+ const frac = places > 0 ? digits.slice(digits.length - places) : '';
316
+ return { negative: d.negative && /[1-9]/.test(int + frac), int, frac };
317
+ }
318
+ const NUM_SYMBOLS = '9zZ8SsVvEe$%(),.CcDdRrBb';
319
+ function formatNum(body, value, loc, lastChance) {
320
+ const toks = tokens(body, NUM_SYMBOLS);
321
+ // Expand symbol runs into single symbols, keeping CR, cr, DB, db pairs together.
322
+ const syms = [];
323
+ for (let i = 0; i < toks.length; i++) {
324
+ const t = toks[i];
325
+ if (!t.sym) {
326
+ syms.push({ sym: '', text: t.text });
327
+ continue;
328
+ }
329
+ const nx = toks[i + 1];
330
+ if ((t.sym === 'C' || t.sym === 'c' || t.sym === 'D' || t.sym === 'd') && t.n === 1 && nx && nx.n === 1) {
331
+ const pair = t.sym + nx.sym;
332
+ if (pair === 'CR' || pair === 'cr' || pair === 'DB' || pair === 'db') {
333
+ syms.push({ sym: pair, text: '' });
334
+ i++;
335
+ continue;
336
+ }
337
+ }
338
+ if ('CcDdRrBb'.includes(t.sym)) {
339
+ syms.push({ sym: '', text: t.sym.repeat(t.n) });
340
+ continue;
341
+ }
342
+ for (let k = 0; k < t.n; k++)
343
+ syms.push({ sym: t.sym, text: '' });
344
+ }
345
+ if (syms.some(s => s.sym === 'E' || s.sym === 'e'))
346
+ return null;
347
+ const radixAt = syms.findIndex(s => s.sym === '.' || s.sym === 'V' || s.sym === 'v');
348
+ const isDigit = (s) => s === '9' || s === 'z' || s === 'Z' || s === '8';
349
+ const intSlots = [];
350
+ const fracSlots = [];
351
+ syms.forEach((s, i) => {
352
+ if (!isDigit(s.sym))
353
+ return;
354
+ if (radixAt >= 0 && i > radixAt)
355
+ fracSlots.push(i);
356
+ else
357
+ intSlots.push(i);
358
+ });
359
+ const hasSign = syms.some(s => ['S', 's', '(', ')', 'CR', 'cr', 'DB', 'db'].includes(s.sym));
360
+ // The percent sign is output as the locale's symbol; the value is not scaled (forms store 8.75 for 8.75%).
361
+ const v = roundDecimal(value, fracSlots.length);
362
+ if (v.negative && !hasSign && !lastChance)
363
+ return null;
364
+ if (v.int.length > intSlots.length)
365
+ return null;
366
+ const out = syms.map(() => '');
367
+ // Integer digits, right to left.
368
+ const shown = new Set();
369
+ for (let k = 0; k < intSlots.length; k++) {
370
+ const idx = intSlots[intSlots.length - 1 - k];
371
+ const sym = syms[idx].sym;
372
+ if (k < v.int.length) {
373
+ out[idx] = v.int[v.int.length - 1 - k];
374
+ shown.add(idx);
375
+ }
376
+ else if (sym === '9') {
377
+ out[idx] = '0';
378
+ shown.add(idx);
379
+ }
380
+ else if (sym === 'Z')
381
+ out[idx] = ' ';
382
+ }
383
+ // A zero integer part with only z slots and no fraction still shows one zero.
384
+ if (v.int === '' && intSlots.length > 0 && shown.size === 0 && fracSlots.length === 0) {
385
+ const last = intSlots[intSlots.length - 1];
386
+ out[last] = '0';
387
+ shown.add(last);
388
+ }
389
+ // Fraction digits, left to right; z and 8 drop trailing zeros, Z turns them into spaces.
390
+ let lastSig = -1;
391
+ for (let k = 0; k < v.frac.length; k++)
392
+ if (v.frac[k] !== '0')
393
+ lastSig = k;
394
+ let anyFrac = false;
395
+ fracSlots.forEach((idx, k) => {
396
+ const sym = syms[idx].sym;
397
+ const digit = v.frac[k] ?? '0';
398
+ if (k > lastSig && sym !== '9') {
399
+ out[idx] = sym === 'Z' ? ' ' : '';
400
+ return;
401
+ }
402
+ out[idx] = digit;
403
+ anyFrac = true;
404
+ });
405
+ // One pass: whether a digit left of here is shown, and the nearest digit symbol left of here, for the separators.
406
+ let shownLeft = false;
407
+ let prevDigit = '';
408
+ for (let i = 0; i < syms.length; i++) {
409
+ const s = syms[i];
410
+ if (i > 0) {
411
+ if (shown.has(i - 1))
412
+ shownLeft = true;
413
+ if (isDigit(syms[i - 1].sym))
414
+ prevDigit = syms[i - 1].sym;
415
+ }
416
+ if (!s.sym) {
417
+ out[i] = s.text;
418
+ continue;
419
+ }
420
+ switch (s.sym) {
421
+ case ',': {
422
+ if (shownLeft)
423
+ out[i] = loc.grouping;
424
+ else
425
+ out[i] = prevDigit === 'Z' ? ' ' : '';
426
+ break;
427
+ }
428
+ case '.':
429
+ out[i] = anyFrac || fracSlots.some(idx => syms[idx].sym === '9') ? loc.decimal : '';
430
+ break;
431
+ case 'V':
432
+ out[i] = loc.decimal;
433
+ break;
434
+ case 'v':
435
+ out[i] = '';
436
+ break;
437
+ case 'S':
438
+ out[i] = v.negative ? loc.minus : ' ';
439
+ break;
440
+ case 's':
441
+ out[i] = v.negative ? loc.minus : '';
442
+ break;
443
+ case '(':
444
+ out[i] = v.negative ? '(' : ' ';
445
+ break;
446
+ case ')':
447
+ out[i] = v.negative ? ')' : ' ';
448
+ break;
449
+ case 'CR':
450
+ case 'DB':
451
+ out[i] = v.negative ? s.sym : ' ';
452
+ break;
453
+ case 'cr':
454
+ case 'db':
455
+ out[i] = v.negative ? s.sym.toUpperCase() : '';
456
+ break;
457
+ case '$':
458
+ out[i] = loc.currencySymbol;
459
+ break;
460
+ case '%':
461
+ out[i] = loc.percent;
462
+ break;
463
+ default:
464
+ break;
465
+ }
466
+ }
467
+ const text = out.join('');
468
+ return v.negative && !hasSign ? loc.minus + text : text;
469
+ }
470
+ function formatText(body, value) {
471
+ let out = '';
472
+ let k = 0;
473
+ const chars = [...value];
474
+ for (const t of tokens(body, '9AXO0t')) {
475
+ if (!t.sym) {
476
+ out += t.text;
477
+ continue;
478
+ }
479
+ for (let r = 0; r < t.n; r++) {
480
+ if (t.sym === 't') {
481
+ out += '\t';
482
+ continue;
483
+ }
484
+ const c = chars[k++];
485
+ if (c === undefined)
486
+ return null;
487
+ const ok = t.sym === 'X' || (t.sym === '9' && /\d/.test(c)) || (t.sym === 'A' && /\p{L}/u.test(c)) || ((t.sym === 'O' || t.sym === '0') && /[\p{L}\d]/u.test(c));
488
+ if (!ok)
489
+ return null;
490
+ out += c;
491
+ }
492
+ }
493
+ return k === chars.length ? out : null;
494
+ }
495
+ function literalBody(body) {
496
+ return tokens(body, '')
497
+ .map(t => t.text)
498
+ .join('');
499
+ }
500
+ function predefined(category, sub, loc) {
501
+ const key = sub === 'medium' || sub === 'default' ? 'med' : sub;
502
+ if (category === 'date')
503
+ return loc.datePatterns[key] ?? null;
504
+ if (category === 'time')
505
+ return loc.timePatterns[key] ?? null;
506
+ if (category === 'num') {
507
+ // Locale patterns show one group ('z,zz9'); widen them so large values still fit.
508
+ const widen = (p) => (p === undefined ? null : p.replace(/z,zz/g, 'z,zzz,zzz,zzz,zzz,zz'));
509
+ if (sub === 'integer')
510
+ return widen((loc.numberPatterns.numeric ?? 'z,zz9').split('.')[0]);
511
+ if (sub === 'decimal')
512
+ return widen(loc.numberPatterns.numeric);
513
+ return widen(loc.numberPatterns[sub]);
514
+ }
515
+ return null;
516
+ }
517
+ function splitDateTime(v) {
518
+ const t = v.indexOf('T');
519
+ if (t < 0)
520
+ return { date: parseDate(v), time: null };
521
+ return { date: parseDate(v.slice(0, t)), time: parseTime(v.slice(t + 1)) };
522
+ }
523
+ /** Formats one category segment; null when it does not apply to the value. */
524
+ function formatSegment(seg, cat, value, kind, locales, ambient, lastChance) {
525
+ const loc = locales.get(seg.locale || ambient);
526
+ let body = seg.body;
527
+ if (seg.sub && body.trim() === '') {
528
+ const p = predefined(cat, seg.sub, loc);
529
+ if (p === null)
530
+ return null;
531
+ if (cat === 'num' && p.includes('|')) {
532
+ const alts = p.split('|');
533
+ const d = parseDecimal(value);
534
+ if (!d)
535
+ return null;
536
+ return formatNum(d.negative && alts[1] ? alts[1] : alts[0], d.negative && alts[1] ? { ...d, negative: false } : d, loc, true);
537
+ }
538
+ body = p;
539
+ }
540
+ switch (cat) {
541
+ case 'date': {
542
+ const p = kind === 'datetime' ? splitDateTime(value).date : parseDate(value);
543
+ return p ? formatDate(body, p, loc) : null;
544
+ }
545
+ case 'time': {
546
+ const p = kind === 'datetime' ? splitDateTime(value).time : parseTime(value);
547
+ return p ? formatTime(body, p, loc) : null;
548
+ }
549
+ case 'datetime': {
550
+ const { date, time } = splitDateTime(value);
551
+ const parts = splitTop(body, 'T');
552
+ if (!date)
553
+ return null;
554
+ const d = formatDate(parts[0], date, loc);
555
+ if (d === null)
556
+ return null;
557
+ if (parts.length < 2)
558
+ return d;
559
+ if (!time)
560
+ return null;
561
+ const t = formatTime(parts.slice(1).join('T'), time, loc);
562
+ return t === null ? null : d + t;
563
+ }
564
+ case 'num': {
565
+ const d = parseDecimal(value);
566
+ return d ? formatNum(body, d, loc, lastChance) : null;
567
+ }
568
+ case 'text':
569
+ return formatText(body, value);
570
+ default:
571
+ return null;
572
+ }
573
+ }
574
+ function defaultCategory(kind) {
575
+ return kind === 'datetime' ? 'datetime' : kind;
576
+ }
577
+ /** Formats a canonical value with a display picture. Null when the picture cannot format it. */
578
+ function formatPicture(a) {
579
+ const alts = splitTop(a.picture, '|');
580
+ const value = a.value;
581
+ const empty = value.trim() === '';
582
+ for (let ai = 0; ai < alts.length; ai++) {
583
+ const segs = segments(alts[ai]);
584
+ const lastChance = ai === alts.length - 1;
585
+ const cats = segs.filter(s => s.category !== null);
586
+ if (cats.length === 1 && cats[0].category === 'null') {
587
+ if (empty)
588
+ return literalBody(cats[0].body);
589
+ continue;
590
+ }
591
+ if (cats.length === 1 && cats[0].category === 'zero') {
592
+ const d = parseDecimal(value);
593
+ if (d && !/[1-9]/.test(d.int + d.frac))
594
+ return literalBody(cats[0].body);
595
+ continue;
596
+ }
597
+ if (empty)
598
+ continue;
599
+ let out = '';
600
+ let ok = true;
601
+ for (const s of segs) {
602
+ if (s.category === null && cats.length > 0) {
603
+ out += literalBody(s.body);
604
+ continue;
605
+ }
606
+ const cat = s.category ?? defaultCategory(a.kind);
607
+ const r = formatSegment(s, cat, value, a.kind, a.locales, a.locale, lastChance);
608
+ if (r === null) {
609
+ ok = false;
610
+ break;
611
+ }
612
+ out += r;
613
+ }
614
+ if (ok)
615
+ return out;
616
+ }
617
+ return empty ? '' : null;
618
+ }
619
+ /** Parses a date written with a simple data picture (D, DD, M, MM, MMM, MMMM, YY, YYYY and literals) to canonical. */
620
+ function parseWithDatePicture(picture, value, loc) {
621
+ const segs = segments(picture);
622
+ const seg = segs.find(s => s.category === 'date') ?? (segs.length === 1 && segs[0].category === null ? segs[0] : null);
623
+ if (!seg)
624
+ return null;
625
+ let i = 0;
626
+ let y = -1;
627
+ let m = -1;
628
+ let d = -1;
629
+ const num = (min, max) => {
630
+ const mm = new RegExp(`^\\d{${min},${max}}`).exec(value.slice(i));
631
+ if (!mm)
632
+ return -1;
633
+ i += mm[0].length;
634
+ return Number(mm[0]);
635
+ };
636
+ for (const t of tokens(seg.body, 'DMYEJ')) {
637
+ if (!t.sym) {
638
+ if (value.startsWith(t.text, i))
639
+ i += t.text.length;
640
+ else
641
+ return null;
642
+ continue;
643
+ }
644
+ if (t.sym === 'D') {
645
+ // D and DD only; a longer run is not a day of the month.
646
+ if (t.n > 2)
647
+ return null;
648
+ d = num(t.n, 2);
649
+ }
650
+ else if (t.sym === 'Y') {
651
+ y = num(t.n, t.n);
652
+ if (t.n === 2 && y >= 0)
653
+ y += y < 30 ? 2000 : 1900;
654
+ }
655
+ else if (t.sym === 'M' && t.n <= 2)
656
+ m = num(t.n, 2);
657
+ else if (t.sym === 'M') {
658
+ const list = t.n === 3 ? loc.monthsAbbr : loc.months;
659
+ const k = list.findIndex(n => value.slice(i).toLowerCase().startsWith(n.toLowerCase()));
660
+ if (k < 0)
661
+ return null;
662
+ i += list[k].length;
663
+ m = k + 1;
664
+ }
665
+ else if (t.sym === 'E') {
666
+ const list = t.n === 3 ? loc.daysAbbr : loc.days;
667
+ const k = list.findIndex(n => value.slice(i).toLowerCase().startsWith(n.toLowerCase()));
668
+ if (k < 0)
669
+ return null;
670
+ i += list[k].length;
671
+ }
672
+ else
673
+ return null;
674
+ }
675
+ if (i !== value.length || y < 0 || m < 1 || m > 12 || d < 1 || d > daysIn(y, m))
676
+ return null;
677
+ return `${pad(y, 4)}-${pad(m, 2)}-${pad(d, 2)}`;
678
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * SASLprep (RFC 4013), the profile of stringprep (RFC 3454) that ISO 32000-2 7.6.4.3.3 applies to a revision 6
3
+ * password before it is encoded as UTF-8. The tables are RFC 3454's, which describe Unicode 3.2.
4
+ */
5
+ /**
6
+ * Prepares a password as pdf.js does: the same two mappings as SASLprep, then NFKC with the Unicode tables of the
7
+ * running engine. It refuses nothing.
8
+ */
9
+ export declare function pdfjsPrep(input: string): string;
10
+ /** Prepares a password with SASLprep, or returns undefined when the profile refuses it. */
11
+ export declare function saslprep(input: string): string | undefined;