@btps/boundview 0.0.0-stage → 1.2.0-rc.3

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.
@@ -0,0 +1,232 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2025 Bhupendra Tamang
4
+ * Licensed under the Apache License, Version 2.0
5
+ * https://www.apache.org/licenses/LICENSE-2.0
6
+ */
7
+ const ARITY = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 };
8
+ /** An arc from `from` as cubic curves, each at most a quarter turn (SVG 1.1, F.6.5 and F.6.6). */
9
+ const arcToCubics = (from, [rxIn, ryIn, angle, large, sweep, x, y]) => {
10
+ let rx = Math.abs(rxIn ?? 0);
11
+ let ry = Math.abs(ryIn ?? 0);
12
+ const to = [x ?? 0, y ?? 0];
13
+ if (rx === 0 || ry === 0 || (from[0] === to[0] && from[1] === to[1]))
14
+ return [{ type: 'L', to }];
15
+ const phi = ((angle ?? 0) * Math.PI) / 180;
16
+ const cos = Math.cos(phi);
17
+ const sin = Math.sin(phi);
18
+ const dx = (from[0] - to[0]) / 2;
19
+ const dy = (from[1] - to[1]) / 2;
20
+ const x1 = cos * dx + sin * dy;
21
+ const y1 = -sin * dx + cos * dy;
22
+ const lambda = (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry);
23
+ if (lambda > 1) {
24
+ rx *= Math.sqrt(lambda);
25
+ ry *= Math.sqrt(lambda);
26
+ }
27
+ const sign = large === sweep ? -1 : 1;
28
+ const numerator = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1;
29
+ const factor = sign * Math.sqrt(Math.max(0, numerator / (rx * rx * y1 * y1 + ry * ry * x1 * x1)));
30
+ const cx1 = (factor * rx * y1) / ry;
31
+ const cy1 = (-factor * ry * x1) / rx;
32
+ const cx = cos * cx1 - sin * cy1 + (from[0] + to[0]) / 2;
33
+ const cy = sin * cx1 + cos * cy1 + (from[1] + to[1]) / 2;
34
+ const angleOf = (ux, uy, vx, vy) => {
35
+ const a = Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
36
+ return a;
37
+ };
38
+ const theta = angleOf(1, 0, (x1 - cx1) / rx, (y1 - cy1) / ry);
39
+ let delta = angleOf((x1 - cx1) / rx, (y1 - cy1) / ry, (-x1 - cx1) / rx, (-y1 - cy1) / ry);
40
+ if (!sweep && delta > 0)
41
+ delta -= 2 * Math.PI;
42
+ if (sweep && delta < 0)
43
+ delta += 2 * Math.PI;
44
+ const parts = Math.ceil(Math.abs(delta) / (Math.PI / 2));
45
+ const step = delta / parts;
46
+ const k = (4 / 3) * Math.tan(step / 4);
47
+ const point = (t) => [
48
+ cx + rx * Math.cos(t) * cos - ry * Math.sin(t) * sin,
49
+ cy + rx * Math.cos(t) * sin + ry * Math.sin(t) * cos,
50
+ ];
51
+ const derivative = (t) => [
52
+ -rx * Math.sin(t) * cos - ry * Math.cos(t) * sin,
53
+ -rx * Math.sin(t) * sin + ry * Math.cos(t) * cos,
54
+ ];
55
+ const out = [];
56
+ for (let i = 0; i < parts; i++) {
57
+ const t1 = theta + i * step;
58
+ const t2 = t1 + step;
59
+ const p1 = point(t1);
60
+ const p2 = point(t2);
61
+ const d1 = derivative(t1);
62
+ const d2 = derivative(t2);
63
+ out.push({
64
+ type: 'C',
65
+ c1: [p1[0] + k * d1[0], p1[1] + k * d1[1]],
66
+ c2: [p2[0] - k * d2[0], p2[1] - k * d2[1]],
67
+ to: i === parts - 1 ? to : p2,
68
+ });
69
+ }
70
+ return out;
71
+ };
72
+ /** SVG path data as absolute M, L, Q, C and Z, or undefined when it cannot be read. */
73
+ export const normalizePath = (d) => {
74
+ const tokens = d.match(/[a-zA-Z]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/g) ?? [];
75
+ const out = [];
76
+ let at = [0, 0];
77
+ let start = [0, 0];
78
+ let lastControl;
79
+ let lastType = '';
80
+ let i = 0;
81
+ let command = '';
82
+ while (i < tokens.length) {
83
+ const token = tokens[i];
84
+ if (/[a-zA-Z]/.test(token)) {
85
+ command = token;
86
+ i++;
87
+ if (command.toUpperCase() === 'Z') {
88
+ out.push({ type: 'Z' });
89
+ at = start;
90
+ lastControl = undefined;
91
+ lastType = 'Z';
92
+ continue;
93
+ }
94
+ }
95
+ else if (command === '')
96
+ return undefined;
97
+ const upper = command.toUpperCase();
98
+ const arity = ARITY[upper];
99
+ if (arity === undefined)
100
+ return undefined;
101
+ const numbers = tokens.slice(i, i + arity).map(Number);
102
+ if (numbers.length < arity || numbers.some((n) => !Number.isFinite(n)))
103
+ return undefined;
104
+ i += arity;
105
+ const relative = command !== upper;
106
+ const abs = (x, y) => (relative ? [at[0] + x, at[1] + y] : [x, y]);
107
+ const [a = 0, b = 0, c = 0, e = 0, f = 0, g = 0] = numbers;
108
+ switch (upper) {
109
+ case 'M':
110
+ at = abs(a, b);
111
+ start = at;
112
+ out.push({ type: 'M', to: at });
113
+ // Further pairs after a move are lines.
114
+ command = relative ? 'l' : 'L';
115
+ lastControl = undefined;
116
+ break;
117
+ case 'L':
118
+ at = abs(a, b);
119
+ out.push({ type: 'L', to: at });
120
+ lastControl = undefined;
121
+ break;
122
+ case 'H':
123
+ at = [relative ? at[0] + a : a, at[1]];
124
+ out.push({ type: 'L', to: at });
125
+ lastControl = undefined;
126
+ break;
127
+ case 'V':
128
+ at = [at[0], relative ? at[1] + a : a];
129
+ out.push({ type: 'L', to: at });
130
+ lastControl = undefined;
131
+ break;
132
+ case 'C': {
133
+ const c1 = abs(a, b);
134
+ const c2 = abs(c, e);
135
+ const to = abs(f, g);
136
+ out.push({ type: 'C', c1, c2, to });
137
+ lastControl = c2;
138
+ at = to;
139
+ break;
140
+ }
141
+ case 'S': {
142
+ const c1 = lastControl && /[CS]/i.test(lastType) ? [2 * at[0] - lastControl[0], 2 * at[1] - lastControl[1]] : at;
143
+ const c2 = abs(a, b);
144
+ const to = abs(c, e);
145
+ out.push({ type: 'C', c1, c2, to });
146
+ lastControl = c2;
147
+ at = to;
148
+ break;
149
+ }
150
+ case 'Q': {
151
+ const c1 = abs(a, b);
152
+ const to = abs(c, e);
153
+ out.push({ type: 'Q', c1, to });
154
+ lastControl = c1;
155
+ at = to;
156
+ break;
157
+ }
158
+ case 'T': {
159
+ const c1 = lastControl && /[QT]/i.test(lastType) ? [2 * at[0] - lastControl[0], 2 * at[1] - lastControl[1]] : at;
160
+ const to = abs(a, b);
161
+ out.push({ type: 'Q', c1, to });
162
+ lastControl = c1;
163
+ at = to;
164
+ break;
165
+ }
166
+ case 'A': {
167
+ const [rx, ry, angle, large, sweep, x, y] = numbers;
168
+ const to = abs(x, y);
169
+ out.push(...arcToCubics(at, [rx, ry, angle, large, sweep, to[0], to[1]]));
170
+ lastControl = undefined;
171
+ at = to;
172
+ break;
173
+ }
174
+ }
175
+ lastType = upper;
176
+ }
177
+ return out;
178
+ };
179
+ /** A point placed by `matrix`. */
180
+ const place = ([x, y], [a, b, c, d, e, f]) => [a * x + c * y + e, b * x + d * y + f];
181
+ const r = (value) => {
182
+ const rounded = Math.round(value * 100) / 100;
183
+ return Object.is(rounded, -0) ? 0 : rounded;
184
+ };
185
+ /** Commands placed on the page by `matrix`, written as BoundView path data. */
186
+ export const pathData = (commands, matrix) => commands
187
+ .map((command) => {
188
+ if (command.type === 'Z')
189
+ return 'Z';
190
+ const to = place(command.to, matrix);
191
+ if (command.type === 'Q') {
192
+ const c1 = place(command.c1, matrix);
193
+ return `Q${r(c1[0])} ${r(c1[1])} ${r(to[0])} ${r(to[1])}`;
194
+ }
195
+ if (command.type === 'C') {
196
+ const c1 = place(command.c1, matrix);
197
+ const c2 = place(command.c2, matrix);
198
+ return `C${r(c1[0])} ${r(c1[1])} ${r(c2[0])} ${r(c2[1])} ${r(to[0])} ${r(to[1])}`;
199
+ }
200
+ return `${command.type}${r(to[0])} ${r(to[1])}`;
201
+ })
202
+ .join('');
203
+ /** A basic shape's outline, as path data in its own coordinates. */
204
+ export const shapeOutline = (tag, attr) => {
205
+ switch (tag) {
206
+ case 'rect': {
207
+ const [x, y, w, h] = [attr('x'), attr('y'), attr('width'), attr('height')];
208
+ let rx = attr('rx');
209
+ let ry = attr('ry');
210
+ if (!rx && ry)
211
+ rx = ry;
212
+ if (!ry && rx)
213
+ ry = rx;
214
+ rx = Math.min(rx, w / 2);
215
+ ry = Math.min(ry, h / 2);
216
+ if (!rx)
217
+ return `M${x} ${y}H${x + w}V${y + h}H${x}Z`;
218
+ 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`;
219
+ }
220
+ case 'circle':
221
+ case 'ellipse': {
222
+ const [cx, cy] = [attr('cx'), attr('cy')];
223
+ const rx = tag === 'circle' ? attr('r') : attr('rx');
224
+ const ry = tag === 'circle' ? attr('r') : attr('ry');
225
+ return `M${cx - rx} ${cy}A${rx} ${ry} 0 1 0 ${cx + rx} ${cy}A${rx} ${ry} 0 1 0 ${cx - rx} ${cy}Z`;
226
+ }
227
+ case 'line':
228
+ return `M${attr('x1')} ${attr('y1')}L${attr('x2')} ${attr('y2')}`;
229
+ default:
230
+ return undefined;
231
+ }
232
+ };
package/dist/svg.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2025 Bhupendra Tamang
4
+ * Licensed under the Apache License, Version 2.0
5
+ * https://www.apache.org/licenses/LICENSE-2.0
6
+ */
7
+ import type { BTPBoundView } from '@btps/sdk/boundview';
8
+ import { type BundledFonts } from './glyphs.js';
9
+ type Doc = Record<string, unknown>;
10
+ /** A drawn signature's stroke, in units of its 1,000 by 300 canvas. */
11
+ export declare const SIGNATURE_STROKE = 8;
12
+ /**
13
+ * The drawing for a BoundView that passed every check, with each binding
14
+ * filled from `document`. Never call it on one that did not: `renderBoundView`
15
+ * checks first. `bundled` holds the bundled fonts it needs
16
+ * (`loadBundledFonts(bundledFontsFor(document, view))`); most need none.
17
+ */
18
+ export declare const boundViewToSvg: (document: Doc, view: BTPBoundView, bundled?: BundledFonts) => string;
19
+ export {};
package/dist/svg.js ADDED
@@ -0,0 +1,249 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2025 Bhupendra Tamang
4
+ * Licensed under the Apache License, Version 2.0
5
+ * https://www.apache.org/licenses/LICENSE-2.0
6
+ */
7
+ /*
8
+ * A checked BoundView as the drawing the engine reads (Rendering page): only
9
+ * rectangles, paths and images, with numbers and colours that passed the
10
+ * schema. Text arrives as glyph paths, each image as a PNG made here from its
11
+ * checked pixels. No value from a canvas is copied in as markup.
12
+ */
13
+ import { boundValue, writtenValue } from '@btps/sdk/boundview';
14
+ import { FALLBACK_FONT } from './glyphs.js';
15
+ import { pngOf } from './png.js';
16
+ /** A drawn signature's stroke, in units of its 1,000 by 300 canvas. */
17
+ export const SIGNATURE_STROKE = 8;
18
+ /** A number as the drawing writes it: finite, at most four decimals. */
19
+ const n = (value) => (Number.isFinite(value) ? String(Math.round(value * 10000) / 10000) : '0');
20
+ /** A colour as `fill`/`stroke` and its opacity. Colours passed the schema: `#rrggbb` or `#rrggbbaa`. */
21
+ const colourOf = (colour) => [
22
+ colour.slice(0, 7),
23
+ colour.length === 9 ? parseInt(colour.slice(7, 9), 16) / 255 : 1,
24
+ ];
25
+ class Drawing {
26
+ defs = [];
27
+ parts = [];
28
+ gradients = 0;
29
+ /** A paint as attributes: a colour with its opacity, or a gradient defined once. */
30
+ paint(kind, paint) {
31
+ if (paint === undefined)
32
+ return ` ${kind}="none"`;
33
+ if (typeof paint === 'string') {
34
+ const [hex, alpha] = colourOf(paint);
35
+ return ` ${kind}="${hex}"${alpha < 1 ? ` ${kind}-opacity="${n(alpha)}"` : ''}`;
36
+ }
37
+ const id = `g${this.gradients++}`;
38
+ const stops = ('linear' in paint ? paint.linear.stops : paint.radial.stops)
39
+ .map(([offset, colour]) => {
40
+ const [hex, alpha] = colourOf(colour);
41
+ return `<stop offset="${n(offset)}" stop-color="${hex}" stop-opacity="${n(alpha)}"/>`;
42
+ })
43
+ .join('');
44
+ if ('linear' in paint) {
45
+ const { from, to } = paint.linear;
46
+ this.defs.push(`<linearGradient id="${id}" gradientUnits="userSpaceOnUse" x1="${n(from[0])}" y1="${n(from[1])}" x2="${n(to[0])}" y2="${n(to[1])}">${stops}</linearGradient>`);
47
+ }
48
+ else {
49
+ const { centre, radius } = paint.radial;
50
+ this.defs.push(`<radialGradient id="${id}" gradientUnits="userSpaceOnUse" cx="${n(centre[0])}" cy="${n(centre[1])}" r="${n(radius)}">${stops}</radialGradient>`);
51
+ }
52
+ return ` ${kind}="url(#${id})"`;
53
+ }
54
+ push(part) {
55
+ this.parts.push(part);
56
+ }
57
+ toString(width, height, background) {
58
+ const [hex, alpha] = colourOf(background);
59
+ return [
60
+ `<svg xmlns="http://www.w3.org/2000/svg" width="${n(width)}" height="${n(height)}" viewBox="0 0 ${n(width)} ${n(height)}">`,
61
+ `<defs>${this.defs.join('')}</defs>`,
62
+ `<rect width="${n(width)}" height="${n(height)}" fill="#ffffff"/>`,
63
+ `<rect width="${n(width)}" height="${n(height)}" fill="${hex}" fill-opacity="${n(alpha)}"/>`,
64
+ ...this.parts,
65
+ '</svg>',
66
+ ].join('');
67
+ }
68
+ }
69
+ /** A rectangle with four radii, each clamped to half its shorter side, as an outline. */
70
+ const roundedRect = (x, y, w, h, radii) => {
71
+ const limit = Math.min(w, h) / 2;
72
+ const [a, b, c, d] = radii.map((r) => Math.min(r, limit));
73
+ return [
74
+ `M${n(x + a)} ${n(y)}H${n(x + w - b)}`,
75
+ b ? `A${n(b)} ${n(b)} 0 0 1 ${n(x + w)} ${n(y + b)}` : '',
76
+ `V${n(y + h - c)}`,
77
+ c ? `A${n(c)} ${n(c)} 0 0 1 ${n(x + w - c)} ${n(y + h)}` : '',
78
+ `H${n(x + d)}`,
79
+ d ? `A${n(d)} ${n(d)} 0 0 1 ${n(x)} ${n(y + h - d)}` : '',
80
+ `V${n(y + a)}`,
81
+ a ? `A${n(a)} ${n(a)} 0 0 1 ${n(x + a)} ${n(y)}` : '',
82
+ 'Z',
83
+ ].join('');
84
+ };
85
+ /** The font a character is drawn with: its own font's glyph, or for a label, its first bundled fallback. */
86
+ const glyphFor = (font, char, label, bundled) => {
87
+ const own = font.glyphs?.[char];
88
+ if (own !== undefined)
89
+ return { glyph: own, font };
90
+ if (!label)
91
+ return undefined;
92
+ for (const name of font.fallback ?? []) {
93
+ const named = FALLBACK_FONT[name];
94
+ const fallback = named === undefined ? undefined : bundled[named];
95
+ const glyph = fallback?.glyphs?.[char];
96
+ if (fallback !== undefined && glyph !== undefined)
97
+ return { glyph, font: fallback };
98
+ }
99
+ return undefined;
100
+ };
101
+ /** A line of text as glyph paths on its baseline, placed by its alignment, shrunk to fit a bound value's box. */
102
+ const drawText = (drawing, element, text, font, bundled) => {
103
+ const characters = [...text];
104
+ const spacing = element.letterSpacing ?? 0;
105
+ const widthAt = (size) => characters.reduce((width, char, i) => {
106
+ const found = glyphFor(font, char, element.type === 'label', bundled);
107
+ if (found === undefined)
108
+ return width + spacing;
109
+ const scale = size / found.font.unitsPerEm;
110
+ const kerning = found.font.kerning?.[`${char}${characters[i + 1] ?? ''}`] ?? 0;
111
+ return width + (found.glyph.advance + kerning) * scale + spacing;
112
+ }, 0);
113
+ let size = element.size;
114
+ if (element.type === 'text' && widthAt(size) > element.width) {
115
+ // Width is linear in size apart from the letter spacing: the size that fits exactly, no smaller than the checks allow.
116
+ const spaced = spacing * characters.length;
117
+ const fit = ((element.width - spaced) / (widthAt(size) - spaced)) * size;
118
+ size = Math.max(fit, Math.max(element.size * 0.75, 6));
119
+ }
120
+ const width = widthAt(size);
121
+ let x = element.align === 'end' ? element.x + element.width - width : element.align === 'centre' ? element.x + (element.width - width) / 2 : element.x;
122
+ const paths = [];
123
+ characters.forEach((char, i) => {
124
+ const found = glyphFor(font, char, element.type === 'label', bundled);
125
+ if (found === undefined) {
126
+ x += spacing;
127
+ return;
128
+ }
129
+ const scale = size / found.font.unitsPerEm;
130
+ if (found.glyph.path !== undefined) {
131
+ paths.push(`<path transform="translate(${n(x)} ${n(element.y)}) scale(${n(scale)})" d="${found.glyph.path}"/>`);
132
+ }
133
+ const kerning = found.font.kerning?.[`${char}${characters[i + 1] ?? ''}`] ?? 0;
134
+ x += (found.glyph.advance + kerning) * scale + spacing;
135
+ });
136
+ drawing.push(`<g fill="${element.colour}">${paths.join('')}</g>`);
137
+ };
138
+ const transformed = (text, transform) => [...text]
139
+ .map((char) => {
140
+ if (transform === undefined)
141
+ return char;
142
+ const changed = transform === 'uppercase' ? char.toUpperCase() : char.toLowerCase();
143
+ return [...changed].length === 1 ? changed : char;
144
+ })
145
+ .join('');
146
+ /** A signature fitted to its box, keeping its proportions, centred: strokes, or text in Caveat. */
147
+ const drawSignature = (drawing, box, signature, bundled) => {
148
+ if (signature.type === 'drawn') {
149
+ const scale = Math.min(box.width / 1000, box.height / 300);
150
+ const left = box.x + (box.width - 1000 * scale) / 2;
151
+ const top = box.y + (box.height - 300 * scale) / 2;
152
+ const d = signature.strokes
153
+ .map((stroke) => stroke
154
+ .reduce((points, value, i) => {
155
+ if (i % 2 === 0)
156
+ points.push(`${points.length === 0 ? 'M' : 'L'}${n(left + value * scale)} `);
157
+ else
158
+ points[points.length - 1] += n(top + value * scale);
159
+ return points;
160
+ }, [])
161
+ .join(''))
162
+ .join('');
163
+ drawing.push(`<path d="${d}" fill="none" stroke="${box.colour}" stroke-width="${n(SIGNATURE_STROKE * scale)}" stroke-linecap="round" stroke-linejoin="round"/>`);
164
+ return;
165
+ }
166
+ const caveat = bundled.caveat;
167
+ if (caveat === undefined)
168
+ return;
169
+ const characters = [...signature.text];
170
+ const units = characters.reduce((width, char, i) => {
171
+ const glyph = caveat.glyphs?.[char];
172
+ return width + (glyph?.advance ?? 0) + (caveat.kerning?.[`${char}${characters[i + 1] ?? ''}`] ?? 0);
173
+ }, 0);
174
+ const tall = caveat.ascent + caveat.descent;
175
+ const size = Math.min(units > 0 ? (box.width * caveat.unitsPerEm) / units : Infinity, (box.height * caveat.unitsPerEm) / tall);
176
+ const scale = size / caveat.unitsPerEm;
177
+ const baseline = box.y + (box.height - tall * scale) / 2 + caveat.ascent * scale;
178
+ drawText(drawing, {
179
+ type: 'label',
180
+ text: signature.text,
181
+ x: box.x,
182
+ y: baseline,
183
+ width: box.width,
184
+ align: 'centre',
185
+ font: 'caveat',
186
+ size,
187
+ colour: box.colour,
188
+ }, signature.text, caveat, bundled);
189
+ };
190
+ /**
191
+ * The drawing for a BoundView that passed every check, with each binding
192
+ * filled from `document`. Never call it on one that did not: `renderBoundView`
193
+ * checks first. `bundled` holds the bundled fonts it needs
194
+ * (`loadBundledFonts(bundledFontsFor(document, view))`); most need none.
195
+ */
196
+ export const boundViewToSvg = (document, view, bundled = {}) => {
197
+ const drawing = new Drawing();
198
+ for (const element of view.elements) {
199
+ const opacity = 'opacity' in element && element.opacity !== undefined ? ` opacity="${n(element.opacity)}"` : '';
200
+ switch (element.type) {
201
+ case 'rect': {
202
+ const radius = element.radius ?? 0;
203
+ const radii = Array.isArray(radius) ? radius : [radius, radius, radius, radius];
204
+ const outline = roundedRect(element.x, element.y, element.width, element.height, radii);
205
+ const stroke = element.stroke ? `${drawing.paint('stroke', element.stroke)} stroke-width="${n(element.strokeWidth ?? 0)}"` : '';
206
+ drawing.push(`<path d="${outline}"${drawing.paint('fill', element.fill)}${stroke}${opacity}/>`);
207
+ break;
208
+ }
209
+ case 'line': {
210
+ const dash = element.dash ? ` stroke-dasharray="${n(element.dash[0])} ${n(element.dash[1])}"` : '';
211
+ drawing.push(`<path d="M${n(element.from[0])} ${n(element.from[1])}L${n(element.to[0])} ${n(element.to[1])}" fill="none"${drawing.paint('stroke', element.stroke)} stroke-width="${n(element.strokeWidth)}" stroke-linecap="butt"${dash}${opacity}/>`);
212
+ break;
213
+ }
214
+ case 'path': {
215
+ const stroke = element.stroke ? `${drawing.paint('stroke', element.stroke)} stroke-width="${n(element.strokeWidth ?? 0)}"` : '';
216
+ drawing.push(`<path d="${element.d}" fill-rule="${element.fillRule ?? 'nonzero'}"${drawing.paint('fill', element.fill)}${stroke}${opacity}/>`);
217
+ break;
218
+ }
219
+ case 'image': {
220
+ const image = view.images?.[element.image];
221
+ if (image === undefined)
222
+ break;
223
+ drawing.push(`<image x="${n(element.x)}" y="${n(element.y)}" width="${n(element.width)}" height="${n(element.height)}" preserveAspectRatio="none" href="data:image/png;base64,${pngOf(image)}"${opacity}/>`);
224
+ break;
225
+ }
226
+ case 'text': {
227
+ const font = view.fonts?.[element.font];
228
+ const value = boundValue(document, element.bind.field);
229
+ if (font === undefined || value === undefined)
230
+ break;
231
+ drawText(drawing, element, transformed(writtenValue(value, element.bind), element.transform), font, bundled);
232
+ break;
233
+ }
234
+ case 'label': {
235
+ const font = view.fonts?.[element.font];
236
+ if (font !== undefined)
237
+ drawText(drawing, element, transformed(element.text, element.transform), font, bundled);
238
+ break;
239
+ }
240
+ case 'signature': {
241
+ const signatory = document.signatory;
242
+ if (signatory?.visualSignature)
243
+ drawSignature(drawing, element, signatory.visualSignature, bundled);
244
+ break;
245
+ }
246
+ }
247
+ }
248
+ return drawing.toString(view.page.width, view.page.height, view.page.background ?? '#ffffff');
249
+ };
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "@btps/boundview",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.2.0-rc.3",
4
+ "description": "BoundView for BTPS: capture a document's HTML preview as a signed canvas bound to its data, and draw a received one in a closed WebAssembly engine.",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/openbtps/btps-node.git",
10
+ "directory": "packages/boundview"
11
+ },
12
+ "homepage": "https://www.openbtps.org/docs/next/protocol/specifications/boundview",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ },
18
+ "./engine.wasm": "./dist/engine.wasm"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "NOTICE"
23
+ ],
24
+ "sideEffects": false,
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "engines": {
29
+ "node": ">=24"
30
+ },
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.build.json && node -e \"require('fs').copyFileSync(require.resolve('@resvg/resvg-wasm/index_bg.wasm'), 'dist/engine.wasm')\"",
33
+ "generate-glyphs": "node scripts/generate-bundled-glyphs.mjs"
34
+ },
35
+ "dependencies": {
36
+ "@btps/sdk": "^1.2.0-rc.3",
37
+ "@resvg/resvg-wasm": "2.6.2",
38
+ "fflate": "^0.8.3",
39
+ "opentype.js": "^2.0.0"
40
+ }
41
+ }