@sentropic/diagram-canvas 0.1.0 → 0.2.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.
Files changed (65) hide show
  1. package/README.md +9 -0
  2. package/contracts/diagram-anatomy.v1.json +2073 -0
  3. package/dist/camera.d.ts +12 -7
  4. package/dist/camera.js +18 -10
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/layout/bundleLabel.d.ts +12 -0
  8. package/dist/layout/bundleLabel.js +16 -0
  9. package/dist/layout/cache.js +7 -7
  10. package/dist/layout/channels.d.ts +28 -0
  11. package/dist/layout/channels.js +14 -0
  12. package/dist/layout/dock.d.ts +1 -0
  13. package/dist/layout/dock.js +18 -37
  14. package/dist/layout/engine.js +4 -4
  15. package/dist/layout/erd.d.ts +5 -0
  16. package/dist/layout/erd.js +85 -0
  17. package/dist/layout/fingerprint.d.ts +2 -2
  18. package/dist/layout/fingerprint.js +6 -2
  19. package/dist/layout/flow-order.d.ts +3 -0
  20. package/dist/layout/flow-order.js +14 -0
  21. package/dist/layout/flows.d.ts +89 -0
  22. package/dist/layout/flows.js +120 -0
  23. package/dist/layout/graph.d.ts +2 -0
  24. package/dist/layout/graph.js +5 -2
  25. package/dist/layout/hints.js +7 -6
  26. package/dist/layout/index.d.ts +3 -0
  27. package/dist/layout/index.js +3 -0
  28. package/dist/layout/labels.d.ts +1 -0
  29. package/dist/layout/labels.js +1 -1
  30. package/dist/layout/measure.d.ts +3 -3
  31. package/dist/layout/measure.js +5 -3
  32. package/dist/layout/measured.js +406 -75
  33. package/dist/layout/route.d.ts +2 -0
  34. package/dist/layout/route.js +2 -70
  35. package/dist/layout/sceneMeasure.d.ts +13 -0
  36. package/dist/layout/sceneMeasure.js +57 -0
  37. package/dist/layout/spacing.generated.d.ts +13 -0
  38. package/dist/layout/spacing.generated.js +8 -0
  39. package/dist/layout/types.d.ts +33 -4
  40. package/dist/layout/types.js +4 -10
  41. package/dist/presentation/diagramCardModel.d.ts +660 -0
  42. package/dist/presentation/diagramCardModel.js +1150 -0
  43. package/dist/presentation/diagramSceneModel.d.ts +70 -2
  44. package/dist/presentation/diagramSceneModel.js +165 -30
  45. package/dist/presentation/diagramSceneTypes.d.ts +91 -0
  46. package/dist/presentation/diagramSceneTypes.js +2 -0
  47. package/dist/presentation/erd.d.ts +81 -0
  48. package/dist/presentation/erd.js +156 -0
  49. package/dist/presentation/flowModel.d.ts +82 -0
  50. package/dist/presentation/flowModel.js +206 -0
  51. package/dist/presentation/index.d.ts +20 -0
  52. package/dist/presentation/index.js +14 -0
  53. package/dist/presentation/registry.d.ts +52 -0
  54. package/dist/presentation/registry.js +81 -0
  55. package/dist/presentation/renderCardSvg.d.ts +73 -0
  56. package/dist/presentation/renderCardSvg.js +144 -0
  57. package/dist/presentation/renderSceneSvg.d.ts +38 -0
  58. package/dist/presentation/renderSceneSvg.js +56 -0
  59. package/dist/presentation/renderZoneSvg.d.ts +79 -0
  60. package/dist/presentation/renderZoneSvg.js +180 -0
  61. package/dist/presentation/sceneLabelClearance.d.ts +8 -0
  62. package/dist/presentation/sceneLabelClearance.js +85 -0
  63. package/dist/visibility.d.ts +1 -1
  64. package/dist/visibility.js +5 -4
  65. package/package.json +31 -5
