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

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.
@@ -140,8 +140,8 @@ export const bindRuns = (document, runs) => {
140
140
  const shown = chars.slice(from, to).join('');
141
141
  return writtenValue(value, full) === shown && free(run, from, to) ? { from, to, bind: full } : undefined;
142
142
  };
143
- /** Binds every place a writing of `field` appears, in `only` runs if given. */
144
- const bindEverywhere = (field, value, only) => {
143
+ /** Binds every place a writing of `field` appears, in `only` runs if given; with `look`, only says whether there is one. */
144
+ const bindEverywhere = (field, value, only, look = false) => {
145
145
  let found = false;
146
146
  for (const { text, bind } of writings(field, value).sort((a, b) => b.text.length - a.text.length)) {
147
147
  const money = value.kind === 'money';
@@ -154,11 +154,15 @@ export const bindRuns = (document, runs) => {
154
154
  if (money) {
155
155
  const placed = moneyAt(run, at, [...number].length, bind, value);
156
156
  if (placed) {
157
+ if (look)
158
+ return true;
157
159
  take(run, placed.from, placed.to, placed.bind);
158
160
  found = true;
159
161
  }
160
162
  }
161
163
  else {
164
+ if (look)
165
+ return true;
162
166
  take(run, at, at + [...number].length, bind);
163
167
  found = true;
164
168
  }
@@ -230,26 +234,29 @@ export const bindRuns = (document, runs) => {
230
234
  return found;
231
235
  };
232
236
  const fields = bindableFields(document);
233
- // Line items, a row at a time: each cell only on its description's band.
237
+ // Line items, a row at a time: each cell only on its band, never loose, where a total or a title could be.
234
238
  const lines = fields.filter(({ field }) => field.startsWith('lineItems.rows.'));
235
239
  const rows = new Map();
236
240
  for (const line of lines) {
237
241
  const row = line.field.split('.').slice(0, 3).join('.');
238
242
  rows.set(row, [...(rows.get(row) ?? []), line]);
239
243
  }
244
+ const onBand = (band) => (run) => Math.abs(runs[run].baseline - band) < 2;
240
245
  for (const cells of rows.values()) {
241
246
  const ordered = [...cells].sort((a, b) => Number(a.value.kind === 'money') - Number(b.value.kind === 'money'));
242
- let band;
243
- for (const { field, value } of ordered) {
244
- // A row's amounts are only taken on the band its text anchors: never loose, where a total could be.
245
- if (band === undefined && value.kind !== 'text')
246
- break;
247
- const before = segments.length;
248
- bindEverywhere(field, value, (run) => band === undefined || Math.abs(runs[run].baseline - band) < 2);
249
- const placed = segments[before];
250
- if (band === undefined && placed !== undefined)
251
- band = runs[placed.run].baseline;
252
- }
247
+ const [first, ...others] = ordered;
248
+ // A row is anchored by its text: one with none is left to the rest.
249
+ if (first === undefined || first.value.kind !== 'text')
250
+ continue;
251
+ // Its band: of the lines its first text is written on, the one where most of its other cells are found too. Its
252
+ // description is often in the title as well ("Website care, December" over a line "Website care").
253
+ const bands = [...new Set(runs.flatMap((r, at) => (bindEverywhere(first.field, first.value, (run) => run === at, true) ? [r.baseline] : [])))];
254
+ const together = (band) => others.filter(({ field, value }) => bindEverywhere(field, value, onBand(band), true)).length;
255
+ const band = bands.reduce((best, line) => (best === undefined || together(line) > together(best) ? line : best), undefined);
256
+ if (band === undefined)
257
+ continue;
258
+ for (const { field, value } of ordered)
259
+ bindEverywhere(field, value, onBand(band));
253
260
  }
254
261
  // Everything else, longest writing first so "A$1,452.00" is not taken as "1,452".
255
262
  const rest = fields
@@ -11,7 +11,7 @@
11
11
  * read from the page's own font files (fonts.ts), images as their pixels, and
12
12
  * an SVG logo as its outlines when they draw exactly as the browser did.
13
13
  */
14
- import opentype from 'opentype.js';
14
+ import * as opentypeModule from 'opentype.js';
15
15
  import { zlibSync } from 'fflate';
16
16
  import { Resvg } from '@resvg/resvg-wasm';
17
17
  import { BtpBoundViewSchema, checkBoundView, boundValue, writtenValue, } from '@btps/sdk/boundview';
@@ -30,6 +30,16 @@ export class BoundViewSnapshotError extends Error {
30
30
  this.name = 'BoundViewSnapshotError';
31
31
  }
32
32
  }
33
+ /**
34
+ * opentype.js's `parse`, from whichever build the app's bundler or Node chose: its ES module build names it and has no
35
+ * default export (a strict bundler refuses a default import of it); its UMD build is a default export holding it.
36
+ */
37
+ const parseFont = (buffer) => {
38
+ const named = opentypeModule.parse;
39
+ // Read by reflection: a bundler that sees `.default` on a module without one warns about it.
40
+ const umd = Reflect.get(opentypeModule, 'default');
41
+ return (named ?? umd.parse)(buffer);
42
+ };
33
43
  const r2 = (value) => Math.round(value * 100) / 100;
34
44
  const ceil2 = (value) => Math.ceil(value * 100 - 1e-6) / 100;
35
45
  // ---- Colour ----
@@ -246,7 +256,7 @@ const fontFile = (family, weight, style) => {
246
256
  if (!best)
247
257
  return undefined;
248
258
  try {
249
- return opentype.parse(await (await fetch(best.url)).arrayBuffer());
259
+ return parseFont(await (await fetch(best.url)).arrayBuffer());
250
260
  }
251
261
  catch {
252
262
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btps/boundview",
3
- "version": "1.2.0-rc.3",
3
+ "version": "1.2.0-rc.3.1",
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",