@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.
- package/LICENSE +21 -0
- package/README.md +44 -2
- package/dist/blob.d.ts +6 -0
- package/dist/blob.d.ts.map +1 -0
- package/dist/blob.js +10 -0
- package/dist/blob.js.map +1 -0
- package/dist/bytes.d.ts +55 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +167 -0
- package/dist/bytes.js.map +1 -0
- package/dist/gantt.d.ts +111 -0
- package/dist/gantt.d.ts.map +1 -0
- package/dist/gantt.js +348 -0
- package/dist/gantt.js.map +1 -0
- package/dist/graphics.d.ts +58 -0
- package/dist/graphics.d.ts.map +1 -0
- package/dist/graphics.js +651 -0
- package/dist/graphics.js.map +1 -0
- package/dist/ics.d.ts +90 -0
- package/dist/ics.d.ts.map +1 -0
- package/dist/ics.js +528 -0
- package/dist/ics.js.map +1 -0
- package/dist/image.d.ts +10 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +49 -0
- package/dist/image.js.map +1 -0
- package/dist/pdf-fonts.d.ts +22 -0
- package/dist/pdf-fonts.d.ts.map +1 -0
- package/dist/pdf-fonts.js +647 -0
- package/dist/pdf-fonts.js.map +1 -0
- package/dist/pdf-images.d.ts +17 -0
- package/dist/pdf-images.d.ts.map +1 -0
- package/dist/pdf-images.js +221 -0
- package/dist/pdf-images.js.map +1 -0
- package/dist/pdf-writer.d.ts +80 -0
- package/dist/pdf-writer.d.ts.map +1 -0
- package/dist/pdf-writer.js +677 -0
- package/dist/pdf-writer.js.map +1 -0
- package/dist/pdf.d.ts +14 -0
- package/dist/pdf.d.ts.map +1 -0
- package/dist/pdf.js +16 -0
- package/dist/pdf.js.map +1 -0
- package/dist/pivot.d.ts +194 -0
- package/dist/pivot.d.ts.map +1 -0
- package/dist/pivot.js +451 -0
- package/dist/pivot.js.map +1 -0
- package/dist/shared.d.ts +26 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +36 -0
- package/dist/shared.js.map +1 -0
- package/dist/xlsx-writer.d.ts +61 -0
- package/dist/xlsx-writer.d.ts.map +1 -0
- package/dist/xlsx-writer.js +759 -0
- package/dist/xlsx-writer.js.map +1 -0
- package/dist/xlsx.d.ts +15 -0
- package/dist/xlsx.d.ts.map +1 -0
- package/dist/xlsx.js +18 -0
- package/dist/xlsx.js.map +1 -0
- package/dist/zip.d.ts +19 -0
- package/dist/zip.d.ts.map +1 -0
- package/dist/zip.js +106 -0
- package/dist/zip.js.map +1 -0
- package/package.json +63 -4
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
// PDF writer: a hand-written PDF 1.4 file with the
|
|
2
|
+
// standard Helvetica fonts (WinAnsiEncoding), one table laid out per page
|
|
3
|
+
// with repeated header rows, group captions, aggregate rows, detail text,
|
|
4
|
+
// a page header/footer with `{page}` / `{pages}` placeholders, and content
|
|
5
|
+
// streams deflated with the platform `CompressionStream` when available.
|
|
6
|
+
// Columns are scaled to the page width unless `columnOverflow: 'paginate'`
|
|
7
|
+
// splits them across page sets. Without `font`, characters outside WinAnsi
|
|
8
|
+
// print as `?`; with `font` every text is drawn with an embedded,
|
|
9
|
+
// subsetted TrueType font (`pdf-fonts.ts`, loaded on demand), which covers
|
|
10
|
+
// CJK, Cyrillic, Greek, Hebrew, and Arabic (joined, right to left).
|
|
11
|
+
// Image cells are drawn as image XObjects (`pdf-images.ts`, loaded
|
|
12
|
+
// on the first image cell), one per distinct image, scaled to fit the cell;
|
|
13
|
+
// the row grows to the image height, up to a page body. `appendImages`
|
|
14
|
+
// adds one page per image after the table (a chart beside a pivot: table
|
|
15
|
+
// and chart in one PDF), scaled to fit the page body.
|
|
16
|
+
// Cell spans: `colSpan` / `rowSpan` of the `cell` hook and
|
|
17
|
+
// `mergeRepeatedValues` merge cells within a page; a row span stops at a
|
|
18
|
+
// non-data line and continues on the next page with the anchor cell drawn
|
|
19
|
+
// again, and repeated values restart on every page.
|
|
20
|
+
import { concat, createSlicer, defaultYield, deflate, latin1 } from './bytes.js';
|
|
21
|
+
import { hexColor, mergeStyle, stylesOf } from './shared.js';
|
|
22
|
+
import { ImageStore, imageType } from './image.js';
|
|
23
|
+
export const PDF_MIME = 'application/pdf';
|
|
24
|
+
/**
|
|
25
|
+
* @internal Shared helpers handed to the lazy `pdf-fonts` and `pdf-images`
|
|
26
|
+
* chunks, which import nothing at run time: the helper modules then stay in
|
|
27
|
+
* the writer chunk (each extra chunk compresses on its own).
|
|
28
|
+
*/
|
|
29
|
+
export const helpers = { concat, deflate, latin1, imageType };
|
|
30
|
+
/** @internal */
|
|
31
|
+
export const PAGE_SIZES = {
|
|
32
|
+
a0: [2383.94, 3370.39],
|
|
33
|
+
a1: [1683.78, 2383.94],
|
|
34
|
+
a2: [1190.55, 1683.78],
|
|
35
|
+
a3: [841.89, 1190.55],
|
|
36
|
+
a4: [595.28, 841.89],
|
|
37
|
+
a5: [419.53, 595.28],
|
|
38
|
+
b4: [708.66, 1000.63],
|
|
39
|
+
b5: [498.9, 708.66],
|
|
40
|
+
letter: [612, 792],
|
|
41
|
+
legal: [612, 1008],
|
|
42
|
+
ledger: [1224, 792],
|
|
43
|
+
tabloid: [792, 1224],
|
|
44
|
+
'half-letter': [396, 612],
|
|
45
|
+
note: [540, 720],
|
|
46
|
+
};
|
|
47
|
+
// Helvetica advance widths (1/1000 em) for WinAnsi 32..126; other codes use 556.
|
|
48
|
+
const HELVETICA = [
|
|
49
|
+
278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556,
|
|
50
|
+
1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556,
|
|
51
|
+
333, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556, 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584,
|
|
52
|
+
];
|
|
53
|
+
const WIN_ANSI = {
|
|
54
|
+
0x20ac: 0x80, 0x201a: 0x82, 0x0192: 0x83, 0x201e: 0x84, 0x2026: 0x85, 0x2020: 0x86, 0x2021: 0x87, 0x02c6: 0x88, 0x2030: 0x89, 0x0160: 0x8a, 0x2039: 0x8b,
|
|
55
|
+
0x0152: 0x8c, 0x017d: 0x8e, 0x2018: 0x91, 0x2019: 0x92, 0x201c: 0x93, 0x201d: 0x94, 0x2022: 0x95, 0x2013: 0x96, 0x2014: 0x97, 0x02dc: 0x98, 0x2122: 0x99,
|
|
56
|
+
0x0161: 0x9a, 0x203a: 0x9b, 0x0153: 0x9c, 0x017e: 0x9e, 0x0178: 0x9f,
|
|
57
|
+
};
|
|
58
|
+
/** WinAnsi codes of a string (unsupported characters become `?`). */
|
|
59
|
+
export function winAnsi(text) {
|
|
60
|
+
const out = [];
|
|
61
|
+
for (const ch of text) {
|
|
62
|
+
const c = ch.codePointAt(0);
|
|
63
|
+
if (c === 9 || c === 10 || c === 13)
|
|
64
|
+
out.push(32);
|
|
65
|
+
else if ((c >= 32 && c <= 126) || (c >= 0xa0 && c <= 0xff))
|
|
66
|
+
out.push(c);
|
|
67
|
+
else
|
|
68
|
+
out.push(WIN_ANSI[c] ?? 63);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
function widthOf(codes, size, bold) {
|
|
73
|
+
let w = 0;
|
|
74
|
+
for (const c of codes)
|
|
75
|
+
w += c >= 32 && c <= 126 ? HELVETICA[c - 32] : 556;
|
|
76
|
+
return (w * size * (bold ? 1.06 : 1)) / 1000;
|
|
77
|
+
}
|
|
78
|
+
/** PDF literal string of WinAnsi codes, ASCII only (octal escapes). */
|
|
79
|
+
function literal(codes) {
|
|
80
|
+
let s = '(';
|
|
81
|
+
for (const c of codes) {
|
|
82
|
+
if (c === 40 || c === 41 || c === 92)
|
|
83
|
+
s += `\\${String.fromCharCode(c)}`;
|
|
84
|
+
else if (c < 32 || c > 126)
|
|
85
|
+
s += `\\${c.toString(8).padStart(3, '0')}`;
|
|
86
|
+
else
|
|
87
|
+
s += String.fromCharCode(c);
|
|
88
|
+
}
|
|
89
|
+
return `${s})`;
|
|
90
|
+
}
|
|
91
|
+
/** Text fitted into `width` (ellipsis when cut). */
|
|
92
|
+
function fit(text, width, size, bold) {
|
|
93
|
+
const codes = winAnsi(text);
|
|
94
|
+
if (widthOf(codes, size, bold) <= width)
|
|
95
|
+
return codes;
|
|
96
|
+
const dots = [46, 46, 46];
|
|
97
|
+
const room = width - widthOf(dots, size, bold);
|
|
98
|
+
let w = 0;
|
|
99
|
+
let i = 0;
|
|
100
|
+
for (; i < codes.length; i++) {
|
|
101
|
+
const cw = widthOf([codes[i]], size, bold);
|
|
102
|
+
if (w + cw > room)
|
|
103
|
+
break;
|
|
104
|
+
w += cw;
|
|
105
|
+
}
|
|
106
|
+
return room > 0 ? [...codes.slice(0, i), ...dots] : [];
|
|
107
|
+
}
|
|
108
|
+
/** @internal */
|
|
109
|
+
export const helvetica = (bold) => ({
|
|
110
|
+
fit(text, width, size) {
|
|
111
|
+
const codes = fit(text, width, size, bold);
|
|
112
|
+
return codes.length ? { s: literal(codes), w: widthOf(codes, size, bold) } : null;
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
/** PDF text string: a literal when WinAnsi holds it, else UTF-16BE hex. */
|
|
116
|
+
/** @internal */
|
|
117
|
+
export function textString(value) {
|
|
118
|
+
if (/^[\x20-\x7e]*$/.test(value))
|
|
119
|
+
return literal(winAnsi(value));
|
|
120
|
+
let hex = '<FEFF';
|
|
121
|
+
for (let i = 0; i < value.length; i++)
|
|
122
|
+
hex += value.charCodeAt(i).toString(16).padStart(4, '0');
|
|
123
|
+
return `${hex}>`;
|
|
124
|
+
}
|
|
125
|
+
/** @internal */
|
|
126
|
+
export function rgb(color) {
|
|
127
|
+
const h = hexColor(color);
|
|
128
|
+
if (!h)
|
|
129
|
+
return null;
|
|
130
|
+
const n = Number.parseInt(h, 16);
|
|
131
|
+
const f = (v) => (Math.round((v / 255) * 1000) / 1000).toString();
|
|
132
|
+
return `${f((n >> 16) & 255)} ${f((n >> 8) & 255)} ${f(n & 255)}`;
|
|
133
|
+
}
|
|
134
|
+
/** @internal */
|
|
135
|
+
export const n2 = (v) => (Math.round(v * 100) / 100).toString();
|
|
136
|
+
/** Writes an export model as PDF bytes. */
|
|
137
|
+
export async function writePdf(model, options = {}) {
|
|
138
|
+
const slicer = createSlicer(Math.max(1, options.sliceMs ?? 25), options.yield ?? defaultYield);
|
|
139
|
+
const theme = stylesOf(options.styles);
|
|
140
|
+
const base = Array.isArray(options.pageSize) ? options.pageSize : PAGE_SIZES[options.pageSize ?? 'a4'];
|
|
141
|
+
const landscape = options.orientation === 'landscape';
|
|
142
|
+
const pageW = landscape ? Math.max(base[0], base[1]) : Math.min(base[0], base[1]);
|
|
143
|
+
const pageH = landscape ? Math.min(base[0], base[1]) : Math.max(base[0], base[1]);
|
|
144
|
+
const margin = options.margin ?? 36;
|
|
145
|
+
const size = options.fontSize ?? 8;
|
|
146
|
+
const rowH = Math.round(size * 1.9 * 100) / 100;
|
|
147
|
+
const pad = 3;
|
|
148
|
+
const usableW = pageW - 2 * margin;
|
|
149
|
+
const rtl = model.direction === 'rtl';
|
|
150
|
+
const compress = options.compress !== false;
|
|
151
|
+
const headerText = options.header ?? '';
|
|
152
|
+
const footerText = options.footer ?? '{page} / {pages}';
|
|
153
|
+
const includeHeaders = options.includeHeaders !== false;
|
|
154
|
+
const headerRows = includeHeaders ? model.headerRows : [];
|
|
155
|
+
// --- fonts -------------------------------------------------------------------
|
|
156
|
+
let regular = helvetica(false);
|
|
157
|
+
let bold = helvetica(true);
|
|
158
|
+
let embedded = null;
|
|
159
|
+
if (options.font) {
|
|
160
|
+
const { createEmbeddedFace } = await import('./pdf-fonts.js');
|
|
161
|
+
const spec = options.font;
|
|
162
|
+
const font = spec.regular ? spec : { regular: options.font };
|
|
163
|
+
const r = createEmbeddedFace(font.regular, rtl, helpers);
|
|
164
|
+
const b = font.bold ? createEmbeddedFace(font.bold, rtl, helpers) : null;
|
|
165
|
+
embedded = { regular: r, bold: b };
|
|
166
|
+
regular = r;
|
|
167
|
+
bold = b ?? r;
|
|
168
|
+
}
|
|
169
|
+
const fakeBold = embedded !== null && embedded.bold === null;
|
|
170
|
+
// --- column page sets ------------------------------------------------------
|
|
171
|
+
const all = model.columns.map((c, i) => ({ column: c, index: i, x: 0, w: c.width * 0.75 }));
|
|
172
|
+
const sets = [];
|
|
173
|
+
if (options.columnOverflow === 'paginate') {
|
|
174
|
+
let current = [];
|
|
175
|
+
let used = 0;
|
|
176
|
+
for (const c of all) {
|
|
177
|
+
const w = Math.min(c.w, usableW);
|
|
178
|
+
if (current.length && used + w > usableW) {
|
|
179
|
+
sets.push(current);
|
|
180
|
+
current = [];
|
|
181
|
+
used = 0;
|
|
182
|
+
}
|
|
183
|
+
c.w = w;
|
|
184
|
+
current.push(c);
|
|
185
|
+
used += w;
|
|
186
|
+
}
|
|
187
|
+
if (current.length)
|
|
188
|
+
sets.push(current);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const total = all.reduce((s, c) => s + c.w, 0);
|
|
192
|
+
const scale = total > usableW ? usableW / total : 1;
|
|
193
|
+
for (const c of all)
|
|
194
|
+
c.w *= scale;
|
|
195
|
+
sets.push(all);
|
|
196
|
+
}
|
|
197
|
+
for (const set of sets) {
|
|
198
|
+
let x = 0;
|
|
199
|
+
const width = set.reduce((s, c) => s + c.w, 0);
|
|
200
|
+
for (const c of set) {
|
|
201
|
+
c.x = rtl ? margin + width - x - c.w : margin + x;
|
|
202
|
+
x += c.w;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (sets.length === 0)
|
|
206
|
+
sets.push([]);
|
|
207
|
+
// --- body lines --------------------------------------------------------------
|
|
208
|
+
// Detail blocks nest: a child model's own detail rows (deeper hierarchy levels) follow their row, one indent deeper.
|
|
209
|
+
const lineCount = (row) => {
|
|
210
|
+
if (row.kind !== 'detail')
|
|
211
|
+
return 1;
|
|
212
|
+
const m = row.detail.model;
|
|
213
|
+
let count = (row.detail.text?.length ?? 0) + (m ? m.headerRows.length : 0);
|
|
214
|
+
for (let k = 0; m && k < m.rowCount; k++)
|
|
215
|
+
count += lineCount(m.getRow(k));
|
|
216
|
+
return count;
|
|
217
|
+
};
|
|
218
|
+
let totalLines = model.footerRows.length;
|
|
219
|
+
for (let i = 0; i < model.rowCount; i++)
|
|
220
|
+
totalLines += lineCount(model.getRow(i));
|
|
221
|
+
function* detailLines(row, base) {
|
|
222
|
+
const depth = base + row.depth;
|
|
223
|
+
for (const text of row.detail.text ?? [])
|
|
224
|
+
yield { kind: 'text', text, depth };
|
|
225
|
+
const m = row.detail.model;
|
|
226
|
+
for (let k = m ? -m.headerRows.length : 0; m && k < m.rowCount; k++) {
|
|
227
|
+
const r = k < 0 ? null : m.getRow(k);
|
|
228
|
+
if (r?.kind === 'detail')
|
|
229
|
+
yield* detailLines(r, depth + 1);
|
|
230
|
+
else
|
|
231
|
+
yield { kind: 'nested', model: m, index: k, depth };
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function* lines() {
|
|
235
|
+
for (const row of model.rows()) {
|
|
236
|
+
if (row.kind === 'detail')
|
|
237
|
+
yield* detailLines(row, 0);
|
|
238
|
+
else
|
|
239
|
+
yield { kind: 'row', row };
|
|
240
|
+
}
|
|
241
|
+
for (const f of model.footerRows)
|
|
242
|
+
yield { kind: 'row', row: f };
|
|
243
|
+
}
|
|
244
|
+
const pageTop = pageH - margin - (headerText ? rowH : 0);
|
|
245
|
+
const pageBottom = margin + (footerText ? rowH : 0);
|
|
246
|
+
const perPage = Math.max(1, Math.floor((pageTop - pageBottom) / rowH) - headerRows.length);
|
|
247
|
+
const bodyPages = Math.max(1, Math.ceil(totalLines / perPage));
|
|
248
|
+
// --- drawing -----------------------------------------------------------------
|
|
249
|
+
const ops = [];
|
|
250
|
+
const fill = (color, x, y, w, h) => {
|
|
251
|
+
const c = rgb(color);
|
|
252
|
+
if (c)
|
|
253
|
+
ops.push(`${c} rg ${n2(x)} ${n2(y)} ${n2(w)} ${n2(h)} re f`);
|
|
254
|
+
};
|
|
255
|
+
/** Draws an image XObject `w` x `h` with its lower left corner at (`x`, `y`). */
|
|
256
|
+
const imageOp = (p, w, h, x, y) => `q ${n2(w)} 0 0 ${n2(h)} ${n2(x)} ${n2(y)} cm /${p.name} Do Q`;
|
|
257
|
+
/** Left edge of content `cw` wide in a box at `x`, `w` wide (RTL swaps start and end). */
|
|
258
|
+
const place = (align, x, w, cw) => {
|
|
259
|
+
// At the end edge: 'end' in LTR, 'start' in RTL.
|
|
260
|
+
return align === 'center' ? x + (w - cw) / 2 : (align === 'end') !== rtl ? x + w - pad - cw : x + pad;
|
|
261
|
+
};
|
|
262
|
+
const text = (value, x, y, w, style, align) => {
|
|
263
|
+
if (!value)
|
|
264
|
+
return;
|
|
265
|
+
const isBold = style.bold === true;
|
|
266
|
+
const fs = style.fontSize ?? size;
|
|
267
|
+
const shown = (isBold ? bold : regular).fit(value, w - 2 * pad, fs);
|
|
268
|
+
if (!shown)
|
|
269
|
+
return;
|
|
270
|
+
const tx = place(align, x, w, shown.w);
|
|
271
|
+
const color = rgb(style.color) ?? '0 0 0';
|
|
272
|
+
const ty = n2(y + (rowH - fs) / 2 + fs * 0.22);
|
|
273
|
+
if (isBold && fakeBold)
|
|
274
|
+
ops.push(`q BT /F1 ${fs} Tf ${color} rg ${color} RG 2 Tr ${n2(fs * 0.025)} w ${n2(tx)} ${ty} Td ${shown.s} Tj ET Q`);
|
|
275
|
+
else
|
|
276
|
+
ops.push(`BT /${isBold ? 'F2' : 'F1'} ${fs} Tf ${color} rg ${n2(tx)} ${ty} Td ${shown.s} Tj ET`);
|
|
277
|
+
};
|
|
278
|
+
const hline = (x0, x1, y) => {
|
|
279
|
+
ops.push(`${n2(x0)} ${n2(y)} m ${n2(x1)} ${n2(y)} l S`);
|
|
280
|
+
};
|
|
281
|
+
const setBounds = (set) => set.length ? [Math.min(...set.map((c) => c.x)), Math.max(...set.map((c) => c.x + c.w))] : [margin, margin];
|
|
282
|
+
const drawHeader = (set, top) => {
|
|
283
|
+
const byIndex = new Map(set.map((c) => [c.index, c]));
|
|
284
|
+
let y = top;
|
|
285
|
+
headerRows.forEach((cells, level) => {
|
|
286
|
+
y = top - (level + 1) * rowH;
|
|
287
|
+
for (const h of cells) {
|
|
288
|
+
const covered = [];
|
|
289
|
+
for (let k = h.colIndex; k < h.colIndex + h.colSpan; k++) {
|
|
290
|
+
const c = byIndex.get(k);
|
|
291
|
+
if (c)
|
|
292
|
+
covered.push(c);
|
|
293
|
+
}
|
|
294
|
+
if (!covered.length)
|
|
295
|
+
continue;
|
|
296
|
+
const [x0, x1] = setBounds(covered);
|
|
297
|
+
const hgt = rowH * h.rowSpan;
|
|
298
|
+
const yb = top - (level + h.rowSpan) * rowH;
|
|
299
|
+
const style = h.style ? mergeStyle(theme.header, h.style) : theme.header;
|
|
300
|
+
fill(style.backgroundColor, x0, yb, x1 - x0, hgt);
|
|
301
|
+
text(h.text, x0, yb + (hgt - rowH) / 2, x1 - x0, style, style.textAlign ?? (h.leaf ? h.textAlign : 'center'));
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
return top - headerRows.length * rowH;
|
|
305
|
+
};
|
|
306
|
+
const aggregateLine = (set, cells, y, style, caption, depth) => {
|
|
307
|
+
const [x0, x1] = setBounds(set);
|
|
308
|
+
fill(style.backgroundColor, x0, y, x1 - x0, rowH);
|
|
309
|
+
const firstIndex = set[0]?.index ?? -1;
|
|
310
|
+
if (caption !== null && firstIndex === 0) {
|
|
311
|
+
// The caption spans until the first column with a caption aggregate.
|
|
312
|
+
let end = x1;
|
|
313
|
+
for (const c of set)
|
|
314
|
+
if (c.index > 0 && cells[c.index]) {
|
|
315
|
+
end = rtl ? x1 : c.x;
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
const indent = depth * size;
|
|
319
|
+
text(caption, rtl ? x0 : x0 + indent, y, end - x0 - indent, style, 'start');
|
|
320
|
+
}
|
|
321
|
+
for (const c of set) {
|
|
322
|
+
const a = cells[c.index];
|
|
323
|
+
if (!a || (caption !== null && c.index === 0))
|
|
324
|
+
continue;
|
|
325
|
+
const st = a.style ? mergeStyle(style, a.style) : style;
|
|
326
|
+
text(a.text, c.x, y, c.w, st, st.textAlign ?? (c.column.textAlign === 'start' && typeof a.value === 'number' ? 'end' : c.column.textAlign));
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
/** One data cell box (spanned boxes are opaque: they cover the grid lines below their first row). */
|
|
330
|
+
const cellBox = (c, line, x, y, w, h, opaque, used) => {
|
|
331
|
+
const cell = line.cells ? line.cells[c.index] : null;
|
|
332
|
+
const style = cell?.style ? mergeStyle(theme.row, cell.style) : theme.row;
|
|
333
|
+
if (opaque)
|
|
334
|
+
fill(style.backgroundColor ?? '#FFFFFF', x, y + 0.3, w, h - 0.6);
|
|
335
|
+
else if (cell?.style?.backgroundColor)
|
|
336
|
+
fill(cell.style.backgroundColor, x, y, w, h);
|
|
337
|
+
const align = style.textAlign ?? c.column.textAlign;
|
|
338
|
+
const pic = line.pictures?.[c.index];
|
|
339
|
+
if (pic && cell?.image) {
|
|
340
|
+
// Scaled to fit the cell box, aspect kept, never enlarged.
|
|
341
|
+
const iw = cell.image.width * 0.75;
|
|
342
|
+
const ih = cell.image.height * 0.75;
|
|
343
|
+
const k = Math.min(1, Math.max(0, w - 2 * pad) / iw, Math.max(0, h - 2 * pad) / ih);
|
|
344
|
+
const dw = iw * k;
|
|
345
|
+
const dh = ih * k;
|
|
346
|
+
const ix = place(align, x, w, dw);
|
|
347
|
+
if (dw > 0 && dh > 0) {
|
|
348
|
+
ops.push(imageOp(pic, dw, dh, ix, y + (h - dh) / 2));
|
|
349
|
+
used.add(pic);
|
|
350
|
+
}
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
// Indent from the start edge: the pivot row header level (`style.indent`) and,
|
|
354
|
+
// in tree mode, the row depth in the tree column.
|
|
355
|
+
const depth = c.index === model.tree?.column ? line.row.depth : 0;
|
|
356
|
+
const indent = ((style.indent ?? 0) + depth) * size;
|
|
357
|
+
text(cell ? cell.text : c.column.text(line.row.row), rtl ? x : x + indent, y + (h - rowH) / 2, w - indent, style, align);
|
|
358
|
+
};
|
|
359
|
+
const drawLine = (set, line, y, h, used, sp, i, hs, late) => {
|
|
360
|
+
const [x0, x1] = setBounds(set);
|
|
361
|
+
if (line.kind === 'text') {
|
|
362
|
+
text(line.text, x0 + (line.depth + 1) * size, y, x1 - x0 - (line.depth + 1) * size, theme.row, 'start');
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (line.kind === 'nested') {
|
|
366
|
+
const m = line.model;
|
|
367
|
+
const indent = (line.depth + 1) * size;
|
|
368
|
+
const w = (x1 - x0 - indent) / Math.max(1, m.columns.length);
|
|
369
|
+
if (line.index < 0) {
|
|
370
|
+
const cells = m.headerRows[m.headerRows.length + line.index] ?? [];
|
|
371
|
+
for (const h of cells)
|
|
372
|
+
text(h.text, x0 + indent + h.colIndex * w, y, w * h.colSpan, theme.header, 'start');
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const r = m.getRow(line.index);
|
|
376
|
+
if (r.kind === 'data')
|
|
377
|
+
m.columns.forEach((c, k) => text(m.cell(r, c).text, x0 + indent + k * w, y, w, theme.row, c.textAlign));
|
|
378
|
+
else if (r.kind === 'group')
|
|
379
|
+
text(r.caption, x0 + indent, y, x1 - x0 - indent, theme.groupCaption, 'start');
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const row = line.row;
|
|
383
|
+
switch (row.kind) {
|
|
384
|
+
case 'data': {
|
|
385
|
+
fill(theme.row.backgroundColor, x0, y, x1 - x0, h);
|
|
386
|
+
for (let j = 0; j < set.length; j++) {
|
|
387
|
+
const c = set[j];
|
|
388
|
+
const k = c.index;
|
|
389
|
+
const cs = sp ? sp[2 * k] : 1;
|
|
390
|
+
if (!cs)
|
|
391
|
+
continue;
|
|
392
|
+
const rs = sp ? sp[2 * k + 1] : 1;
|
|
393
|
+
let bx = c.x;
|
|
394
|
+
let bw = c.w;
|
|
395
|
+
for (let q = 1; q < cs; q++) {
|
|
396
|
+
const o = set[j + q];
|
|
397
|
+
if (o?.index !== k + q)
|
|
398
|
+
break;
|
|
399
|
+
bw += o.w;
|
|
400
|
+
bx = Math.min(bx, o.x);
|
|
401
|
+
}
|
|
402
|
+
let bh = h;
|
|
403
|
+
for (let q = 1; q < rs; q++)
|
|
404
|
+
bh += hs[i + q];
|
|
405
|
+
if (rs > 1)
|
|
406
|
+
late.push(() => cellBox(c, line, bx, y + h - bh, bw, bh, true, used));
|
|
407
|
+
else
|
|
408
|
+
cellBox(c, line, bx, y, bw, h, false, used);
|
|
409
|
+
}
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
case 'group':
|
|
413
|
+
aggregateLine(set, row.aggregates, y, theme.groupCaption, row.caption, row.depth);
|
|
414
|
+
return;
|
|
415
|
+
case 'groupFooter':
|
|
416
|
+
case 'aggregate':
|
|
417
|
+
aggregateLine(set, row.aggregates, y, theme.aggregate, null, 0);
|
|
418
|
+
return;
|
|
419
|
+
default:
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
// --- objects -----------------------------------------------------------------
|
|
424
|
+
const objects = [];
|
|
425
|
+
const offsets = [];
|
|
426
|
+
const addObject = (id, body) => {
|
|
427
|
+
objects[id] = typeof body === 'string' ? latin1(`${id} 0 obj\n${body}\nendobj\n`) : body;
|
|
428
|
+
};
|
|
429
|
+
const streamObject = async (id, content, dict = '') => {
|
|
430
|
+
const raw = typeof content === 'string' ? latin1(content) : content;
|
|
431
|
+
const packed = compress ? await deflate(raw, 'deflate') : null;
|
|
432
|
+
const data = packed ?? raw;
|
|
433
|
+
const head = latin1(`${id} 0 obj\n<< /Length ${data.length}${packed ? ' /Filter /FlateDecode' : ''}${dict ? ` ${dict}` : ''} >>\nstream\n`);
|
|
434
|
+
objects[id] = concat([head, data, latin1('\nendstream\nendobj\n')]);
|
|
435
|
+
};
|
|
436
|
+
// --- cell hook: cells, pictures, and line heights ---------------------------
|
|
437
|
+
// With a `cell` hook, data lines are measured while they are paginated (the
|
|
438
|
+
// hook runs once per cell); a line holding an image grows to the image
|
|
439
|
+
// height, so the page count is known only at the end (also with
|
|
440
|
+
// `appendImages`): the page header, footer, and page objects are written
|
|
441
|
+
// then, for every page.
|
|
442
|
+
const hooked = model.hasCellHook;
|
|
443
|
+
const capacity = perPage * rowH;
|
|
444
|
+
let pictures = null;
|
|
445
|
+
const pictureList = [];
|
|
446
|
+
/** A new image XObject (image cells and `appendImages`), or null when the image cannot be embedded. */
|
|
447
|
+
const picture = async (image) => {
|
|
448
|
+
const { pdfImage } = await import('./pdf-images.js');
|
|
449
|
+
const prepared = await pdfImage(image, helpers);
|
|
450
|
+
if (!prepared)
|
|
451
|
+
return null;
|
|
452
|
+
const p = { name: `Im${pictureList.length}`, image: prepared, id: 0 };
|
|
453
|
+
pictureList.push(p);
|
|
454
|
+
return p;
|
|
455
|
+
};
|
|
456
|
+
const measure = async (line) => {
|
|
457
|
+
if (line.kind !== 'row' || line.row.kind !== 'data')
|
|
458
|
+
return line;
|
|
459
|
+
const row = line.row;
|
|
460
|
+
const cells = model.columns.map((c) => model.cell(row, c));
|
|
461
|
+
for (const cell of cells)
|
|
462
|
+
if ((cell.colSpan ?? 1) > 1 || (cell.rowSpan ?? 1) > 1)
|
|
463
|
+
spanned = true;
|
|
464
|
+
let pics;
|
|
465
|
+
let h = rowH;
|
|
466
|
+
for (let k = 0; k < cells.length; k++) {
|
|
467
|
+
const image = cells[k].image;
|
|
468
|
+
if (!image)
|
|
469
|
+
continue;
|
|
470
|
+
const pic = await (pictures ??= new ImageStore()).get(image.data, () => picture(image));
|
|
471
|
+
if (!pic)
|
|
472
|
+
continue;
|
|
473
|
+
(pics ??= new Array(cells.length).fill(null))[k] = pic;
|
|
474
|
+
const iw = image.width * 0.75;
|
|
475
|
+
const fitW = Math.max(1, all[k].w - 2 * pad);
|
|
476
|
+
h = Math.max(h, Math.min(capacity, image.height * 0.75 * Math.min(1, fitW / iw) + 2 * pad));
|
|
477
|
+
}
|
|
478
|
+
return { kind: 'row', row, cells, ...(pics ? { pictures: pics } : {}), h };
|
|
479
|
+
};
|
|
480
|
+
// --- spans: per data line of a page, [colSpan, rowSpan] per column (0 = covered) ---
|
|
481
|
+
const merge = options.mergeRepeatedValues === true;
|
|
482
|
+
let spanned = false;
|
|
483
|
+
let carry = [];
|
|
484
|
+
const n = model.columns.length;
|
|
485
|
+
const textAt = (l, c) => l.kind === 'row' && l.cells ? l.cells[c].text : model.columns[c].text(l.row.row);
|
|
486
|
+
const planPage = (buffer) => {
|
|
487
|
+
if (!merge && !spanned)
|
|
488
|
+
return null;
|
|
489
|
+
const plan = buffer.map((l) => (l.kind === 'row' && l.row.kind === 'data' ? new Array(2 * n).fill(1) : null));
|
|
490
|
+
const next = [];
|
|
491
|
+
const first = buffer[0];
|
|
492
|
+
// A row span cut by the page break draws its anchor cell again on this page.
|
|
493
|
+
if (first?.kind === 'row' && first.cells && carry.length)
|
|
494
|
+
buffer[0] = { ...first, cells: first.cells.map((cell, c) => carry[c]?.[2] ?? cell) };
|
|
495
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
496
|
+
const p = plan[i];
|
|
497
|
+
if (!p)
|
|
498
|
+
continue;
|
|
499
|
+
const line = buffer[i];
|
|
500
|
+
for (let c = 0; c < n; c++) {
|
|
501
|
+
if (!p[2 * c])
|
|
502
|
+
continue;
|
|
503
|
+
const held = i ? undefined : carry[c];
|
|
504
|
+
const cell = (held?.[2] ?? line.cells?.[c]);
|
|
505
|
+
const cs = Math.max(1, Math.min(n - c, held ? held[1] : (cell?.colSpan ?? 1)));
|
|
506
|
+
const rs = held ? held[0] : (cell?.rowSpan ?? 1);
|
|
507
|
+
let fit = 1;
|
|
508
|
+
if (rs > 1) {
|
|
509
|
+
while (fit < rs && plan[i + fit])
|
|
510
|
+
fit++;
|
|
511
|
+
if (fit < rs && i + fit === buffer.length)
|
|
512
|
+
next[c] = [rs - fit, cs, cell];
|
|
513
|
+
}
|
|
514
|
+
else if (merge && cs === 1) {
|
|
515
|
+
const t = textAt(line, c);
|
|
516
|
+
while (plan[i + fit]?.[2 * c] && textAt(buffer[i + fit], c) === t)
|
|
517
|
+
fit++;
|
|
518
|
+
}
|
|
519
|
+
for (let k = 0; k < fit; k++)
|
|
520
|
+
for (let j = 0; j < cs; j++)
|
|
521
|
+
if (k || j)
|
|
522
|
+
plan[i + k][2 * (c + j)] = 0;
|
|
523
|
+
p[2 * c] = cs;
|
|
524
|
+
p[2 * c + 1] = fit;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
carry = next;
|
|
528
|
+
return plan;
|
|
529
|
+
};
|
|
530
|
+
let pageNo = 0;
|
|
531
|
+
const iter = lines();
|
|
532
|
+
let buffer = [];
|
|
533
|
+
let carried = null;
|
|
534
|
+
// Pages are written once the page count is known; page `n` (from 1) is object 4 + 2n, its content stream the next.
|
|
535
|
+
const deferred = [];
|
|
536
|
+
const idOf = (page) => 4 + 2 * page;
|
|
537
|
+
const pageText = (value, page, pages) => value.replace(/\{page\}/g, String(page)).replace(/\{pages\}/g, String(pages));
|
|
538
|
+
const pageObject = (pageId, used) => {
|
|
539
|
+
const xobjects = used.size ? ` /XObject << ${[...used].map((p) => `/${p.name} ${p.id} 0 R`).join(' ')} >>` : '';
|
|
540
|
+
addObject(pageId, `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 ${n2(pageW)} ${n2(pageH)}] /Resources << /Font << /F1 3 0 R /F2 ${embedded && !embedded.bold ? 3 : 4} 0 R >>${xobjects} >> /Contents ${pageId + 1} 0 R >>`);
|
|
541
|
+
};
|
|
542
|
+
/** One page content stream: grid lines, header text, body, footer text. */
|
|
543
|
+
const pageStream = async (page, pages, body) => {
|
|
544
|
+
ops.length = 0;
|
|
545
|
+
ops.push('0.75 0.75 0.75 RG 0.5 w');
|
|
546
|
+
if (headerText)
|
|
547
|
+
text(pageText(headerText, page, pages), margin, pageH - margin - rowH, usableW, { bold: true }, 'start');
|
|
548
|
+
if (body)
|
|
549
|
+
ops.push(body);
|
|
550
|
+
if (footerText)
|
|
551
|
+
text(pageText(footerText, page, pages), margin, margin, usableW, {}, 'end');
|
|
552
|
+
await streamObject(idOf(page) + 1, ops.join('\n'));
|
|
553
|
+
};
|
|
554
|
+
for (let p = 0; hooked || p < bodyPages; p++) {
|
|
555
|
+
buffer = [];
|
|
556
|
+
if (hooked) {
|
|
557
|
+
let filled = 0;
|
|
558
|
+
for (;;) {
|
|
559
|
+
let line = carried;
|
|
560
|
+
carried = null;
|
|
561
|
+
if (!line) {
|
|
562
|
+
const next = iter.next();
|
|
563
|
+
if (next.done)
|
|
564
|
+
break;
|
|
565
|
+
line = await measure(next.value);
|
|
566
|
+
}
|
|
567
|
+
const h = (line.kind === 'row' && line.h) || rowH;
|
|
568
|
+
if (buffer.length && filled + h > capacity + 1e-6) {
|
|
569
|
+
carried = line;
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
buffer.push(line);
|
|
573
|
+
filled += h;
|
|
574
|
+
}
|
|
575
|
+
if (p > 0 && !buffer.length)
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
for (let k = 0; k < perPage; k++) {
|
|
580
|
+
const next = iter.next();
|
|
581
|
+
if (next.done)
|
|
582
|
+
break;
|
|
583
|
+
buffer.push(next.value);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
const plan = planPage(buffer);
|
|
587
|
+
const hs = buffer.map((line) => (line.kind === 'row' && line.h) || rowH);
|
|
588
|
+
for (const set of sets) {
|
|
589
|
+
pageNo++;
|
|
590
|
+
ops.length = 0;
|
|
591
|
+
const [x0, x1] = setBounds(set);
|
|
592
|
+
let y = drawHeader(set, pageTop);
|
|
593
|
+
if (headerRows.length)
|
|
594
|
+
hline(x0, x1, y);
|
|
595
|
+
const used = new Set();
|
|
596
|
+
const late = [];
|
|
597
|
+
buffer.forEach((line, i) => {
|
|
598
|
+
const h = hs[i];
|
|
599
|
+
y -= h;
|
|
600
|
+
drawLine(set, line, y, h, used, plan?.[i], i, hs, late);
|
|
601
|
+
hline(x0, x1, y);
|
|
602
|
+
});
|
|
603
|
+
for (const draw of late)
|
|
604
|
+
draw();
|
|
605
|
+
deferred.push({ page: pageNo, body: ops.join('\n'), used });
|
|
606
|
+
if (slicer.due())
|
|
607
|
+
await slicer.pause();
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
// Image pages after the table (`appendImages`), scaled to fit the page body.
|
|
611
|
+
for (const image of options.appendImages ?? []) {
|
|
612
|
+
const p = await picture(image);
|
|
613
|
+
if (!p)
|
|
614
|
+
continue;
|
|
615
|
+
// CSS px to pt (0.75), never enlarged.
|
|
616
|
+
const k = Math.min(0.75, usableW / image.width, (pageTop - pageBottom) / image.height);
|
|
617
|
+
const dw = image.width * k;
|
|
618
|
+
const dh = image.height * k;
|
|
619
|
+
deferred.push({ page: ++pageNo, body: imageOp(p, dw, dh, margin + (usableW - dw) / 2, pageTop - dh), used: new Set([p]) });
|
|
620
|
+
}
|
|
621
|
+
for (const d of deferred) {
|
|
622
|
+
await pageStream(d.page, pageNo, d.body);
|
|
623
|
+
if (slicer.due())
|
|
624
|
+
await slicer.pause();
|
|
625
|
+
}
|
|
626
|
+
addObject(1, '<< /Type /Catalog /Pages 2 0 R >>');
|
|
627
|
+
addObject(2, `<< /Type /Pages /Kids [${deferred.map((d) => `${idOf(d.page)} 0 R`).join(' ')}] /Count ${pageNo} >>`);
|
|
628
|
+
let next = idOf(pageNo + 1);
|
|
629
|
+
if (embedded) {
|
|
630
|
+
// Type0 fonts at 3 (regular) and 4 (bold, or the regular CIDFont when bold is synthetic); the rest follow the pages.
|
|
631
|
+
let reserved4 = embedded.bold === null;
|
|
632
|
+
const alloc = () => {
|
|
633
|
+
if (reserved4) {
|
|
634
|
+
reserved4 = false;
|
|
635
|
+
return 4;
|
|
636
|
+
}
|
|
637
|
+
return next++;
|
|
638
|
+
};
|
|
639
|
+
await embedded.regular.write(3, alloc, addObject, streamObject);
|
|
640
|
+
if (embedded.bold)
|
|
641
|
+
await embedded.bold.write(4, alloc, addObject, streamObject);
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
addObject(3, '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>');
|
|
645
|
+
addObject(4, '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding >>');
|
|
646
|
+
}
|
|
647
|
+
// Image XObjects (and soft masks) follow the pages and fonts.
|
|
648
|
+
const rawStream = (id, dict, data) => {
|
|
649
|
+
objects[id] = concat([latin1(`${id} 0 obj\n<< /Length ${data.length} ${dict} >>\nstream\n`), data, latin1('\nendstream\nendobj\n')]);
|
|
650
|
+
};
|
|
651
|
+
for (const pic of pictureList) {
|
|
652
|
+
pic.id = next++;
|
|
653
|
+
const mask = pic.image.smask;
|
|
654
|
+
const smask = mask ? next++ : 0;
|
|
655
|
+
rawStream(pic.id, `${pic.image.dict}${smask ? ` /SMask ${smask} 0 R` : ''}`, pic.image.data);
|
|
656
|
+
if (mask)
|
|
657
|
+
rawStream(smask, mask.dict, mask.data);
|
|
658
|
+
}
|
|
659
|
+
for (const d of deferred)
|
|
660
|
+
pageObject(idOf(d.page), d.used);
|
|
661
|
+
addObject(5, `<< /Producer ${literal(winAnsi('Salmon UI'))}${options.title ? ` /Title ${textString(options.title)}` : ''} >>`);
|
|
662
|
+
const out = [latin1('%PDF-1.4\n%âãÏÓ\n')];
|
|
663
|
+
let at = out[0].length;
|
|
664
|
+
for (let id = 1; id < objects.length; id++) {
|
|
665
|
+
const o = objects[id];
|
|
666
|
+
offsets[id] = at;
|
|
667
|
+
out.push(o);
|
|
668
|
+
at += o.length;
|
|
669
|
+
}
|
|
670
|
+
let xref = `xref\n0 ${objects.length}\n0000000000 65535 f \n`;
|
|
671
|
+
for (let id = 1; id < objects.length; id++)
|
|
672
|
+
xref += `${String(offsets[id]).padStart(10, '0')} 00000 n \n`;
|
|
673
|
+
xref += `trailer\n<< /Size ${objects.length} /Root 1 0 R /Info 5 0 R >>\nstartxref\n${at}\n%%EOF\n`;
|
|
674
|
+
out.push(latin1(xref));
|
|
675
|
+
return concat(out);
|
|
676
|
+
}
|
|
677
|
+
//# sourceMappingURL=pdf-writer.js.map
|