@@ -0,0 +1,1150 @@
1
+ import { DIAGRAM_SCENE_SPACING as spacing } from '../layout/spacing.generated.js';
2
+ import { getDiagramGlyph } from "./registry.js";
3
+ /**
4
+ * DiagramCard shared model — geometry, projection and line layout (DC-1).
5
+ *
6
+ * Pure TypeScript, zero imports: the Svelte `DiagramCard` (barrel graph)
7
+ * and the `diagram-render` pure renderer (subpath graph) both consume this
8
+ * module, so it must live outside both subpath directories and import
9
+ * nothing. Glyph path data arrives via props (resolved by the caller from
10
+ * the `diagram-icons` subpath); this module never resolves a glyph name.
11
+ *
12
+ * One geometry source (§5.6): every number below is frozen by
13
+ * spec/SPEC_EVOL_DS_DIAGRAM_CARDS.md §5.3/Appendix A and mirrors
14
+ * - the `diagramCard` component tokens (`packages/tokens/src/component.ts`),
15
+ * - `packages/diagram-canvas/src/layout/measure.ts`
16
+ * (`FIXED_CARD_WIDTH 460`, `FIXED_CARD_HEIGHT 200`,
17
+ * `TOGGLE_ALLOCATION 66`, `CONTENT_INSET 16`, `RULE_GAP 4`).
18
+ * The token↔constant agreement test asserts the sources agree.
19
+ *
20
+ * Line boxes use the kit's 1/64 px grid (`floor(size * 1.3 * 64) / 64`,
21
+ * cf. `frameHeaderContent`): with those boxes the frozen content sums
22
+ * exactly — footer text-box bottom at `198 − 9.25`, rule gaps 4/4 — instead
23
+ * of drifting by fractional pixels (DIF-04). Text baselines follow the
24
+ * kit's glyph bounds: a 24 px role sits 22 px above its baseline, the
25
+ * 22 px kicker and the 32 px title at their own size (cf. the frozen SVG:
26
+ * kicker baseline `headerY + 16 + 22`, title `+ 32`, subtitle `+ 22`).
27
+ */
28
+ /** Frozen card geometry, in card pixels (spec §5.3, Appendix A). */
29
+ export const CARD_GEOMETRY = {
30
+ width: 460,
31
+ height: 200,
32
+ borderWidth: 2,
33
+ accentWidth: 8,
34
+ padding: 8,
35
+ /** Card left edge → header icon left edge (accent 8 + padding 8). */
36
+ headerInset: 16,
37
+ iconSize: 70,
38
+ iconTextGap: 6,
39
+ kicker: { size: 22, weight: 400, lineHeight: 1.3, letterSpacingEm: 0.04 },
40
+ title: { size: 32, weight: 700, lineHeight: 1.3 },
41
+ /** Header row → body block. */
42
+ gap: 6,
43
+ body: { size: 24, weight: 400, lineHeight: 1.3 },
44
+ /**
45
+ * Edge-label text (BF-6): 18px so one line plus margins fits the 32px
46
+ * gutter cap (22 + 2 × 4 = 30 ≤ 32). Mirrors the
47
+ * `diagramScene.edgeLabelFontSize` / `edgeLabelLineHeight` tokens;
48
+ * additive, frozen card sizes unchanged.
49
+ */
50
+ edgeLabel: { size: 18, weight: 400, lineHeight: 22 / 18 },
51
+ /** Accent-bar left edge → first body glyph. */
52
+ bodyInsetFromBar: 14,
53
+ ruleHeight: 1,
54
+ ruleMargin: 4,
55
+ ruleInsetStart: 16,
56
+ ruleInsetEnd: 10,
57
+ footer: { size: 24, weight: 700, lineHeight: 1.3 },
58
+ /** Footer text-box bottom → inner edge of the bottom border. */
59
+ footerBottomInset: 9.25,
60
+ /**
61
+ * Title/kicker width reservation when the toggle slot is present
62
+ * (hrpn tints.css `header { padding-right: 66px }`: the 66 px the +/−
63
+ * control needs in the header's inline-end).
64
+ */
65
+ toggleAllocation: 66,
66
+ /** The toggle slot box itself: the 32 px DS IconButton (sm) square. */
67
+ toggleBoxSize: 32,
68
+ toggleInset: 8,
69
+ /**
70
+ * Header text width of a folded card (toggle present, icon present):
71
+ * the owner-pinned 290 px — the hrpn folded-card title width
72
+ * (356 − 66 = 290). Plain cards keep `contentRight − headerTextX`
73
+ * (358 px with the icon).
74
+ */
75
+ foldedHeaderTextWidth: 290,
76
+ /** Card allocations, frozen-box-derived (see module doc). */
77
+ allocatedLines: { kicker: 1, title: 2, body: 2, footer: 1 },
78
+ };
79
+ /**
80
+ * Edge label typography (DC-2a, tokens `diagramScene.edgeLabelFontSize` /
81
+ * `diagramScene.edgeLabelLineHeight`): 18 px on a 22 px line box. One line
82
+ * plus the engine's two 4 px label margins is 30 px, inside the 32 px sibling
83
+ * gap cap; at the 0.45 readability floor it renders at 8.1 CSS px.
84
+ */
85
+ export const FLOW_BADGE_GEOMETRY = { size: 16, lineHeight: 20 };
86
+ export const EDGE_LABEL_GEOMETRY = { size: 18, weight: 400, lineHeight: 22 };
87
+ /** Line box height for a size at 1.3, on the 1/64 px grid. */
88
+ export function lineBoxHeight(size) {
89
+ return Math.floor(size * 1.3 * 64) / 64;
90
+ }
91
+ /**
92
+ * Alphabetic baseline inside a line box starting at `top`, per the kit's
93
+ * glyph bounds: 24 px roles (body/subtitle/footer) sit 22 px above the
94
+ * baseline; the 22 px kicker and the 32 px title at their own size.
95
+ */
96
+ export function baselineOf(top, size) {
97
+ return top + (size === 24 ? 22 : size);
98
+ }
99
+ /**
100
+ * Shared `EntityPresentation` → props projection (§4.2), consumed by both
101
+ * renderers. A missing presentation entry (or a non-card form, or no title)
102
+ * resolves to `{ kind: "label" }`: the host renders a simple label node and
103
+ * no card (L-R12). The glyph name maps through the injected resolver, which
104
+ * the `diagram-render` subpath wires to the icon registry; a name with no
105
+ * record yields no glyph and the layout raises `card-icon-unknown`.
106
+ */
107
+ export function projectCardProps(presentation, opts) {
108
+ if (presentation === undefined || presentation.form !== "card")
109
+ return { kind: "label" };
110
+ const title = presentation.text.title ?? "";
111
+ if (title === "")
112
+ return { kind: "label" };
113
+ const bodyLines = [];
114
+ if (presentation.text.body !== undefined && presentation.text.body !== "") {
115
+ bodyLines.push(presentation.text.body);
116
+ }
117
+ let detailCount = 0;
118
+ if (presentation.text.detail !== undefined && presentation.text.detail !== "") {
119
+ bodyLines.push(presentation.text.detail);
120
+ detailCount = 1;
121
+ }
122
+ const icon = presentation.icon ?? "base";
123
+ return {
124
+ kind: "card",
125
+ props: {
126
+ entityId: opts.entityId,
127
+ ...(presentation.text.kicker === undefined || presentation.text.kicker === ""
128
+ ? {}
129
+ : { kicker: presentation.text.kicker }),
130
+ title,
131
+ body: bodyLines,
132
+ ...(detailCount === 0 ? {} : { detailCount }),
133
+ ...(presentation.text.footer === undefined || presentation.text.footer === ""
134
+ ? {}
135
+ : { footer: presentation.text.footer }),
136
+ ...(icon === undefined ? {} : { icon }),
137
+ ...(icon === undefined || opts.resolveGlyph === undefined
138
+ ? {}
139
+ : (() => {
140
+ const glyph = opts.resolveGlyph(icon);
141
+ return glyph === undefined ? {} : { iconGlyph: glyph };
142
+ })()),
143
+ ...(presentation.categoryRef === undefined ? {} : { tone: presentation.categoryRef }),
144
+ ...(presentation.proposed === undefined ? {} : { proposed: presentation.proposed }),
145
+ ...(opts.toggle === undefined ? {} : { toggle: opts.toggle }),
146
+ },
147
+ };
148
+ }
149
+ /** The same deterministic wrapper with widths supplied by a mounted host. */
150
+ export function createCardMeasure(widthOf, version, breakWords = false, lineHeightOf) {
151
+ return Object.assign((text, role, availableWidth) => {
152
+ const lines = [];
153
+ let tooWide = false;
154
+ for (const paragraph of text.split("\n")) {
155
+ let line = "";
156
+ let spaced = false;
157
+ for (const token of paragraph.match(/\s+|[^\s·/_.-]+[·/_.-]?|[·/_.-]/gu) ?? []) {
158
+ if (/^\s+$/.test(token)) {
159
+ spaced = true;
160
+ continue;
161
+ }
162
+ const join = spaced && line !== "" ? " " : "";
163
+ spaced = false;
164
+ const candidate = line + join + token;
165
+ if (line !== "" && widthOf(candidate, role) > availableWidth) {
166
+ lines.push(line);
167
+ line = "";
168
+ }
169
+ if (breakWords && widthOf(token, role) > availableWidth) {
170
+ for (const char of token) {
171
+ if (line !== "" && widthOf(line + char, role) > availableWidth) {
172
+ lines.push(line);
173
+ line = "";
174
+ }
175
+ line += char;
176
+ if (widthOf(char, role) > availableWidth)
177
+ tooWide = true;
178
+ }
179
+ }
180
+ else {
181
+ line = line === "" ? token : candidate;
182
+ if (widthOf(token, role) > availableWidth)
183
+ tooWide = true;
184
+ }
185
+ }
186
+ if (line !== "")
187
+ lines.push(line);
188
+ }
189
+ const lineHeight = lineHeightOf?.(role);
190
+ return { lines, tooWide, lineWidths: lines.map(line => widthOf(line, role)), ...(lineHeight === undefined ? {} : { lineHeight }) };
191
+ }, { version });
192
+ }
193
+ /** Scene cards can reserve additional lines for long identifiers. */
194
+ export function sceneCardMeasure(measure) {
195
+ const version = measure.version;
196
+ if (version?.endsWith(":scene-wrap") || measure("Mm09", "body", 1000, 1).lineWidths === undefined)
197
+ return measure;
198
+ return createCardMeasure((text, role) => {
199
+ const measured = measure(text, role, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
200
+ const width = Math.max(0, ...(measured.lineWidths ?? [text.length * CARD_GEOMETRY[role].size]));
201
+ return measure === defaultCardMeasure ? (width === 0 ? 0 : (width - 2) * CARD_FALLBACK_WIDTH_FACTOR + 2) : width;
202
+ }, `${version ?? "inter-fallback-1.12-v2"}:scene-wrap`, true, role => measure("Mg", role, 1000, 1).lineHeight);
203
+ }
204
+ /**
205
+ * Pinned per-character advances at 1 px (data, not code).
206
+ *
207
+ * Source: the pinned kit 'governance-font-metrics.js' (browser-measured
208
+ * Inter 400 advances for body/kicker, Dossier Inter 700 advances for
209
+ * title/footer — the same tables the reference layout wraps against).
210
+ * The DS default measurer consumes them so static strings wrap exactly
211
+ * where the reference wraps; production hosts inject their measured
212
+ * LineMeasurer (Inter-stack advances) instead. Measurer version:
213
+ * 'pinned-kit-1px'. Missing glyphs fall back to 0.7 em (the kit's
214
+ * unknown-glyph advance).
215
+ */
216
+ const CARD_ADVANCES_TITLE = {
217
+ "0": 0.67,
218
+ "1": 0.43,
219
+ "2": 0.63,
220
+ "3": 0.65,
221
+ "4": 0.68,
222
+ "5": 0.62,
223
+ "6": 0.65,
224
+ "7": 0.58,
225
+ "8": 0.65,
226
+ "9": 0.65,
227
+ "\n": 0.24,
228
+ " ": 0.24,
229
+ "\"": 0.55,
230
+ "#": 0.65,
231
+ "%": 1.02,
232
+ "&": 0.67,
233
+ "'": 0.34,
234
+ "(": 0.38,
235
+ ")": 0.38,
236
+ "*": 0.56,
237
+ "+": 0.68,
238
+ ",": 0.33,
239
+ "-": 0.47,
240
+ ".": 0.33,
241
+ "/": 0.39,
242
+ ":": 0.33,
243
+ ";": 0.34,
244
+ "<": 0.68,
245
+ "=": 0.68,
246
+ ">": 0.68,
247
+ "@": 1.02,
248
+ "A": 0.75,
249
+ "B": 0.66,
250
+ "C": 0.74,
251
+ "D": 0.72,
252
+ "E": 0.61,
253
+ "F": 0.59,
254
+ "G": 0.75,
255
+ "H": 0.75,
256
+ "I": 0.28,
257
+ "J": 0.58,
258
+ "K": 0.72,
259
+ "L": 0.57,
260
+ "M": 0.93,
261
+ "N": 0.76,
262
+ "O": 0.77,
263
+ "P": 0.65,
264
+ "Q": 0.78,
265
+ "R": 0.66,
266
+ "S": 0.65,
267
+ "T": 0.67,
268
+ "U": 0.73,
269
+ "V": 0.75,
270
+ "W": 1.04,
271
+ "X": 0.74,
272
+ "Y": 0.73,
273
+ "Z": 0.66,
274
+ "[": 0.38,
275
+ "\\": 0.39,
276
+ "]": 0.38,
277
+ "_": 0.48,
278
+ "a": 0.58,
279
+ "b": 0.63,
280
+ "c": 0.59,
281
+ "d": 0.63,
282
+ "e": 0.6,
283
+ "f": 0.4,
284
+ "g": 0.63,
285
+ "h": 0.62,
286
+ "i": 0.27,
287
+ "j": 0.27,
288
+ "k": 0.58,
289
+ "l": 0.27,
290
+ "m": 0.91,
291
+ "n": 0.62,
292
+ "o": 0.61,
293
+ "p": 0.63,
294
+ "q": 0.63,
295
+ "r": 0.41,
296
+ "s": 0.56,
297
+ "t": 0.37,
298
+ "u": 0.62,
299
+ "v": 0.6,
300
+ "w": 0.85,
301
+ "x": 0.58,
302
+ "y": 0.6,
303
+ "z": 0.57,
304
+ "{": 0.47,
305
+ "}": 0.47,
306
+ "§": 0.57,
307
+ "«": 0.66,
308
+ "·": 0.33,
309
+ "»": 0.66,
310
+ "À": 0.75,
311
+ "Ç": 0.74,
312
+ "È": 0.61,
313
+ "É": 0.61,
314
+ "×": 0.68,
315
+ "à": 0.58,
316
+ "â": 0.58,
317
+ "ç": 0.59,
318
+ "è": 0.6,
319
+ "é": 0.6,
320
+ "ê": 0.6,
321
+ "î": 0.27,
322
+ "ô": 0.61,
323
+ "ù": 0.62,
324
+ "–": 0.5,
325
+ "’": 0.31,
326
+ "\u2026": 1,
327
+ "→": 1,
328
+ "↔": 1,
329
+ "≠": 0.83203,
330
+ "≥": 0.83203
331
+ };
332
+ const CARD_ADVANCES_BODY = {
333
+ "0": 0.59961,
334
+ "1": 0.59961,
335
+ "2": 0.59961,
336
+ "3": 0.59961,
337
+ "4": 0.59961,
338
+ "5": 0.59961,
339
+ "6": 0.59961,
340
+ "7": 0.59961,
341
+ "8": 0.59961,
342
+ "9": 0.59961,
343
+ "\n": 0.2998,
344
+ " ": 0.2998,
345
+ "\"": 0.45996,
346
+ "#": 0.59961,
347
+ "%": 0.69922,
348
+ "&": 0.67969,
349
+ "'": 0.2998,
350
+ "(": 0.2998,
351
+ ")": 0.2998,
352
+ "*": 0.59961,
353
+ "+": 0.59961,
354
+ ",": 0.2998,
355
+ "-": 0.2998,
356
+ ".": 0.2998,
357
+ "/": 0.51953,
358
+ ":": 0.2998,
359
+ ";": 0.2998,
360
+ "<": 0.59961,
361
+ "=": 0.59961,
362
+ ">": 0.59961,
363
+ "@": 0.7998,
364
+ "A": 0.55957,
365
+ "B": 0.61914,
366
+ "C": 0.61914,
367
+ "D": 0.65918,
368
+ "E": 0.55957,
369
+ "F": 0.47949,
370
+ "G": 0.65918,
371
+ "H": 0.65918,
372
+ "I": 0.25977,
373
+ "J": 0.39941,
374
+ "K": 0.61914,
375
+ "L": 0.5,
376
+ "M": 0.81934,
377
+ "N": 0.65918,
378
+ "O": 0.65918,
379
+ "P": 0.55957,
380
+ "Q": 0.65918,
381
+ "R": 0.61914,
382
+ "S": 0.61914,
383
+ "T": 0.5,
384
+ "U": 0.63965,
385
+ "V": 0.55957,
386
+ "W": 0.81934,
387
+ "X": 0.53906,
388
+ "Y": 0.55957,
389
+ "Z": 0.53906,
390
+ "[": 0.2998,
391
+ "\\": 0.51953,
392
+ "]": 0.2998,
393
+ "_": 0.5,
394
+ "a": 0.53906,
395
+ "b": 0.53906,
396
+ "c": 0.47949,
397
+ "d": 0.53906,
398
+ "e": 0.53906,
399
+ "f": 0.2998,
400
+ "g": 0.5,
401
+ "h": 0.53906,
402
+ "i": 0.23926,
403
+ "j": 0.21973,
404
+ "k": 0.5,
405
+ "l": 0.23926,
406
+ "m": 0.81934,
407
+ "n": 0.53906,
408
+ "o": 0.53906,
409
+ "p": 0.53906,
410
+ "q": 0.53906,
411
+ "r": 0.31934,
412
+ "s": 0.51953,
413
+ "t": 0.31934,
414
+ "u": 0.53906,
415
+ "v": 0.43945,
416
+ "w": 0.65918,
417
+ "x": 0.43945,
418
+ "y": 0.41992,
419
+ "z": 0.41992,
420
+ "{": 0.2998,
421
+ "}": 0.2998,
422
+ "§": 0.59961,
423
+ "«": 0.39941,
424
+ "·": 0.23926,
425
+ "»": 0.39941,
426
+ "À": 0.55957,
427
+ "Ç": 0.61914,
428
+ "È": 0.55957,
429
+ "É": 0.55957,
430
+ "×": 0.59961,
431
+ "à": 0.53906,
432
+ "â": 0.53906,
433
+ "ç": 0.47949,
434
+ "è": 0.53906,
435
+ "é": 0.53906,
436
+ "ê": 0.53906,
437
+ "î": 0.23926,
438
+ "ô": 0.53906,
439
+ "ù": 0.53906,
440
+ "–": 0.55566,
441
+ "’": 0.22168,
442
+ "\u2026": 1,
443
+ "→": 1,
444
+ "↔": 1,
445
+ "≠": 0.83203,
446
+ "≥": 0.83203
447
+ };
448
+ /**
449
+ * Deterministic greedy wrapper over the pinned per-character advances.
450
+ * Production hosts inject their measured ‘LineMeasurer’; tests and
451
+ * static strings use this. It mirrors the frozen reference measurer
452
+ * (`cardTextWidth`/`wrapText` of the pinned kit): every candidate line is
453
+ * measured as a whole — `sum(advance × size) + 2` px safety — with the
454
+ * unknown-glyph fallback at 0.7 em, and the kicker is uppercased and
455
+ * tracked at 0.04 em before lookup (the kicker renders uppercase with
456
+ * `letter-spacing: 0.04em`). Corpus fixtures wrap exactly where the
457
+ * reference wraps; overflow fixtures overflow under every metric.
458
+ */
459
+ export const CARD_FALLBACK_WIDTH_FACTOR = 1.12;
460
+ export const DEFAULT_CARD_MEASURER_VERSION = "inter-fallback-1.12-v2";
461
+ export function defaultCardMeasure(text, role, availableWidth) {
462
+ const size = role === "kicker"
463
+ ? CARD_GEOMETRY.kicker.size
464
+ : role === "title"
465
+ ? CARD_GEOMETRY.title.size
466
+ : role === "footer"
467
+ ? CARD_GEOMETRY.footer.size
468
+ : CARD_GEOMETRY.body.size;
469
+ const table = role === "title" || role === "footer" ? CARD_ADVANCES_TITLE : CARD_ADVANCES_BODY;
470
+ const letterSpacing = role === "kicker" ? size * CARD_GEOMETRY.kicker.letterSpacingEm : 0;
471
+ const measureString = (value) => {
472
+ if (value.length === 0)
473
+ return 0;
474
+ const normalized = role === "kicker" ? value.toUpperCase() : value;
475
+ let sum = 0;
476
+ for (const char of normalized) {
477
+ sum += (table[char] ?? 0.7) * size + letterSpacing;
478
+ }
479
+ return sum + 2;
480
+ };
481
+ return createCardMeasure(measureString, DEFAULT_CARD_MEASURER_VERSION, true)(text, role, availableWidth, Number.MAX_SAFE_INTEGER);
482
+ }
483
+ /**
484
+ * Bridge from the card measurer to the canvas `LineMeasurer` shape: a host
485
+ * pairing `diagram-canvas` with these renderers passes `cardLineMeasurer()`
486
+ * straight as the injected `LineMeasurer` — no adapter, no role cast.
487
+ * Every canvas role maps onto the card role with the same font size
488
+ * (subtitle, detail and legend-label measure as body), `width` is the
489
+ * widest emitted line, `height` the line count at the role's line box, and
490
+ * `overflow` reports a too-wide line or a line-count overflow.
491
+ *
492
+ * The edge label has its own size (`EDGE_LABEL_GEOMETRY`, 18 px on 22 px):
493
+ * it is the body face scaled by 18/24. The injected measure only knows the
494
+ * four card roles, so the label is wrapped as body text in a width scaled up
495
+ * by 24/18 and its line widths are scaled back down — advances are linear in
496
+ * the font size, so this is the 18 px measurement.
497
+ */
498
+ export function cardLineMeasurer(measure = defaultCardMeasure) {
499
+ return Object.assign((request) => {
500
+ const role = request.role === "kicker" || request.role === "title" || request.role === "footer"
501
+ ? request.role
502
+ : "body";
503
+ if (request.role === "edge-label" || request.role === "flow-badge") {
504
+ const labelSize = request.role === "flow-badge" ? FLOW_BADGE_GEOMETRY.size : EDGE_LABEL_GEOMETRY.size;
505
+ const labelHeight = request.role === "flow-badge" ? FLOW_BADGE_GEOMETRY.lineHeight : EDGE_LABEL_GEOMETRY.lineHeight;
506
+ const scale = labelSize / CARD_GEOMETRY.body.size;
507
+ const label = measure(request.text, "body", request.availableWidth / scale, request.allocatedLines);
508
+ return {
509
+ lines: label.lines,
510
+ width: label.lineWidths === undefined ? request.availableWidth : Math.max(0, ...label.lineWidths) * scale,
511
+ height: label.lines.length * Math.max(labelHeight, (label.lineHeight ?? 0) * scale),
512
+ overflow: label.tooWide || label.lines.length > request.allocatedLines,
513
+ };
514
+ }
515
+ const measured = measure(request.text, role, request.availableWidth, request.allocatedLines);
516
+ const size = role === "kicker"
517
+ ? CARD_GEOMETRY.kicker.size
518
+ : role === "title"
519
+ ? CARD_GEOMETRY.title.size
520
+ : CARD_GEOMETRY.body.size;
521
+ const widest = measured.lineWidths === undefined
522
+ ? request.availableWidth
523
+ : Math.max(0, ...measured.lineWidths);
524
+ return {
525
+ lines: measured.lines,
526
+ width: widest,
527
+ height: measured.lines.length * lineBoxHeight(size),
528
+ overflow: measured.tooWide || measured.lines.length > request.allocatedLines,
529
+ };
530
+ }, { version: measure.version ?? DEFAULT_CARD_MEASURER_VERSION });
531
+ }
532
+ /** First content diagnostic, for reporting only; renderers retain every slot. */
533
+ export function firstOverflowDiagnostic(diagnostics) {
534
+ return diagnostics.find(diagnostic => diagnostic.code === "card-overflow" || diagnostic.code === "zone-overflow");
535
+ }
536
+ export function headerSlotGeometry(opts) {
537
+ const g = CARD_GEOMETRY;
538
+ const kickerHeight = lineBoxHeight(g.kicker.size);
539
+ const titleHeight = lineBoxHeight(g.title.size);
540
+ const contentHeight = opts.kickerLines * kickerHeight + opts.titleLines * titleHeight;
541
+ const blockHeight = opts.hasIcon ? Math.max(g.iconSize, contentHeight) : contentHeight;
542
+ return {
543
+ kickerHeight,
544
+ titleHeight,
545
+ contentHeight,
546
+ blockHeight,
547
+ textTopOffset: (blockHeight - contentHeight) / 2,
548
+ iconTopOffset: opts.hasIcon ? (blockHeight - g.iconSize) / 2 : 0,
549
+ };
550
+ }
551
+ /** Fixed-slot truncation shared by cards and zones (owner A14). */
552
+ export function truncateSlot(text, role, width, budget, measure, report) {
553
+ const measured = measure(text, role, width, budget);
554
+ const fits = (line) => { const value = measure(line, role, width, 1); return !value.tooWide && value.lines.length <= 1 && (value.lineWidths?.[0] ?? width) <= width; };
555
+ const lines = measured.lines.slice(0, budget).map((line, index) => {
556
+ if ((measured.lineWidths?.[index] !== undefined && measured.lineWidths[index] <= width) || fits(line))
557
+ return line;
558
+ return shorten(line);
559
+ });
560
+ function shorten(line) {
561
+ const chars = [...line];
562
+ while (chars.length > 0 && !fits(chars.join('').trimEnd() + '…'))
563
+ chars.pop();
564
+ const prefix = chars.join('').trimEnd();
565
+ const word = prefix.replace(/\s+\S*$/u, '');
566
+ return (word && word !== prefix ? word : prefix) + '…';
567
+ }
568
+ if (measured.lines.length > budget && lines.length > 0)
569
+ lines[lines.length - 1] = shorten(lines.at(-1).replace(/…$/u, ''));
570
+ if (measured.lines.length > budget || lines.some((line, index) => line !== measured.lines[index])) {
571
+ report({ neededPx: measured.lines.length * lineBoxHeight(CARD_GEOMETRY[role].size), allocatedPx: budget * lineBoxHeight(CARD_GEOMETRY[role].size),
572
+ text, severity: 'info', reason: 'truncated', hiddenLength: Math.max(1, text.length - lines.join(' ').replaceAll('…', '').length) });
573
+ }
574
+ return lines;
575
+ }
576
+ /** Full, untruncated text for tooltips, assistive technology and fallbacks. */
577
+ export function cardFullText(props) {
578
+ return [props.kicker, props.title, ...props.body, props.footer].filter(Boolean).join('\n');
579
+ }
580
+ export function zoneFullText(props) {
581
+ return [props.kicker, props.title, props.subtitle, props.footer].filter(Boolean).join('\n');
582
+ }
583
+ /**
584
+ * Lay out fixed card slots with measured ellipses and a pinned footer.
585
+ * Informational diagnostics retain the original text and hidden length.
586
+ * Missing glyphs use the visible unknown icon (owner A2/A11).
587
+ */
588
+ export function layoutCard(props, measure) {
589
+ const icon = props.icon ?? "base";
590
+ const suppressed = icon === "none";
591
+ const missing = !suppressed && props.iconGlyph === undefined && getDiagramGlyph(icon) === undefined;
592
+ props = { ...props, autoSize: props.autoSize ?? true, icon: missing ? "unknown" : icon,
593
+ iconGlyph: suppressed ? { face: "" } : props.iconGlyph ?? getDiagramGlyph(icon) ?? getDiagramGlyph("unknown") };
594
+ const g = CARD_GEOMETRY;
595
+ const diagnostics = missing ? [{ code: "card-icon-missing", slot: "icon", entityId: props.entityId, neededPx: 1, allocatedPx: 0 }] : [];
596
+ const align = props.align ?? "left";
597
+ const bodyMode = props.bodyMode ?? "leaf";
598
+ const contentTop = g.borderWidth + g.padding;
599
+ const contentRight = g.width - g.borderWidth - g.padding;
600
+ // The icon slot collapses when the glyph is missing (DIF-14): the header
601
+ // text then starts at the content inset and no space is reserved.
602
+ const hasIcon = !suppressed && props.icon !== undefined && props.iconGlyph !== undefined;
603
+ const headerTextX = hasIcon ? g.headerInset + g.iconSize + g.iconTextGap : g.headerInset;
604
+ const toggle = props.toggle !== undefined;
605
+ // Toggle present: the header text keeps the owner-pinned folded width
606
+ // (290 px with the icon — the hrpn folded-card title width 356 − 66);
607
+ // the 66 px reservation covers the +/− control's inline-end room.
608
+ const headerTextWidth = toggle
609
+ ? hasIcon
610
+ ? g.foldedHeaderTextWidth
611
+ : contentRight - g.toggleAllocation - headerTextX
612
+ : contentRight - headerTextX;
613
+ const headerAnchor = align === "center" ? "middle" : "start";
614
+ const headerLineX = align === "center" ? headerTextX + headerTextWidth / 2 : headerTextX;
615
+ const fitSlot = (text, role, width, budget, slot = role) => truncateSlot(text, role, width, budget, measure, diagnostic => diagnostics.push({ ...diagnostic, code: "card-overflow", slot, entityId: props.entityId }));
616
+ if (missing) {
617
+ diagnostics.push({
618
+ code: "card-icon-unknown",
619
+ slot: "icon",
620
+ entityId: props.entityId,
621
+ neededPx: 1,
622
+ allocatedPx: 0,
623
+ });
624
+ }
625
+ const kickerTexts = props.kicker === undefined ? [] : fitSlot(props.kicker, "kicker", headerTextWidth, g.allocatedLines.kicker);
626
+ // Reserve one body line when content is present; the title still has a
627
+ // maximum of two lines. This keeps every populated card readable at 200.
628
+ const footerReserve = props.footer === undefined ? g.borderWidth + g.padding
629
+ : lineBoxHeight(g.footer.size) + g.ruleHeight + 2 * g.ruleMargin + g.borderWidth + g.footerBottomInset;
630
+ const titleBudget = Math.max(1, Math.min(g.allocatedLines.title, Math.floor((g.height - contentTop - g.gap - footerReserve
631
+ - (props.body.length > 0 ? lineBoxHeight(g.body.size) : 0) - kickerTexts.length * lineBoxHeight(g.kicker.size)) / lineBoxHeight(g.title.size))));
632
+ const titleTexts = fitSlot(props.title, "title", headerTextWidth, titleBudget);
633
+ // Optional slots (review pass 2, item 10): one shared derivation — the
634
+ // 70 px icon floor applies only when the icon is present, and the text
635
+ // band is centred in the block exactly like the Svelte header row.
636
+ // Pass 3: the kicker/title line heights and the icon offset come from the
637
+ // same derivation too — no header geometry is recomputed outside it.
638
+ const headerGeo = headerSlotGeometry({
639
+ hasIcon,
640
+ kickerLines: kickerTexts.length,
641
+ titleLines: titleTexts.length,
642
+ });
643
+ const kickerHeight = headerGeo.kickerHeight;
644
+ const titleHeight = headerGeo.titleHeight;
645
+ const headerBlockHeight = headerGeo.blockHeight;
646
+ const textTop = contentTop + headerGeo.textTopOffset;
647
+ // Pass 3, item 11: the icon is centred in the header block exactly like
648
+ // the Svelte header row (align-items: center), so both renderers place it
649
+ // at contentTop + iconTopOffset.
650
+ const iconBox = hasIcon
651
+ ? { x: g.headerInset, y: contentTop + headerGeo.iconTopOffset, size: g.iconSize }
652
+ : undefined;
653
+ const bodyHeight = lineBoxHeight(g.body.size);
654
+ const bodyY = contentTop + headerBlockHeight + g.gap;
655
+ // bodyInsetFromBar measures accent-bar edge → first glyph (14). The accent
656
+ // bar occupies the card's left edge, so the body x is the token value
657
+ // itself — 2 px left of the header icon edge (16). In `subtitle` body mode
658
+ // (collapsed zone, §6.2.1) the body is the centred subtitle: wrapped at the
659
+ // frozen 396 px measure (width − 2 × (collapsedInset + marginInline)).
660
+ const subtitleMode = bodyMode === "subtitle";
661
+ const subtitleMeasure = g.width - 2 * (ZONE_GEOMETRY.collapsedInset + ZONE_GEOMETRY.collapsedSubtitleMarginInline);
662
+ const bodyTextX = subtitleMode ? (g.width - subtitleMeasure) / 2 : g.bodyInsetFromBar;
663
+ const bodyWidth = subtitleMode ? subtitleMeasure : contentRight - bodyTextX;
664
+ const bodyAnchor = subtitleMode || align === "center" ? "middle" : "start";
665
+ const bodyLineX = bodyAnchor === "middle" ? bodyTextX + bodyWidth / 2 : bodyTextX;
666
+ const wrappedBody = [];
667
+ let detailLines = 0;
668
+ for (const [bodyIndex, line] of props.body.entries()) {
669
+ const measured = measure(line, "body", bodyWidth, g.allocatedLines.body);
670
+ wrappedBody.push(...measured.lines);
671
+ if (bodyIndex >= props.body.length - (props.detailCount ?? 0))
672
+ detailLines += measured.lines.length;
673
+ }
674
+ const bodySlot = subtitleMode ? "collapsed-subtitle" : "body";
675
+ const bodySourceText = props.body.join("\n");
676
+ const footerHeight = lineBoxHeight(g.footer.size);
677
+ const footerBottom = g.height - g.borderWidth - g.footerBottomInset;
678
+ // Footer: wrapped as measured (never joined back), pinned bottom-up — the
679
+ // last line's box bottom sits at footerBottom; the rule stays ruleMargin
680
+ // above the footer block (§5.4). No footer: no rule (DH-04).
681
+ let footerLines = [];
682
+ let ruleY;
683
+ if (props.footer !== undefined) {
684
+ const footerWidth = contentRight - g.headerInset;
685
+ const footerTexts = fitSlot(props.footer, "footer", footerWidth, g.allocatedLines.footer);
686
+ const footerAnchor = align === "center" ? "middle" : "start";
687
+ const footerLineX = footerAnchor === "middle" ? g.headerInset + footerWidth / 2 : g.headerInset;
688
+ const footerBlockHeight = footerTexts.length * footerHeight;
689
+ footerLines = footerTexts.map((text, index) => ({
690
+ text,
691
+ x: footerLineX,
692
+ baseline: baselineOf(footerBottom - footerBlockHeight + index * footerHeight, g.footer.size),
693
+ size: g.footer.size,
694
+ weight: g.footer.weight,
695
+ secondary: false,
696
+ anchor: footerAnchor,
697
+ }));
698
+ ruleY = footerBottom - footerBlockHeight - g.ruleHeight - g.ruleMargin;
699
+ }
700
+ const bodyAreaBottom = ruleY === undefined ? g.height - g.borderWidth - g.padding : ruleY - g.ruleMargin;
701
+ const bodyBudget = Math.max(0, Math.floor((bodyAreaBottom - bodyY) / bodyHeight));
702
+ const bodyTexts = fitSlot(bodySourceText, "body", bodyWidth, bodyBudget, bodySlot);
703
+ // Subtitle mode centres the line block vertically inside the body area
704
+ // (§6.2.1); leaf lines stay top-aligned (§5.2).
705
+ const bodyBlockTop = subtitleMode
706
+ ? bodyY + Math.max(0, (bodyAreaBottom - bodyY - bodyTexts.length * bodyHeight) / 2)
707
+ : bodyY;
708
+ const detailStart = wrappedBody.length - detailLines;
709
+ const body = bodyTexts.map((text, index) => ({
710
+ text,
711
+ x: bodyLineX,
712
+ baseline: baselineOf(bodyBlockTop + index * bodyHeight, g.body.size),
713
+ size: g.body.size,
714
+ weight: g.body.weight,
715
+ secondary: !subtitleMode && index >= detailStart && (props.detailCount ?? 0) > 0,
716
+ anchor: bodyAnchor,
717
+ }));
718
+ const bodyBottom = bodyY + bodyTexts.length * bodyHeight;
719
+ const kicker = kickerTexts.map((text, index) => ({
720
+ text,
721
+ x: headerLineX,
722
+ baseline: baselineOf(textTop + index * kickerHeight, g.kicker.size),
723
+ size: g.kicker.size,
724
+ weight: g.kicker.weight,
725
+ secondary: true,
726
+ anchor: headerAnchor,
727
+ }));
728
+ // Pass 3, item 10: title placement follows the MEASURED kicker-line
729
+ // presence — an empty or whitespace-only kicker measures zero lines and
730
+ // reserves no line, exactly like the shared header derivation above.
731
+ const titleTop = textTop + kickerTexts.length * kickerHeight;
732
+ const title = titleTexts.map((text, index) => ({
733
+ text,
734
+ x: headerLineX,
735
+ baseline: baselineOf(titleTop + index * titleHeight, g.title.size),
736
+ size: g.title.size,
737
+ weight: g.title.weight,
738
+ secondary: false,
739
+ anchor: headerAnchor,
740
+ }));
741
+ // Toggle slot: top right of the content box with the card's own margins
742
+ // (hrpn folded-card placement, y = content top = 10 at the frozen values).
743
+ // The slot box is the 32 px IconButton; the 66 px text reservation is
744
+ // carried by headerTextWidth above.
745
+ const toggleBox = props.toggle === undefined
746
+ ? undefined
747
+ : {
748
+ x: g.width - g.toggleInset - g.toggleBoxSize,
749
+ y: g.toggleInset,
750
+ size: g.toggleBoxSize,
751
+ };
752
+ return {
753
+ icon: { name: props.icon, glyph: props.iconGlyph },
754
+ box: { width: g.width, height: g.height },
755
+ contentTop,
756
+ contentRight,
757
+ ...(iconBox === undefined ? {} : { iconBox }),
758
+ headerTextX,
759
+ headerTextWidth,
760
+ kicker,
761
+ title,
762
+ headerBlockHeight,
763
+ bodyX: bodyTextX,
764
+ bodyWidth,
765
+ bodyArea: { top: bodyY, bottom: bodyAreaBottom },
766
+ body,
767
+ bodyBottom,
768
+ ...(ruleY === undefined ? {} : { ruleY }),
769
+ ruleX1: g.ruleInsetStart,
770
+ ruleX2: g.width - g.ruleInsetEnd,
771
+ footerLines,
772
+ footerBottom,
773
+ ...(toggleBox === undefined ? {} : { toggleBox }),
774
+ diagnostics,
775
+ };
776
+ }
777
+ /* ------------------------------------------------------------------ *
778
+ * DC-2 static half — the wrapping card `DiagramZone` (spec §6). *
779
+ * Same module, same rules: zero imports, one geometry source. *
780
+ * ------------------------------------------------------------------ */
781
+ /**
782
+ * Frozen zone geometry (spec §6.4). Mirrors the `diagramZone` component
783
+ * tokens (`packages/tokens/src/component.ts`) and the `diagram-canvas`
784
+ * `measure.ts` constants (`CONTENT_INSET 16`, `RULE_GAP 4`,
785
+ * `SUBTITLE_TO_RULE_GAP 4`, `RULE_TO_FIRST_CHILD_GAP 16`,
786
+ * `LAST_CHILD_TO_BOTTOM_GAP 16`, `TOGGLE_ALLOCATION 66`,
787
+ * `FIXED_CARD_WIDTH 460`, `FIXED_CARD_HEIGHT 200`); the token↔constant
788
+ * agreement test asserts the sources agree. Fields shared with the atomic
789
+ * card alias `CARD_GEOMETRY` directly — one geometry source (§5.6).
790
+ */
791
+ export const ZONE_GEOMETRY = {
792
+ radius: 0,
793
+ borderWidth: 1.5,
794
+ // Shared with the atomic card by construction (one geometry source §5.6):
795
+ // every field below aliases CARD_GEOMETRY unless the zone frame differs.
796
+ headerInset: CARD_GEOMETRY.headerInset,
797
+ iconSize: CARD_GEOMETRY.iconSize,
798
+ iconTextGap: CARD_GEOMETRY.iconTextGap,
799
+ kicker: CARD_GEOMETRY.kicker,
800
+ title: CARD_GEOMETRY.title,
801
+ subtitle: CARD_GEOMETRY.body,
802
+ /** Rendered title → subtitle gap: alias of the card `gap` (§6.4). */
803
+ subtitleGap: CARD_GEOMETRY.gap,
804
+ headerRuleGap: 4,
805
+ contentInsetTop: spacing.containerPadding,
806
+ contentInsetBottom: spacing.containerPadding,
807
+ contentInsetInline: spacing.containerPadding,
808
+ ruleHeight: CARD_GEOMETRY.ruleHeight,
809
+ ruleMargin: CARD_GEOMETRY.ruleMargin,
810
+ ruleInsetStart: CARD_GEOMETRY.ruleInsetStart,
811
+ ruleInsetEnd: CARD_GEOMETRY.ruleInsetEnd,
812
+ footer: CARD_GEOMETRY.footer,
813
+ footerBottomInset: CARD_GEOMETRY.footerBottomInset,
814
+ toggleAllocation: CARD_GEOMETRY.toggleAllocation,
815
+ toggleBoxSize: CARD_GEOMETRY.toggleBoxSize,
816
+ /**
817
+ * Expanded toggle top inset: the frozen SVG places every zone toggle at
818
+ * `y + 10` (flush under the frame's top edge), while the header band
819
+ * starts at `y + 16`.
820
+ */
821
+ toggleTopInset: CARD_GEOMETRY.toggleInset,
822
+ toggleInset: CARD_GEOMETRY.toggleInset,
823
+ /**
824
+ * Expanded header text inset from the frame's left edge: the kit wraps
825
+ * kicker/title at `width − 178` (icon 16 + 70 + gap 6, toggle room, right
826
+ * inset) — 314 px at the PEOPLE width 492.
827
+ */
828
+ headerTextInset: 178,
829
+ collapsedWidth: CARD_GEOMETRY.width,
830
+ collapsedHeight: CARD_GEOMETRY.height,
831
+ collapsedInset: 16,
832
+ collapsedSubtitleMarginInline: 16,
833
+ allocatedLines: { kicker: 1, title: 2, subtitle: 2, footer: 1 },
834
+ };
835
+ /** Centred collapsed-subtitle measure (§6.2.1): 460 − 2 × (16 + 16). */
836
+ export const COLLAPSED_SUBTITLE_MEASURE = ZONE_GEOMETRY.collapsedWidth -
837
+ 2 * (ZONE_GEOMETRY.collapsedInset + ZONE_GEOMETRY.collapsedSubtitleMarginInline);
838
+ /**
839
+ * Collapsed-zone → atomic-card props mapping (§6.2 field table): the
840
+ * subtitle becomes the centred body (`bodyMode: "subtitle"`), the footer
841
+ * pins the rule, the toggle takes the top-right slot. Shared by
842
+ * `layoutZone` and both renderers so the collapsed zone is the card.
843
+ */
844
+ export function zoneCollapsedCardProps(props) {
845
+ props = { ...props, icon: props.icon ?? "cluster" };
846
+ return {
847
+ ...(props.autoSize === undefined ? {} : { autoSize: props.autoSize }),
848
+ ...(props.height === undefined ? {} : { height: props.height }),
849
+ entityId: props.entityId,
850
+ ...(props.kicker === undefined ? {} : { kicker: props.kicker }),
851
+ title: props.title,
852
+ body: props.subtitle === undefined || props.subtitle === "" ? [] : [props.subtitle],
853
+ ...(props.footer === undefined ? {} : { footer: props.footer }),
854
+ ...(props.icon === undefined ? {} : { icon: props.icon }),
855
+ ...(props.iconGlyph === undefined ? {} : { iconGlyph: props.iconGlyph }),
856
+ ...(props.tone === undefined ? {} : { tone: props.tone }),
857
+ ...(props.proposed === undefined ? {} : { proposed: props.proposed }),
858
+ ...(props.toggleLabel === undefined
859
+ ? {}
860
+ : { toggle: { collapsed: true, label: props.toggleLabel } }),
861
+ ...(props.align === undefined ? {} : { align: props.align }),
862
+ bodyMode: "subtitle",
863
+ };
864
+ }
865
+ /**
866
+ * Layout height of one child spec (review pass 3, item 9): a card child is
867
+ * the frozen 460 × 200; a zone child is laid out through `layoutZone` —
868
+ * recursively, so an auto-sized child contributes its own computed header +
869
+ * children + footer height, and an explicit or collapsed child its own box.
870
+ * A parent never assumes a 200 px child.
871
+ */
872
+ function childLayoutHeight(child, measure) {
873
+ if (child.kind === "card")
874
+ return child.card === undefined ? CARD_GEOMETRY.height : layoutCard(child.card, measure).box.height;
875
+ if (child.zone === undefined)
876
+ return ZONE_GEOMETRY.collapsedHeight;
877
+ return layoutZone(child.zone, measure, child.children ?? []).box.height;
878
+ }
879
+ /**
880
+ * Lay out one zone. Collapsed, the zone IS the atomic card (§6.2): the
881
+ * subtitle wraps at the frozen 396 px measure and centres in the card body
882
+ * area (`bodyMode: "subtitle"`), the footer pins the rule, and the toggle
883
+ * takes the top-right slot. Expanded, the frame carries the shared header
884
+ * (icon 70 at the 16 px inset, text at x + 92), the subtitle under the
885
+ * icon from the left inset, the header rule 4 px under the subtitle (or
886
+ * the header block), and the content box 16 px under the rule.
887
+ */
888
+ export function layoutZone(props, measure, children = []) {
889
+ const icon = props.icon ?? "cluster";
890
+ const suppressed = icon === "none";
891
+ const missing = !suppressed && props.iconGlyph === undefined && getDiagramGlyph(icon) === undefined;
892
+ props = { ...props, autoSize: props.autoSize ?? true, icon: missing ? "unknown" : icon,
893
+ iconGlyph: suppressed ? { face: "" } : props.iconGlyph ?? getDiagramGlyph(icon) ?? getDiagramGlyph("unknown") };
894
+ const g = { ...ZONE_GEOMETRY, ...(props.containerPadding === undefined ? {} : { contentInsetTop: props.containerPadding, contentInsetBottom: props.containerPadding, contentInsetInline: props.containerPadding }) };
895
+ const x = props.x ?? 0;
896
+ const y = props.y ?? 0;
897
+ const diagnostics = missing ? [{ code: "card-icon-missing", slot: "icon", entityId: props.entityId, neededPx: 1, allocatedPx: 0 }] : [];
898
+ if (missing) {
899
+ diagnostics.push({
900
+ code: "zone-icon-unknown",
901
+ slot: "icon",
902
+ entityId: props.entityId,
903
+ neededPx: 1,
904
+ allocatedPx: 0,
905
+ });
906
+ }
907
+ if (props.collapsed) {
908
+ const card = layoutCard(zoneCollapsedCardProps(props), measure);
909
+ for (const diagnostic of card.diagnostics) {
910
+ diagnostics.push({
911
+ code: diagnostic.code === "card-icon-missing" ? "card-icon-missing" : diagnostic.code === "card-icon-unknown" ? "zone-icon-unknown" : diagnostic.code === "card-grown" ? "zone-grown" : "zone-overflow",
912
+ slot: diagnostic.slot,
913
+ entityId: diagnostic.entityId,
914
+ neededPx: diagnostic.neededPx,
915
+ allocatedPx: diagnostic.allocatedPx,
916
+ ...(diagnostic.text === undefined ? {} : { text: diagnostic.text }),
917
+ ...(diagnostic.hiddenLength === undefined ? {} : { hiddenLength: diagnostic.hiddenLength, severity: diagnostic.severity, reason: diagnostic.reason }),
918
+ });
919
+ }
920
+ return {
921
+ icon: { name: props.icon, glyph: props.iconGlyph },
922
+ box: { x, y, width: g.collapsedWidth, height: card.box.height },
923
+ collapsed: true,
924
+ card,
925
+ diagnostics,
926
+ };
927
+ }
928
+ // Expanded frame.
929
+ const width = props.width ?? g.collapsedWidth;
930
+ const contentRight = x + width - g.contentInsetInline;
931
+ const hasIcon = !suppressed && props.icon !== undefined && props.iconGlyph !== undefined;
932
+ const headerTextX = hasIcon ? x + g.headerInset + g.iconSize + g.iconTextGap : x + g.headerInset;
933
+ // Kit header rule: with the icon and the toggle room both present,
934
+ // kicker/title wrap at `width − 178` (314 px at the PEOPLE width 492).
935
+ // Without the icon the text starts at the 16 px inset; without the
936
+ // toggle there is no inline-end reservation — the 178 applies only when
937
+ // icon and toggle are both there.
938
+ const rightHeaderReserve = props.toggleLabel === undefined
939
+ ? g.headerInset
940
+ : g.headerTextInset - (g.headerInset + g.iconSize + g.iconTextGap);
941
+ const headerTextWidth = Math.max(0, x + width - headerTextX - rightHeaderReserve);
942
+ const align = props.align ?? "left";
943
+ const headerAnchor = align === "center" ? "middle" : "start";
944
+ const headerLineX = align === "center" ? headerTextX + headerTextWidth / 2 : headerTextX;
945
+ const fitSlot = (text, role, width, budget, slot = role) => truncateSlot(text, role, width, budget, measure, diagnostic => diagnostics.push({ ...diagnostic, code: "zone-overflow", slot, entityId: props.entityId }));
946
+ // The subtitle box occupies exactly its shared line boxes.
947
+ const subtitleLineHeight = lineBoxHeight(g.subtitle.size);
948
+ const kickerTexts = props.kicker === undefined ? [] : fitSlot(props.kicker, "kicker", headerTextWidth, g.allocatedLines.kicker);
949
+ const titleTexts = fitSlot(props.title, "title", headerTextWidth, g.allocatedLines.title);
950
+ // Optional slots (review pass 2, item 10): the same shared derivation as
951
+ // the card — the 70 px icon floor applies only with the icon, and the
952
+ // text band is centred in the block exactly like the Svelte header row
953
+ // (align-items: center), so both renderers place it identically. Pass 3:
954
+ // the kicker/title line heights come from the same derivation too.
955
+ const headerGeo = headerSlotGeometry({
956
+ hasIcon,
957
+ kickerLines: kickerTexts.length,
958
+ titleLines: titleTexts.length,
959
+ });
960
+ const kickerHeight = headerGeo.kickerHeight;
961
+ const titleHeight = headerGeo.titleHeight;
962
+ const headerBlockHeight = headerGeo.blockHeight;
963
+ const headerTop = y + g.headerInset;
964
+ const textTop = headerTop + headerGeo.textTopOffset;
965
+ const iconBox = hasIcon
966
+ ? { x: x + g.headerInset, y: headerTop + headerGeo.iconTopOffset, size: g.iconSize }
967
+ : undefined;
968
+ const kicker = kickerTexts.map((text, index) => ({
969
+ text,
970
+ x: headerLineX,
971
+ baseline: baselineOf(textTop + index * kickerHeight, g.kicker.size),
972
+ size: g.kicker.size,
973
+ weight: g.kicker.weight,
974
+ secondary: true,
975
+ anchor: headerAnchor,
976
+ }));
977
+ // Pass 3, item 10: title placement follows the MEASURED kicker-line
978
+ // presence — an empty or whitespace-only kicker measures zero lines and
979
+ // reserves no line, exactly like the shared header derivation above.
980
+ const titleTop = textTop + kickerTexts.length * kickerHeight;
981
+ const title = titleTexts.map((text, index) => ({
982
+ text,
983
+ x: headerLineX,
984
+ baseline: baselineOf(titleTop + index * titleHeight, g.title.size),
985
+ size: g.title.size,
986
+ weight: g.title.weight,
987
+ secondary: false,
988
+ anchor: headerAnchor,
989
+ }));
990
+ // Subtitle wraps under the icon from the left inset at x + 16 (§6.2).
991
+ // Kit rule: min(width, 460) − 26, i.e. the same 16/10 insets as the
992
+ // header rule inside a box capped at the atomic card width.
993
+ const subtitleX = x + g.headerInset;
994
+ const subtitleWidth = Math.min(width, g.collapsedWidth) - g.ruleInsetStart - g.ruleInsetEnd;
995
+ const subtitleAnchor = align === "center" ? "middle" : "start";
996
+ const subtitleLineX = align === "center" ? subtitleX + subtitleWidth / 2 : subtitleX;
997
+ const subtitleTexts = fitSlot(props.subtitle ?? "", "body", subtitleWidth, g.allocatedLines.subtitle, "subtitle");
998
+ const subtitleTop = headerTop + headerBlockHeight + g.subtitleGap;
999
+ const subtitle = subtitleTexts.map((text, index) => ({
1000
+ text,
1001
+ x: subtitleLineX,
1002
+ baseline: baselineOf(subtitleTop + index * subtitleLineHeight, g.subtitle.size),
1003
+ size: g.subtitle.size,
1004
+ weight: g.subtitle.weight,
1005
+ secondary: false,
1006
+ anchor: subtitleAnchor,
1007
+ }));
1008
+ // Full line boxes, including descent, determine the subtitle bottom.
1009
+ const subtitleBottom = subtitleTexts.length === 0
1010
+ ? subtitleTop
1011
+ : subtitleTop + subtitleTexts.length * subtitleLineHeight;
1012
+ // Header rule 4 px under the subtitle last line, or under the header
1013
+ // block when there is no subtitle (§6.2, §6.4 headerRuleGap).
1014
+ const ruleY = subtitleTexts.length > 0
1015
+ ? subtitleBottom + g.headerRuleGap
1016
+ : headerTop + headerBlockHeight + g.headerRuleGap;
1017
+ const contentTop = ruleY + g.contentInsetTop;
1018
+ // Footer, expanded state too (§6.1: folding never changes the footer):
1019
+ // wrapped as measured, pinned bottom-up at the frame bottom like the
1020
+ // card, with its own rule ruleMargin above the footer block. Measured
1021
+ // before the default height so an auto-sized frame reserves the footer.
1022
+ const footerHeight = lineBoxHeight(g.footer.size);
1023
+ const footerX = x + g.contentInsetInline;
1024
+ const footerWidth = x + width - g.contentInsetInline - footerX;
1025
+ const footerMeasured = props.footer === undefined ? undefined : measure(props.footer, "footer", footerWidth, g.allocatedLines.footer);
1026
+ const footerTexts = fitSlot(props.footer ?? "", "footer", footerWidth, g.allocatedLines.footer);
1027
+ const footerBlockHeight = footerTexts.length * footerHeight;
1028
+ // Frame height (review pass 2, item 9; pass 3, item 9): the supplied box,
1029
+ // or — when the host lets the zone size itself — the full vertical
1030
+ // requirement: header + rule (contentTop) or children, whichever is
1031
+ // lower, plus the bottom content inset and the footer reserve (footer
1032
+ // block + rule + both rule gaps + bottom inset). Children contribute
1033
+ // their own recursively computed layout height (never an assumed 200 px),
1034
+ // so a nested auto-sized child is fully inside the frame. The old
1035
+ // formula ignored the header, so a tall header silently overflowed an
1036
+ // auto-sized 200 px frame.
1037
+ const childrenBottom = children.reduce((bottom, child) => Math.max(bottom, child.y + childLayoutHeight(child, measure)), y);
1038
+ const footerReserve = footerTexts.length === 0
1039
+ ? 0
1040
+ : footerBlockHeight + g.ruleHeight + 2 * g.ruleMargin + g.footerBottomInset;
1041
+ const requiredHeight = Math.max(childrenBottom, contentTop) + g.contentInsetBottom - y + footerReserve;
1042
+ const height = props.autoSize ? Math.max(props.height ?? g.collapsedHeight, requiredHeight)
1043
+ : props.height ?? Math.max(g.collapsedHeight, requiredHeight);
1044
+ // An explicitly undersized frame reports a hard-cap diagnostic.
1045
+ // Renderers retain all content (owner A11).
1046
+ if (!props.autoSize && props.height !== undefined && requiredHeight > props.height) {
1047
+ diagnostics.push({
1048
+ code: "zone-overflow",
1049
+ slot: "frame",
1050
+ entityId: props.entityId,
1051
+ neededPx: requiredHeight,
1052
+ allocatedPx: props.height,
1053
+ text: props.title,
1054
+ });
1055
+ }
1056
+ // Toggle slot: the frozen SVG places every expanded-zone toggle at
1057
+ // y + 10 (the header band itself starts at y + 16).
1058
+ const toggleBox = props.toggleLabel === undefined
1059
+ ? undefined
1060
+ : { x: x + width - g.toggleInset - g.toggleBoxSize, y: y + g.toggleInset, size: g.toggleBoxSize };
1061
+ const footerBottom = y + height - g.borderWidth - g.footerBottomInset;
1062
+ let footer = [];
1063
+ let footerRuleY;
1064
+ if (footerMeasured !== undefined) {
1065
+ const footerAnchor = align === "center" ? "middle" : "start";
1066
+ const footerLineX = footerAnchor === "middle" ? footerX + footerWidth / 2 : footerX;
1067
+ footer = footerTexts.map((text, index) => ({
1068
+ text,
1069
+ x: footerLineX,
1070
+ baseline: baselineOf(footerBottom - footerBlockHeight + index * footerHeight, g.footer.size),
1071
+ size: g.footer.size,
1072
+ weight: g.footer.weight,
1073
+ secondary: false,
1074
+ anchor: footerAnchor,
1075
+ }));
1076
+ footerRuleY = footerBottom - footerBlockHeight - g.ruleHeight - g.ruleMargin;
1077
+ }
1078
+ return {
1079
+ icon: { name: props.icon, glyph: props.iconGlyph },
1080
+ box: { x, y, width, height },
1081
+ collapsed: false,
1082
+ header: {
1083
+ ...(iconBox === undefined ? {} : { iconBox }),
1084
+ headerTextX,
1085
+ headerTextWidth,
1086
+ kicker,
1087
+ title,
1088
+ headerBlockHeight,
1089
+ subtitle,
1090
+ subtitleWidth,
1091
+ subtitleBottom,
1092
+ ruleY,
1093
+ contentTop,
1094
+ ...(toggleBox === undefined ? {} : { toggleBox }),
1095
+ footer,
1096
+ ...(footerRuleY === undefined ? {} : { footerRuleY }),
1097
+ footerBottom,
1098
+ },
1099
+ diagnostics,
1100
+ };
1101
+ }
1102
+ /**
1103
+ * Shared `EntityPresentation` → zone props projection (§4.2/§6.1): container
1104
+ * and legend forms resolve to a zone; a missing entry or no title resolves
1105
+ * to `{ kind: "label" }` (L-R12).
1106
+ */
1107
+ export function projectZoneProps(presentation, opts) {
1108
+ if (presentation === undefined ||
1109
+ (presentation.form !== "container" && presentation.form !== "legend")) {
1110
+ return { kind: "label" };
1111
+ }
1112
+ const title = presentation.text.title ?? "";
1113
+ if (title === "")
1114
+ return { kind: "label" };
1115
+ const icon = presentation.icon ?? "cluster";
1116
+ return {
1117
+ kind: "zone",
1118
+ props: {
1119
+ entityId: opts.entityId,
1120
+ ...(opts.containerPadding === undefined ? {} : { containerPadding: opts.containerPadding }),
1121
+ ...(presentation.text.kicker === undefined || presentation.text.kicker === ""
1122
+ ? {}
1123
+ : { kicker: presentation.text.kicker }),
1124
+ title,
1125
+ ...(presentation.text.subtitle === undefined || presentation.text.subtitle === ""
1126
+ ? {}
1127
+ : { subtitle: presentation.text.subtitle }),
1128
+ ...(presentation.text.footer === undefined || presentation.text.footer === ""
1129
+ ? {}
1130
+ : { footer: presentation.text.footer }),
1131
+ ...(icon === undefined ? {} : { icon }),
1132
+ ...(icon === undefined || opts.resolveGlyph === undefined
1133
+ ? {}
1134
+ : (() => {
1135
+ const glyph = opts.resolveGlyph(icon);
1136
+ return glyph === undefined ? {} : { iconGlyph: glyph };
1137
+ })()),
1138
+ ...(presentation.categoryRef === undefined ? {} : { tone: presentation.categoryRef }),
1139
+ ...(presentation.proposed === undefined ? {} : { proposed: presentation.proposed }),
1140
+ collapsed: opts.collapsed,
1141
+ ...(opts.toggleLabel === undefined ? {} : { toggleLabel: opts.toggleLabel }),
1142
+ ...(opts.align === undefined ? {} : { align: opts.align }),
1143
+ ...(opts.x === undefined ? {} : { x: opts.x }),
1144
+ ...(opts.y === undefined ? {} : { y: opts.y }),
1145
+ ...(opts.width === undefined ? {} : { width: opts.width }),
1146
+ ...(opts.height === undefined ? {} : { height: opts.height }),
1147
+ },
1148
+ };
1149
+ }
1150
+ //# sourceMappingURL=diagramCardModel.js.map