@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
package/dist/graphics.js
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
// `@salmonui/export/graphics`: standalone SVG drawings as
|
|
2
|
+
// vector PDF pages. The input is SVG markup (a visualization's `toBlob('svg')`
|
|
3
|
+
// text, or any server-rendered SVG with resolved colors), parsed here without
|
|
4
|
+
// a DOM, so the writer runs in browsers, workers and Node alike. Paths,
|
|
5
|
+
// rectangles, circles, ellipses, lines, polylines and polygons become PDF path
|
|
6
|
+
// operators (arcs as cubic Béziers, transforms applied), `<text>` / `<tspan>`
|
|
7
|
+
// become text operators in the PDF writer's fonts (Helvetica WinAnsi or an
|
|
8
|
+
// embedded TrueType `font`), rectangle and path clip paths clip, opacity uses
|
|
9
|
+
// graphics states, and gradients paint with their first stop. The page,
|
|
10
|
+
// fonts, `{page}` / `{pages}` header and footer and the object layout follow
|
|
11
|
+
// the Gantt PDF writer (`gantt.ts`), reusing the same helpers.
|
|
12
|
+
import { concat, createSlicer, defaultYield, deflate, latin1 } from './bytes.js';
|
|
13
|
+
import { helpers, helvetica, n2, PAGE_SIZES, textString } from './pdf-writer.js';
|
|
14
|
+
const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' };
|
|
15
|
+
const decode = (s) => s.replace(/&(#x[\da-f]+|#\d+|\w+);/gi, (m, e) => (e[0] === '#' ? String.fromCodePoint(e[1] === 'x' || e[1] === 'X' ? Number.parseInt(e.slice(2), 16) : Number(e.slice(1))) : (ENTITIES[e] ?? m)));
|
|
16
|
+
/** A small, forgiving XML reader for SVG (no DTDs, no namespaces beyond prefixes). */
|
|
17
|
+
export function parseSvgXml(src) {
|
|
18
|
+
const root = { tag: '#', attrs: {}, children: [] };
|
|
19
|
+
const stack = [root];
|
|
20
|
+
const re = /<!--[\s\S]*?-->|<\?[\s\S]*?\?>|<!\[CDATA\[([\s\S]*?)\]\]>|<![^>]*>|<\/([^\s>]+)\s*>|<([^\s/>]+)((?:\s+[^\s=/>]+(?:\s*=\s*(?:"[^"]*"|'[^']*'))?)*)\s*(\/?)>|([^<]+)/g;
|
|
21
|
+
for (let m = re.exec(src); m; m = re.exec(src)) {
|
|
22
|
+
const top = stack[stack.length - 1];
|
|
23
|
+
if (m[1] !== undefined)
|
|
24
|
+
top.children.push(m[1]);
|
|
25
|
+
else if (m[2] !== undefined) {
|
|
26
|
+
if (stack.length > 1)
|
|
27
|
+
stack.pop();
|
|
28
|
+
}
|
|
29
|
+
else if (m[3] !== undefined) {
|
|
30
|
+
const attrs = {};
|
|
31
|
+
for (const a of (m[4] ?? '').matchAll(/([^\s=]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g))
|
|
32
|
+
attrs[a[1]] = decode(a[2] ?? a[3] ?? '');
|
|
33
|
+
const el = { tag: m[3].replace(/^.*:/, ''), attrs, children: [] };
|
|
34
|
+
top.children.push(el);
|
|
35
|
+
if (!m[5])
|
|
36
|
+
stack.push(el);
|
|
37
|
+
}
|
|
38
|
+
else if (m[6] !== undefined)
|
|
39
|
+
top.children.push(decode(m[6]));
|
|
40
|
+
}
|
|
41
|
+
return root.children.find((c) => typeof c !== 'string' && c.tag === 'svg') ?? null;
|
|
42
|
+
}
|
|
43
|
+
const NAMED = { black: '000', white: 'fff', red: 'f00', green: '008000', blue: '00f', gray: '808080', grey: '808080', silver: 'c0c0c0', yellow: 'ff0', orange: 'ffa500' };
|
|
44
|
+
const clamp01 = (v) => Math.min(1, Math.max(0, v));
|
|
45
|
+
const gamma = (v) => (v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055);
|
|
46
|
+
function oklab(l, a, b, alpha) {
|
|
47
|
+
const L = (l + 0.3963377774 * a + 0.2158037573 * b) ** 3;
|
|
48
|
+
const M = (l - 0.1055613458 * a - 0.0638541728 * b) ** 3;
|
|
49
|
+
const S = (l - 0.0894841775 * a - 1.291485548 * b) ** 3;
|
|
50
|
+
return [clamp01(gamma(4.0767416621 * L - 3.3077115913 * M + 0.2309699292 * S)), clamp01(gamma(-1.2684380046 * L + 2.6097574011 * M - 0.3413193965 * S)), clamp01(gamma(-0.0041960863 * L - 0.7034186147 * M + 1.707614701 * S)), alpha];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Parses a CSS color as browsers serialize computed values (hex, `rgb()`,
|
|
54
|
+
* `hsl()`, `oklch()`, `oklab()`, `color(srgb ...)`, a few names); null for
|
|
55
|
+
* `none`, `transparent` and anything unknown.
|
|
56
|
+
*/
|
|
57
|
+
export function parseColor(input) {
|
|
58
|
+
if (!input)
|
|
59
|
+
return null;
|
|
60
|
+
let s = input.trim().toLowerCase();
|
|
61
|
+
const ld = /^light-dark\(\s*([^,]+(?:\([^)]*\))?)\s*,/.exec(s);
|
|
62
|
+
if (ld)
|
|
63
|
+
s = ld[1].trim();
|
|
64
|
+
const fallback = /^var\([^,]+,\s*(.+)\)$/.exec(s);
|
|
65
|
+
if (fallback)
|
|
66
|
+
return parseColor(fallback[1]);
|
|
67
|
+
if (NAMED[s])
|
|
68
|
+
s = `#${NAMED[s]}`;
|
|
69
|
+
if (s[0] === '#') {
|
|
70
|
+
let h = s.slice(1);
|
|
71
|
+
if (h.length === 3 || h.length === 4)
|
|
72
|
+
h = [...h].map((c) => c + c).join('');
|
|
73
|
+
if (!/^[\da-f]{6}([\da-f]{2})?$/.test(h))
|
|
74
|
+
return null;
|
|
75
|
+
const n = (i) => Number.parseInt(h.slice(i, i + 2), 16) / 255;
|
|
76
|
+
return [n(0), n(2), n(4), h.length === 8 ? n(6) : 1];
|
|
77
|
+
}
|
|
78
|
+
const fn = /^([a-z]+)\((.*)\)$/.exec(s);
|
|
79
|
+
if (!fn)
|
|
80
|
+
return null;
|
|
81
|
+
const parts = fn[2].replace(/[,/]/g, ' ').trim().split(/\s+/);
|
|
82
|
+
const num = (v, scale = 1) => (v === undefined || v === 'none' ? 0 : v.endsWith('%') ? (Number.parseFloat(v) / 100) * scale : Number.parseFloat(v));
|
|
83
|
+
const alpha = parts.length > 3 ? clamp01(num(parts[parts.length - 1], 1)) : 1;
|
|
84
|
+
switch (fn[1]) {
|
|
85
|
+
case 'rgb':
|
|
86
|
+
case 'rgba':
|
|
87
|
+
return [clamp01(num(parts[0], 255) / 255), clamp01(num(parts[1], 255) / 255), clamp01(num(parts[2], 255) / 255), alpha];
|
|
88
|
+
case 'hsl':
|
|
89
|
+
case 'hsla': {
|
|
90
|
+
const h = num(parts[0]) / 360;
|
|
91
|
+
const sat = num(parts[1], 1);
|
|
92
|
+
const l = num(parts[2], 1);
|
|
93
|
+
const q = l < 0.5 ? l * (1 + sat) : l + sat - l * sat;
|
|
94
|
+
const p = 2 * l - q;
|
|
95
|
+
const f = (t) => {
|
|
96
|
+
const u = t < 0 ? t + 1 : t > 1 ? t - 1 : t;
|
|
97
|
+
return u < 1 / 6 ? p + (q - p) * 6 * u : u < 1 / 2 ? q : u < 2 / 3 ? p + (q - p) * (2 / 3 - u) * 6 : p;
|
|
98
|
+
};
|
|
99
|
+
return [f(h + 1 / 3), f(h), f(h - 1 / 3), alpha];
|
|
100
|
+
}
|
|
101
|
+
case 'oklch': {
|
|
102
|
+
const c = num(parts[1], 0.4);
|
|
103
|
+
const h = (num(parts[2]) * Math.PI) / 180;
|
|
104
|
+
return oklab(num(parts[0], 1), c * Math.cos(h), c * Math.sin(h), alpha);
|
|
105
|
+
}
|
|
106
|
+
case 'oklab':
|
|
107
|
+
return oklab(num(parts[0], 1), num(parts[1], 0.4), num(parts[2], 0.4), alpha);
|
|
108
|
+
case 'color':
|
|
109
|
+
return parts[0] === 'srgb' ? [clamp01(num(parts[1], 1)), clamp01(num(parts[2], 1)), clamp01(num(parts[3], 1)), parts.length > 4 ? clamp01(num(parts[4], 1)) : 1] : null;
|
|
110
|
+
default:
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const ID = [1, 0, 0, 1, 0, 0];
|
|
115
|
+
const mul = (m, n) => [m[0] * n[0] + m[2] * n[1], m[1] * n[0] + m[3] * n[1], m[0] * n[2] + m[2] * n[3], m[1] * n[2] + m[3] * n[3], m[0] * n[4] + m[2] * n[5] + m[4], m[1] * n[4] + m[3] * n[5] + m[5]];
|
|
116
|
+
const apply = (m, x, y) => [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
|
|
117
|
+
const nums = (s) => (s ? (s.match(/[-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) ?? []).map(Number) : []);
|
|
118
|
+
function parseTransform(s) {
|
|
119
|
+
let m = ID;
|
|
120
|
+
if (!s)
|
|
121
|
+
return m;
|
|
122
|
+
for (const t of s.matchAll(/(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/g)) {
|
|
123
|
+
const v = nums(t[2]);
|
|
124
|
+
const [a = 0, b, c] = v;
|
|
125
|
+
let n = ID;
|
|
126
|
+
if (t[1] === 'matrix' && v.length === 6)
|
|
127
|
+
n = v;
|
|
128
|
+
else if (t[1] === 'translate')
|
|
129
|
+
n = [1, 0, 0, 1, a, b ?? 0];
|
|
130
|
+
else if (t[1] === 'scale')
|
|
131
|
+
n = [a, 0, 0, b ?? a, 0, 0];
|
|
132
|
+
else if (t[1] === 'rotate') {
|
|
133
|
+
const r = (a * Math.PI) / 180;
|
|
134
|
+
const cos = Math.cos(r);
|
|
135
|
+
const sin = Math.sin(r);
|
|
136
|
+
n = [cos, sin, -sin, cos, 0, 0];
|
|
137
|
+
if (b !== undefined)
|
|
138
|
+
n = mul(mul([1, 0, 0, 1, b, c ?? 0], n), [1, 0, 0, 1, -b, -(c ?? 0)]);
|
|
139
|
+
}
|
|
140
|
+
else if (t[1] === 'skewX')
|
|
141
|
+
n = [1, 0, Math.tan((a * Math.PI) / 180), 1, 0, 0];
|
|
142
|
+
else if (t[1] === 'skewY')
|
|
143
|
+
n = [1, Math.tan((a * Math.PI) / 180), 0, 1, 0, 0];
|
|
144
|
+
m = mul(m, n);
|
|
145
|
+
}
|
|
146
|
+
return m;
|
|
147
|
+
}
|
|
148
|
+
/** SVG path data to absolute PDF path operators under `m` (arcs and quadratics become cubics). */
|
|
149
|
+
export function pathOps(d, m) {
|
|
150
|
+
const tokens = d.match(/[a-df-z]|[-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) ?? [];
|
|
151
|
+
const out = [];
|
|
152
|
+
const pt = (x, y) => {
|
|
153
|
+
const [px, py] = apply(m, x, y);
|
|
154
|
+
return `${n2(px)} ${n2(py)}`;
|
|
155
|
+
};
|
|
156
|
+
let i = 0;
|
|
157
|
+
let cmd = '';
|
|
158
|
+
let x = 0;
|
|
159
|
+
let y = 0;
|
|
160
|
+
let sx = 0;
|
|
161
|
+
let sy = 0;
|
|
162
|
+
let cx = 0;
|
|
163
|
+
let cy = 0;
|
|
164
|
+
let prev = '';
|
|
165
|
+
const next = () => Number(tokens[i++]);
|
|
166
|
+
const cubic = (x1, y1, x2, y2, ex, ey) => {
|
|
167
|
+
out.push(`${pt(x1, y1)} ${pt(x2, y2)} ${pt(ex, ey)} c`);
|
|
168
|
+
cx = x2;
|
|
169
|
+
cy = y2;
|
|
170
|
+
x = ex;
|
|
171
|
+
y = ey;
|
|
172
|
+
};
|
|
173
|
+
while (i < tokens.length) {
|
|
174
|
+
const t = tokens[i];
|
|
175
|
+
if (/[a-z]/i.test(t)) {
|
|
176
|
+
cmd = t;
|
|
177
|
+
i++;
|
|
178
|
+
if (cmd === 'z' || cmd === 'Z') {
|
|
179
|
+
out.push('h');
|
|
180
|
+
x = sx;
|
|
181
|
+
y = sy;
|
|
182
|
+
prev = 'z';
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else if (!cmd)
|
|
187
|
+
break;
|
|
188
|
+
const rel = cmd === cmd.toLowerCase();
|
|
189
|
+
const ox = rel ? x : 0;
|
|
190
|
+
const oy = rel ? y : 0;
|
|
191
|
+
const C = cmd.toUpperCase();
|
|
192
|
+
if (C === 'M') {
|
|
193
|
+
x = ox + next();
|
|
194
|
+
y = oy + next();
|
|
195
|
+
sx = x;
|
|
196
|
+
sy = y;
|
|
197
|
+
out.push(`${pt(x, y)} m`);
|
|
198
|
+
cmd = rel ? 'l' : 'L';
|
|
199
|
+
}
|
|
200
|
+
else if (C === 'L') {
|
|
201
|
+
x = ox + next();
|
|
202
|
+
y = oy + next();
|
|
203
|
+
out.push(`${pt(x, y)} l`);
|
|
204
|
+
}
|
|
205
|
+
else if (C === 'H') {
|
|
206
|
+
x = ox + next();
|
|
207
|
+
out.push(`${pt(x, y)} l`);
|
|
208
|
+
}
|
|
209
|
+
else if (C === 'V') {
|
|
210
|
+
y = oy + next();
|
|
211
|
+
out.push(`${pt(x, y)} l`);
|
|
212
|
+
}
|
|
213
|
+
else if (C === 'C') {
|
|
214
|
+
const v = [next(), next(), next(), next(), next(), next()];
|
|
215
|
+
cubic(ox + v[0], oy + v[1], ox + v[2], oy + v[3], ox + v[4], oy + v[5]);
|
|
216
|
+
}
|
|
217
|
+
else if (C === 'S') {
|
|
218
|
+
const v = [next(), next(), next(), next()];
|
|
219
|
+
const smooth = /[cs]/i.test(prev);
|
|
220
|
+
cubic(smooth ? 2 * x - cx : x, smooth ? 2 * y - cy : y, ox + v[0], oy + v[1], ox + v[2], oy + v[3]);
|
|
221
|
+
}
|
|
222
|
+
else if (C === 'Q' || C === 'T') {
|
|
223
|
+
const smooth = C === 'T' && /[qt]/i.test(prev);
|
|
224
|
+
const qx = C === 'Q' ? ox + next() : smooth ? 2 * x - cx : x;
|
|
225
|
+
const qy = C === 'Q' ? oy + next() : smooth ? 2 * y - cy : y;
|
|
226
|
+
const ex = ox + next();
|
|
227
|
+
const ey = oy + next();
|
|
228
|
+
const x0 = x;
|
|
229
|
+
const y0 = y;
|
|
230
|
+
cubic(x0 + (2 / 3) * (qx - x0), y0 + (2 / 3) * (qy - y0), ex + (2 / 3) * (qx - ex), ey + (2 / 3) * (qy - ey), ex, ey);
|
|
231
|
+
cx = qx;
|
|
232
|
+
cy = qy;
|
|
233
|
+
}
|
|
234
|
+
else if (C === 'A') {
|
|
235
|
+
const [rxIn, ryIn, rot, large, sweep] = [next(), next(), next(), next(), next()];
|
|
236
|
+
const ex = ox + next();
|
|
237
|
+
const ey = oy + next();
|
|
238
|
+
arc(x, y, Math.abs(rxIn), Math.abs(ryIn), rot, large !== 0, sweep !== 0, ex, ey, cubic);
|
|
239
|
+
x = ex;
|
|
240
|
+
y = ey;
|
|
241
|
+
}
|
|
242
|
+
else
|
|
243
|
+
break;
|
|
244
|
+
prev = C;
|
|
245
|
+
}
|
|
246
|
+
return out.join(' ');
|
|
247
|
+
}
|
|
248
|
+
/** SVG arc (F.6.5 endpoint to center) as cubic Béziers of at most 90 degrees. */
|
|
249
|
+
function arc(x1, y1, rx, ry, rotDeg, large, sweep, x2, y2, cubic) {
|
|
250
|
+
if (!rx || !ry || (x1 === x2 && y1 === y2)) {
|
|
251
|
+
cubic(x1, y1, x2, y2, x2, y2);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const phi = (rotDeg * Math.PI) / 180;
|
|
255
|
+
const cos = Math.cos(phi);
|
|
256
|
+
const sin = Math.sin(phi);
|
|
257
|
+
const dx = (x1 - x2) / 2;
|
|
258
|
+
const dy = (y1 - y2) / 2;
|
|
259
|
+
const xp = cos * dx + sin * dy;
|
|
260
|
+
const yp = -sin * dx + cos * dy;
|
|
261
|
+
const lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
|
|
262
|
+
if (lambda > 1) {
|
|
263
|
+
rx *= Math.sqrt(lambda);
|
|
264
|
+
ry *= Math.sqrt(lambda);
|
|
265
|
+
}
|
|
266
|
+
const num = rx * rx * ry * ry - rx * rx * yp * yp - ry * ry * xp * xp;
|
|
267
|
+
const k = (large === sweep ? -1 : 1) * Math.sqrt(Math.max(0, num / (rx * rx * yp * yp + ry * ry * xp * xp)));
|
|
268
|
+
const cxp = (k * rx * yp) / ry;
|
|
269
|
+
const cyp = (-k * ry * xp) / rx;
|
|
270
|
+
const ccx = cos * cxp - sin * cyp + (x1 + x2) / 2;
|
|
271
|
+
const ccy = sin * cxp + cos * cyp + (y1 + y2) / 2;
|
|
272
|
+
const ang = (ux, uy, vx, vy) => Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
273
|
+
const t1 = ang(1, 0, (xp - cxp) / rx, (yp - cyp) / ry);
|
|
274
|
+
let dt = ang((xp - cxp) / rx, (yp - cyp) / ry, (-xp - cxp) / rx, (-yp - cyp) / ry);
|
|
275
|
+
if (!sweep && dt > 0)
|
|
276
|
+
dt -= 2 * Math.PI;
|
|
277
|
+
if (sweep && dt < 0)
|
|
278
|
+
dt += 2 * Math.PI;
|
|
279
|
+
const segs = Math.ceil(Math.abs(dt) / (Math.PI / 2) - 1e-9);
|
|
280
|
+
const step = dt / segs;
|
|
281
|
+
const alpha = (4 / 3) * Math.tan(step / 4);
|
|
282
|
+
const at = (t) => {
|
|
283
|
+
const ct = Math.cos(t);
|
|
284
|
+
const st = Math.sin(t);
|
|
285
|
+
return [ccx + rx * ct * cos - ry * st * sin, ccy + rx * ct * sin + ry * st * cos, -rx * st * cos - ry * ct * sin, -rx * st * sin + ry * ct * cos];
|
|
286
|
+
};
|
|
287
|
+
for (let s = 0; s < segs; s++) {
|
|
288
|
+
const a = at(t1 + s * step);
|
|
289
|
+
const b = at(t1 + (s + 1) * step);
|
|
290
|
+
cubic(a[0] + alpha * a[2], a[1] + alpha * a[3], b[0] - alpha * b[2], b[1] - alpha * b[3], b[0], b[1]);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/** Path data of a basic shape element; null when it is not a shape. */
|
|
294
|
+
function shapeData(el, vw, vh) {
|
|
295
|
+
const a = el.attrs;
|
|
296
|
+
const len = (v, of) => (v === undefined ? 0 : v.endsWith('%') ? (Number.parseFloat(v) / 100) * of : Number.parseFloat(v) || 0);
|
|
297
|
+
switch (el.tag) {
|
|
298
|
+
case 'path':
|
|
299
|
+
return a['d'] ?? null;
|
|
300
|
+
case 'rect': {
|
|
301
|
+
const x = len(a['x'], vw);
|
|
302
|
+
const y = len(a['y'], vh);
|
|
303
|
+
const w = len(a['width'], vw);
|
|
304
|
+
const h = len(a['height'], vh);
|
|
305
|
+
if (w <= 0 || h <= 0)
|
|
306
|
+
return null;
|
|
307
|
+
const rx = Math.min(w / 2, len(a['rx'] ?? a['ry'], vw));
|
|
308
|
+
const ry = Math.min(h / 2, len(a['ry'] ?? a['rx'], vh));
|
|
309
|
+
if (!rx || !ry)
|
|
310
|
+
return `M${x},${y}H${x + w}V${y + h}H${x}Z`;
|
|
311
|
+
return `M${x + rx},${y}H${x + w - rx}A${rx},${ry} 0 0 1 ${x + w},${y + ry}V${y + h - ry}A${rx},${ry} 0 0 1 ${x + w - rx},${y + h}H${x + rx}A${rx},${ry} 0 0 1 ${x},${y + h - ry}V${y + ry}A${rx},${ry} 0 0 1 ${x + rx},${y}Z`;
|
|
312
|
+
}
|
|
313
|
+
case 'circle':
|
|
314
|
+
case 'ellipse': {
|
|
315
|
+
const cx = len(a['cx'], vw);
|
|
316
|
+
const cy = len(a['cy'], vh);
|
|
317
|
+
const rx = len(a['r'] ?? a['rx'], vw);
|
|
318
|
+
const ry = len(a['r'] ?? a['ry'], vh);
|
|
319
|
+
if (rx <= 0 || ry <= 0)
|
|
320
|
+
return null;
|
|
321
|
+
return `M${cx - rx},${cy}A${rx},${ry} 0 1 0 ${cx + rx},${cy}A${rx},${ry} 0 1 0 ${cx - rx},${cy}Z`;
|
|
322
|
+
}
|
|
323
|
+
case 'line':
|
|
324
|
+
return `M${len(a['x1'], vw)},${len(a['y1'], vh)}L${len(a['x2'], vw)},${len(a['y2'], vh)}`;
|
|
325
|
+
case 'polyline':
|
|
326
|
+
case 'polygon': {
|
|
327
|
+
const p = nums(a['points']);
|
|
328
|
+
if (p.length < 4)
|
|
329
|
+
return null;
|
|
330
|
+
let d = `M${p[0]},${p[1]}`;
|
|
331
|
+
for (let k = 2; k + 1 < p.length; k += 2)
|
|
332
|
+
d += `L${p[k]},${p[k + 1]}`;
|
|
333
|
+
return el.tag === 'polygon' ? `${d}Z` : d;
|
|
334
|
+
}
|
|
335
|
+
default:
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
const INHERITED = { fill: '#000', stroke: 'none', strokeWidth: 1, fillOpacity: 1, strokeOpacity: 1, opacity: 1, fontSize: 16, bold: false, anchor: 'start', baseline: 'auto', dash: 'none', cap: 'butt', join: 'miter', color: '#000', fillRule: 'nonzero' };
|
|
340
|
+
const SKIP = new Set(['defs', 'clipPath', 'mask', 'pattern', 'linearGradient', 'radialGradient', 'title', 'desc', 'style', 'metadata', 'foreignObject', 'symbol', 'marker', 'script']);
|
|
341
|
+
const size = (v, base) => {
|
|
342
|
+
const n = Number.parseFloat(v);
|
|
343
|
+
if (!Number.isFinite(n))
|
|
344
|
+
return base;
|
|
345
|
+
return v.endsWith('rem') ? n * 16 : v.endsWith('em') ? n * base : v.endsWith('%') ? (n / 100) * base : v.endsWith('pt') ? n / 0.75 : n;
|
|
346
|
+
};
|
|
347
|
+
function styleOf(el, parent) {
|
|
348
|
+
const decl = { ...el.attrs };
|
|
349
|
+
for (const part of (el.attrs['style'] ?? '').split(';')) {
|
|
350
|
+
const at = part.indexOf(':');
|
|
351
|
+
if (at > 0)
|
|
352
|
+
decl[part.slice(0, at).trim().toLowerCase()] = part.slice(at + 1).trim();
|
|
353
|
+
}
|
|
354
|
+
const g = (k) => {
|
|
355
|
+
const v = decl[k];
|
|
356
|
+
return v === undefined || v === 'inherit' ? undefined : v.replace(/\s*!important$/, '');
|
|
357
|
+
};
|
|
358
|
+
const color = g('color') ?? parent.color;
|
|
359
|
+
const paint = (v, inherited) => (v === undefined ? inherited : v.toLowerCase() === 'currentcolor' ? color : v);
|
|
360
|
+
const weight = g('font-weight');
|
|
361
|
+
const st = {
|
|
362
|
+
fill: paint(g('fill'), parent.fill),
|
|
363
|
+
stroke: paint(g('stroke'), parent.stroke),
|
|
364
|
+
strokeWidth: g('stroke-width') !== undefined ? size(g('stroke-width'), 1) : parent.strokeWidth,
|
|
365
|
+
fillOpacity: g('fill-opacity') !== undefined ? Number(g('fill-opacity')) : parent.fillOpacity,
|
|
366
|
+
strokeOpacity: g('stroke-opacity') !== undefined ? Number(g('stroke-opacity')) : parent.strokeOpacity,
|
|
367
|
+
opacity: parent.opacity * (g('opacity') !== undefined ? Number(g('opacity')) : 1),
|
|
368
|
+
fontSize: g('font-size') !== undefined ? size(g('font-size'), parent.fontSize) : parent.fontSize,
|
|
369
|
+
bold: weight !== undefined ? weight === 'bold' || weight === 'bolder' || Number(weight) >= 600 : parent.bold,
|
|
370
|
+
anchor: g('text-anchor') ?? parent.anchor,
|
|
371
|
+
baseline: g('dominant-baseline') ?? parent.baseline,
|
|
372
|
+
dash: g('stroke-dasharray') ?? parent.dash,
|
|
373
|
+
cap: g('stroke-linecap') ?? parent.cap,
|
|
374
|
+
join: g('stroke-linejoin') ?? parent.join,
|
|
375
|
+
color,
|
|
376
|
+
fillRule: g('fill-rule') ?? parent.fillRule,
|
|
377
|
+
};
|
|
378
|
+
return { style: st, hidden: g('display') === 'none' || g('visibility') === 'hidden' || g('visibility') === 'collapse' };
|
|
379
|
+
}
|
|
380
|
+
function paintOf(value, ids) {
|
|
381
|
+
const url = /^url\(\s*['"]?#([^'")]+)['"]?\s*\)/.exec(value.trim());
|
|
382
|
+
if (url) {
|
|
383
|
+
// Gradients paint with their first stop (a vector approximation).
|
|
384
|
+
const stop = ids.get(url[1])?.children.find((c) => typeof c !== 'string' && c.tag === 'stop');
|
|
385
|
+
if (!stop)
|
|
386
|
+
return null;
|
|
387
|
+
const decl = { ...stop.attrs };
|
|
388
|
+
for (const part of (stop.attrs['style'] ?? '').split(';')) {
|
|
389
|
+
const at = part.indexOf(':');
|
|
390
|
+
if (at > 0)
|
|
391
|
+
decl[part.slice(0, at).trim()] = part.slice(at + 1).trim();
|
|
392
|
+
}
|
|
393
|
+
const c = parseColor(decl['stop-color'] ?? '#000');
|
|
394
|
+
return c ? [c[0], c[1], c[2], c[3] * (decl['stop-opacity'] !== undefined ? Number(decl['stop-opacity']) : 1)] : null;
|
|
395
|
+
}
|
|
396
|
+
return parseColor(value);
|
|
397
|
+
}
|
|
398
|
+
const rgbOp = (c, op) => `${n2r(c[0])} ${n2r(c[1])} ${n2r(c[2])} ${op}`;
|
|
399
|
+
const n2r = (v) => (Math.round(v * 1000) / 1000).toString();
|
|
400
|
+
const scaleOf = (m) => Math.sqrt(Math.abs(m[0] * m[3] - m[1] * m[2]));
|
|
401
|
+
/** The graphics state name for a fill / stroke alpha pair (`/GSn gs`). */
|
|
402
|
+
function alphaOp(cv, fa, sa) {
|
|
403
|
+
if (fa >= 0.999 && sa >= 0.999)
|
|
404
|
+
return '';
|
|
405
|
+
const key = `${n2r(fa)} ${n2r(sa)}`;
|
|
406
|
+
let name = cv.states.get(key);
|
|
407
|
+
if (!name) {
|
|
408
|
+
name = `GS${cv.states.size}`;
|
|
409
|
+
cv.states.set(key, name);
|
|
410
|
+
}
|
|
411
|
+
return `/${name} gs `;
|
|
412
|
+
}
|
|
413
|
+
function draw(el, m, parent, cv, vw, vh) {
|
|
414
|
+
if (SKIP.has(el.tag))
|
|
415
|
+
return;
|
|
416
|
+
const { style, hidden } = styleOf(el, parent);
|
|
417
|
+
if (hidden)
|
|
418
|
+
return;
|
|
419
|
+
let ctm = mul(m, parseTransform(el.attrs['transform']));
|
|
420
|
+
let w = vw;
|
|
421
|
+
let h = vh;
|
|
422
|
+
if (el.tag === 'svg') {
|
|
423
|
+
// Nested viewport (combined exports place each drawing at x / y).
|
|
424
|
+
const x = Number.parseFloat(el.attrs['x'] ?? '0') || 0;
|
|
425
|
+
const y = Number.parseFloat(el.attrs['y'] ?? '0') || 0;
|
|
426
|
+
ctm = mul(ctm, [1, 0, 0, 1, x, y]);
|
|
427
|
+
const box = nums(el.attrs['viewBox']);
|
|
428
|
+
w = Number.parseFloat(el.attrs['width'] ?? '') || box[2] || vw;
|
|
429
|
+
h = Number.parseFloat(el.attrs['height'] ?? '') || box[3] || vh;
|
|
430
|
+
if (box.length === 4 && box[2] && box[3])
|
|
431
|
+
ctm = mul(ctm, [w / box[2], 0, 0, h / box[3], -box[0] * (w / box[2]), -box[1] * (h / box[3])]);
|
|
432
|
+
if (box.length === 4 && box[2] && box[3]) {
|
|
433
|
+
w = box[2];
|
|
434
|
+
h = box[3];
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
const clip = /^url\(\s*['"]?#([^'")]+)['"]?\s*\)/.exec(el.attrs['clip-path'] ?? '');
|
|
438
|
+
const clipEl = clip ? cv.ids.get(clip[1]) : undefined;
|
|
439
|
+
let saved = false;
|
|
440
|
+
if (clipEl) {
|
|
441
|
+
const parts = clipEl.children.filter((c) => typeof c !== 'string').map((c) => {
|
|
442
|
+
const d = shapeData(c, w, h);
|
|
443
|
+
return d ? pathOps(d, mul(ctm, parseTransform(c.attrs['transform']))) : '';
|
|
444
|
+
});
|
|
445
|
+
if (parts.some(Boolean)) {
|
|
446
|
+
cv.ops.push(`q ${parts.join(' ')} W n`);
|
|
447
|
+
saved = true;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (el.tag === 'text')
|
|
451
|
+
drawText(el, ctm, style, cv);
|
|
452
|
+
else {
|
|
453
|
+
const d = shapeData(el, w, h);
|
|
454
|
+
if (d)
|
|
455
|
+
drawShape(d, ctm, style, cv, el.tag === 'line' || el.tag === 'polyline');
|
|
456
|
+
else
|
|
457
|
+
for (const c of el.children)
|
|
458
|
+
if (typeof c !== 'string')
|
|
459
|
+
draw(c, ctm, style, cv, w, h);
|
|
460
|
+
}
|
|
461
|
+
if (saved)
|
|
462
|
+
cv.ops.push('Q');
|
|
463
|
+
}
|
|
464
|
+
function drawShape(d, m, st, cv, open) {
|
|
465
|
+
const fill = open ? null : paintOf(st.fill, cv.ids);
|
|
466
|
+
const stroke = st.stroke === 'none' || st.strokeWidth <= 0 ? null : paintOf(st.stroke, cv.ids);
|
|
467
|
+
const fa = fill ? fill[3] * st.fillOpacity * st.opacity : 0;
|
|
468
|
+
const sa = stroke ? stroke[3] * st.strokeOpacity * st.opacity : 0;
|
|
469
|
+
if (fa <= 0.001 && sa <= 0.001)
|
|
470
|
+
return;
|
|
471
|
+
const ops = pathOps(d, m);
|
|
472
|
+
if (!ops)
|
|
473
|
+
return;
|
|
474
|
+
const k = scaleOf(m);
|
|
475
|
+
const dash = st.dash === 'none' ? [] : nums(st.dash).map((v) => n2(v * k));
|
|
476
|
+
const parts = [`q ${alphaOp(cv, fa > 0.001 ? fa : 1, sa > 0.001 ? sa : 1)}`];
|
|
477
|
+
if (fa > 0.001 && fill)
|
|
478
|
+
parts.push(`${rgbOp(fill, 'rg')} `);
|
|
479
|
+
if (sa > 0.001 && stroke)
|
|
480
|
+
parts.push(`${rgbOp(stroke, 'RG')} ${n2(st.strokeWidth * k)} w [${dash.join(' ')}] 0 d ${st.cap === 'round' ? 1 : st.cap === 'square' ? 2 : 0} J ${st.join === 'round' ? 1 : st.join === 'bevel' ? 2 : 0} j `);
|
|
481
|
+
const even = st.fillRule === 'evenodd' ? '*' : '';
|
|
482
|
+
const paint = fa > 0.001 && sa > 0.001 ? `B${even}` : fa > 0.001 ? `f${even}` : 'S';
|
|
483
|
+
cv.ops.push(`${parts.join('')}${ops} ${paint} Q`);
|
|
484
|
+
}
|
|
485
|
+
function drawText(el, m, base, cv) {
|
|
486
|
+
const pos = { x: nums(el.attrs['x'])[0] ?? 0, y: nums(el.attrs['y'])[0] ?? 0 };
|
|
487
|
+
const run = (node, st) => {
|
|
488
|
+
for (const c of node.children) {
|
|
489
|
+
if (typeof c === 'string') {
|
|
490
|
+
const text = c.replace(/\s+/g, ' ').trim();
|
|
491
|
+
if (!text)
|
|
492
|
+
continue;
|
|
493
|
+
const fill = paintOf(st.fill, cv.ids);
|
|
494
|
+
const alpha = fill ? fill[3] * st.fillOpacity * st.opacity : 0;
|
|
495
|
+
if (!fill || alpha <= 0.001)
|
|
496
|
+
continue;
|
|
497
|
+
const face = st.bold ? cv.bold : cv.regular;
|
|
498
|
+
const shown = face.fit(text, 1e6, st.fontSize);
|
|
499
|
+
if (!shown)
|
|
500
|
+
continue;
|
|
501
|
+
const dx = st.anchor === 'middle' ? -shown.w / 2 : st.anchor === 'end' ? -shown.w : 0;
|
|
502
|
+
const dy = st.baseline === 'middle' || st.baseline === 'central' ? st.fontSize * 0.35 : st.baseline === 'hanging' || st.baseline === 'text-before-edge' ? st.fontSize * 0.8 : 0;
|
|
503
|
+
const tm = mul(m, [1, 0, 0, -1, pos.x + dx, pos.y + dy]);
|
|
504
|
+
const font = st.bold && !cv.fakeBold ? 'F2' : 'F1';
|
|
505
|
+
const fake = st.bold && cv.fakeBold ? `${rgbOp(fill, 'RG')} 2 Tr ${n2(st.fontSize * 0.025)} w ` : '';
|
|
506
|
+
cv.ops.push(`q ${alphaOp(cv, alpha, alpha)}BT /${font} ${n2(st.fontSize)} Tf ${rgbOp(fill, 'rg')} ${fake}${tm.map((v) => n2r(v)).join(' ')} Tm ${shown.s} Tj ET Q`);
|
|
507
|
+
pos.x += shown.w;
|
|
508
|
+
}
|
|
509
|
+
else if (c.tag === 'tspan') {
|
|
510
|
+
const { style, hidden } = styleOf(c, st);
|
|
511
|
+
if (hidden)
|
|
512
|
+
continue;
|
|
513
|
+
const xs = nums(c.attrs['x']);
|
|
514
|
+
const ys = nums(c.attrs['y']);
|
|
515
|
+
if (xs.length)
|
|
516
|
+
pos.x = xs[0];
|
|
517
|
+
if (ys.length)
|
|
518
|
+
pos.y = ys[0];
|
|
519
|
+
pos.x += size(c.attrs['dx'] ?? '0', style.fontSize);
|
|
520
|
+
pos.y += size(c.attrs['dy'] ?? '0', style.fontSize);
|
|
521
|
+
run(c, style);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
pos.x += size(el.attrs['dx'] ?? '0', base.fontSize);
|
|
526
|
+
pos.y += size(el.attrs['dy'] ?? '0', base.fontSize);
|
|
527
|
+
run(el, base);
|
|
528
|
+
}
|
|
529
|
+
function collectIds(el, out) {
|
|
530
|
+
if (el.attrs['id'])
|
|
531
|
+
out.set(el.attrs['id'], el);
|
|
532
|
+
for (const c of el.children)
|
|
533
|
+
if (typeof c !== 'string')
|
|
534
|
+
collectIds(c, out);
|
|
535
|
+
}
|
|
536
|
+
/** Drawing size in px: `width` / `height`, else the `viewBox`. */
|
|
537
|
+
function drawingSize(svg) {
|
|
538
|
+
const box = nums(svg.attrs['viewBox']);
|
|
539
|
+
const w = Number.parseFloat(svg.attrs['width'] ?? '') || box[2] || 300;
|
|
540
|
+
const h = Number.parseFloat(svg.attrs['height'] ?? '') || box[3] || 150;
|
|
541
|
+
return [w, h];
|
|
542
|
+
}
|
|
543
|
+
/** Writes SVG drawings as a vector PDF, one page per drawing, each scaled to fit the page. */
|
|
544
|
+
export async function writeGraphicsPdf(pages, options = {}) {
|
|
545
|
+
const list = (Array.isArray(pages) ? pages : [pages]);
|
|
546
|
+
const drawings = list.map((p) => parseSvgXml(typeof p === 'string' ? p : p.svg));
|
|
547
|
+
if (!drawings.length || drawings.some((d) => !d))
|
|
548
|
+
throw new Error('writeGraphicsPdf: every page needs SVG markup with an <svg> root.');
|
|
549
|
+
const slicer = createSlicer(25, defaultYield);
|
|
550
|
+
const ps = options.pageSize;
|
|
551
|
+
const base = Array.isArray(ps) ? ps : typeof ps === 'object' ? [ps.width, ps.height] : (PAGE_SIZES[(ps ?? 'a4').toLowerCase()] ?? PAGE_SIZES.a4);
|
|
552
|
+
const margin = options.margin ?? 36;
|
|
553
|
+
const lineH = 15.2;
|
|
554
|
+
const headerText = options.header ?? '';
|
|
555
|
+
const footerText = options.footer ?? '';
|
|
556
|
+
let regular = helvetica(false);
|
|
557
|
+
let bold = helvetica(true);
|
|
558
|
+
let embedded = null;
|
|
559
|
+
if (options.font) {
|
|
560
|
+
const { createEmbeddedFace } = await import('./pdf-fonts.js');
|
|
561
|
+
const spec = options.font;
|
|
562
|
+
const font = spec.regular ? spec : { regular: options.font };
|
|
563
|
+
const r = createEmbeddedFace(font.regular, false, helpers);
|
|
564
|
+
const b = font.bold ? createEmbeddedFace(font.bold, false, helpers) : null;
|
|
565
|
+
embedded = { regular: r, bold: b };
|
|
566
|
+
regular = r;
|
|
567
|
+
bold = b ?? r;
|
|
568
|
+
}
|
|
569
|
+
const objects = [];
|
|
570
|
+
const addObject = (id, body) => {
|
|
571
|
+
objects[id] = latin1(`${id} 0 obj\n${body}\nendobj\n`);
|
|
572
|
+
};
|
|
573
|
+
const streamObject = async (id, content, dict = '') => {
|
|
574
|
+
const raw = typeof content === 'string' ? latin1(content) : content;
|
|
575
|
+
const packed = options.compress === false ? null : await deflate(raw, 'deflate');
|
|
576
|
+
const data = packed ?? raw;
|
|
577
|
+
objects[id] = concat([latin1(`${id} 0 obj\n<< /Length ${data.length}${packed ? ' /Filter /FlateDecode' : ''}${dict ? ` ${dict}` : ''} >>\nstream\n`), data, latin1('\nendstream\nendobj\n')]);
|
|
578
|
+
};
|
|
579
|
+
const count = drawings.length;
|
|
580
|
+
const idOf = (page) => 4 + 2 * page;
|
|
581
|
+
const pageText = (value, page) => value.replace(/\{page\}/g, String(page)).replace(/\{pages\}/g, String(count));
|
|
582
|
+
for (let p = 0; p < count; p++) {
|
|
583
|
+
const svg = drawings[p];
|
|
584
|
+
const [w, h] = drawingSize(svg);
|
|
585
|
+
const landscape = options.orientation ? options.orientation === 'landscape' : w > h;
|
|
586
|
+
const pageW = landscape ? Math.max(base[0], base[1]) : Math.min(base[0], base[1]);
|
|
587
|
+
const pageH = landscape ? Math.min(base[0], base[1]) : Math.max(base[0], base[1]);
|
|
588
|
+
const top = pageH - margin - (headerText ? lineH : 0);
|
|
589
|
+
const bottom = margin + (footerText ? lineH : 0);
|
|
590
|
+
const s = Math.min((pageW - 2 * margin) / w, Math.max(1, top - bottom) / h);
|
|
591
|
+
const ox = (pageW - w * s) / 2;
|
|
592
|
+
// SVG px (y down) to PDF points (y up), top-aligned and centered.
|
|
593
|
+
const page = [s, 0, 0, -s, ox, top];
|
|
594
|
+
const ids = new Map();
|
|
595
|
+
collectIds(svg, ids);
|
|
596
|
+
const cv = { ops: [], ids, regular, bold, fakeBold: embedded !== null && embedded.bold === null, states: new Map() };
|
|
597
|
+
draw(svg, page, INHERITED, cv, w, h);
|
|
598
|
+
const label = (value, y, end) => {
|
|
599
|
+
const shown = regular.fit(pageText(value, p + 1), pageW - 2 * margin, 8);
|
|
600
|
+
if (shown)
|
|
601
|
+
cv.ops.push(`BT /F1 8 Tf 0 0 0 rg ${n2(end ? pageW - margin - shown.w : margin)} ${n2(y)} Td ${shown.s} Tj ET`);
|
|
602
|
+
};
|
|
603
|
+
if (headerText)
|
|
604
|
+
label(headerText, pageH - margin - 10, false);
|
|
605
|
+
if (footerText)
|
|
606
|
+
label(footerText, margin + 2, true);
|
|
607
|
+
await streamObject(idOf(p + 1) + 1, cv.ops.join('\n'));
|
|
608
|
+
const states = [...cv.states].map(([key, name]) => {
|
|
609
|
+
const [fa, sa] = key.split(' ');
|
|
610
|
+
return `/${name} << /ca ${fa} /CA ${sa} >>`;
|
|
611
|
+
});
|
|
612
|
+
addObject(idOf(p + 1), `<< /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 >>${states.length ? ` /ExtGState << ${states.join(' ')} >>` : ''} >> /Contents ${idOf(p + 1) + 1} 0 R >>`);
|
|
613
|
+
if (slicer.due())
|
|
614
|
+
await slicer.pause();
|
|
615
|
+
}
|
|
616
|
+
addObject(1, '<< /Type /Catalog /Pages 2 0 R >>');
|
|
617
|
+
addObject(2, `<< /Type /Pages /Kids [${Array.from({ length: count }, (_, i) => `${idOf(i + 1)} 0 R`).join(' ')}] /Count ${count} >>`);
|
|
618
|
+
if (embedded) {
|
|
619
|
+
let next = idOf(count + 1);
|
|
620
|
+
let reserved4 = embedded.bold === null;
|
|
621
|
+
const alloc = () => {
|
|
622
|
+
if (reserved4) {
|
|
623
|
+
reserved4 = false;
|
|
624
|
+
return 4;
|
|
625
|
+
}
|
|
626
|
+
return next++;
|
|
627
|
+
};
|
|
628
|
+
await embedded.regular.write(3, alloc, addObject, streamObject);
|
|
629
|
+
if (embedded.bold)
|
|
630
|
+
await embedded.bold.write(4, alloc, addObject, streamObject);
|
|
631
|
+
}
|
|
632
|
+
else {
|
|
633
|
+
addObject(3, '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>');
|
|
634
|
+
addObject(4, '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding >>');
|
|
635
|
+
}
|
|
636
|
+
addObject(5, `<< /Producer (Salmon UI)${options.title ? ` /Title ${textString(options.title)}` : ''} >>`);
|
|
637
|
+
const out = [latin1('%PDF-1.4\n%\xe2\xe3\xcf\xd3\n')];
|
|
638
|
+
let at = out[0].length;
|
|
639
|
+
let xref = `xref\n0 ${objects.length}\n0000000000 65535 f \n`;
|
|
640
|
+
for (let id = 1; id < objects.length; id++) {
|
|
641
|
+
const o = objects[id];
|
|
642
|
+
xref += `${String(at).padStart(10, '0')} 00000 n \n`;
|
|
643
|
+
out.push(o);
|
|
644
|
+
at += o.length;
|
|
645
|
+
}
|
|
646
|
+
xref += `trailer\n<< /Size ${objects.length} /Root 1 0 R /Info 5 0 R >>\nstartxref\n${at}\n%%EOF\n`;
|
|
647
|
+
out.push(latin1(xref));
|
|
648
|
+
const name = options.fileName ?? 'Chart.pdf';
|
|
649
|
+
return { fileName: /\.pdf$/i.test(name) ? name : `${name}.pdf`, mimeType: 'application/pdf', data: concat(out) };
|
|
650
|
+
}
|
|
651
|
+
//# sourceMappingURL=graphics.js.map
|