@wirelith/core 0.0.0 → 0.1.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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +48 -0
  3. package/README.md +49 -1
  4. package/dist/ai-spec.d.ts +5 -0
  5. package/dist/ai-spec.js +7 -0
  6. package/dist/ai-spec.js.map +1 -0
  7. package/dist/api.d.ts +54 -0
  8. package/dist/api.js +236 -0
  9. package/dist/api.js.map +1 -0
  10. package/dist/components.d.ts +43 -0
  11. package/dist/components.js +159 -0
  12. package/dist/components.js.map +1 -0
  13. package/dist/data/ai-spec.d.ts +2 -0
  14. package/dist/data/ai-spec.js +5 -0
  15. package/dist/data/ai-spec.js.map +1 -0
  16. package/dist/data/fonts.d.ts +2 -0
  17. package/dist/data/fonts.js +5 -0
  18. package/dist/data/fonts.js.map +1 -0
  19. package/dist/data/icons.d.ts +4 -0
  20. package/dist/data/icons.js +7 -0
  21. package/dist/data/icons.js.map +1 -0
  22. package/dist/format.d.ts +7 -0
  23. package/dist/format.js +189 -0
  24. package/dist/format.js.map +1 -0
  25. package/dist/index.d.ts +10 -0
  26. package/dist/index.js +11 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/layout.d.ts +156 -0
  29. package/dist/layout.js +1118 -0
  30. package/dist/layout.js.map +1 -0
  31. package/dist/measure.d.ts +27 -0
  32. package/dist/measure.js +105 -0
  33. package/dist/measure.js.map +1 -0
  34. package/dist/overview.d.ts +37 -0
  35. package/dist/overview.js +523 -0
  36. package/dist/overview.js.map +1 -0
  37. package/dist/parse.d.ts +57 -0
  38. package/dist/parse.js +609 -0
  39. package/dist/parse.js.map +1 -0
  40. package/dist/render.d.ts +12 -0
  41. package/dist/render.js +980 -0
  42. package/dist/render.js.map +1 -0
  43. package/dist/svg.d.ts +18 -0
  44. package/dist/svg.js +97 -0
  45. package/dist/svg.js.map +1 -0
  46. package/dist/theme.d.ts +42 -0
  47. package/dist/theme.js +220 -0
  48. package/dist/theme.js.map +1 -0
  49. package/dist/types.d.ts +106 -0
  50. package/dist/types.js +3 -0
  51. package/dist/types.js.map +1 -0
  52. package/dist/validate.d.ts +35 -0
  53. package/dist/validate.js +450 -0
  54. package/dist/validate.js.map +1 -0
  55. package/package.json +38 -5
