@btps/boundview 1.2.0-rc.3.1 → 1.2.0-rc.3.2

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/dist/svg.js CHANGED
@@ -13,10 +13,7 @@
13
13
  import { boundValue, writtenValue } from '@btps/sdk/boundview';
14
14
  import { FALLBACK_FONT } from './glyphs.js';
15
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');
16
+ import { n, signatureShapes } from './signature.js';
20
17
  /** A colour as `fill`/`stroke` and its opacity. Colours passed the schema: `#rrggbb` or `#rrggbbaa`. */
21
18
  const colourOf = (colour) => [
22
19
  colour.slice(0, 7),
@@ -54,6 +51,12 @@ class Drawing {
54
51
  push(part) {
55
52
  this.parts.push(part);
56
53
  }
54
+ /** A clip to an outline, defined once, as an attribute. */
55
+ clip(outline) {
56
+ const id = `c${this.gradients++}`;
57
+ this.defs.push(`<clipPath id="${id}"><path d="${outline}"/></clipPath>`);
58
+ return ` clip-path="url(#${id})"`;
59
+ }
57
60
  toString(width, height, background) {
58
61
  const [hex, alpha] = colourOf(background);
59
62
  return [
@@ -118,7 +121,11 @@ const drawText = (drawing, element, text, font, bundled) => {
118
121
  size = Math.max(fit, Math.max(element.size * 0.75, 6));
119
122
  }
120
123
  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;
124
+ let x = element.align === 'end'
125
+ ? element.x + element.width - width
126
+ : element.align === 'centre'
127
+ ? element.x + (element.width - width) / 2
128
+ : element.x;
122
129
  const paths = [];
123
130
  characters.forEach((char, i) => {
124
131
  const found = glyphFor(font, char, element.type === 'label', bundled);
@@ -146,30 +153,23 @@ const transformed = (text, transform) => [...text]
146
153
  /** A signature fitted to its box, keeping its proportions, centred: strokes, or text in Caveat. */
147
154
  const drawSignature = (drawing, box, signature, bundled) => {
148
155
  if (signature.type === 'drawn') {
156
+ // By the one rule a platform's page shows it with (signature.ts), fitted to the box, centred.
149
157
  const scale = Math.min(box.width / 1000, box.height / 300);
150
158
  const left = box.x + (box.width - 1000 * scale) / 2;
151
159
  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"/>`);
160
+ for (const shape of signatureShapes(signature.strokes, (x, y) => [left + x * scale, top + y * scale], scale, box.colour))
161
+ drawing.push(shape);
164
162
  return;
165
163
  }
166
- const caveat = bundled.caveat;
164
+ // In Caveat; a character it has no glyph for (another script) in Arimo, as a label falls back.
165
+ const caveat = bundled.caveat === undefined ? undefined : { ...bundled.caveat, fallback: ['arimo'] };
167
166
  if (caveat === undefined)
168
167
  return;
169
168
  const characters = [...signature.text];
170
169
  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);
170
+ const found = glyphFor(caveat, char, true, bundled);
171
+ const advance = found ? (found.glyph.advance * caveat.unitsPerEm) / found.font.unitsPerEm : 0;
172
+ return width + advance + (caveat.kerning?.[`${char}${characters[i + 1] ?? ''}`] ?? 0);
173
173
  }, 0);
174
174
  const tall = caveat.ascent + caveat.descent;
175
175
  const size = Math.min(units > 0 ? (box.width * caveat.unitsPerEm) / units : Infinity, (box.height * caveat.unitsPerEm) / tall);
@@ -191,28 +191,36 @@ const drawSignature = (drawing, box, signature, bundled) => {
191
191
  * The drawing for a BoundView that passed every check, with each binding
192
192
  * filled from `document`. Never call it on one that did not: `renderBoundView`
193
193
  * checks first. `bundled` holds the bundled fonts it needs
194
- * (`loadBundledFonts(bundledFontsFor(document, view))`); most need none.
194
+ * (`loadBundledFonts(bundledFontsFor(document, view), bundledCharactersFor(document, view))`); most need none.
195
195
  */
196
196
  export const boundViewToSvg = (document, view, bundled = {}) => {
197
197
  const drawing = new Drawing();
198
198
  for (const element of view.elements) {
199
- const opacity = 'opacity' in element && element.opacity !== undefined ? ` opacity="${n(element.opacity)}"` : '';
199
+ const opacity = 'opacity' in element && element.opacity !== undefined
200
+ ? ` opacity="${n(element.opacity)}"`
201
+ : '';
200
202
  switch (element.type) {
201
203
  case 'rect': {
202
204
  const radius = element.radius ?? 0;
203
205
  const radii = Array.isArray(radius) ? radius : [radius, radius, radius, radius];
204
206
  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)}"` : '';
207
+ const stroke = element.stroke
208
+ ? `${drawing.paint('stroke', element.stroke)} stroke-width="${n(element.strokeWidth ?? 0)}"`
209
+ : '';
206
210
  drawing.push(`<path d="${outline}"${drawing.paint('fill', element.fill)}${stroke}${opacity}/>`);
207
211
  break;
208
212
  }
209
213
  case 'line': {
210
- const dash = element.dash ? ` stroke-dasharray="${n(element.dash[0])} ${n(element.dash[1])}"` : '';
214
+ const dash = element.dash
215
+ ? ` stroke-dasharray="${n(element.dash[0])} ${n(element.dash[1])}"`
216
+ : '';
211
217
  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
218
  break;
213
219
  }
214
220
  case 'path': {
215
- const stroke = element.stroke ? `${drawing.paint('stroke', element.stroke)} stroke-width="${n(element.strokeWidth ?? 0)}"` : '';
221
+ const stroke = element.stroke
222
+ ? `${drawing.paint('stroke', element.stroke)} stroke-width="${n(element.strokeWidth ?? 0)}"`
223
+ : '';
216
224
  drawing.push(`<path d="${element.d}" fill-rule="${element.fillRule ?? 'nonzero'}"${drawing.paint('fill', element.fill)}${stroke}${opacity}/>`);
217
225
  break;
218
226
  }
@@ -220,7 +228,12 @@ export const boundViewToSvg = (document, view, bundled = {}) => {
220
228
  const image = view.images?.[element.image];
221
229
  if (image === undefined)
222
230
  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}/>`);
231
+ const radius = element.radius ?? 0;
232
+ const radii = Array.isArray(radius) ? radius : [radius, radius, radius, radius];
233
+ const clip = radii.some((r) => r > 0)
234
+ ? drawing.clip(roundedRect(element.x, element.y, element.width, element.height, radii))
235
+ : '';
236
+ 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)}"${clip}${opacity}/>`);
224
237
  break;
225
238
  }
226
239
  case 'text': {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btps/boundview",
3
- "version": "1.2.0-rc.3.1",
3
+ "version": "1.2.0-rc.3.2",
4
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
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -15,6 +15,10 @@
15
15
  "types": "./dist/index.d.ts",
16
16
  "import": "./dist/index.js"
17
17
  },
18
+ "./signature": {
19
+ "types": "./dist/signature.d.ts",
20
+ "import": "./dist/signature.js"
21
+ },
18
22
  "./engine.wasm": "./dist/engine.wasm"
19
23
  },
20
24
  "files": [
@@ -33,8 +37,9 @@
33
37
  "generate-glyphs": "node scripts/generate-bundled-glyphs.mjs"
34
38
  },
35
39
  "dependencies": {
36
- "@btps/sdk": "^1.2.0-rc.3",
40
+ "@btps/sdk": "^1.2.0-rc.3.2",
37
41
  "@resvg/resvg-wasm": "2.6.2",
42
+ "brotli": "2.1.0",
38
43
  "fflate": "^0.8.3",
39
44
  "opentype.js": "^2.0.0"
40
45
  }