@salmonui/export 0.0.0-stage → 0.3.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 (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +44 -2
  3. package/dist/blob.d.ts +6 -0
  4. package/dist/blob.d.ts.map +1 -0
  5. package/dist/blob.js +10 -0
  6. package/dist/blob.js.map +1 -0
  7. package/dist/bytes.d.ts +55 -0
  8. package/dist/bytes.d.ts.map +1 -0
  9. package/dist/bytes.js +167 -0
  10. package/dist/bytes.js.map +1 -0
  11. package/dist/gantt.d.ts +111 -0
  12. package/dist/gantt.d.ts.map +1 -0
  13. package/dist/gantt.js +348 -0
  14. package/dist/gantt.js.map +1 -0
  15. package/dist/graphics.d.ts +58 -0
  16. package/dist/graphics.d.ts.map +1 -0
  17. package/dist/graphics.js +651 -0
  18. package/dist/graphics.js.map +1 -0
  19. package/dist/ics.d.ts +90 -0
  20. package/dist/ics.d.ts.map +1 -0
  21. package/dist/ics.js +528 -0
  22. package/dist/ics.js.map +1 -0
  23. package/dist/image.d.ts +10 -0
  24. package/dist/image.d.ts.map +1 -0
  25. package/dist/image.js +49 -0
  26. package/dist/image.js.map +1 -0
  27. package/dist/pdf-fonts.d.ts +22 -0
  28. package/dist/pdf-fonts.d.ts.map +1 -0
  29. package/dist/pdf-fonts.js +647 -0
  30. package/dist/pdf-fonts.js.map +1 -0
  31. package/dist/pdf-images.d.ts +17 -0
  32. package/dist/pdf-images.d.ts.map +1 -0
  33. package/dist/pdf-images.js +221 -0
  34. package/dist/pdf-images.js.map +1 -0
  35. package/dist/pdf-writer.d.ts +80 -0
  36. package/dist/pdf-writer.d.ts.map +1 -0
  37. package/dist/pdf-writer.js +677 -0
  38. package/dist/pdf-writer.js.map +1 -0
  39. package/dist/pdf.d.ts +14 -0
  40. package/dist/pdf.d.ts.map +1 -0
  41. package/dist/pdf.js +16 -0
  42. package/dist/pdf.js.map +1 -0
  43. package/dist/pivot.d.ts +194 -0
  44. package/dist/pivot.d.ts.map +1 -0
  45. package/dist/pivot.js +451 -0
  46. package/dist/pivot.js.map +1 -0
  47. package/dist/shared.d.ts +26 -0
  48. package/dist/shared.d.ts.map +1 -0
  49. package/dist/shared.js +36 -0
  50. package/dist/shared.js.map +1 -0
  51. package/dist/xlsx-writer.d.ts +61 -0
  52. package/dist/xlsx-writer.d.ts.map +1 -0
  53. package/dist/xlsx-writer.js +759 -0
  54. package/dist/xlsx-writer.js.map +1 -0
  55. package/dist/xlsx.d.ts +15 -0
  56. package/dist/xlsx.d.ts.map +1 -0
  57. package/dist/xlsx.js +18 -0
  58. package/dist/xlsx.js.map +1 -0
  59. package/dist/zip.d.ts +19 -0
  60. package/dist/zip.d.ts.map +1 -0
  61. package/dist/zip.js +106 -0
  62. package/dist/zip.js.map +1 -0
  63. package/package.json +63 -4
@@ -0,0 +1,22 @@
1
+ import type { PdfHelpers } from './pdf-writer.js';
2
+ /** Font file bytes. */
3
+ export type PdfFontData = Uint8Array | ArrayBuffer;
4
+ /** Embedded TrueType fonts; `bold` defaults to a stroked (synthetic) bold of `regular`. */
5
+ export interface PdfFont {
6
+ readonly regular: PdfFontData;
7
+ readonly bold?: PdfFontData;
8
+ }
9
+ /** @internal Text fitted into a width: the PDF string operand and its width in points. */
10
+ export interface ShownText {
11
+ readonly s: string;
12
+ readonly w: number;
13
+ }
14
+ /** @internal */
15
+ export interface EmbeddedFace {
16
+ fit(text: string, width: number, size: number): ShownText | null;
17
+ /** Writes the font objects; `type0` is the id the pages reference. */
18
+ write(type0: number, alloc: () => number, add: (id: number, body: string) => void, stream: (id: number, data: Uint8Array, dict?: string) => Promise<void>): Promise<void>;
19
+ }
20
+ /** @internal Creates the embedded face of one font file. */
21
+ export declare function createEmbeddedFace(data: PdfFontData, rtl: boolean, helpers: PdfHelpers): EmbeddedFace;
22
+ //# sourceMappingURL=pdf-fonts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf-fonts.d.ts","sourceRoot":"","sources":["../src/pdf-fonts.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAKlD,uBAAuB;AACvB,MAAM,MAAM,WAAW,GAAG,UAAU,GAAG,WAAW,CAAC;AAEnD,2FAA2F;AAC3F,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC;CAC7B;AAED,0FAA0F;AAC1F,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,gBAAgB;AAChB,MAAM,WAAW,YAAY;IAC3B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;IACjE,sEAAsE;IACtE,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3K;AA0WD,4DAA4D;AAC5D,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,GAAG,YAAY,CAiOrG"}
@@ -0,0 +1,647 @@
1
+ // Embedded TrueType fonts for the PDF writer. Loaded with dynamic
2
+ // `import()` only when `PdfOptions.font` is set, so a Helvetica-only export
3
+ // never pays for it. Hand-written, no third-party code:
4
+ //
5
+ // - Parses a TrueType (`glyf` outlines) font or the first font of a `.ttc`
6
+ // collection: `cmap` formats 4 and 12, `hmtx`, `loca`/`glyf`, and the
7
+ // `init`/`medi`/`fina`/`isol` (single) and `rlig` (ligature) lookups of
8
+ // `GSUB` for Arabic joining. Without those lookups the Arabic
9
+ // Presentation Forms-B code points are used when the font maps them.
10
+ // - Writes a Type0 font with `Identity-H` encoding over a CIDFontType2
11
+ // (`CIDToGIDMap /Identity`), a `/W` widths array, and a `ToUnicode` CMap,
12
+ // so text stays searchable and copyable.
13
+ // - Subsets: only the glyphs the document uses (plus composite components)
14
+ // are embedded, renumbered densely from 0 (`.notdef`).
15
+ // - Lays out bidirectional text with a simplified Unicode Bidirectional
16
+ // Algorithm (strong L/R, European/Arabic numbers, neutrals, mirrored
17
+ // brackets; no explicit embeddings or isolates) per cell.
18
+ //
19
+ // CFF-flavored OpenType fonts (`OTTO`) are rejected with an error: they
20
+ // need a CFF subsetter. GPOS (mark positioning, kerning) is not applied.
21
+ let concat;
22
+ let latin1;
23
+ const bytesOf = (d) => (d instanceof Uint8Array ? d : ArrayBuffer.isView(d) ? new Uint8Array(d.buffer, d.byteOffset, d.byteLength) : new Uint8Array(d));
24
+ const hex4 = (n) => n.toString(16).padStart(4, '0').toUpperCase();
25
+ // --- Unicode classes ---------------------------------------------------------
26
+ const L = 0;
27
+ const R = 1;
28
+ const EN = 2;
29
+ const N = 3;
30
+ const NSM = 4;
31
+ const isMark = (c) => (c >= 0x591 && c <= 0x5bd) || c === 0x5bf || c === 0x5c1 || c === 0x5c2 || c === 0x5c4 || c === 0x5c5 || c === 0x5c7 ||
32
+ (c >= 0x610 && c <= 0x61a) || (c >= 0x64b && c <= 0x65f) || c === 0x670 || (c >= 0x6d6 && c <= 0x6dc) || (c >= 0x6df && c <= 0x6e4) ||
33
+ c === 0x6e7 || c === 0x6e8 || (c >= 0x6ea && c <= 0x6ed) || (c >= 0x300 && c <= 0x36f);
34
+ function bidiClass(c) {
35
+ if ((c >= 0x30 && c <= 0x39) || (c >= 0x660 && c <= 0x669) || (c >= 0x6f0 && c <= 0x6f9))
36
+ return EN;
37
+ if (isMark(c))
38
+ return NSM;
39
+ if ((c >= 0x590 && c <= 0x8ff) || (c >= 0xfb1d && c <= 0xfdff) || (c >= 0xfe70 && c <= 0xfefc))
40
+ return R;
41
+ if (c < 0x30 || (c >= 0x3a && c <= 0x40) || (c >= 0x5b && c <= 0x60) || (c >= 0x7b && c <= 0xbf) || c === 0xd7 || c === 0xf7 || (c >= 0x2000 && c <= 0x206f) || (c >= 0x3000 && c <= 0x3003))
42
+ return N;
43
+ return L;
44
+ }
45
+ const RTL_TEXT = /[֐-ࣿיִ-﷿ﹰ-ﻼ]/;
46
+ const MIRROR = { 40: 41, 41: 40, 60: 62, 62: 60, 91: 93, 93: 91, 123: 125, 125: 123, 0xab: 0xbb, 0xbb: 0xab, 0x2039: 0x203a, 0x203a: 0x2039 };
47
+ // Arabic joining types: 0 none, 1 right-joining, 2 dual-joining, 3 transparent.
48
+ const JOIN_0621 = `URRRRDRDRDDDDDRRRR${'D'.repeat(21)}RDD`;
49
+ const JOIN_R_0671 = new Set([0x671, 0x672, 0x673, 0x675, 0x676, 0x677, 0x6c0, 0x6c3, 0x6c4, 0x6c5, 0x6c6, 0x6c7, 0x6c8, 0x6c9, 0x6ca, 0x6cb, 0x6cd, 0x6cf, 0x6d2, 0x6d3, 0x6d5]);
50
+ function joinType(c) {
51
+ if (isMark(c))
52
+ return 3;
53
+ if (c >= 0x621 && c <= 0x64a)
54
+ return JOIN_0621[c - 0x621] === 'U' ? 0 : JOIN_0621[c - 0x621] === 'R' ? 1 : 2;
55
+ if (c >= 0x671 && c <= 0x6d5)
56
+ return JOIN_R_0671.has(c) || (c >= 0x688 && c <= 0x699) ? 1 : c === 0x674 || c === 0x6d4 ? 0 : 2;
57
+ return 0;
58
+ }
59
+ const SEPARATORS = new Set([44, 46, 47, 58, 0xa0, 0x66b, 0x66c]);
60
+ const TERMINATORS = new Set([35, 36, 37, 43, 45, 0xb0, 0x20ac, 0x66a]);
61
+ const ALEF = new Set([0x622, 0x623, 0x625, 0x627]);
62
+ // Arabic Presentation Forms-B: first code point (isolated form) of 0621..064A.
63
+ const PRES_B = (() => {
64
+ const out = new Map();
65
+ let at = 0xfe80;
66
+ for (let c = 0x621; c <= 0x64a; c++) {
67
+ if (c >= 0x63b && c <= 0x640)
68
+ continue;
69
+ out.set(c, at);
70
+ at += c === 0x621 ? 1 : joinType(c) === 1 || c === 0x649 ? 2 : 4;
71
+ }
72
+ return out;
73
+ })();
74
+ function parseFont(bytes) {
75
+ const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
76
+ const tag4 = (at) => String.fromCharCode(bytes[at], bytes[at + 1], bytes[at + 2], bytes[at + 3]);
77
+ let base = 0;
78
+ if (tag4(0) === 'ttcf')
79
+ base = v.getUint32(12);
80
+ const flavor = tag4(base);
81
+ if (flavor === 'OTTO')
82
+ throw new Error('PDF font: CFF-based OpenType fonts are not supported; pass a TrueType (glyf) font.');
83
+ const tables = new Map();
84
+ const count = v.getUint16(base + 4);
85
+ for (let i = 0; i < count; i++) {
86
+ const r = base + 12 + i * 16;
87
+ tables.set(tag4(r), { o: v.getUint32(r + 8), l: v.getUint32(r + 12) });
88
+ }
89
+ for (const t of ['head', 'hhea', 'maxp', 'hmtx', 'loca', 'glyf', 'cmap'])
90
+ if (!tables.has(t))
91
+ throw new Error(`PDF font: missing the "${t}" table (a TrueType font with glyf outlines is required).`);
92
+ const T = (t) => tables.get(t);
93
+ const head = T('head').o;
94
+ // cmap: prefer a full-repertoire format 12 subtable, else a BMP format 4 one.
95
+ const cmap = T('cmap').o;
96
+ let f12 = -1;
97
+ let f4 = -1;
98
+ for (let i = 0, n = v.getUint16(cmap + 2); i < n; i++) {
99
+ const r = cmap + 4 + i * 8;
100
+ const platform = v.getUint16(r);
101
+ const encoding = v.getUint16(r + 2);
102
+ const sub = cmap + v.getUint32(r + 4);
103
+ const format = v.getUint16(sub);
104
+ if (platform !== 0 && !(platform === 3 && (encoding === 1 || encoding === 10)))
105
+ continue;
106
+ if (format === 12 && f12 < 0)
107
+ f12 = sub;
108
+ else if (format === 4 && f4 < 0)
109
+ f4 = sub;
110
+ }
111
+ const lookup = (cp) => {
112
+ if (f12 >= 0) {
113
+ let lo = 0;
114
+ let hi = v.getUint32(f12 + 12) - 1;
115
+ while (lo <= hi) {
116
+ const mid = (lo + hi) >> 1;
117
+ const g = f12 + 16 + mid * 12;
118
+ if (cp < v.getUint32(g))
119
+ hi = mid - 1;
120
+ else if (cp > v.getUint32(g + 4))
121
+ lo = mid + 1;
122
+ else
123
+ return v.getUint32(g + 8) + cp - v.getUint32(g);
124
+ }
125
+ return 0;
126
+ }
127
+ if (f4 < 0 || cp > 0xffff)
128
+ return 0;
129
+ const seg2 = v.getUint16(f4 + 6);
130
+ const ends = f4 + 14;
131
+ let lo = 0;
132
+ let hi = seg2 / 2 - 1;
133
+ while (lo < hi) {
134
+ const mid = (lo + hi) >> 1;
135
+ if (v.getUint16(ends + mid * 2) < cp)
136
+ lo = mid + 1;
137
+ else
138
+ hi = mid;
139
+ }
140
+ const s = lo * 2;
141
+ const start = v.getUint16(ends + seg2 + 2 + s);
142
+ if (cp < start || cp > v.getUint16(ends + s))
143
+ return 0;
144
+ const delta = v.getUint16(ends + 2 * seg2 + 2 + s);
145
+ const rangeAt = ends + 3 * seg2 + 2 + s;
146
+ const range = v.getUint16(rangeAt);
147
+ if (range === 0)
148
+ return (cp + delta) & 0xffff;
149
+ const g = v.getUint16(rangeAt + range + (cp - start) * 2);
150
+ return g === 0 ? 0 : (g + delta) & 0xffff;
151
+ };
152
+ const cache = new Map();
153
+ const numGlyphs = v.getUint16(T('maxp').o + 4);
154
+ const gid = (cp) => {
155
+ let g = cache.get(cp);
156
+ if (g === undefined) {
157
+ g = lookup(cp);
158
+ if (g >= numGlyphs)
159
+ g = 0;
160
+ cache.set(cp, g);
161
+ }
162
+ return g;
163
+ };
164
+ // GSUB: Arabic forms (single substitution) and required ligatures.
165
+ const forms = [new Map(), new Map(), new Map(), new Map()];
166
+ const ligatures = new Map();
167
+ const gsub = tables.get('GSUB');
168
+ if (gsub) {
169
+ const g0 = gsub.o;
170
+ const coverage = (at) => {
171
+ const out = [];
172
+ const format = v.getUint16(at);
173
+ const n = v.getUint16(at + 2);
174
+ for (let i = 0; i < n; i++) {
175
+ if (format === 1)
176
+ out.push(v.getUint16(at + 4 + i * 2));
177
+ else {
178
+ const r = at + 4 + i * 6;
179
+ for (let g = v.getUint16(r); g <= v.getUint16(r + 2); g++)
180
+ out.push(g);
181
+ }
182
+ }
183
+ return out;
184
+ };
185
+ const featureList = g0 + v.getUint16(g0 + 6);
186
+ const lookupList = g0 + v.getUint16(g0 + 8);
187
+ const wanted = { isol: 0, fina: 1, init: 2, medi: 3, rlig: 4 };
188
+ for (let i = 0, n = v.getUint16(featureList); i < n; i++) {
189
+ const r = featureList + 2 + i * 6;
190
+ const slot = wanted[tag4(r)];
191
+ if (slot === undefined)
192
+ continue;
193
+ const feature = featureList + v.getUint16(r + 4);
194
+ for (let k = 0, m = v.getUint16(feature + 2); k < m; k++) {
195
+ const lookupAt = lookupList + v.getUint16(lookupList + 2 + v.getUint16(feature + 4 + k * 2) * 2);
196
+ let type = v.getUint16(lookupAt);
197
+ for (let s = 0, sn = v.getUint16(lookupAt + 4); s < sn; s++) {
198
+ let sub = lookupAt + v.getUint16(lookupAt + 6 + s * 2);
199
+ if (type === 7) {
200
+ type = v.getUint16(sub + 2);
201
+ sub += v.getUint32(sub + 4);
202
+ }
203
+ const format = v.getUint16(sub);
204
+ const cov = coverage(sub + v.getUint16(sub + 2));
205
+ if (type === 1 && slot < 4) {
206
+ const map = forms[slot];
207
+ cov.forEach((g, ci) => {
208
+ if (!map.has(g))
209
+ map.set(g, format === 1 ? (g + v.getInt16(sub + 4)) & 0xffff : v.getUint16(sub + 6 + ci * 2));
210
+ });
211
+ }
212
+ else if (type === 4 && slot === 4) {
213
+ cov.forEach((g, ci) => {
214
+ const set = sub + v.getUint16(sub + 6 + ci * 2);
215
+ for (let j = 0, ln = v.getUint16(set); j < ln; j++) {
216
+ const lig = set + v.getUint16(set + 2 + j * 2);
217
+ const c = [];
218
+ for (let q = 1, qn = v.getUint16(lig + 2); q < qn; q++)
219
+ c.push(v.getUint16(lig + 2 + q * 2));
220
+ const list = ligatures.get(g) ?? [];
221
+ list.push({ c, g: v.getUint16(lig) });
222
+ ligatures.set(g, list);
223
+ }
224
+ });
225
+ }
226
+ type = v.getUint16(lookupAt);
227
+ }
228
+ }
229
+ }
230
+ }
231
+ return {
232
+ bytes,
233
+ v,
234
+ tables,
235
+ upem: v.getUint16(head + 18),
236
+ numGlyphs,
237
+ numHMetrics: v.getUint16(T('hhea').o + 34),
238
+ longLoca: v.getInt16(head + 50) === 1,
239
+ gid,
240
+ forms,
241
+ ligatures,
242
+ };
243
+ }
244
+ function advance(f, g) {
245
+ const h = f.tables.get('hmtx').o;
246
+ return f.v.getUint16(h + Math.min(g, f.numHMetrics - 1) * 4);
247
+ }
248
+ function glyphRange(f, g) {
249
+ const loca = f.tables.get('loca').o;
250
+ const glyf = f.tables.get('glyf').o;
251
+ const a = f.longLoca ? f.v.getUint32(loca + g * 4) : f.v.getUint16(loca + g * 2) * 2;
252
+ const b = f.longLoca ? f.v.getUint32(loca + g * 4 + 4) : f.v.getUint16(loca + g * 2 + 2) * 2;
253
+ return [glyf + a, Math.max(0, b - a)];
254
+ }
255
+ /** Component glyph offsets (glyph index positions) of a composite glyph. */
256
+ function components(f, g) {
257
+ const [at, len] = glyphRange(f, g);
258
+ if (len < 10 || f.v.getInt16(at) >= 0)
259
+ return [];
260
+ const out = [];
261
+ let p = at + 10;
262
+ for (;;) {
263
+ const flags = f.v.getUint16(p);
264
+ out.push(p + 2 - at);
265
+ p += 4 + (flags & 1 ? 4 : 2) + (flags & 8 ? 2 : flags & 0x40 ? 4 : flags & 0x80 ? 8 : 0);
266
+ if (!(flags & 0x20))
267
+ return out;
268
+ }
269
+ }
270
+ // --- subsetting ------------------------------------------------------------------
271
+ function checksum(data) {
272
+ let sum = 0;
273
+ const v = new DataView(data.buffer, data.byteOffset, data.byteLength);
274
+ const n = data.length & ~3;
275
+ for (let i = 0; i < n; i += 4)
276
+ sum = (sum + v.getUint32(i)) >>> 0;
277
+ let tail = 0;
278
+ for (let i = n; i < data.length; i++)
279
+ tail |= data[i] << (24 - 8 * (i - n));
280
+ return (sum + tail) >>> 0;
281
+ }
282
+ /** A TrueType font holding `order` (old glyph ids; index = new id). */
283
+ function subsetFont(f, order, newId) {
284
+ const src = (t) => {
285
+ const r = f.tables.get(t);
286
+ return r ? f.bytes.slice(r.o, r.o + r.l) : null;
287
+ };
288
+ const n = order.length;
289
+ const glyphs = [];
290
+ const loca = new Uint8Array((n + 1) * 4);
291
+ const lv = new DataView(loca.buffer);
292
+ const hmtx = new Uint8Array(n * 4);
293
+ const hv = new DataView(hmtx.buffer);
294
+ const hm = f.tables.get('hmtx').o;
295
+ let at = 0;
296
+ order.forEach((old, i) => {
297
+ const [o, len] = glyphRange(f, old);
298
+ const data = new Uint8Array((len + 3) & ~3);
299
+ data.set(f.bytes.subarray(o, o + len));
300
+ const dv = new DataView(data.buffer);
301
+ for (const c of components(f, old))
302
+ dv.setUint16(c, newId(dv.getUint16(c)));
303
+ glyphs.push(data);
304
+ lv.setUint32(i * 4, at);
305
+ at += data.length;
306
+ hv.setUint16(i * 4, advance(f, old));
307
+ hv.setInt16(i * 4 + 2, f.v.getInt16(old < f.numHMetrics ? hm + old * 4 + 2 : hm + f.numHMetrics * 4 + (old - f.numHMetrics) * 2));
308
+ });
309
+ lv.setUint32(n * 4, at);
310
+ const head = src('head');
311
+ const headV = new DataView(head.buffer);
312
+ headV.setUint32(8, 0);
313
+ headV.setInt16(50, 1);
314
+ const hhea = src('hhea');
315
+ new DataView(hhea.buffer).setUint16(34, n);
316
+ const maxp = src('maxp');
317
+ new DataView(maxp.buffer).setUint16(4, n);
318
+ const post = new Uint8Array(32);
319
+ const postSrc = src('post');
320
+ if (postSrc)
321
+ post.set(postSrc.subarray(0, 32));
322
+ new DataView(post.buffer).setUint32(0, 0x00030000);
323
+ const out = [
324
+ ['glyf', concat(glyphs, at)],
325
+ ['head', head],
326
+ ['hhea', hhea],
327
+ ['hmtx', hmtx],
328
+ ['loca', loca],
329
+ ['maxp', maxp],
330
+ ['post', post],
331
+ ];
332
+ for (const t of ['cvt ', 'fpgm', 'prep']) {
333
+ const d = src(t);
334
+ if (d)
335
+ out.push([t, d]);
336
+ }
337
+ out.sort((a, b) => (a[0] < b[0] ? -1 : 1));
338
+ const count = out.length;
339
+ const dir = new Uint8Array(12 + count * 16);
340
+ const dv = new DataView(dir.buffer);
341
+ let pow = 1;
342
+ let log = 0;
343
+ while (pow * 2 <= count) {
344
+ pow *= 2;
345
+ log++;
346
+ }
347
+ dv.setUint32(0, 0x00010000);
348
+ dv.setUint16(4, count);
349
+ dv.setUint16(6, pow * 16);
350
+ dv.setUint16(8, log);
351
+ dv.setUint16(10, count * 16 - pow * 16);
352
+ const parts = [dir];
353
+ let offset = dir.length;
354
+ out.forEach(([tag, data], i) => {
355
+ const r = 12 + i * 16;
356
+ for (let k = 0; k < 4; k++)
357
+ dir[r + k] = tag.charCodeAt(k);
358
+ dv.setUint32(r + 4, checksum(data));
359
+ dv.setUint32(r + 8, offset);
360
+ dv.setUint32(r + 12, data.length);
361
+ const padded = new Uint8Array((data.length + 3) & ~3);
362
+ padded.set(data);
363
+ parts.push(padded);
364
+ offset += padded.length;
365
+ });
366
+ const font = concat(parts, offset);
367
+ const headAt = new DataView(dir.buffer).getUint32(12 + out.findIndex((t) => t[0] === 'head') * 16 + 8);
368
+ new DataView(font.buffer).setUint32(headAt + 8, (0xb1b0afba - checksum(font)) >>> 0);
369
+ return font;
370
+ }
371
+ /** @internal Creates the embedded face of one font file. */
372
+ export function createEmbeddedFace(data, rtl, helpers) {
373
+ ({ concat, latin1 } = helpers);
374
+ const font = parseFont(bytesOf(data));
375
+ const scale = 1000 / font.upem;
376
+ const order = [0];
377
+ const unicode = [''];
378
+ const ids = new Map([[0, 0]]);
379
+ const use = (old, text) => {
380
+ let id = ids.get(old);
381
+ if (id !== undefined && text && !unicode[id])
382
+ unicode[id] = text;
383
+ if (id === undefined) {
384
+ id = order.length;
385
+ ids.set(old, id);
386
+ order.push(old);
387
+ unicode.push(text);
388
+ const [at] = glyphRange(font, old);
389
+ for (const c of components(font, old))
390
+ use(font.v.getUint16(at + c), '');
391
+ }
392
+ return id;
393
+ };
394
+ const glyphFor = (cp) => font.gid(cp);
395
+ const gsubForms = font.forms.some((m) => m.size > 0);
396
+ /** Clusters of a text in logical order (marks attach to their base). */
397
+ const shape = (text) => {
398
+ const cps = Array.from(text, (ch) => {
399
+ const c = ch.codePointAt(0);
400
+ return c === 9 || c === 10 || c === 13 ? 32 : c;
401
+ });
402
+ const out = [];
403
+ const push = (old, chars, cls) => {
404
+ const id = use(old, chars);
405
+ const w = advance(font, old);
406
+ const last = out[out.length - 1];
407
+ if (cls === NSM && last) {
408
+ last.ids.push(id);
409
+ last.w += w;
410
+ }
411
+ else
412
+ out.push({ ids: [id], w, cls: cls === NSM ? N : cls });
413
+ };
414
+ const arabic = RTL_TEXT.test(text);
415
+ const types = arabic ? cps.map(joinType) : null;
416
+ for (let i = 0; i < cps.length; i++) {
417
+ const c = cps[i];
418
+ const cls = bidiClass(c);
419
+ const jt = types ? types[i] : 0;
420
+ if (!types || jt === 0 || jt === 3) {
421
+ push(glyphFor(c), String.fromCodePoint(c), cls);
422
+ continue;
423
+ }
424
+ let p = i - 1;
425
+ while (p >= 0 && types[p] === 3)
426
+ p--;
427
+ let n = i + 1;
428
+ while (n < cps.length && types[n] === 3)
429
+ n++;
430
+ const joinPrev = p >= 0 && types[p] === 2;
431
+ const next = n < cps.length ? types[n] : 0;
432
+ // Lam-alef required ligature.
433
+ if (c === 0x644 && n < cps.length && ALEF.has(cps[n])) {
434
+ const lam = font.forms[joinPrev ? 3 : 2]?.get(glyphFor(c)) ?? glyphFor(c);
435
+ const alef = font.forms[1]?.get(glyphFor(cps[n])) ?? glyphFor(cps[n]);
436
+ const lig = font.ligatures.get(lam)?.find((x) => x.c.length === 1 && x.c[0] === alef)?.g;
437
+ const k = [0x622, 0x623, 0x625, 0x627].indexOf(cps[n]);
438
+ const pres = lig === undefined ? glyphFor(0xfef5 + k * 2 + (joinPrev ? 1 : 0)) : 0;
439
+ if (lig !== undefined || pres) {
440
+ const marks = cps.slice(i + 1, n);
441
+ push(lig ?? pres, String.fromCodePoint(c, ...marks, cps[n]), R);
442
+ i = n;
443
+ continue;
444
+ }
445
+ }
446
+ const joinNext = jt === 2 && (next === 1 || next === 2);
447
+ const form = joinPrev ? (joinNext ? 3 : 1) : joinNext ? 2 : 0;
448
+ const base = glyphFor(c);
449
+ let g;
450
+ if (gsubForms)
451
+ g = font.forms[form].get(base);
452
+ else {
453
+ const pb = PRES_B.get(c);
454
+ if (pb !== undefined)
455
+ g = glyphFor(pb + (c === 0x621 ? 0 : form)) || undefined;
456
+ }
457
+ push(g ?? base, String.fromCodePoint(c), cls);
458
+ }
459
+ return out;
460
+ };
461
+ /** Visual order of logical clusters (simplified UBA), with mirrored brackets. */
462
+ const reorder = (clusters, mirrored) => {
463
+ let para = rtl ? 1 : 0;
464
+ for (const c of clusters)
465
+ if (c.cls === L || c.cls === R) {
466
+ para = c.cls;
467
+ break;
468
+ }
469
+ const n = clusters.length;
470
+ const levels = new Uint8Array(n);
471
+ let lastStrong = para;
472
+ const resolved = clusters.map((c) => {
473
+ if (c.cls === L || c.cls === R)
474
+ lastStrong = c.cls;
475
+ return c.cls === EN ? (lastStrong === R ? EN : L) : c.cls;
476
+ });
477
+ // Number separators between numbers and terminators next to them join the number (W4, W5).
478
+ for (let i = 0; i < n; i++) {
479
+ if (resolved[i] !== N)
480
+ continue;
481
+ const ch = unicode[clusters[i].ids[0]].charCodeAt(0);
482
+ const before = resolved[i - 1];
483
+ const after = resolved[i + 1];
484
+ if ((SEPARATORS.has(ch) && before === EN && after === EN) || (TERMINATORS.has(ch) && (before === EN || after === EN)))
485
+ resolved[i] = EN;
486
+ }
487
+ for (let i = 0; i < n; i++) {
488
+ let cls = resolved[i];
489
+ if (cls === N) {
490
+ // Neutrals between same-direction strongs (numbers count as R) take it; else the paragraph direction.
491
+ let a = i - 1;
492
+ while (a >= 0 && resolved[a] === N)
493
+ a--;
494
+ let b = i + 1;
495
+ while (b < n && resolved[b] === N)
496
+ b++;
497
+ const dir = (k) => (k < 0 || k >= n ? para : resolved[k] === L ? L : R);
498
+ cls = dir(a) === dir(b) ? dir(a) : para;
499
+ }
500
+ levels[i] = para === 0 ? (cls === R ? 1 : cls === EN ? 2 : 0) : cls === R ? 1 : 2;
501
+ }
502
+ const idx = Array.from({ length: n }, (_, i) => i);
503
+ let max = 0;
504
+ for (const l of levels)
505
+ max = Math.max(max, l);
506
+ for (let level = max; level >= 1; level--) {
507
+ for (let i = 0; i < n;) {
508
+ if (levels[idx[i]] < level) {
509
+ i++;
510
+ continue;
511
+ }
512
+ let j = i;
513
+ while (j < n && levels[idx[j]] >= level)
514
+ j++;
515
+ const run = idx.slice(i, j).reverse();
516
+ for (let k = 0; k < run.length; k++)
517
+ idx[i + k] = run[k];
518
+ i = j;
519
+ }
520
+ }
521
+ return idx.map((i) => levels[i] % 2 === 1 && mirrored[i] != null ? -1 - mirrored[i] : i);
522
+ };
523
+ const cache = new Map();
524
+ let ellipsis = null;
525
+ const fit = (text, width, size) => {
526
+ let clusters = cache.get(text);
527
+ if (!clusters) {
528
+ if (cache.size >= 20_000)
529
+ cache.clear();
530
+ clusters = shape(text);
531
+ cache.set(text, clusters);
532
+ }
533
+ const k = (size * scale) / 1000;
534
+ let total = 0;
535
+ for (const c of clusters)
536
+ total += c.w;
537
+ let shown = clusters;
538
+ if (total * k > width) {
539
+ ellipsis ??= shape(glyphFor(0x2026) ? '…' : '...');
540
+ const dots = ellipsis.reduce((s, c) => s + c.w, 0);
541
+ const room = width / k - dots;
542
+ if (room <= 0)
543
+ return null;
544
+ let w = 0;
545
+ let i = 0;
546
+ while (i < clusters.length && w + clusters[i].w <= room)
547
+ w += clusters[i++].w;
548
+ shown = [...clusters.slice(0, i), ...ellipsis];
549
+ total = w + dots;
550
+ }
551
+ if (!shown.length)
552
+ return null;
553
+ let hex = '<';
554
+ if (shown.some((c) => c.cls === R || (rtl && c.cls === EN))) {
555
+ const mirrors = shown.map((c) => {
556
+ const u = unicode[c.ids[0]];
557
+ const m = u.length === 1 ? MIRROR[u.charCodeAt(0)] : undefined;
558
+ return m === undefined ? null : use(glyphFor(m), String.fromCharCode(m));
559
+ });
560
+ for (const i of reorder(shown, mirrors)) {
561
+ if (i < 0)
562
+ hex += hex4(-1 - i);
563
+ else
564
+ for (const id of shown[i].ids)
565
+ hex += hex4(id);
566
+ }
567
+ }
568
+ else
569
+ for (const c of shown)
570
+ for (const id of c.ids)
571
+ hex += hex4(id);
572
+ return { s: `${hex}>`, w: total * k };
573
+ };
574
+ return {
575
+ fit,
576
+ async write(type0, alloc, add, stream) {
577
+ const cid = alloc();
578
+ const descriptor = alloc();
579
+ const file = alloc();
580
+ const toUnicode = alloc();
581
+ let hash = 2166136261;
582
+ for (const g of order)
583
+ hash = Math.imul(hash ^ g, 16777619) >>> 0;
584
+ let tag = '';
585
+ for (let i = 0; i < 6; i++) {
586
+ tag += String.fromCharCode(65 + (hash % 26));
587
+ hash = Math.floor(hash / 26) + i * 7919;
588
+ }
589
+ const name = `${tag}+${fontName(font)}`;
590
+ const v = font.v;
591
+ const head = font.tables.get('head').o;
592
+ const hhea = font.tables.get('hhea').o;
593
+ const os2 = font.tables.get('OS/2');
594
+ const post = font.tables.get('post');
595
+ const s = (n) => Math.round(n * scale);
596
+ const ascent = s(v.getInt16(hhea + 4));
597
+ const descent = s(v.getInt16(hhea + 6));
598
+ const capHeight = os2 && os2.l >= 90 && v.getUint16(os2.o) >= 2 ? s(v.getInt16(os2.o + 88)) : Math.round(ascent * 0.7);
599
+ const italic = post ? v.getInt32(post.o + 4) / 65536 : 0;
600
+ const fixed = post ? v.getUint32(post.o + 12) !== 0 : false;
601
+ const bbox = [36, 38, 40, 42].map((o) => s(v.getInt16(head + o))).join(' ');
602
+ add(type0, `<< /Type /Font /Subtype /Type0 /BaseFont /${name} /Encoding /Identity-H /DescendantFonts [${cid} 0 R] /ToUnicode ${toUnicode} 0 R >>`);
603
+ const widths = [];
604
+ for (let i = 0; i < order.length; i += 64)
605
+ widths.push(order.slice(i, i + 64).map((g) => Math.round(advance(font, g) * scale)).join(' '));
606
+ add(cid, `<< /Type /Font /Subtype /CIDFontType2 /BaseFont /${name} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor ${descriptor} 0 R /DW 1000 /W [0 [${widths.join('\n')}]] /CIDToGIDMap /Identity >>`);
607
+ add(descriptor, `<< /Type /FontDescriptor /FontName /${name} /Flags ${4 | (fixed ? 1 : 0) | (italic ? 64 : 0)} /FontBBox [${bbox}] /ItalicAngle ${Math.round(italic * 100) / 100} /Ascent ${ascent} /Descent ${descent} /CapHeight ${capHeight} /StemV 80 /FontFile2 ${file} 0 R >>`);
608
+ const program = subsetFont(font, order, (old) => ids.get(old) ?? 0);
609
+ await stream(file, program, `/Length1 ${program.length}`);
610
+ // Glyphs only used as composite components are never shown and get no entry.
611
+ const entries = [];
612
+ unicode.forEach((u, id) => {
613
+ if (u)
614
+ entries.push(`<${hex4(id)}> <${Array.from({ length: u.length }, (_, q) => hex4(u.charCodeAt(q))).join('')}>`);
615
+ });
616
+ const lines = [];
617
+ for (let i = 0; i < entries.length; i += 100) {
618
+ const chunk = entries.slice(i, i + 100);
619
+ lines.push(`${chunk.length} beginbfchar\n${chunk.join('\n')}\nendbfchar`);
620
+ }
621
+ const cmap = `/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n/CMapName /Adobe-Identity-UCS def\n/CMapType 2 def\n1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n${lines.join('\n')}\nendcmap\nCMapName currentdict /CMap defineresource pop\nend\nend`;
622
+ await stream(toUnicode, latin1(cmap));
623
+ },
624
+ };
625
+ }
626
+ /** PostScript name (name id 6) as a PDF name token. */
627
+ function fontName(f) {
628
+ const t = f.tables.get('name');
629
+ let out = '';
630
+ if (t) {
631
+ const v = f.v;
632
+ const strings = t.o + v.getUint16(t.o + 4);
633
+ for (let i = 0, n = v.getUint16(t.o + 2); i < n && !out; i++) {
634
+ const r = t.o + 6 + i * 12;
635
+ if (v.getUint16(r + 6) !== 6)
636
+ continue;
637
+ const platform = v.getUint16(r);
638
+ const len = v.getUint16(r + 8);
639
+ const at = strings + v.getUint16(r + 10);
640
+ for (let k = 0; k < len; k += platform === 1 ? 1 : 2)
641
+ out += String.fromCharCode(platform === 1 ? f.bytes[at + k] : v.getUint16(at + k));
642
+ }
643
+ }
644
+ out = out.replace(/[^\x21-\x7e]|[()<>[\]{}/%#]/g, '');
645
+ return out || 'EmbeddedFont';
646
+ }
647
+ //# sourceMappingURL=pdf-fonts.js.map