package/dist/layout.js ADDED
@@ -0,0 +1,1118 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Layout (spec section 10 and Appendix C): a box for every element, then the overview of frames,
3
+ // notes and arrows (sections 6, 7.4 and 10.6).
4
+ import { DEVICES, OVERLAYS } from './components.js';
5
+ import { lineWidth, longestWord, measure, r, wrap } from './measure.js';
6
+ import { diagnostic } from './parse.js';
7
+ import { textOf, walk } from './validate.js';
8
+ const MAIN_H = new Set(['row', 'header', 'footer']);
9
+ /** A layout-only node: a card's title, drawn as the first entry of the card's header (Appendix C.7). */
10
+ export const CARD_TITLE = 'card-title';
11
+ /** Leaves drawn at their own width at the start of a wider box (Appendix C.1). */
12
+ const OWN_WIDTH = new Set(['badge', 'avatar', 'icon', 'checkbox', 'toggle', 'radio']);
13
+ const CONTAINERS = new Set([
14
+ 'row',
15
+ 'column',
16
+ 'grid',
17
+ 'header',
18
+ 'footer',
19
+ 'sidebar',
20
+ 'card',
21
+ 'tabs',
22
+ 'list',
23
+ 'empty',
24
+ 'error',
25
+ 'nav',
26
+ 'table',
27
+ ]);
28
+ const has = (n, f) => n.flags?.includes(f) ?? false;
29
+ const num = (v) => (typeof v === 'number' ? v : undefined);
30
+ export class Layout {
31
+ doc;
32
+ theme;
33
+ u;
34
+ warnings = [];
35
+ index = new Map();
36
+ /** The title nodes of cards whose title moved into their header, keyed by the header. */
37
+ barTitles = new Map();
38
+ constructor(doc, theme) {
39
+ this.doc = doc;
40
+ this.theme = theme;
41
+ this.u = theme.space;
42
+ for (const w of walk(doc))
43
+ this.index.set(w.node, w.index);
44
+ }
45
+ // ---- fonts ------------------------------------------------------------------------------
46
+ body(size, weight) {
47
+ return { font: this.theme.fontBody, weight, size };
48
+ }
49
+ heading(size, weight) {
50
+ return { font: this.theme.fontHeading, weight, size };
51
+ }
52
+ /** Font and line height of a text component (Appendix C.4). */
53
+ textStyle(type) {
54
+ switch (type) {
55
+ case 'h1':
56
+ return { f: this.heading(32, 700), lh: 40 };
57
+ case 'h2':
58
+ return { f: this.heading(24, 700), lh: 32 };
59
+ case 'h3':
60
+ return { f: this.heading(18, 600), lh: 24 };
61
+ case 'label':
62
+ return { f: this.body(13, 500), lh: 18 };
63
+ default:
64
+ return { f: this.body(15, 400), lh: 22 };
65
+ }
66
+ }
67
+ /** Wrapped lines of a text. */
68
+ wrapText(text, f, width) {
69
+ return wrap(text, f, width).lines;
70
+ }
71
+ // ---- spacing ----------------------------------------------------------------------------
72
+ /** A size word in pixels (section 10.1). */
73
+ scale(word, fallback) {
74
+ const u = this.u;
75
+ switch (word) {
76
+ case 'none':
77
+ return 0;
78
+ case 'xs':
79
+ return u;
80
+ case 'sm':
81
+ return 2 * u;
82
+ case 'md':
83
+ return 4 * u;
84
+ case 'lg':
85
+ return 6 * u;
86
+ case 'xl':
87
+ return 8 * u;
88
+ default:
89
+ return fallback;
90
+ }
91
+ }
92
+ gap(n) {
93
+ return this.scale(n.attrs?.gap, 4 * this.u);
94
+ }
95
+ pad(n) {
96
+ const u = this.u;
97
+ const fallback = n.type === 'card' ? 4 * u : n.type === 'sidebar' ? 4 * u : 0;
98
+ return this.scale(n.attrs?.pad, fallback);
99
+ }
100
+ align(n) {
101
+ const a = n.attrs?.align;
102
+ if (a === 'start' || a === 'center' || a === 'end' || a === 'stretch')
103
+ return a;
104
+ return MAIN_H.has(n.type) ? 'center' : 'stretch';
105
+ }
106
+ justify(n) {
107
+ const j = n.attrs?.justify;
108
+ return j === 'center' || j === 'end' || j === 'between' ? j : 'start';
109
+ }
110
+ // ---- intrinsic widths (section 10.4) ----------------------------------------------------
111
+ /** Preferred and minimum widths. */
112
+ intrinsic(n) {
113
+ const u = this.u;
114
+ const fixed = num(n.attrs?.width);
115
+ if (fixed !== undefined)
116
+ return { pref: fixed, min: fixed };
117
+ const label = n.label ?? '';
118
+ switch (n.type) {
119
+ case 'h1':
120
+ case 'h2':
121
+ case 'h3':
122
+ case 'text':
123
+ case 'label':
124
+ case 'link': {
125
+ const { f } = this.textStyle(n.type);
126
+ if (n.type === 'text' && n.label === undefined)
127
+ return { pref: 240, min: 96 };
128
+ return { pref: lineWidth(label, f), min: longestWord(label, f) };
129
+ }
130
+ case 'badge': {
131
+ const w = measure(label, this.body(13, 500)) + 4 * u;
132
+ return { pref: w, min: w };
133
+ }
134
+ case 'button': {
135
+ const icon = n.attrs?.icon !== undefined;
136
+ const h = this.buttonHeight(n);
137
+ if (icon && n.label === undefined)
138
+ return { pref: h, min: h };
139
+ const w = measure(label, this.body(15, 600)) + 8 * u + (icon ? 6 * u : 0);
140
+ return { pref: w, min: w };
141
+ }
142
+ case 'input':
143
+ case 'textarea':
144
+ case 'select':
145
+ return { pref: 240, min: 96 };
146
+ case 'checkbox':
147
+ case 'toggle': {
148
+ const mark = n.type === 'toggle' ? 36 : 16;
149
+ const w = mark + 2 * u + measure(label, this.body(15, 400));
150
+ return { pref: w, min: w };
151
+ }
152
+ case 'radio': {
153
+ let w = n.label !== undefined ? measure(label, this.body(13, 500)) : 0;
154
+ for (const it of n.items ?? [])
155
+ w = Math.max(w, 16 + 2 * u + measure(it, this.body(15, 400)));
156
+ return { pref: w, min: w };
157
+ }
158
+ case 'icon': {
159
+ const s = this.iconSize(n);
160
+ return { pref: s, min: s };
161
+ }
162
+ case 'avatar': {
163
+ const s = this.avatarSize(n);
164
+ return { pref: s, min: s };
165
+ }
166
+ case 'image':
167
+ return { pref: 160, min: 160 };
168
+ case 'spacer': {
169
+ const s = n.attrs?.size;
170
+ const w = typeof s === 'number' ? s : s !== undefined ? this.scale(s, 0) : 0;
171
+ return { pref: w, min: w };
172
+ }
173
+ case 'divider':
174
+ return { pref: this.theme.stroke, min: this.theme.stroke };
175
+ case 'item': {
176
+ let w = 6 * u + measure(label, this.body(15, 400));
177
+ if (n.attrs?.icon !== undefined)
178
+ w += 16 + 2 * u;
179
+ const b = textOf(n.attrs?.badge);
180
+ if (b !== undefined)
181
+ w += 2 * u + measure(b, this.body(13, 500)) + 4 * u;
182
+ return { pref: w, min: w };
183
+ }
184
+ case 'breadcrumbs': {
185
+ const items = n.items ?? [];
186
+ let w = 0;
187
+ items.forEach((it, i) => {
188
+ w += measure(it, this.body(15, i === items.length - 1 ? 500 : 400));
189
+ if (i > 0)
190
+ w += 16 + 2 * u;
191
+ });
192
+ return { pref: w, min: w };
193
+ }
194
+ case 'nav': {
195
+ const entries = this.navEntries(n);
196
+ const vertical = has(n, 'vertical');
197
+ let sum = 0;
198
+ let max = 0;
199
+ for (const e of entries) {
200
+ const ew = measure(e.text, this.body(15, 600)) + (e.icon ? 16 + 2 * u : 0);
201
+ sum += ew;
202
+ max = Math.max(max, ew);
203
+ }
204
+ const w = vertical ? max + 6 * u : sum + Math.max(0, entries.length - 1) * 6 * u;
205
+ return { pref: w, min: vertical ? w : max };
206
+ }
207
+ case 'tabs': {
208
+ let w = 0;
209
+ for (const t of n.items ?? [])
210
+ w += measure(t, this.body(15, 600)) + 8 * u;
211
+ const c = this.columnIntrinsic(n.children ?? [], 0);
212
+ return { pref: Math.max(w, c.pref), min: Math.max(Math.min(w, 96), c.min) };
213
+ }
214
+ case 'list': {
215
+ if (n.items) {
216
+ let w = 0;
217
+ for (const it of n.items)
218
+ w = Math.max(w, 5 * u + measure(it, this.body(15, 400)));
219
+ return { pref: w, min: Math.min(w, 96) };
220
+ }
221
+ return this.columnIntrinsic(n.children ?? [], 0);
222
+ }
223
+ case 'table': {
224
+ const cols = Math.max(1, n.items?.length ?? 1);
225
+ return { pref: 120 * cols, min: 64 * cols };
226
+ }
227
+ case 'card': {
228
+ const p = this.pad(n);
229
+ const lead = this.leadHeader(n);
230
+ if (lead) {
231
+ this.cardTitleInHeader(n, lead);
232
+ return this.leadIntrinsic(n, lead, p);
233
+ }
234
+ const c = this.columnIntrinsic(n.children ?? [], 0);
235
+ const t = n.label !== undefined ? measure(n.label, this.heading(15, 600)) : 0;
236
+ return { pref: Math.max(c.pref, t) + 2 * p, min: Math.max(c.min, Math.min(t, 96)) + 2 * p };
237
+ }
238
+ case 'empty':
239
+ case 'error':
240
+ case 'loading': {
241
+ const msg = n.label !== undefined ? lineWidth(n.label, this.body(15, 400)) : 0;
242
+ const c = this.columnIntrinsic(n.children ?? [], 0);
243
+ const w = Math.max(32, msg, c.pref);
244
+ return { pref: w, min: Math.max(32, c.min, n.label ? longestWord(n.label, this.body(15, 400)) : 0) };
245
+ }
246
+ case 'sidebar': {
247
+ const w = num(n.attrs?.width) ?? 240;
248
+ return { pref: w, min: w };
249
+ }
250
+ case 'column': {
251
+ const lead = this.leadHeader(n);
252
+ if (lead)
253
+ return this.leadIntrinsic(n, lead, this.pad(n));
254
+ return this.columnIntrinsic(n.children ?? [], this.pad(n));
255
+ }
256
+ case CARD_TITLE: {
257
+ const w = measure(label, this.heading(15, 600));
258
+ return { pref: w, min: Math.min(w, 96) };
259
+ }
260
+ case 'swatches':
261
+ return { pref: 3 * 200 + 2 * 4 * u, min: 3 * 200 + 2 * 4 * u };
262
+ case 'row':
263
+ case 'header':
264
+ case 'footer': {
265
+ const kids = this.barChildren(n);
266
+ const p = this.pad(n);
267
+ const g = this.gap(n);
268
+ let pref = 0;
269
+ let min = 0;
270
+ for (const c of kids) {
271
+ const i = this.intrinsic(c);
272
+ pref += i.pref;
273
+ min += i.min;
274
+ }
275
+ const gaps = Math.max(0, kids.length - 1) * g;
276
+ return {
277
+ pref: pref + gaps + 2 * p,
278
+ min: (has(n, 'wrap') ? Math.max(0, ...kids.map((c) => this.intrinsic(c).min)) : min + gaps) + 2 * p,
279
+ };
280
+ }
281
+ case 'grid': {
282
+ const cols = this.cols(n);
283
+ const g = this.gap(n);
284
+ let max = 0;
285
+ let maxMin = 0;
286
+ for (const c of n.children ?? []) {
287
+ const i = this.intrinsic(c);
288
+ max = Math.max(max, i.pref / Math.min(cols, this.span(c, cols)));
289
+ maxMin = Math.max(maxMin, i.min / Math.min(cols, this.span(c, cols)));
290
+ }
291
+ const p = this.pad(n);
292
+ return {
293
+ pref: r(max * cols + g * (cols - 1) + 2 * p),
294
+ min: r(maxMin * cols + g * (cols - 1) + 2 * p),
295
+ };
296
+ }
297
+ default:
298
+ return this.columnIntrinsic(n.children ?? [], this.pad(n));
299
+ }
300
+ }
301
+ columnIntrinsic(kids, pad) {
302
+ let pref = 0;
303
+ let min = 0;
304
+ for (const c of kids) {
305
+ if (c.type === 'note' || OVERLAYS.has(c.type))
306
+ continue;
307
+ const i = this.intrinsic(c);
308
+ pref = Math.max(pref, i.pref);
309
+ min = Math.max(min, i.min);
310
+ }
311
+ return { pref: pref + 2 * pad, min: min + 2 * pad };
312
+ }
313
+ // ---- sizes of parts ----------------------------------------------------------------------
314
+ buttonHeight(n) {
315
+ return (has(n, 'sm') ? 7 : has(n, 'lg') ? 11 : 9) * this.u;
316
+ }
317
+ iconSize(n) {
318
+ return has(n, 'sm') ? 16 : has(n, 'lg') ? 24 : 20;
319
+ }
320
+ avatarSize(n) {
321
+ return has(n, 'sm') ? 24 : has(n, 'lg') ? 48 : 32;
322
+ }
323
+ cols(n) {
324
+ const c = n.attrs?.cols;
325
+ return typeof c === 'number' && Number.isInteger(c) && c >= 1 && c <= 12 ? c : 2;
326
+ }
327
+ span(n, cols) {
328
+ const s = n.attrs?.span;
329
+ const v = typeof s === 'number' && Number.isInteger(s) && s >= 1 && s <= 12 ? s : 1;
330
+ return Math.min(v, cols);
331
+ }
332
+ /** The children that are laid out in a container's flow (notes and overlays are not). */
333
+ flowChildren(n) {
334
+ return (n.children ?? []).filter((c) => c.type !== 'note' && !OVERLAYS.has(c.type));
335
+ }
336
+ /** A card's or column's header, when it is the container's first child (section 5.11). */
337
+ leadHeader(n) {
338
+ if (n.type !== 'card' && n.type !== 'column')
339
+ return undefined;
340
+ const first = n.children?.[0];
341
+ return first?.type === 'header' ? first : undefined;
342
+ }
343
+ /** A header's entries in its row: its children, led by its card's title if that moved into it. */
344
+ barChildren(n) {
345
+ const t = this.barTitles.get(n);
346
+ return t ? [t, ...this.flowChildren(n)] : this.flowChildren(n);
347
+ }
348
+ /** Moves a card's title into its leading header (Appendix C.7). */
349
+ cardTitleInHeader(card, header) {
350
+ if (card.label === undefined || this.barTitles.has(header))
351
+ return;
352
+ this.barTitles.set(header, { type: CARD_TITLE, label: card.label, loc: card.loc });
353
+ }
354
+ /** Intrinsic widths of a card or column that leads with a header: the bar, or the rest. */
355
+ leadIntrinsic(n, header, p) {
356
+ const kids = this.barChildren(header);
357
+ let pref = 0;
358
+ let min = 0;
359
+ for (const c of kids) {
360
+ const i = this.intrinsic(c);
361
+ pref += i.pref;
362
+ min += i.min;
363
+ }
364
+ const gaps = Math.max(0, kids.length - 1) * this.gap(header);
365
+ const rest = this.columnIntrinsic((n.children ?? []).slice(1), p);
366
+ return { pref: Math.max(pref + gaps + 2 * p, rest.pref), min: Math.max(min + gaps + 2 * p, rest.min) };
367
+ }
368
+ /** Places a card's or column's leading header across its full width, with the container's side padding. */
369
+ placeLeadHeader(header, x, y, w, padH) {
370
+ const prev = this.barPad;
371
+ this.barPad = padH;
372
+ const l = this.place(header, x, y, w);
373
+ this.barPad = prev;
374
+ return l;
375
+ }
376
+ navEntries(n) {
377
+ const active = textOf(n.attrs?.active);
378
+ if (n.items)
379
+ return n.items.map((t) => ({ text: t, active: t === active }));
380
+ return (n.children ?? [])
381
+ .filter((c) => c.type === 'item')
382
+ .map((c) => {
383
+ const e = {
384
+ text: c.label ?? '',
385
+ active: has(c, 'active') || (c.label !== undefined && c.label === active),
386
+ node: c,
387
+ };
388
+ const icon = textOf(c.attrs?.icon);
389
+ if (icon !== undefined)
390
+ e.icon = icon;
391
+ return e;
392
+ });
393
+ }
394
+ /** A child's width in a container whose content width is `w` (section 10.3). */
395
+ widthIn(c, w) {
396
+ const v = c.attrs?.width;
397
+ if (typeof v === 'number')
398
+ return v;
399
+ if (typeof v === 'string') {
400
+ if (v === 'fill')
401
+ return w;
402
+ const m = /^([0-9]+(\.[0-9]+)?)%$/.exec(v);
403
+ if (m)
404
+ return r((w * Number(m[1])) / 100);
405
+ }
406
+ return undefined;
407
+ }
408
+ // ---- layout ------------------------------------------------------------------------------
409
+ laid(n, x, y, w, h) {
410
+ return {
411
+ node: n,
412
+ index: this.index.get(n) ?? 0,
413
+ box: { x: r(x), y: r(y), w: r(w), h: r(h) },
414
+ children: [],
415
+ };
416
+ }
417
+ warn(code, n, message) {
418
+ this.warnings.push(diagnostic(code, n.loc, `Line ${n.loc.line}: ${message}`));
419
+ }
420
+ /** Lays out an element at (x, y) with width w. Returns it with its height. */
421
+ place(n, x, y, w) {
422
+ const u = this.u;
423
+ const label = n.label ?? '';
424
+ const fixedH = this.fixedHeight(n);
425
+ let out;
426
+ switch (n.type) {
427
+ case 'h1':
428
+ case 'h2':
429
+ case 'h3':
430
+ case 'text':
431
+ case 'link': {
432
+ const { f, lh } = this.textStyle(n.type);
433
+ if (n.type === 'text' && n.label === undefined) {
434
+ const lines = num(n.attrs?.lines);
435
+ const count = lines !== undefined && Number.isInteger(lines) && lines >= 1 && lines <= 100 ? lines : 3;
436
+ out = this.laid(n, x, y, w, count * lh);
437
+ break;
438
+ }
439
+ const wr = wrap(label, f, w);
440
+ if (wr.clipped)
441
+ this.warn('W602', n, 'a word is wider than its line and was clipped');
442
+ out = this.laid(n, x, y, w, wr.lines.length * lh);
443
+ out.lines = wr.lines;
444
+ break;
445
+ }
446
+ case 'label':
447
+ out = this.laid(n, x, y, w, 18);
448
+ break;
449
+ case 'button':
450
+ out = this.laid(n, x, y, w, this.buttonHeight(n));
451
+ break;
452
+ case 'input':
453
+ case 'select':
454
+ case 'textarea': {
455
+ let h = n.label !== undefined ? 18 + u : 0;
456
+ if (n.type === 'textarea') {
457
+ const rows = num(n.attrs?.rows);
458
+ h +=
459
+ (rows !== undefined && Number.isInteger(rows) && rows >= 1 && rows <= 100 ? rows : 3) * 22 +
460
+ 4 * u;
461
+ }
462
+ else {
463
+ h += 10 * u;
464
+ }
465
+ if (n.type === 'input' && textOf(n.attrs?.error) !== undefined)
466
+ h += u + 18;
467
+ out = this.laid(n, x, y, w, h);
468
+ break;
469
+ }
470
+ case 'checkbox':
471
+ case 'toggle':
472
+ out = this.laid(n, x, y, w, 6 * u);
473
+ break;
474
+ case 'radio':
475
+ out = this.laid(n, x, y, w, (n.label !== undefined ? 18 + u : 0) + (n.items?.length ?? 0) * 6 * u);
476
+ break;
477
+ case 'icon':
478
+ out = this.laid(n, x, y, w, this.iconSize(n));
479
+ break;
480
+ case 'avatar':
481
+ out = this.laid(n, x, y, w, this.avatarSize(n));
482
+ break;
483
+ case 'badge':
484
+ out = this.laid(n, x, y, w, 20);
485
+ break;
486
+ case 'image': {
487
+ let ratio = 4 / 3;
488
+ const rv = n.attrs?.ratio;
489
+ const m = typeof rv === 'string' ? /^([0-9]+(\.[0-9]+)?):([0-9]+(\.[0-9]+)?)$/.exec(rv) : null;
490
+ if (m && Number(m[1]) > 0 && Number(m[3]) > 0)
491
+ ratio = Number(m[1]) / Number(m[3]);
492
+ // A fixed height is the image's height (section 10.4), not a limit its content overflows.
493
+ out = this.laid(n, x, y, w, fixedH ?? r(w / ratio));
494
+ break;
495
+ }
496
+ case 'spacer': {
497
+ const s = n.attrs?.size;
498
+ const v = typeof s === 'number' ? s : s !== undefined ? this.scale(s, 0) : 0;
499
+ out = this.laid(n, x, y, w, v);
500
+ break;
501
+ }
502
+ case 'divider':
503
+ out = this.laid(n, x, y, w, this.theme.stroke);
504
+ break;
505
+ case 'item':
506
+ out = this.laid(n, x, y, w, 9 * u);
507
+ break;
508
+ case 'breadcrumbs':
509
+ out = this.laid(n, x, y, w, 6 * u);
510
+ break;
511
+ case 'nav':
512
+ out = this.placeNav(n, x, y, w);
513
+ break;
514
+ case 'tabs':
515
+ out = this.placeTabs(n, x, y, w);
516
+ break;
517
+ case 'list':
518
+ out = this.placeList(n, x, y, w);
519
+ break;
520
+ case 'table':
521
+ out = this.placeTable(n, x, y, w);
522
+ break;
523
+ case 'card': {
524
+ const p = this.pad(n);
525
+ const lead = this.leadHeader(n);
526
+ if (lead)
527
+ this.cardTitleInHeader(n, lead);
528
+ const bar = lead ? this.placeLeadHeader(lead, x, y, w, p) : undefined;
529
+ const barH = bar?.box.h ?? 0;
530
+ const titleH = !lead && n.label !== undefined ? 22 + this.gap(n) : 0;
531
+ const inner = this.placeColumn(n, this.flowChildren(n).filter((c) => c !== lead), x + p, y + barH + p + titleH, w - 2 * p, this.gap(n), 'stretch', undefined);
532
+ out = this.laid(n, x, y, w, barH + 2 * p + titleH + inner.h);
533
+ out.children = bar ? [bar, ...inner.children] : inner.children;
534
+ break;
535
+ }
536
+ case CARD_TITLE:
537
+ out = this.laid(n, x, y, w, 22);
538
+ break;
539
+ case 'empty':
540
+ case 'error':
541
+ case 'loading': {
542
+ const f = this.body(15, 400);
543
+ let h = 8 * u + (n.type === 'loading' ? 24 : 32);
544
+ let lines;
545
+ if (n.label !== undefined) {
546
+ lines = wrap(n.label, f, w).lines;
547
+ h += 2 * u + lines.length * 22;
548
+ }
549
+ const kids = this.flowChildren(n);
550
+ let children = [];
551
+ if (kids.length > 0 && n.type !== 'loading') {
552
+ h += 4 * u;
553
+ const inner = this.placeColumn(n, kids, x, y + h, w, 4 * u, 'center', undefined);
554
+ children = inner.children;
555
+ h += inner.h;
556
+ }
557
+ h += 8 * u;
558
+ out = this.laid(n, x, y, w, h);
559
+ if (lines)
560
+ out.lines = lines;
561
+ out.children = children;
562
+ break;
563
+ }
564
+ case 'swatches':
565
+ out = this.laid(n, x, y, w, 3 * 36 + 2 * 4 * u);
566
+ break;
567
+ case 'row':
568
+ case 'header':
569
+ case 'footer':
570
+ out = this.placeRow(n, x, y, w);
571
+ break;
572
+ case 'grid':
573
+ out = this.placeGrid(n, x, y, w);
574
+ break;
575
+ case 'sidebar':
576
+ case 'column': {
577
+ const p = this.pad(n);
578
+ const lead = this.leadHeader(n);
579
+ const bar = lead ? this.placeLeadHeader(lead, x, y, w, p) : undefined;
580
+ const barH = bar?.box.h ?? 0;
581
+ const inner = this.placeColumn(n, this.flowChildren(n).filter((c) => c !== lead), x + p, y + barH + p, w - 2 * p, this.gap(n), this.align(n), fixedH !== undefined ? fixedH - barH - 2 * p : undefined);
582
+ out = this.laid(n, x, y, w, barH + inner.h + 2 * p);
583
+ out.children = bar ? [bar, ...inner.children] : inner.children;
584
+ break;
585
+ }
586
+ default:
587
+ out = this.laid(n, x, y, w, 0);
588
+ }
589
+ if (fixedH !== undefined) {
590
+ if (out.box.h > fixedH)
591
+ this.warn('W601', n, `the content is ${r(out.box.h - fixedH)} px taller than height=${fixedH}`);
592
+ out.box.h = r(fixedH);
593
+ }
594
+ return out;
595
+ }
596
+ fixedHeight(n) {
597
+ const h = n.attrs?.height;
598
+ return typeof h === 'number' ? h : undefined;
599
+ }
600
+ /** A vertical container's children (section 10.3, steps 2, 4 and 5). */
601
+ placeColumn(owner, kids, x, y, w, gap, align, room) {
602
+ const children = [];
603
+ let cy = y;
604
+ for (const c of kids) {
605
+ const fixed = this.widthIn(c, w);
606
+ let cw;
607
+ if (fixed !== undefined)
608
+ cw = fixed;
609
+ else if (align === 'stretch' && !OWN_WIDTH.has(c.type))
610
+ cw = w;
611
+ else
612
+ cw = Math.min(this.intrinsic(c).pref, w);
613
+ let cx = x;
614
+ const a = align === 'stretch' ? 'start' : align;
615
+ if (a === 'center')
616
+ cx = x + r((w - cw) / 2);
617
+ else if (a === 'end')
618
+ cx = x + w - cw;
619
+ const l = this.place(c, cx, cy, cw);
620
+ children.push(l);
621
+ cy += l.box.h + gap;
622
+ }
623
+ let h = kids.length > 0 ? cy - gap - y : 0;
624
+ // Free room: shared among spacers without a size and children with grow, else justify.
625
+ if (room !== undefined && room > h) {
626
+ const free = room - h;
627
+ const growers = children.filter((l) => (l.node.type === 'spacer' && l.node.attrs?.size === undefined) || has(l.node, 'grow'));
628
+ if (growers.length > 0) {
629
+ const shares = share(free, growers.length);
630
+ let shift = 0;
631
+ for (const l of children) {
632
+ l.box.y = r(l.box.y + shift);
633
+ shiftChildren(l, 0, shift);
634
+ const gi = growers.indexOf(l);
635
+ if (gi >= 0) {
636
+ const add = shares[gi] ?? 0;
637
+ l.box.h = r(l.box.h + add);
638
+ shift += add;
639
+ }
640
+ }
641
+ }
642
+ else {
643
+ const j = this.justify(owner);
644
+ const offsets = justifyOffsets(j, free, children.length);
645
+ children.forEach((l, i) => {
646
+ const d = offsets[i] ?? 0;
647
+ l.box.y = r(l.box.y + d);
648
+ shiftChildren(l, 0, d);
649
+ });
650
+ }
651
+ h = room;
652
+ }
653
+ void owner;
654
+ return { children, h };
655
+ }
656
+ /** A horizontal container (section 10.3, step 3). */
657
+ placeRow(n, x, y, w) {
658
+ const u = this.u;
659
+ const isBar = n.type === 'header' || n.type === 'footer';
660
+ const padV = isBar ? 3 * u : this.pad(n);
661
+ const padH = isBar ? (this.barPad ?? this.pad(n)) : this.pad(n);
662
+ const gap = this.gap(n);
663
+ const W = w - 2 * padH;
664
+ const kids = this.barChildren(n);
665
+ const lines = [];
666
+ if (has(n, 'wrap')) {
667
+ let line = [];
668
+ let used = 0;
669
+ for (const c of kids) {
670
+ const cw = this.widthIn(c, W) ?? this.intrinsic(c).pref;
671
+ if (line.length > 0 && used + gap + cw > W) {
672
+ lines.push(line);
673
+ line = [];
674
+ used = 0;
675
+ }
676
+ used += (line.length > 0 ? gap : 0) + cw;
677
+ line.push(c);
678
+ }
679
+ if (line.length > 0)
680
+ lines.push(line);
681
+ }
682
+ else {
683
+ lines.push(kids);
684
+ }
685
+ const children = [];
686
+ let cy = y + padV;
687
+ for (const line of lines) {
688
+ const placed = this.placeRowLine(n, line, x + padH, cy, W, gap);
689
+ children.push(...placed.children);
690
+ cy += placed.h + gap;
691
+ }
692
+ const contentH = lines.length > 0 ? cy - gap - (y + padV) : 0;
693
+ const out = this.laid(n, x, y, w, contentH + 2 * padV);
694
+ out.children = children;
695
+ return out;
696
+ }
697
+ /** Set while laying out a screen's header and footer, whose side padding is the screen's. */
698
+ barPad;
699
+ placeRowLine(owner, kids, x, y, W, gap) {
700
+ const n = kids.length;
701
+ const widths = new Array(n).fill(0);
702
+ const growers = [];
703
+ const flexible = [];
704
+ let used = Math.max(0, n - 1) * gap;
705
+ kids.forEach((c, i) => {
706
+ const fixed = c.type === 'sidebar' ? (num(c.attrs?.width) ?? 240) : this.widthIn(c, W);
707
+ const isFill = c.attrs?.width === 'fill';
708
+ if (fixed !== undefined && !isFill) {
709
+ widths[i] = fixed;
710
+ }
711
+ else if (isFill || has(c, 'grow') || (c.type === 'spacer' && c.attrs?.size === undefined)) {
712
+ growers.push(i);
713
+ }
714
+ else {
715
+ widths[i] = this.intrinsic(c).pref;
716
+ flexible.push(i);
717
+ }
718
+ used += widths[i] ?? 0;
719
+ });
720
+ let free = W - used;
721
+ if (free >= 0 && growers.length > 0) {
722
+ const shares = share(free, growers.length);
723
+ growers.forEach((gi, k) => {
724
+ widths[gi] = shares[k] ?? 0;
725
+ });
726
+ free = 0;
727
+ }
728
+ else if (free < 0) {
729
+ // Shrink flexible children by a common factor, to no less than their minimum.
730
+ const mins = kids.map((c) => this.intrinsic(c).min);
731
+ let active = [...flexible];
732
+ for (let guard = 0; guard < n + 1 && active.length > 0; guard++) {
733
+ const total = active.reduce((s, i) => s + (widths[i] ?? 0), 0);
734
+ const target = total + free;
735
+ if (total <= 0)
736
+ break;
737
+ const factor = Math.max(0, target / total);
738
+ let frozen = false;
739
+ for (const i of active) {
740
+ const want = r((widths[i] ?? 0) * factor);
741
+ const min = mins[i] ?? 0;
742
+ if (want < min) {
743
+ free += (widths[i] ?? 0) - min;
744
+ widths[i] = min;
745
+ frozen = true;
746
+ }
747
+ }
748
+ if (frozen) {
749
+ active = active.filter((i) => (widths[i] ?? 0) > (mins[i] ?? 0));
750
+ continue;
751
+ }
752
+ let gained = 0;
753
+ for (const i of active) {
754
+ const nw = r((widths[i] ?? 0) * factor);
755
+ gained += (widths[i] ?? 0) - nw;
756
+ widths[i] = nw;
757
+ }
758
+ free += gained;
759
+ break;
760
+ }
761
+ if (free < 0)
762
+ this.warn('W601', owner, `this row is ${r(-free)} px too wide for its container`);
763
+ }
764
+ // Main-axis positions.
765
+ const offsets = growers.length > 0 || free <= 0 ? new Array(n).fill(0) : justifyOffsets(this.justify(owner), free, n);
766
+ const children = [];
767
+ let cx = x;
768
+ kids.forEach((c, i) => {
769
+ const l = this.place(c, cx + (offsets[i] ?? 0), y, widths[i] ?? 0);
770
+ children.push(l);
771
+ cx += (widths[i] ?? 0) + gap;
772
+ });
773
+ // Cross-axis alignment.
774
+ const H = children.reduce((m, l) => Math.max(m, l.box.h), 0);
775
+ const align = this.align(owner);
776
+ for (const l of children) {
777
+ const c = l.node;
778
+ if (c.type === 'sidebar' || c.type === 'divider' || (align === 'stretch' && CONTAINERS.has(c.type))) {
779
+ l.box.h = H;
780
+ continue;
781
+ }
782
+ let dy = 0;
783
+ if (align === 'center')
784
+ dy = r((H - l.box.h) / 2);
785
+ else if (align === 'end')
786
+ dy = H - l.box.h;
787
+ if (dy !== 0) {
788
+ l.box.y = r(l.box.y + dy);
789
+ shiftChildren(l, 0, dy);
790
+ }
791
+ }
792
+ return { children, h: H };
793
+ }
794
+ placeGrid(n, x, y, w) {
795
+ const p = this.pad(n);
796
+ const g = this.gap(n);
797
+ const cols = this.cols(n);
798
+ const W = w - 2 * p;
799
+ const widths = share(W - g * (cols - 1), cols);
800
+ const colX = [];
801
+ let acc = x + p;
802
+ for (let i = 0; i < cols; i++) {
803
+ colX.push(acc);
804
+ acc += (widths[i] ?? 0) + g;
805
+ }
806
+ const children = [];
807
+ let col = 0;
808
+ let rowY = y + p;
809
+ let row = [];
810
+ const finishRow = () => {
811
+ const H = row.reduce((m, l) => Math.max(m, l.box.h), 0);
812
+ for (const l of row)
813
+ if (CONTAINERS.has(l.node.type) || l.node.type === 'image')
814
+ l.box.h = H;
815
+ rowY += H + g;
816
+ row = [];
817
+ };
818
+ for (const c of this.flowChildren(n)) {
819
+ const s = this.span(c, cols);
820
+ if (col + s > cols) {
821
+ finishRow();
822
+ col = 0;
823
+ }
824
+ let cw = 0;
825
+ for (let k = col; k < col + s; k++)
826
+ cw += widths[k] ?? 0;
827
+ cw += (s - 1) * g;
828
+ const l = this.place(c, colX[col] ?? x, rowY, cw);
829
+ children.push(l);
830
+ row.push(l);
831
+ col += s;
832
+ }
833
+ if (row.length > 0)
834
+ finishRow();
835
+ const h = children.length > 0 ? rowY - g - (y + p) : 0;
836
+ const out = this.laid(n, x, y, w, h + 2 * p);
837
+ out.children = children;
838
+ return out;
839
+ }
840
+ placeNav(n, x, y, w) {
841
+ const u = this.u;
842
+ const entries = this.navEntries(n);
843
+ const vertical = has(n, 'vertical');
844
+ const out = this.laid(n, x, y, w, vertical ? entries.length * 9 * u : 9 * u);
845
+ out.entries = [];
846
+ let cx = x;
847
+ entries.forEach((e, i) => {
848
+ let box;
849
+ if (vertical) {
850
+ box = { x: r(x), y: r(y + i * 9 * u), w: r(w), h: 9 * u };
851
+ }
852
+ else {
853
+ const ew = measure(e.text, this.body(15, e.active ? 600 : 400)) + (e.icon ? 16 + 2 * u : 0);
854
+ box = { x: r(cx), y: r(y), w: ew, h: 9 * u };
855
+ cx += ew + 6 * u;
856
+ }
857
+ const entry = {
858
+ text: e.text,
859
+ box,
860
+ active: e.active,
861
+ };
862
+ if (e.icon !== undefined)
863
+ entry.icon = e.icon;
864
+ out.entries?.push(entry);
865
+ if (e.node) {
866
+ const l = this.laid(e.node, box.x, box.y, box.w, box.h);
867
+ out.children.push(l);
868
+ }
869
+ });
870
+ return out;
871
+ }
872
+ placeTabs(n, x, y, w) {
873
+ const u = this.u;
874
+ const items = n.items ?? [];
875
+ const active = textOf(n.attrs?.active) ?? items[0];
876
+ const out = this.laid(n, x, y, w, 10 * u);
877
+ out.entries = [];
878
+ let cx = x;
879
+ for (const t of items) {
880
+ const tw = measure(t, this.body(15, t === active ? 600 : 400)) + 8 * u;
881
+ out.entries.push({ text: t, box: { x: r(cx), y: r(y), w: tw, h: 10 * u }, active: t === active });
882
+ cx += tw;
883
+ }
884
+ const kids = this.flowChildren(n);
885
+ if (kids.length > 0) {
886
+ const inner = this.placeColumn(n, kids, x, y + 10 * u + 4 * u, w, 4 * u, 'stretch', undefined);
887
+ out.children = inner.children;
888
+ out.box.h = r(10 * u + 4 * u + inner.h);
889
+ }
890
+ return out;
891
+ }
892
+ placeList(n, x, y, w) {
893
+ const u = this.u;
894
+ if (n.items) {
895
+ const out = this.laid(n, x, y, w, n.items.length * 9 * u);
896
+ out.entries = n.items.map((t, i) => ({
897
+ text: t,
898
+ box: { x: r(x), y: r(y + i * 9 * u), w: r(w), h: 9 * u },
899
+ active: false,
900
+ }));
901
+ return out;
902
+ }
903
+ const kids = this.flowChildren(n);
904
+ const children = [];
905
+ let cy = y;
906
+ for (const c of kids) {
907
+ const l = this.place(c, x, cy, w);
908
+ children.push(l);
909
+ cy += l.box.h;
910
+ }
911
+ const out = this.laid(n, x, y, w, cy - y);
912
+ out.children = children;
913
+ return out;
914
+ }
915
+ placeTable(n, x, y, w) {
916
+ const u = this.u;
917
+ const cols = Math.max(1, n.items?.length ?? 1);
918
+ const captionH = n.label !== undefined ? 22 + 2 * u : 0;
919
+ const rowsAttr = num(n.attrs?.rows);
920
+ const rowNodes = (n.children ?? []).filter((c) => c.type === 'table-row');
921
+ const bodyRows = rowNodes.length > 0
922
+ ? rowNodes.length
923
+ : rowsAttr !== undefined && Number.isInteger(rowsAttr) && rowsAttr >= 1 && rowsAttr <= 100
924
+ ? rowsAttr
925
+ : 3;
926
+ const out = this.laid(n, x, y, w, captionH + 10 * u * (1 + bodyRows));
927
+ out.tableTop = r(y + captionH);
928
+ const widths = share(w, cols);
929
+ out.columns = [];
930
+ let acc = 0;
931
+ for (const cw of widths) {
932
+ out.columns.push(acc);
933
+ acc += cw;
934
+ }
935
+ rowNodes.forEach((rn, i) => {
936
+ out.children.push(this.laid(rn, x, y + captionH + 10 * u * (1 + i), w, 10 * u));
937
+ });
938
+ return out;
939
+ }
940
+ // ---- screens ------------------------------------------------------------------------------
941
+ device(s) {
942
+ const size = s.attrs?.size;
943
+ const m = typeof size === 'string' ? /^([0-9]+)x([0-9]+)$/.exec(size) : null;
944
+ if (m && Number(m[1]) >= 1 && Number(m[1]) <= 16000 && Number(m[2]) >= 1 && Number(m[2]) <= 16000) {
945
+ return { name: 'custom', w: Number(m[1]), h: Number(m[2]) };
946
+ }
947
+ for (const d of ['phone', 'tablet', 'desktop']) {
948
+ if (has(s, d))
949
+ return { name: d, w: DEVICES[d][0], h: DEVICES[d][1] };
950
+ }
951
+ // A variant uses its base's device.
952
+ const of = s.attrs?.of;
953
+ if (typeof of === 'object' && of !== null && !Array.isArray(of)) {
954
+ const base = this.doc.screens.find((b) => b.id === of.ref);
955
+ if (base && base !== s)
956
+ return this.device(base);
957
+ }
958
+ return { name: 'desktop', w: 1280, h: 800 };
959
+ }
960
+ /** Lays out a screen's frame, with the given overlays open. */
961
+ frame(s, key, open, extra) {
962
+ const u = this.u;
963
+ const dev = this.device(s);
964
+ const W = dev.w;
965
+ const P = dev.name === 'phone' ? 4 * u : 6 * u;
966
+ const kids = s.children ?? [];
967
+ const headerN = kids.find((c) => c.type === 'header');
968
+ const footerN = kids.find((c) => c.type === 'footer');
969
+ const left = kids.find((c) => c.type === 'sidebar' && !has(c, 'right'));
970
+ const right = kids.find((c) => c.type === 'sidebar' && has(c, 'right'));
971
+ const bodyKids = kids.filter((c) => c !== headerN &&
972
+ c !== footerN &&
973
+ c !== left &&
974
+ c !== right &&
975
+ c.type !== 'note' &&
976
+ !OVERLAYS.has(c.type));
977
+ this.barPad = P;
978
+ const header = headerN ? this.place(headerN, 0, 0, W) : undefined;
979
+ const footerProbe = footerN ? this.place(footerN, 0, 0, W) : undefined;
980
+ this.barPad = undefined;
981
+ const headerH = header?.box.h ?? 0;
982
+ const footerH = footerProbe?.box.h ?? 0;
983
+ const lw = left ? (num(left.attrs?.width) ?? 240) : 0;
984
+ const rw = right ? (num(right.attrs?.width) ?? 240) : 0;
985
+ const bodyW = Math.max(0, W - lw - rw);
986
+ const room = Math.max(0, dev.h - headerH - footerH - 2 * P);
987
+ const body = this.placeColumn(s, bodyKids, lw + P, headerH + P, bodyW - 2 * P, 4 * u, 'stretch', room);
988
+ const bodyH = body.h + 2 * P;
989
+ const frameH = r(Math.max(dev.h, headerH + bodyH + footerH));
990
+ let footer;
991
+ if (footerN) {
992
+ this.barPad = P;
993
+ footer = this.place(footerN, 0, frameH - footerH, W);
994
+ this.barPad = undefined;
995
+ }
996
+ const sideH = frameH - headerH - footerH;
997
+ const sidebars = [];
998
+ for (const [sb, sx, sw] of [
999
+ [left, 0, lw],
1000
+ [right, W - rw, rw],
1001
+ ]) {
1002
+ if (!sb)
1003
+ continue;
1004
+ const l = this.place(sb, sx, headerH, sw);
1005
+ l.box.h = r(sideH);
1006
+ sidebars.push(l);
1007
+ }
1008
+ const openLaid = [];
1009
+ let overlay;
1010
+ for (const o of open) {
1011
+ const l = this.placeOverlay(o, W, frameH, dev.h);
1012
+ openLaid.push(l);
1013
+ if (o === extra)
1014
+ overlay = l;
1015
+ }
1016
+ const f = {
1017
+ key,
1018
+ screen: s,
1019
+ open: openLaid,
1020
+ title: this.frameTitle(s),
1021
+ x: 0,
1022
+ y: 0,
1023
+ w: W,
1024
+ h: frameH,
1025
+ deviceH: dev.h,
1026
+ sidebars,
1027
+ body: body.children,
1028
+ notes: [],
1029
+ notesW: 0,
1030
+ notesH: 0,
1031
+ };
1032
+ if (header)
1033
+ f.header = header;
1034
+ if (footer)
1035
+ f.footer = footer;
1036
+ if (overlay)
1037
+ f.overlay = overlay;
1038
+ return f;
1039
+ }
1040
+ frameTitle(s) {
1041
+ const state = textOf(s.attrs?.state);
1042
+ const of = s.attrs?.of;
1043
+ const isVariant = typeof of === 'object' && of !== null && !Array.isArray(of);
1044
+ return isVariant && state !== undefined ? `${s.label ?? ''} (${state})` : (s.label ?? '');
1045
+ }
1046
+ placeOverlay(o, W, frameH, deviceH) {
1047
+ const u = this.u;
1048
+ const p = 6 * u;
1049
+ const kids = this.flowChildren(o);
1050
+ if (o.type === 'modal') {
1051
+ const want = num(o.attrs?.width) ?? 480;
1052
+ const w = Math.min(want, W - 8 * u);
1053
+ const x = r((W - w) / 2);
1054
+ const inner = this.placeColumn(o, kids, x + p, 0, w - 2 * p, 4 * u, 'stretch', undefined);
1055
+ const h = p + 24 + (kids.length > 0 ? 4 * u + inner.h : 0) + p;
1056
+ const y = Math.max(4 * u, r((deviceH - h) / 2));
1057
+ const placed = this.placeColumn(o, kids, x + p, y + p + 24 + 4 * u, w - 2 * p, 4 * u, 'stretch', undefined);
1058
+ const l = this.laid(o, x, y, w, h);
1059
+ l.children = placed.children;
1060
+ return l;
1061
+ }
1062
+ const bottom = has(o, 'bottom');
1063
+ const isLeft = has(o, 'left') && !has(o, 'right');
1064
+ let box;
1065
+ if (bottom) {
1066
+ const h = r(deviceH * 0.5);
1067
+ box = { x: 0, y: r(frameH - h), w: W, h };
1068
+ }
1069
+ else {
1070
+ const w = Math.min(num(o.attrs?.width) ?? 360, W);
1071
+ box = { x: isLeft ? 0 : r(W - w), y: 0, w, h: frameH };
1072
+ }
1073
+ const placed = this.placeColumn(o, kids, box.x + p, box.y + p + 24 + 4 * u, box.w - 2 * p, 4 * u, 'stretch', undefined);
1074
+ const l = this.laid(o, box.x, box.y, box.w, box.h);
1075
+ l.children = placed.children;
1076
+ return l;
1077
+ }
1078
+ }
1079
+ /** Shares room equally: each share rounded, the last takes what is left (section 10.1). */
1080
+ export function share(room, n) {
1081
+ const out = [];
1082
+ let left = room;
1083
+ for (let i = 0; i < n; i++) {
1084
+ const s = i === n - 1 ? r(left) : r(room / n);
1085
+ out.push(s);
1086
+ left -= s;
1087
+ }
1088
+ return out;
1089
+ }
1090
+ function justifyOffsets(j, free, n) {
1091
+ const out = [];
1092
+ for (let i = 0; i < n; i++) {
1093
+ if (j === 'center')
1094
+ out.push(r(free / 2));
1095
+ else if (j === 'end')
1096
+ out.push(free);
1097
+ else if (j === 'between' && n > 1)
1098
+ out.push(r((free * i) / (n - 1)));
1099
+ else
1100
+ out.push(0);
1101
+ }
1102
+ return out;
1103
+ }
1104
+ /** Moves everything under a laid-out element. */
1105
+ export function shiftChildren(l, dx, dy) {
1106
+ for (const c of l.children) {
1107
+ c.box.x = r(c.box.x + dx);
1108
+ c.box.y = r(c.box.y + dy);
1109
+ shiftChildren(c, dx, dy);
1110
+ }
1111
+ for (const e of l.entries ?? []) {
1112
+ e.box.x = r(e.box.x + dx);
1113
+ e.box.y = r(e.box.y + dy);
1114
+ }
1115
+ if (l.tableTop !== undefined)
1116
+ l.tableTop = r(l.tableTop + dy);
1117
+ }
1118
+ //# sourceMappingURL=layout.js.map