@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/glyphs/arimo-more.json +1 -0
- package/dist/glyphs/caladea-more.json +1 -0
- package/dist/glyphs/carlito-more.json +1 -0
- package/dist/glyphs/caveat-more.json +1 -0
- package/dist/glyphs/cousine-more.json +1 -0
- package/dist/glyphs/tinos-more.json +1 -0
- package/dist/glyphs.d.ts +7 -2
- package/dist/glyphs.js +41 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/render.js +15 -3
- package/dist/signature.d.ts +35 -0
- package/dist/signature.js +86 -0
- package/dist/snapshot/bind.d.ts +13 -4
- package/dist/snapshot/bind.js +334 -118
- package/dist/snapshot/decor.d.ts +70 -0
- package/dist/snapshot/decor.js +644 -0
- package/dist/snapshot/fonts.d.ts +28 -3
- package/dist/snapshot/fonts.js +23 -4
- package/dist/snapshot/index.d.ts +46 -1
- package/dist/snapshot/index.js +1257 -181
- package/dist/snapshot/layers.d.ts +32 -0
- package/dist/snapshot/layers.js +232 -0
- package/dist/snapshot/woff2.d.ts +10 -0
- package/dist/snapshot/woff2.js +391 -0
- package/dist/svg.d.ts +1 -3
- package/dist/svg.js +39 -26
- package/package.json +7 -2
package/dist/snapshot/bind.js
CHANGED
|
@@ -7,11 +7,16 @@
|
|
|
7
7
|
/*
|
|
8
8
|
* Binding by value: which text in a preview shows which signed field, and in
|
|
9
9
|
* which format. The preview is the platform's own; nothing in it is marked.
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
*
|
|
11
|
+
* The page's text is read as one stream, in the order the page has it, with
|
|
12
|
+
* each break between its lines and elements: a value may be split across
|
|
13
|
+
* elements (<h1>300</h1><h2>Empire</h2>, <span>INV-</span><b>0042</b>) or
|
|
14
|
+
* wrapped over lines, and is found whole all the same, then bound in the
|
|
15
|
+
* pieces it is drawn in, each by its range of the value as written. A binding
|
|
16
|
+
* is kept only when the receiver would write the field exactly as the preview
|
|
17
|
+
* showed it (`writtenValue`), so a binding can never change a fact.
|
|
13
18
|
*/
|
|
14
|
-
import { boundValue, writtenValue } from '@btps/sdk/boundview';
|
|
19
|
+
import { boundValue, keyFacts, markdownHidden, writtenValue } from '@btps/sdk/boundview';
|
|
15
20
|
const DATE_PATTERNS = [
|
|
16
21
|
'd MMMM yyyy',
|
|
17
22
|
'MMMM d, yyyy',
|
|
@@ -77,105 +82,252 @@ const writings = (field, value) => {
|
|
|
77
82
|
return out;
|
|
78
83
|
};
|
|
79
84
|
const isSpace = (char) => char === undefined || /\s/u.test(char);
|
|
85
|
+
/** Whether `field` is written in markdown (a letter's content, or a note), whose marks are not drawn. */
|
|
86
|
+
const markdownOf = (document, field) => {
|
|
87
|
+
const value = field === 'body.content' ? document.body : field === 'description' ? document.description : undefined;
|
|
88
|
+
return typeof value === 'object' && value !== null && value.format === 'markdown';
|
|
89
|
+
};
|
|
80
90
|
/** A match starts and ends a word: nothing but space or punctuation beside it. */
|
|
81
|
-
const isEdge = (char) => char === undefined || /[\s,.;:()[\]{}"'
|
|
91
|
+
const isEdge = (char) => char === undefined || /[\s,.;:()[\]{}"'“”‘’!?/|–—·•-]/u.test(char);
|
|
82
92
|
/**
|
|
83
93
|
* Binds the preview's runs to `document`'s fields. Line items first, a row at
|
|
84
|
-
* a time
|
|
85
|
-
* then every other field, the longest writing first
|
|
86
|
-
* Whatever is left is the sender's own words.
|
|
94
|
+
* a time where its cells stand together, so a line's amount is never taken for
|
|
95
|
+
* a total; then every other field, the longest writing first. Fields written
|
|
96
|
+
* alike share their places. Whatever is left is the sender's own words.
|
|
87
97
|
*/
|
|
88
98
|
export const bindRuns = (document, runs) => {
|
|
89
99
|
const characters = runs.map((run) => [...run.text]);
|
|
90
100
|
const taken = characters.map((chars) => chars.map(() => false));
|
|
91
101
|
const segments = [];
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
102
|
+
const top = (run) => runs[run]?.top ?? (runs[run]?.baseline ?? 0) - 1;
|
|
103
|
+
const bottom = (run) => runs[run]?.bottom ?? (runs[run]?.baseline ?? 0) + 1;
|
|
104
|
+
// ---- The stream ----
|
|
105
|
+
const stream = [];
|
|
106
|
+
const startOf = [];
|
|
107
|
+
runs.forEach((run, index) => {
|
|
108
|
+
if (index > 0) {
|
|
109
|
+
const before = runs[index - 1];
|
|
110
|
+
const sameLine = Math.abs(before.baseline - run.baseline) < 2;
|
|
111
|
+
const gap = (run.chars[0]?.left ?? 0) - (before.chars.at(-1)?.right ?? 0);
|
|
112
|
+
const size = run.size ?? before.size ?? 12;
|
|
113
|
+
// The next line of the same text, or of the same block's inline text: a wrap. Beside it on one line: touching,
|
|
114
|
+
// a join; a space or so apart, a space. Anything else (another block, far apart): a break.
|
|
115
|
+
const wraps = run.node !== undefined && run.node === before.node;
|
|
116
|
+
const flows = !sameLine && run.block !== undefined && run.block === before.block;
|
|
117
|
+
stream.push({
|
|
118
|
+
gap: wraps
|
|
119
|
+
? 'wrap'
|
|
120
|
+
: flows
|
|
121
|
+
? 'flow'
|
|
122
|
+
: sameLine && gap >= -1 && gap <= 0.15 * size
|
|
123
|
+
? 'join'
|
|
124
|
+
: sameLine && gap > 0 && gap <= 1.2 * size
|
|
125
|
+
? 'space'
|
|
126
|
+
: 'break',
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
startOf.push(stream.length);
|
|
130
|
+
characters[index].forEach((char, at) => stream.push({ run: index, at, char }));
|
|
131
|
+
});
|
|
132
|
+
/** Where each character first appears in the stream, so a search starts only where a value can. */
|
|
133
|
+
const byChar = new Map();
|
|
134
|
+
stream.forEach((item, at) => {
|
|
135
|
+
if ('char' in item)
|
|
136
|
+
byChar.set(item.char, [...(byChar.get(item.char) ?? []), at]);
|
|
137
|
+
});
|
|
138
|
+
const isFree = (placed) => placed.every(({ run, at }) => !taken[run][at]);
|
|
139
|
+
/**
|
|
140
|
+
* Whether what stands beside a match ends a word: nothing, a break, or an edge character, looking through a `join`
|
|
141
|
+
* or `wrap` to the character beyond it ("No. " and "A1" in touching elements); a `flow` ends a word.
|
|
142
|
+
*/
|
|
143
|
+
const edgeAt = (at, step) => {
|
|
144
|
+
for (let i = at;; i += step) {
|
|
145
|
+
const item = stream[i];
|
|
146
|
+
if (item === undefined)
|
|
147
|
+
return true;
|
|
148
|
+
if ('char' in item)
|
|
149
|
+
return isEdge(item.char);
|
|
150
|
+
if (item.gap === 'space' || item.gap === 'break' || item.gap === 'flow')
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
97
153
|
};
|
|
154
|
+
const edgeBefore = (at) => edgeAt(at - 1, -1);
|
|
155
|
+
const edgeAfter = (at) => edgeAt(at, 1);
|
|
98
156
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
157
|
+
* `want` matched from stream position `start`: its characters' places, and where the stream goes on after it;
|
|
158
|
+
* undefined where it does not stand there. A space in `want` matches any run of whitespace and breaks; a character
|
|
159
|
+
* `hidden` says is not drawn (a markdown mark) may be missing.
|
|
101
160
|
*/
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
161
|
+
const matchAt = (want, hidden, start) => {
|
|
162
|
+
const placed = [];
|
|
163
|
+
let w = 0;
|
|
164
|
+
let s = start;
|
|
165
|
+
while (w < want.length) {
|
|
166
|
+
const char = want[w];
|
|
167
|
+
if (isSpace(char)) {
|
|
168
|
+
const from = w;
|
|
169
|
+
while (w < want.length && isSpace(want[w]))
|
|
170
|
+
w++;
|
|
171
|
+
const spaces = [];
|
|
172
|
+
let broke = false;
|
|
173
|
+
let crossed = 0;
|
|
174
|
+
while (s < stream.length) {
|
|
175
|
+
const item = stream[s];
|
|
176
|
+
if ('gap' in item)
|
|
177
|
+
broke = true;
|
|
178
|
+
else if (isSpace(item.char))
|
|
179
|
+
spaces.push({ run: item.run, at: item.at, of: from + spaces.length });
|
|
180
|
+
else
|
|
181
|
+
break;
|
|
182
|
+
s++;
|
|
183
|
+
crossed++;
|
|
184
|
+
}
|
|
185
|
+
// Spaces drawn as the value has them, the same characters on one line, are part of the piece; anything else
|
|
186
|
+
// (a line break in the value laid out as a space) divides it, so each piece writes exactly what it shows.
|
|
187
|
+
const same = spaces.every((space) => characters[space.run][space.at] === want[space.of]);
|
|
188
|
+
if (!broke && spaces.length === w - from && same)
|
|
189
|
+
placed.push(...spaces);
|
|
190
|
+
if (crossed === 0) {
|
|
191
|
+
// A space the page does not draw: allowed only where a markdown mark beside it is not drawn either.
|
|
192
|
+
if (!(hidden[from - 1] || hidden[w] || from === 0))
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
continue;
|
|
109
196
|
}
|
|
197
|
+
// What is never drawn (a link's destination) is never looked for on the page.
|
|
198
|
+
if (hidden[w] === 2) {
|
|
199
|
+
w++;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const item = stream[s];
|
|
203
|
+
if (item !== undefined && 'gap' in item && item.gap !== 'space' && item.gap !== 'break' && placed.length > 0) {
|
|
204
|
+
s++;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (item !== undefined && 'char' in item && item.char === char) {
|
|
208
|
+
placed.push({ run: item.run, at: item.at, of: w });
|
|
209
|
+
w++;
|
|
210
|
+
s++;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (hidden[w]) {
|
|
214
|
+
w++;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
return undefined;
|
|
110
218
|
}
|
|
111
|
-
return
|
|
219
|
+
return placed.length > 0 ? { placed, end: s } : undefined;
|
|
112
220
|
};
|
|
113
|
-
/**
|
|
114
|
-
const
|
|
115
|
-
const
|
|
221
|
+
/** Every place `text` is written in the stream, whole, as a word (`word`), each free of any fact already bound. */
|
|
222
|
+
const findAll = (text, { word, markdown }) => {
|
|
223
|
+
const want = [...text];
|
|
224
|
+
const hidden = markdown ? markdownHidden(text) : want.map(() => 0);
|
|
225
|
+
let first = 0;
|
|
226
|
+
while (first < want.length && (hidden[first] || isSpace(want[first])))
|
|
227
|
+
first++;
|
|
228
|
+
if (first === want.length)
|
|
229
|
+
return [];
|
|
230
|
+
const found = [];
|
|
231
|
+
let after = -1;
|
|
232
|
+
for (const start of byChar.get(want[first]) ?? []) {
|
|
233
|
+
if (start < after)
|
|
234
|
+
continue;
|
|
235
|
+
if (word && !edgeBefore(start))
|
|
236
|
+
continue;
|
|
237
|
+
const match = matchAt(want.slice(first), hidden.slice(first), start);
|
|
238
|
+
if (!match || !isFree(match.placed))
|
|
239
|
+
continue;
|
|
240
|
+
if (word && !edgeAfter(match.end))
|
|
241
|
+
continue;
|
|
242
|
+
found.push(match.placed.map((place) => ({ ...place, of: place.of + first })));
|
|
243
|
+
after = match.end;
|
|
244
|
+
}
|
|
245
|
+
return found;
|
|
246
|
+
};
|
|
247
|
+
/** An amount found by its digits, with its sign, prefix and suffix from the runs it starts and ends in. */
|
|
248
|
+
const moneyPlace = (placed, bind, value) => {
|
|
249
|
+
const firstPlace = placed[0];
|
|
250
|
+
const lastPlace = placed.at(-1);
|
|
251
|
+
const head = characters[firstPlace.run];
|
|
252
|
+
const tail = characters[lastPlace.run];
|
|
253
|
+
const at = firstPlace.at;
|
|
116
254
|
let from = at;
|
|
117
|
-
while (from > 0 && !isSpace(
|
|
255
|
+
while (from > 0 && !isSpace(head[from - 1]) && at - from < 8 && !/\d/.test(head[from - 1] ?? ''))
|
|
118
256
|
from--;
|
|
119
257
|
// A code before the number with a space between: "AUD 1,452.00".
|
|
120
|
-
if (from === at &&
|
|
258
|
+
if (from === at && head[at - 1] === ' ' && /^[A-Z]{3}$/.test(head.slice(at - 4, at - 1).join('')) && isSpace(head[at - 5])) {
|
|
121
259
|
from = at - 4;
|
|
122
260
|
}
|
|
123
|
-
|
|
124
|
-
|
|
261
|
+
const end = lastPlace.at + 1;
|
|
262
|
+
let to = end;
|
|
263
|
+
while (to < tail.length && !isSpace(tail[to]) && to - end < 8 && !/\d/.test(tail[to] ?? ''))
|
|
125
264
|
to++;
|
|
126
|
-
if (to ===
|
|
265
|
+
if (to === end && tail[to] === ' ' && /^[A-Z]{3}$/.test(tail.slice(to + 1, to + 4).join('')) && isSpace(tail[to + 4]))
|
|
127
266
|
to += 4;
|
|
128
|
-
}
|
|
129
267
|
// A negative amount is written with '-' before its prefix.
|
|
130
|
-
const sign = value.kind === 'money' && value.amount < 0 &&
|
|
131
|
-
const prefix =
|
|
132
|
-
const suffix =
|
|
133
|
-
if (!isEdge(
|
|
268
|
+
const sign = value.kind === 'money' && value.amount < 0 && head[from] === '-' ? 1 : 0;
|
|
269
|
+
const prefix = head.slice(from + sign, at).join('');
|
|
270
|
+
const suffix = tail.slice(end, to).join('');
|
|
271
|
+
if (!isEdge(head[from - 1]) || !isEdge(tail[to]))
|
|
134
272
|
return undefined;
|
|
135
273
|
const format = bind.format.money;
|
|
136
|
-
const full = {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
274
|
+
const full = { field: bind.field, format: { money: { ...format, ...(prefix && { prefix }), ...(suffix && { suffix }) } } };
|
|
275
|
+
const lead = at - from;
|
|
276
|
+
const whole = [
|
|
277
|
+
...Array.from({ length: lead }, (_, i) => ({ run: firstPlace.run, at: from + i, of: i })),
|
|
278
|
+
...placed.map((place) => ({ ...place, of: place.of + lead })),
|
|
279
|
+
...Array.from({ length: to - end }, (_, i) => ({ run: lastPlace.run, at: end + i, of: lead + placed.length + i })),
|
|
280
|
+
];
|
|
281
|
+
// Every character shown is the value's own, as written, and every one but a space (where it wraps) is shown.
|
|
282
|
+
const written = [...writtenValue(value, full)];
|
|
283
|
+
const shows = whole.every(({ run, at: index, of }) => characters[run][index] === written[of]);
|
|
284
|
+
const covered = new Set(whole.map(({ of }) => of));
|
|
285
|
+
const complete = written.every((char, index) => covered.has(index) || isSpace(char));
|
|
286
|
+
return shows && complete && isFree(whole) ? { placed: whole, bind: full, core: placed } : undefined;
|
|
142
287
|
};
|
|
143
|
-
/**
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
288
|
+
/** Every place a field is written, with the binding that writes it there: the longest writing first. */
|
|
289
|
+
/** `core`: an amount's digits, without the sign, prefix or suffix found beside them. */
|
|
290
|
+
const placesOf = (field, value, only) => {
|
|
291
|
+
const out = [];
|
|
292
|
+
const markdown = markdownOf(document, field);
|
|
293
|
+
for (const { text, bind } of writings(field, value).sort((a, b) => [...b.text].length - [...a.text].length)) {
|
|
147
294
|
const money = value.kind === 'money';
|
|
148
295
|
// An amount is found by its digits: its sign and prefix come before them.
|
|
149
296
|
const number = money && value.amount < 0 ? text.slice(1) : text;
|
|
150
|
-
for (
|
|
151
|
-
if (only && !only(run))
|
|
297
|
+
for (const placed of findAll(number, { word: !money, markdown })) {
|
|
298
|
+
if (only && !placed.every(({ run }) => only(run)))
|
|
152
299
|
continue;
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (placed) {
|
|
157
|
-
if (look)
|
|
158
|
-
return true;
|
|
159
|
-
take(run, placed.from, placed.to, placed.bind);
|
|
160
|
-
found = true;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
if (look)
|
|
165
|
-
return true;
|
|
166
|
-
take(run, at, at + [...number].length, bind);
|
|
167
|
-
found = true;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
300
|
+
const full = money ? moneyPlace(placed, bind, value) : { placed, bind };
|
|
301
|
+
if (full && !out.some((other) => other.placed.some((a) => full.placed.some((b) => a.run === b.run && a.at === b.at))))
|
|
302
|
+
out.push(full);
|
|
170
303
|
}
|
|
171
|
-
if (
|
|
172
|
-
|
|
304
|
+
if (out.length > 0)
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
return out;
|
|
308
|
+
};
|
|
309
|
+
/** Binds a field where it was found: each piece it is drawn in, by its range of the value as written. */
|
|
310
|
+
const take = ({ placed, bind }, value) => {
|
|
311
|
+
const length = [...writtenValue(value, { ...bind, range: undefined })].length;
|
|
312
|
+
const pieces = [];
|
|
313
|
+
for (const { run, at, of } of placed) {
|
|
314
|
+
taken[run][at] = true;
|
|
315
|
+
const last = pieces.at(-1);
|
|
316
|
+
if (last && last.run === run && last.to === at && last.end === of) {
|
|
317
|
+
last.to++;
|
|
318
|
+
last.end++;
|
|
319
|
+
}
|
|
320
|
+
else
|
|
321
|
+
pieces.push({ run, from: at, to: at + 1, of, end: of + 1 });
|
|
322
|
+
}
|
|
323
|
+
for (const { run, from, to, of, end } of pieces) {
|
|
324
|
+
const whole = pieces.length === 1 && of === 0 && end === length;
|
|
325
|
+
segments.push({ run, from, to, bind: { ...bind, ...(!whole && { range: [of, end] }) } });
|
|
173
326
|
}
|
|
174
|
-
return found;
|
|
175
327
|
};
|
|
176
328
|
/** Text shown in groups: "51 824 753 556" for "51824753556". */
|
|
177
|
-
const bindGroups = (field,
|
|
178
|
-
const want = [...text.replace(/ /g, '')];
|
|
329
|
+
const bindGroups = (field, value) => {
|
|
330
|
+
const want = [...value.text.replace(/ /g, '')];
|
|
179
331
|
if (want.length < 4)
|
|
180
332
|
return false;
|
|
181
333
|
for (let run = 0; run < runs.length; run++) {
|
|
@@ -203,73 +355,137 @@ export const bindRuns = (document, runs) => {
|
|
|
203
355
|
else
|
|
204
356
|
break;
|
|
205
357
|
}
|
|
206
|
-
|
|
207
|
-
|
|
358
|
+
const placed = Array.from({ length: i - at }, (_, n) => ({ run, at: at + n, of: n }));
|
|
359
|
+
if (matched === want.length && separator !== undefined && isEdge(chars[i]) && isFree(placed)) {
|
|
360
|
+
take({ placed, bind: { field, format: { groups: { sizes: [...sizes, size], separator } } } }, value);
|
|
208
361
|
return true;
|
|
209
362
|
}
|
|
210
363
|
}
|
|
211
364
|
}
|
|
212
365
|
return false;
|
|
213
366
|
};
|
|
214
|
-
/** A long text over several runs: each run that is part of it, bound by its range, in order. */
|
|
215
|
-
const bindRanges = (field, text) => {
|
|
216
|
-
const value = [...text];
|
|
217
|
-
let cursor = 0;
|
|
218
|
-
let found = false;
|
|
219
|
-
for (let run = 0; run < runs.length; run++) {
|
|
220
|
-
const trimmed = characters[run].join('').trim();
|
|
221
|
-
if (trimmed.length < 2 || !free(run, 0, characters[run].length))
|
|
222
|
-
continue;
|
|
223
|
-
const lead = characters[run].findIndex((char) => !isSpace(char));
|
|
224
|
-
const want = [...trimmed];
|
|
225
|
-
for (let at = cursor; at + want.length <= value.length; at++) {
|
|
226
|
-
if (want.every((char, i) => value[at + i] === char)) {
|
|
227
|
-
take(run, lead, lead + want.length, { field, range: [at, at + want.length] });
|
|
228
|
-
cursor = at + want.length;
|
|
229
|
-
found = true;
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return found;
|
|
235
|
-
};
|
|
236
367
|
const fields = bindableFields(document);
|
|
237
|
-
// Line items
|
|
368
|
+
// ---- Line items: a row at a time, where its cells stand together ----
|
|
238
369
|
const lines = fields.filter(({ field }) => field.startsWith('lineItems.rows.'));
|
|
239
370
|
const rows = new Map();
|
|
240
371
|
for (const line of lines) {
|
|
241
372
|
const row = line.field.split('.').slice(0, 3).join('.');
|
|
242
373
|
rows.set(row, [...(rows.get(row) ?? []), line]);
|
|
243
374
|
}
|
|
244
|
-
const
|
|
375
|
+
const roles = (document.lineItems?.roles ?? {});
|
|
376
|
+
const spanOf = (placed) => ({
|
|
377
|
+
top: Math.min(...placed.map(({ run }) => top(run))),
|
|
378
|
+
bottom: Math.max(...placed.map(({ run }) => bottom(run))),
|
|
379
|
+
});
|
|
380
|
+
const within = (span) => (run) => top(run) < span.bottom && bottom(run) > span.top;
|
|
381
|
+
// A place inside a longer match of another field is that field's (a description "Studio" in the name "Ζήτα Studio").
|
|
382
|
+
const longer = fields
|
|
383
|
+
.filter(({ field }) => !field.startsWith('lineItems.rows.'))
|
|
384
|
+
.flatMap(({ field, value }) => placesOf(field, value).map(({ placed }) => ({ kind: value.kind, at: new Set(placed.map(({ run, at }) => `${run}:${at}`)) })));
|
|
385
|
+
/** `whole`: the match `placed` is part of; another field's match of exactly that text, and of its kind, is the same value, not longer. */
|
|
386
|
+
const insideLonger = (placed, whole = placed, kind) => longer.some((other) => other.at.size > placed.length &&
|
|
387
|
+
placed.every(({ run, at }) => other.at.has(`${run}:${at}`)) &&
|
|
388
|
+
!(other.kind === kind && other.at.size === whole.length && whole.every(({ run, at }) => other.at.has(`${run}:${at}`))));
|
|
389
|
+
const lineHeight = (span) => Math.max(1, span.bottom - span.top);
|
|
390
|
+
// First where each row is: its text cell (the description) where most of its other cells stand beside it, on its
|
|
391
|
+
// lines or (a row laid out as a card) just below them.
|
|
392
|
+
const chosen = [];
|
|
393
|
+
const used = new Set();
|
|
245
394
|
for (const cells of rows.values()) {
|
|
246
395
|
const ordered = [...cells].sort((a, b) => Number(a.value.kind === 'money') - Number(b.value.kind === 'money'));
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
if (
|
|
250
|
-
|
|
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)
|
|
396
|
+
const anchorCell = ordered.find(({ field, value }) => value.kind === 'text' && roles[field.split('.').slice(3).join('.')] === 'description') ??
|
|
397
|
+
ordered.find(({ value }) => value.kind === 'text');
|
|
398
|
+
if (!anchorCell) {
|
|
399
|
+
chosen.push({ cells: ordered });
|
|
257
400
|
continue;
|
|
258
|
-
|
|
259
|
-
|
|
401
|
+
}
|
|
402
|
+
const others = ordered.filter((cell) => cell !== anchorCell);
|
|
403
|
+
const previous = [...chosen].reverse().find((row) => row.span)?.span;
|
|
404
|
+
const candidates = placesOf(anchorCell.field, anchorCell.value)
|
|
405
|
+
.filter(({ placed }) => !placed.some(({ run, at }) => used.has(`${run}:${at}`)) && !insideLonger(placed))
|
|
406
|
+
.map((place) => {
|
|
407
|
+
const span = spanOf(place.placed);
|
|
408
|
+
// Above it too: a cell of several lines aligned to the middle of its row starts above its description.
|
|
409
|
+
const reach = 1.5 * (cells.length + 1) * lineHeight(span);
|
|
410
|
+
const near = { top: span.top - reach, bottom: span.bottom + reach };
|
|
411
|
+
// Which of its other cells stand near it, and how near: a description is where its own cells are.
|
|
412
|
+
// In whole lines: a cell aligned to the middle of its row is no further than one on its first line.
|
|
413
|
+
const distances = others.flatMap(({ field, value }) => {
|
|
414
|
+
const tops = placesOf(field, value, within(near))
|
|
415
|
+
.filter(({ placed }) => !insideLonger(placed))
|
|
416
|
+
.map(({ placed }) => Math.floor(Math.abs(top(placed[0].run) - span.top) / lineHeight(span)));
|
|
417
|
+
return tops.length > 0 ? [Math.min(...tops)] : [];
|
|
418
|
+
});
|
|
419
|
+
const distance = distances.reduce((sum, d) => sum + d, 0);
|
|
420
|
+
return { place, span, close: distances.length, distance, below: previous === undefined || span.top >= previous.top - 1 };
|
|
421
|
+
})
|
|
422
|
+
// Most of its cells near it; then after the row before it; then nearest them; then first on the page.
|
|
423
|
+
.sort((a, b) => b.close - a.close || Number(b.below) - Number(a.below) || a.distance - b.distance || a.span.top - b.span.top);
|
|
424
|
+
// Rows written alike are in their order: of places as good but for a line a cell, the first on the page.
|
|
425
|
+
const first = candidates[0];
|
|
426
|
+
const best = first && candidates
|
|
427
|
+
.filter((c) => c.close === first.close && c.below === first.below && c.distance <= first.distance + others.length)
|
|
428
|
+
.reduce((a, b) => (b.span.top < a.span.top ? b : a));
|
|
429
|
+
if (best)
|
|
430
|
+
for (const { run, at } of best.place.placed)
|
|
431
|
+
used.add(`${run}:${at}`);
|
|
432
|
+
chosen.push({ cells: ordered, ...(best && { anchor: { ...best.place, value: anchorCell.value }, span: best.span }) });
|
|
260
433
|
}
|
|
261
|
-
//
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
434
|
+
// Then each row's cells, once each, in the order the document lists its columns: on the row's lines, or below them
|
|
435
|
+
// before the next row (a row laid out as a card). A unit price and an amount that are the same stand side by side.
|
|
436
|
+
chosen.forEach(({ cells, anchor, span }, index) => {
|
|
437
|
+
if (!anchor || !span)
|
|
438
|
+
return;
|
|
439
|
+
take(anchor, anchor.value);
|
|
440
|
+
// The row's own stretch of the page: from just after the row before it to just before the row after it, so a cell
|
|
441
|
+
// set above its description (a table's cells aligned to the middle) or below it (a card, a line a cell) is found.
|
|
442
|
+
// The first and last rows have no neighbour that side: as far as a line a cell, and some, would reach.
|
|
443
|
+
const previous = [...chosen.slice(0, index)].reverse().find((row) => row.span)?.span;
|
|
444
|
+
const next = chosen.slice(index + 1).find((row) => row.span)?.span;
|
|
445
|
+
const reach = 1.5 * (cells.length + 1) * lineHeight(span);
|
|
446
|
+
const from = previous?.bottom ?? span.top - reach;
|
|
447
|
+
const to = next?.top ?? span.bottom + reach;
|
|
448
|
+
const stretch = (run) => bottom(run) > from && top(run) < to;
|
|
449
|
+
// The longest written first, so an amount "CHF 3 150,00" is its own before a tax "CHF 150,00" looks inside it.
|
|
450
|
+
const written = (cell) => Math.max(0, ...writings(cell.field, cell.value).map(({ text }) => [...text].length));
|
|
451
|
+
for (const { field, value } of [...cells].sort((a, b) => written(b) - written(a))) {
|
|
452
|
+
if (field === anchor.bind.field)
|
|
453
|
+
continue;
|
|
454
|
+
// Never a place inside a longer match of another field: a quantity "10" in a date "10/02/2026".
|
|
455
|
+
// An amount by its digits: "A1" is an invoice number, not the amount 1 with a prefix "A".
|
|
456
|
+
const own = (only) => placesOf(field, value, only).find(({ placed, core }) => !insideLonger(core ?? placed, placed, value.kind));
|
|
457
|
+
const place = own(within(span)) ?? own(stretch);
|
|
458
|
+
if (place)
|
|
459
|
+
take(place, value);
|
|
460
|
+
}
|
|
267
461
|
});
|
|
268
|
-
|
|
269
|
-
|
|
462
|
+
// ---- Everything else: every place each field is written, the longest match first ----
|
|
463
|
+
// A match inside a longer one is the longer one's: "2026-12-17" in "INV-2026-12-17" is the invoice number's, and
|
|
464
|
+
// "1,452" in "A$1,452.00" is the amount's. Fields written alike (a receipt's total and the amount paid) share their
|
|
465
|
+
// places, each taking the first free, so every one of them is bound where it can be.
|
|
466
|
+
const rest = fields.filter(({ field }) => !field.startsWith('lineItems.rows.'));
|
|
467
|
+
const candidates = rest.flatMap(({ field, value }, order) => placesOf(field, value).map((place, nth) => ({ ...place, value, order, nth })));
|
|
468
|
+
candidates.sort((a, b) => b.placed.length - a.placed.length || a.nth - b.nth || a.order - b.order);
|
|
469
|
+
const takes = new Map();
|
|
470
|
+
const where = (placed) => placed.map(({ run, at }) => `${run}:${at}`).join(',');
|
|
471
|
+
const key = new Set(keyFacts(document));
|
|
472
|
+
for (const candidate of candidates) {
|
|
473
|
+
if (!isFree(candidate.placed))
|
|
270
474
|
continue;
|
|
271
|
-
|
|
272
|
-
|
|
475
|
+
// Of the fields that could be bound here, the one bound the fewest times yet; where text could be either of two
|
|
476
|
+
// (02/01/2026: one date day first, another month first), a fact the document must show before one it need not.
|
|
477
|
+
const here = where(candidate.placed);
|
|
478
|
+
const chosen = candidates
|
|
479
|
+
.filter((other) => where(other.placed) === here)
|
|
480
|
+
.sort((a, b) => (takes.get(a.bind.field) ?? 0) - (takes.get(b.bind.field) ?? 0) ||
|
|
481
|
+
Number(key.has(b.bind.field)) - Number(key.has(a.bind.field)) ||
|
|
482
|
+
a.order - b.order)[0];
|
|
483
|
+
take(chosen, chosen.value);
|
|
484
|
+
takes.set(chosen.bind.field, (takes.get(chosen.bind.field) ?? 0) + 1);
|
|
485
|
+
}
|
|
486
|
+
for (const { field, value } of rest) {
|
|
487
|
+
if (!takes.has(field) && value.kind === 'text')
|
|
488
|
+
bindGroups(field, value);
|
|
273
489
|
}
|
|
274
490
|
// The sender's own words: every stretch of a run no field took.
|
|
275
491
|
characters.forEach((chars, run) => {
|
|
@@ -0,0 +1,70 @@
|
|
|
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 { BTPBoundViewElement } from '@btps/sdk/boundview';
|
|
8
|
+
type Element = BTPBoundViewElement;
|
|
9
|
+
export type Rect = {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
};
|
|
15
|
+
/** A corner's horizontal and vertical radius. Corners in CSS order: top left, top right, bottom right, bottom left. */
|
|
16
|
+
export type Corner = [number, number];
|
|
17
|
+
export type Corners = [Corner, Corner, Corner, Corner];
|
|
18
|
+
export type Side = {
|
|
19
|
+
width: number;
|
|
20
|
+
style: string;
|
|
21
|
+
colour?: string;
|
|
22
|
+
};
|
|
23
|
+
/** A CSS list split at its commas outside any brackets: "linear-gradient(90deg, rgb(1, 2, 3) 0%), url(a.png)" is two. */
|
|
24
|
+
export declare const splitTop: (value: string) => string[];
|
|
25
|
+
/** Each corner's radii from computed style ("12px", "12px 8px", "50%"), scaled down together where they overlap (CSS). */
|
|
26
|
+
export declare const cornersOf: (css: CSSStyleDeclaration, width: number, height: number) => Corners;
|
|
27
|
+
export declare const isRound: (corners: Corners) => boolean;
|
|
28
|
+
/** Corners `by` further out (negative: in), as a border's inner edge or an outline's outer one; a square corner stays square. */
|
|
29
|
+
export declare const grown: (corners: Corners, top: number, right: number, bottom: number, left: number) => Corners;
|
|
30
|
+
/** A rectangle with its corners, as path data: drawn clockwise from the top left. */
|
|
31
|
+
export declare const roundedRect: (r: Rect, corners?: Corners) => string;
|
|
32
|
+
/** A box's border, as the browser paints it from its four sides and its corners. */
|
|
33
|
+
export declare const borders: (box: Rect, corners: Corners, sides: [Side, Side, Side, Side]) => Element[];
|
|
34
|
+
type Shadow = {
|
|
35
|
+
x: number;
|
|
36
|
+
y: number;
|
|
37
|
+
blur: number;
|
|
38
|
+
spread: number;
|
|
39
|
+
colour: string;
|
|
40
|
+
alpha: number;
|
|
41
|
+
inset: boolean;
|
|
42
|
+
};
|
|
43
|
+
/** Computed box-shadow ("rgba(0, 0, 0, 0.2) 0px 1px 3px 0px, ... inset") as its shadows, first painted last. */
|
|
44
|
+
export declare const shadowsOf: (value: string, colourOf: (css: string) => string | undefined) => Shadow[];
|
|
45
|
+
/**
|
|
46
|
+
* A box's outer shadows, drawn before its background, outside the box only, as the browser clips them to it. A blurred
|
|
47
|
+
* edge as layers: each the shadow grown by a step of the blur, faint enough that where all lie it has its full strength,
|
|
48
|
+
* so the edge fades over the blur's width as the browser's Gaussian does.
|
|
49
|
+
*/
|
|
50
|
+
export declare const outerShadows: (box: Rect, corners: Corners, shadows: Shadow[]) => Element[];
|
|
51
|
+
/** A box's inset shadows, drawn over its background: inside its padding box, where the shifted, shrunk box is not. */
|
|
52
|
+
export declare const insetShadows: (padding: Rect, corners: Corners, shadows: Shadow[]) => Element[];
|
|
53
|
+
/** A box's outline: outside its border by its offset, following its corners (as Chrome draws it since 94). */
|
|
54
|
+
export declare const outline: (box: Rect, corners: Corners, css: CSSStyleDeclaration, colourOf: (css: string) => string | undefined) => Element[];
|
|
55
|
+
/** A position ("50%", "10px", "left", "right 8px", "calc(100% - 8px)") along `room`, as background and gradient centres use. */
|
|
56
|
+
export declare const positionAlong: (value: string, room: number) => number;
|
|
57
|
+
/**
|
|
58
|
+
* A radial-gradient layer in a box: a circle as BoundView's radial paint, exactly; an ellipse as bands of its colours,
|
|
59
|
+
* each cut to the box, at most a step of 1/32 of the gradient apart.
|
|
60
|
+
*/
|
|
61
|
+
export declare const radialBackground: (box: Rect, corners: Corners, value: string, colourOf: (css: string) => string | undefined) => Element[] | undefined;
|
|
62
|
+
/** A background image's drawn size from background-size, in its positioning area: cover, contain, lengths, auto. */
|
|
63
|
+
export declare const backgroundSize: (value: string, areaW: number, areaH: number, imageW: number, imageH: number) => [number, number];
|
|
64
|
+
/** A linear-gradient layer as BoundView's linear paint, along the gradient line CSS draws for its angle. */
|
|
65
|
+
export declare const linearPaint: (box: Rect, value: string, colourOf: (css: string) => string | undefined) => unknown;
|
|
66
|
+
/** A repeating-linear-gradient layer: its stripes, each cut to the box, soft edges in a few steps. */
|
|
67
|
+
export declare const repeatingLinear: (box: Rect, corners: Corners, value: string, colourOf: (css: string) => string | undefined) => Element[] | undefined;
|
|
68
|
+
/** A line drawn as text decoration: solid, double, dotted, dashed or wavy, from x1 to x2 at y (its middle). */
|
|
69
|
+
export declare const decorationLine: (x1: number, x2: number, y: number, thickness: number, style: string, colour: string) => Element[];
|
|
70
|
+
export {};
|