@nowline/renderer 0.0.0-dev.20260601071750.g04bdff9

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.
@@ -0,0 +1,2204 @@
1
+ import type {
2
+ InlineDatePin,
3
+ Point,
4
+ PositionedAnchor,
5
+ PositionedCapacity,
6
+ PositionedDependencyEdge,
7
+ PositionedFootnoteArea,
8
+ PositionedGroup,
9
+ PositionedHeader,
10
+ PositionedIncludeRegion,
11
+ PositionedItem,
12
+ PositionedMilestone,
13
+ PositionedNowline,
14
+ PositionedParallel,
15
+ PositionedRoadmap,
16
+ PositionedSwimlane,
17
+ PositionedTimelineScale,
18
+ PositionedTrackChild,
19
+ ResolvedStyle,
20
+ Theme,
21
+ } from '@nowline/layout';
22
+ import {
23
+ ACCENT_DASH_PATTERN,
24
+ ATTRIBUTION_BAR_LOGICAL_WIDTH,
25
+ ATTRIBUTION_BAR_LOGICAL_X,
26
+ ATTRIBUTION_INE_LOGICAL_X,
27
+ ATTRIBUTION_LINK,
28
+ ATTRIBUTION_NOW_LOGICAL_X,
29
+ ATTRIBUTION_PREFIX_FONT_SIZE,
30
+ ATTRIBUTION_SCALE,
31
+ ATTRIBUTION_TEXT,
32
+ ATTRIBUTION_WORDMARK_FONT_SIZE,
33
+ CORNER_RADIUS_PX,
34
+ EDGE_CORNER_RADIUS,
35
+ estimateCapacitySuffixWidth,
36
+ FONT_STACK,
37
+ FOOTNOTE_HEADER_BASELINE_OFFSET_PX,
38
+ FOOTNOTE_HEADER_HEIGHT_PX,
39
+ FOOTNOTE_PANEL_PADDING_PX,
40
+ FOOTNOTE_ROW_HEIGHT,
41
+ FRAME_TAB_HEIGHT_PX,
42
+ FRAME_TAB_LABEL_BASELINE_OFFSET_PX,
43
+ frameTabGeometry,
44
+ GROUP_BRACKET_LABEL_OVERHANG_PX,
45
+ GROUP_TITLE_TAB_CHAR_WIDTH_PX,
46
+ GROUP_TITLE_TAB_HEIGHT_PX,
47
+ GROUP_TITLE_TAB_LABEL_BASELINE_OFFSET_PX,
48
+ GROUP_TITLE_TAB_LABEL_FONT_SIZE_PX,
49
+ GROUP_TITLE_TAB_PAD_X_PX,
50
+ HEADER_AUTHOR_FONT_SIZE_PX,
51
+ HEADER_AUTHOR_LINE_HEIGHT_PX,
52
+ HEADER_CARD_PADDING_TOP,
53
+ HEADER_CARD_PADDING_X,
54
+ HEADER_TITLE_FONT_SIZE_PX,
55
+ HEADER_TITLE_LINE_HEIGHT_PX,
56
+ HEADER_TITLE_TO_AUTHOR_GAP_PX,
57
+ ITEM_CAPTION_INSET_X_PX,
58
+ ITEM_CAPTION_META_BASELINE_OFFSET_PX,
59
+ ITEM_CAPTION_META_FONT_SIZE_PX,
60
+ ITEM_CAPTION_SPILL_GAP_PX,
61
+ ITEM_CAPTION_TITLE_BASELINE_OFFSET_PX,
62
+ ITEM_CAPTION_TITLE_FONT_SIZE_PX,
63
+ ITEM_DECORATION_SPILL_GAP_PX,
64
+ ITEM_FOOTNOTE_INDICATOR_BASELINE_OFFSET_PX,
65
+ ITEM_FOOTNOTE_INDICATOR_INSET_RIGHT_PX,
66
+ ITEM_FOOTNOTE_INDICATOR_STEP_PX,
67
+ ITEM_LINK_ICON_INSET_PX,
68
+ ITEM_LINK_ICON_TILE_SIZE_PX,
69
+ ITEM_STATUS_DOT_INSET_RIGHT_PX,
70
+ ITEM_STATUS_DOT_INSET_TOP_PX,
71
+ ITEM_STATUS_DOT_RADIUS_PX,
72
+ includeChromeGeometry,
73
+ NOW_PILL_CORNER_RADIUS_PX,
74
+ NOW_PILL_HEIGHT_PX,
75
+ NOW_PILL_LABEL_BASELINE_OFFSET_PX,
76
+ NOW_PILL_LABEL_FONT_SIZE_PX,
77
+ NOW_PILL_LABEL_INSET_X_PX,
78
+ NOWLINE_STROKE_WIDTH_PX,
79
+ PROGRESS_STRIP_HEIGHT_PX,
80
+ TEXT_SIZE_PX,
81
+ TIMELINE_TICK_LABEL_BASELINE_OFFSET_PX,
82
+ } from '@nowline/layout';
83
+ import { BUILTIN_ICON_SVG, CAPACITY_ICON_SVG } from './icons.js';
84
+ import { IdGenerator } from './ids.js';
85
+ import { sanitizeSvg } from './sanitize.js';
86
+ import { allShadowDefs, shadowFilterUrl } from './shadow.js';
87
+ import { attrs, escAttr, escText, num, tag, textTag } from './xml.js';
88
+
89
+ // Browser-safe types. The renderer never touches `fs`, `path`, or `Buffer`.
90
+ // Callers inject an AssetResolver when they want logos embedded.
91
+ export interface AssetBytes {
92
+ bytes: Uint8Array;
93
+ mime: string;
94
+ }
95
+
96
+ export type AssetResolver = (ref: string) => Promise<AssetBytes>;
97
+
98
+ export interface RenderOptions {
99
+ assetResolver?: AssetResolver;
100
+ noLinks?: boolean;
101
+ strict?: boolean;
102
+ warn?: (message: string) => void;
103
+ // Override the deterministic id prefix (defaults to 'nl').
104
+ idPrefix?: string;
105
+ }
106
+
107
+ // `TEXT_SIZE_PX`, `CORNER_RADIUS_PX`, `FONT_STACK` come from
108
+ // `@nowline/layout` (themes/shared) so a typography or radius change
109
+ // flows from one place to both layout and renderer.
110
+ //
111
+ // `WEIGHT_NUM` lives here only because no DSL `weight` table exists in
112
+ // shared yet. If/when it does, hoist this alongside `FONT_STACK`.
113
+ const WEIGHT_NUM: Record<string, number> = {
114
+ thin: 100,
115
+ light: 300,
116
+ normal: 400,
117
+ bold: 700,
118
+ };
119
+
120
+ // `style.textSize` is `SizeBucket` which includes `'full'`; the shared
121
+ // `TEXT_SIZE_PX` table only carries the size buckets (no `'full'` —
122
+ // that's a corner-radius-only value). Widen the lookup so a stray
123
+ // `'full'` falls through to the `?? 14` fallback instead of compiling.
124
+ function textSizePx(bucket: ResolvedStyle['textSize']): number {
125
+ return (TEXT_SIZE_PX as Record<string, number>)[bucket] ?? 14;
126
+ }
127
+
128
+ function fontAttrs(style: ResolvedStyle, overrideSize?: number): Record<string, string | number> {
129
+ return {
130
+ 'font-family': FONT_STACK[style.font],
131
+ 'font-size': overrideSize ?? textSizePx(style.textSize),
132
+ 'font-weight': WEIGHT_NUM[style.weight] ?? 400,
133
+ 'font-style': style.italic ? 'italic' : 'normal',
134
+ fill: style.text,
135
+ };
136
+ }
137
+
138
+ function strokeDash(style: ResolvedStyle): string | undefined {
139
+ if (style.border === 'dashed') return '4 3';
140
+ if (style.border === 'dotted') return '1 3';
141
+ return undefined;
142
+ }
143
+
144
+ /**
145
+ * sRGB → relative luminance per WCAG 2.x. Input may be `#rrggbb`,
146
+ * `#rgb`, or a non-hex token like `none` / `transparent`. Non-hex
147
+ * inputs return 1 (treated as light) so a transparent bar reuses
148
+ * the chart's light bg. Mostly used to choose between two
149
+ * status-dot palettes (`onLight` vs `onDark`) so the dot reads on
150
+ * any bar fill — see `pickStatusDotPalette` and
151
+ * `specs/rendering.md`'s status-dot section.
152
+ */
153
+ function relativeLuminance(hex: string): number {
154
+ if (!hex || hex === 'none' || hex === 'transparent') return 1;
155
+ let h = hex.startsWith('#') ? hex.slice(1) : hex;
156
+ if (h.length === 3) {
157
+ h = h
158
+ .split('')
159
+ .map((c) => c + c)
160
+ .join('');
161
+ }
162
+ if (h.length !== 6) return 1;
163
+ const r = parseInt(h.slice(0, 2), 16) / 255;
164
+ const g = parseInt(h.slice(2, 4), 16) / 255;
165
+ const b = parseInt(h.slice(4, 6), 16) / 255;
166
+ const lin = (c: number): number => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);
167
+ return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
168
+ }
169
+
170
+ /**
171
+ * Pick the status-dot palette whose tone contrasts best with the
172
+ * given bar bg.
173
+ *
174
+ * The crossover threshold is the bar luminance at which a deep dot
175
+ * (avg luminance ≈ 0.045 across `onLight` palette entries) and a
176
+ * pale dot (avg ≈ 0.85 across `onDark` entries) give equal WCAG
177
+ * contrast. Solving `(L_bar + 0.05)² ≈ 0.86 × 0.095` gives
178
+ * `L_bar ≈ 0.24`, so:
179
+ * - bars with luminance ≥ 0.24 (most label-driven mid-tones AND
180
+ * all pale status-tint bars) → `onLight` deep dot
181
+ * - bars with luminance < 0.24 (dark status-tint bars in dark
182
+ * theme, e.g. `#172554`) → `onDark` pale dot
183
+ */
184
+ function pickStatusDotPalette(bg: string, palette: Theme): Theme['statusDot']['onLight'] {
185
+ return relativeLuminance(bg) >= 0.24 ? palette.statusDot.onLight : palette.statusDot.onDark;
186
+ }
187
+
188
+ /**
189
+ * Approx. rendered width (px) of `text` at `fontSizePx`. Mirrors the
190
+ * `0.58 em / char` heuristic the layout uses for spill detection so the
191
+ * renderer's positioning of the capacity suffix lines up with what the
192
+ * layout reserved.
193
+ */
194
+ function estimateCaptionWidthPx(text: string, fontSizePx: number): number {
195
+ return text.length * fontSizePx * 0.58;
196
+ }
197
+
198
+ /**
199
+ * Paint an item / lane capacity suffix starting at `(x0, baselineY)`. The
200
+ * capacity model arrives pre-resolved from layout (`PositionedCapacity`):
201
+ * `text` is the formatted number, `icon` is either `null` (no glyph), a
202
+ * built-in name, or an inline literal string.
203
+ *
204
+ * Three rendering paths:
205
+ *
206
+ * 1. `icon === null` (the resolved `capacity-icon` was `'none'`): paint
207
+ * the bare number as a single `<text>` node.
208
+ * 2. `icon.kind === 'builtin'` and `name === 'multiplier'`: paint
209
+ * `5×` as a single `<text>` node — the multiplication sign is a
210
+ * typographic operator with consistent rendering across system
211
+ * fonts and built-in side bearing, so no `<tspan dx>` separator.
212
+ * 3. `icon.kind === 'builtin'` and `name` ∈ {person, people, points,
213
+ * time}: paint the number as `<text>`, then drop the curated SVG
214
+ * icon at the next column (`0.1em` gap, sized to one em). The
215
+ * icon's `style="color:..."` propagates through the
216
+ * `currentColor`-bound paths in the icon library.
217
+ * 4. `icon.kind === 'literal'` (inline Unicode literal or
218
+ * dereferenced custom `symbol`): paint number + glyph in a single
219
+ * `<text>`, with a `<tspan dx="0.1em">` separator before the
220
+ * glyph payload.
221
+ *
222
+ * `precedingText` is the existing meta-line text (or `undefined` when the
223
+ * suffix is standalone). When present, the suffix's left edge starts one
224
+ * space's width past the meta text's estimated right edge so `2w 5×`
225
+ * reads as a single caption rather than running text together.
226
+ */
227
+ function renderCapacitySuffix(
228
+ capacity: PositionedCapacity,
229
+ precedingText: string | undefined,
230
+ x0: number,
231
+ baselineY: number,
232
+ fontSize: number,
233
+ fontFamily: string,
234
+ color: string,
235
+ ): string {
236
+ const charWidthPx = fontSize * 0.58;
237
+ const precedingWidthPx = precedingText ? estimateCaptionWidthPx(precedingText, fontSize) : 0;
238
+ const separatorPx = precedingText ? charWidthPx : 0;
239
+ const numberX = x0 + precedingWidthPx + separatorPx;
240
+ const { text: numberStr, icon } = capacity;
241
+ const numberWidthPx = estimateCaptionWidthPx(numberStr, fontSize);
242
+ const baseAttrs = {
243
+ 'font-family': fontFamily,
244
+ 'font-size': fontSize,
245
+ fill: color,
246
+ } as const;
247
+
248
+ if (!icon) {
249
+ return textTag({ x: num(numberX), y: num(baselineY), ...baseAttrs }, numberStr);
250
+ }
251
+
252
+ if (icon.kind === 'builtin' && icon.name === 'multiplier') {
253
+ return textTag({ x: num(numberX), y: num(baselineY), ...baseAttrs }, `${numberStr}\u00D7`);
254
+ }
255
+
256
+ if (icon.kind === 'builtin') {
257
+ const def = CAPACITY_ICON_SVG[icon.name];
258
+ // Render `<text>5</text>` followed by the curated SVG icon. The
259
+ // icon sits at one font-em wide and tall, with a 0.1em separator
260
+ // gap. Vertical positioning lifts the icon so its visual center
261
+ // sits on the text x-height (`baselineY - fontSize * 0.85`); this
262
+ // matches how Lucide-style outline icons read in inline text.
263
+ const numberSvg = textTag({ x: num(numberX), y: num(baselineY), ...baseAttrs }, numberStr);
264
+ const gapPx = fontSize * 0.1;
265
+ const iconSize = fontSize;
266
+ const iconX = numberX + numberWidthPx + gapPx;
267
+ const iconY = baselineY - fontSize * 0.85;
268
+ const iconSvg = `<svg x="${num(iconX)}" y="${num(iconY)}" width="${num(iconSize)}" height="${num(iconSize)}" viewBox="${def.viewBox}" style="color:${escAttr(color)}" aria-hidden="true">${def.body}</svg>`;
269
+ return numberSvg + iconSvg;
270
+ }
271
+
272
+ // Literal glyph (inline Unicode literal or dereferenced custom glyph).
273
+ // Single <text> node with the number + a tspan-separated glyph.
274
+ const dx = num(fontSize * 0.1);
275
+ return (
276
+ `<text x="${num(numberX)}" y="${num(baselineY)}" font-family="${escAttr(fontFamily)}"` +
277
+ ` font-size="${fontSize}" fill="${escAttr(color)}">` +
278
+ `${escText(numberStr)}<tspan dx="${dx}">${escText(icon.text)}</tspan>` +
279
+ '</text>'
280
+ );
281
+ }
282
+
283
+ /**
284
+ * Em-distance the renderer reserves between an item's metaText and its
285
+ * capacity suffix (or any inline trailing token). Half a space's worth
286
+ * — wide enough to read as a separator, narrow enough not to look like
287
+ * a stray gap. SVG `<tspan dx>` honors this exactly so we don't depend
288
+ * on per-character estimation for the gap itself.
289
+ */
290
+ const META_SUFFIX_DX_EM = 0.6;
291
+
292
+ /**
293
+ * Tighter em-per-char width estimate used inside `renderItemMetaLine`
294
+ * for positioning a built-in SVG icon after a metaText + number. Layout
295
+ * still uses 0.58 em/char to pessimistically reserve bar width for
296
+ * spill detection (so `metaText 5×` always fits its row); the renderer
297
+ * paints the icon at a tighter offset so there's no visible
298
+ * dead-space between the rendered number and its glyph. Lands inside
299
+ * the layout reservation either way (`0.5 < 0.58`).
300
+ */
301
+ const META_ICON_EM_PER_CHAR = 0.5;
302
+
303
+ /**
304
+ * Paint the item meta line (metaText plus optional capacity suffix) as
305
+ * a single SVG fragment. Call sites used to emit metaText and the
306
+ * suffix as two unrelated `<text>` nodes whose horizontal offsets came
307
+ * from `estimateCaptionWidthPx` — fine for short metas like `2w`, but
308
+ * the per-character estimate over-reserves for longer captions like
309
+ * `L 1w — 1w remaining`, leaving a visible gap before `2×`.
310
+ *
311
+ * The fix flows the suffix INSIDE the same `<text>` element via
312
+ * `<tspan dx>` whenever the suffix is pure inline text (multiplier,
313
+ * literal glyph, or no glyph). Browsers compute the dx anchor relative
314
+ * to the previously rendered glyph so the gap matches the spec
315
+ * regardless of how wide metaText actually rendered.
316
+ *
317
+ * Built-in SVG icons (person/people/points/time) still need an `<svg>`
318
+ * sibling outside `<text>`, so the icon's x is computed via the
319
+ * tighter `META_ICON_EM_PER_CHAR` constant — which still lands inside
320
+ * the layout's pessimistic spill reservation.
321
+ */
322
+ function renderItemMetaLine(opts: {
323
+ metaText: string | undefined;
324
+ capacity: PositionedCapacity | null;
325
+ x: number;
326
+ baselineY: number;
327
+ fontSize: number;
328
+ fontFamily: string;
329
+ color: string;
330
+ }): string {
331
+ const { metaText, capacity, x, baselineY, fontSize, fontFamily, color } = opts;
332
+ if (!metaText && !capacity) return '';
333
+ const baseAttrs = {
334
+ 'font-family': fontFamily,
335
+ 'font-size': fontSize,
336
+ fill: color,
337
+ } as const;
338
+ if (!capacity) {
339
+ return textTag({ x: num(x), y: num(baselineY), ...baseAttrs }, metaText!);
340
+ }
341
+ if (!metaText) {
342
+ return renderCapacitySuffix(capacity, undefined, x, baselineY, fontSize, fontFamily, color);
343
+ }
344
+ const sepDx = num(fontSize * META_SUFFIX_DX_EM);
345
+ const openText =
346
+ `<text x="${num(x)}" y="${num(baselineY)}" font-family="${escAttr(fontFamily)}"` +
347
+ ` font-size="${fontSize}" fill="${escAttr(color)}">`;
348
+ const { text: numberStr, icon } = capacity;
349
+ if (!icon) {
350
+ return (
351
+ openText +
352
+ escText(metaText) +
353
+ `<tspan dx="${sepDx}">${escText(numberStr)}</tspan>` +
354
+ '</text>'
355
+ );
356
+ }
357
+ if (icon.kind === 'builtin' && icon.name === 'multiplier') {
358
+ return (
359
+ openText +
360
+ escText(metaText) +
361
+ `<tspan dx="${sepDx}">${escText(numberStr)}\u00D7</tspan>` +
362
+ '</text>'
363
+ );
364
+ }
365
+ if (icon.kind === 'literal') {
366
+ const glyphDx = num(fontSize * 0.1);
367
+ return (
368
+ openText +
369
+ escText(metaText) +
370
+ `<tspan dx="${sepDx}">${escText(numberStr)}</tspan>` +
371
+ `<tspan dx="${glyphDx}">${escText(icon.text)}</tspan>` +
372
+ '</text>'
373
+ );
374
+ }
375
+ // Built-in SVG icon — text element holds metaText + tspan-separated
376
+ // number, then the icon SVG sits at an estimated position. Tighter
377
+ // per-character estimate than layout's spill detector so the icon
378
+ // visually hugs the number.
379
+ const def = CAPACITY_ICON_SVG[icon.name];
380
+ const tightWidth = (s: string) => s.length * fontSize * META_ICON_EM_PER_CHAR;
381
+ const numberStartX = x + tightWidth(metaText) + fontSize * META_SUFFIX_DX_EM;
382
+ const iconGapPx = fontSize * 0.1;
383
+ const iconSize = fontSize;
384
+ const iconX = numberStartX + tightWidth(numberStr) + iconGapPx;
385
+ const iconY = baselineY - fontSize * 0.85;
386
+ const textPart =
387
+ openText +
388
+ escText(metaText) +
389
+ `<tspan dx="${sepDx}">${escText(numberStr)}</tspan>` +
390
+ '</text>';
391
+ const iconPart = `<svg x="${num(iconX)}" y="${num(iconY)}" width="${num(iconSize)}" height="${num(iconSize)}" viewBox="${def.viewBox}" style="color:${escAttr(color)}" aria-hidden="true">${def.body}</svg>`;
392
+ return textPart + iconPart;
393
+ }
394
+
395
+ /**
396
+ * Paint one inline-date pin (`after:DATE` / `before:DATE`) as a small
397
+ * `calendar` glyph from the curated icon library. Wrapped in a `<g>`
398
+ * carrying `<title>YYYY-MM-DD</title>` so browsers surface the date as
399
+ * a native hover tooltip; non-interactive exports (PDF / PNG) still
400
+ * carry the date in the title text. Z-order: above the bar fill,
401
+ * alongside status dot and footnote indicators, below dependency
402
+ * arrowheads.
403
+ *
404
+ * Shared by item, group, and parallel render paths so the visual is
405
+ * identical across every entity type.
406
+ */
407
+ function renderInlineDatePin(pin: InlineDatePin, color: string): string {
408
+ const def = BUILTIN_ICON_SVG.calendar;
409
+ const inner =
410
+ `<svg x="${num(pin.glyphTopLeft.x)}" y="${num(pin.glyphTopLeft.y)}"` +
411
+ ` width="${num(pin.glyphSize)}" height="${num(pin.glyphSize)}"` +
412
+ ` viewBox="${def.viewBox}" style="color:${escAttr(color)}" aria-hidden="true">${def.body}</svg>`;
413
+ const titleEl = `<title>${escText(pin.isoDate)}</title>`;
414
+ return tag(
415
+ 'g',
416
+ {
417
+ 'data-layer': 'inline-date-pin',
418
+ 'data-side': pin.side,
419
+ 'data-date': pin.isoDate,
420
+ 'data-spilled': pin.spilled ? 'true' : null,
421
+ },
422
+ titleEl + inner,
423
+ );
424
+ }
425
+
426
+ function renderInlineDatePins(pins: InlineDatePin[] | undefined, color: string): string {
427
+ if (!pins || pins.length === 0) return '';
428
+ return pins.map((pin) => renderInlineDatePin(pin, color)).join('');
429
+ }
430
+
431
+ function rectFrame(
432
+ x: number,
433
+ y: number,
434
+ w: number,
435
+ h: number,
436
+ style: ResolvedStyle,
437
+ extra: Record<string, string | number | undefined | null | boolean> = {},
438
+ ): string {
439
+ const rx = Math.min(CORNER_RADIUS_PX[style.cornerRadius] ?? 4, h / 2);
440
+ return tag('rect', {
441
+ x: num(x),
442
+ y: num(y),
443
+ width: num(w),
444
+ height: num(h),
445
+ rx: num(rx),
446
+ ry: num(rx),
447
+ fill: style.bg === 'none' ? 'transparent' : style.bg,
448
+ stroke: style.fg,
449
+ 'stroke-width': 1,
450
+ 'stroke-dasharray': strokeDash(style) ?? null,
451
+ ...extra,
452
+ });
453
+ }
454
+
455
+ function renderHeader(h: PositionedHeader, idPrefix: string, palette: Theme): string {
456
+ // The layout has already sized the card to its (wrapped) text content
457
+ // and stashed the bounds in `h.cardBox`, with `h.titleLines` /
458
+ // `h.authorLines` ready to render line-by-line. See sizeBesideHeader
459
+ // in @nowline/layout.
460
+ const cardX = h.box.x + h.cardBox.x;
461
+ const cardY = h.box.y + h.cardBox.y;
462
+ const cardWidth = h.cardBox.width;
463
+ const cardHeight = h.cardBox.height;
464
+ const cardFill = h.style.bg === 'none' ? palette.surface.headerBox : h.style.bg;
465
+ const borderColor = palette.header.cardBorder;
466
+ const card = tag('rect', {
467
+ x: num(cardX),
468
+ y: num(cardY),
469
+ width: num(cardWidth),
470
+ height: num(cardHeight),
471
+ rx: 6,
472
+ ry: 6,
473
+ fill: cardFill,
474
+ stroke: borderColor,
475
+ 'stroke-width': 1,
476
+ filter: `url(#${idPrefix}-shadow-subtle)`,
477
+ });
478
+ // Title and author baselines come from `@nowline/layout`'s
479
+ // `header-card-geometry` module so the renderer paints with the
480
+ // exact metrics `sizeBesideHeader` sized the card to.
481
+ const titleParts: string[] = [];
482
+ h.titleLines.forEach((line, i) => {
483
+ titleParts.push(
484
+ textTag(
485
+ {
486
+ x: num(cardX + HEADER_CARD_PADDING_X),
487
+ y: num(cardY + HEADER_CARD_PADDING_TOP + i * HEADER_TITLE_LINE_HEIGHT_PX),
488
+ 'font-family': FONT_STACK[h.style.font],
489
+ 'font-size': HEADER_TITLE_FONT_SIZE_PX,
490
+ 'font-weight': 600,
491
+ fill: h.style.text,
492
+ },
493
+ line,
494
+ ),
495
+ );
496
+ });
497
+ const titleText = titleParts.join('');
498
+ const lastTitleY =
499
+ cardY +
500
+ HEADER_CARD_PADDING_TOP +
501
+ Math.max(0, h.titleLines.length - 1) * HEADER_TITLE_LINE_HEIGHT_PX;
502
+ const authorColor = palette.header.author;
503
+ const authorParts: string[] = [];
504
+ h.authorLines.forEach((line, j) => {
505
+ authorParts.push(
506
+ textTag(
507
+ {
508
+ x: num(cardX + HEADER_CARD_PADDING_X),
509
+ y: num(
510
+ lastTitleY +
511
+ HEADER_TITLE_TO_AUTHOR_GAP_PX +
512
+ j * HEADER_AUTHOR_LINE_HEIGHT_PX,
513
+ ),
514
+ 'font-family': FONT_STACK[h.style.font],
515
+ 'font-size': HEADER_AUTHOR_FONT_SIZE_PX,
516
+ fill: authorColor,
517
+ },
518
+ line,
519
+ ),
520
+ );
521
+ });
522
+ const authorText = authorParts.join('');
523
+ return tag(
524
+ 'g',
525
+ { 'data-layer': 'header', 'data-id': `${idPrefix}-header` },
526
+ card + titleText + authorText,
527
+ );
528
+ }
529
+
530
+ // Renders the chart-body vertical grid lines (major dotted at every
531
+ // labeled tick, plus optional faint minor lines at every tick when
532
+ // `minorGrid` is set). Emitted as its own layer drawn AFTER the
533
+ // swimlane backgrounds so the lines actually span the chart body
534
+ // instead of being occluded — without this layer the lines would only
535
+ // be visible inside the timeline header strip. Grid lines stay BEHIND
536
+ // items, edges, anchor/milestone cuts, and the now-line so item bars
537
+ // sit cleanly on top of the ruled-paper backdrop.
538
+ function renderGridLines(t: PositionedTimelineScale, swimlaneTopY: number, palette: Theme): string {
539
+ const gridColor = palette.timeline.gridLine;
540
+ const minorGridColor = palette.timeline.minorGridLine;
541
+ // Major lines thread through the FULL timeline strip — they start
542
+ // at the top of the top date-label panel (when present) and run
543
+ // all the way through the bottom date-label panel (when one is
544
+ // mirrored at the chart bottom via `timeline-position:both` or
545
+ // `timeline-position:bottom`). This ties date labels at both ends
546
+ // to their column boundaries.
547
+ //
548
+ // Minor lines stay quieter: they start at the TOP OF THE TOPMOST
549
+ // SWIMLANE (i.e. below the top date panel AND below the marker
550
+ // row, so they don't streak through anchor/milestone diamonds in
551
+ // the header) and stop ABOVE the bottom date panel.
552
+ //
553
+ // Anchor diamonds, milestone markers, and date label text are
554
+ // rendered later in the orchestrator so they sit on top of any
555
+ // crossing line.
556
+ const bottomTickPanelHeight = t.bottomTickPanelHeight ?? 0;
557
+ const hasBottomTickPanel = t.bottomTickPanelY !== undefined && bottomTickPanelHeight > 0;
558
+ const majorTopY = t.tickPanelY;
559
+ const majorBottomY = hasBottomTickPanel
560
+ ? t.bottomTickPanelY! + bottomTickPanelHeight
561
+ : t.box.y + t.box.height;
562
+ // Use the topmost swimlane's top edge directly — `markerRow.height`
563
+ // alone misses the small gap (`timelineHeightBudget` slack) between
564
+ // the marker row and the swimlane area, which would leave the minor
565
+ // lines short and floating in dead space above the swimlane.
566
+ const minorTopY = swimlaneTopY;
567
+ const minorBottomY = t.box.y + t.box.height;
568
+ const parts: string[] = [];
569
+ for (const tick of t.ticks) {
570
+ if (tick.major) {
571
+ // Solid major line at every labeled tick — the dominant
572
+ // column boundary, drawn in the stronger gridLine color.
573
+ parts.push(
574
+ tag('line', {
575
+ x1: num(tick.x),
576
+ y1: num(majorTopY),
577
+ x2: num(tick.x),
578
+ y2: num(majorBottomY),
579
+ stroke: gridColor,
580
+ 'stroke-width': 1,
581
+ }),
582
+ );
583
+ } else if (t.minorGrid) {
584
+ // Solid faint minor line at every non-major tick boundary.
585
+ // Hierarchy is established by color (lighter than the major)
586
+ // rather than by texture. Skip the very last tick since it
587
+ // has no following column — a line at the chart's right edge
588
+ // just doubles up the chart border.
589
+ if (tick.labelX === undefined) continue;
590
+ parts.push(
591
+ tag('line', {
592
+ x1: num(tick.x),
593
+ y1: num(minorTopY),
594
+ x2: num(tick.x),
595
+ y2: num(minorBottomY),
596
+ stroke: minorGridColor,
597
+ 'stroke-width': 1,
598
+ }),
599
+ );
600
+ }
601
+ }
602
+ return tag('g', { 'data-layer': 'grid' }, parts.join(''));
603
+ }
604
+
605
+ function renderTimeline(t: PositionedTimelineScale, palette: Theme): string {
606
+ const panelFill = palette.timeline.panelFill;
607
+ const borderColor = palette.timeline.border;
608
+ const labelColor = palette.timeline.labelText;
609
+ const parts: string[] = [];
610
+ // Header layout from top: now-pill row → tick-label panel → marker row.
611
+ // The pill row owns its space (no panel rect); the now-line crosses it
612
+ // visually. Marker row is omitted entirely when empty. The top tick
613
+ // panel is also omitted when the roadmap requested
614
+ // `timeline-position:bottom` (height 0). The optional bottom tick
615
+ // panel mirrors the top panel directly above the footnote area.
616
+ const tickPanelY = t.tickPanelY;
617
+ const tickPanelHeight = t.tickPanelHeight;
618
+ const hasTopTickPanel = tickPanelHeight > 0;
619
+ const hasMarkerRow = t.markerRow.height > 0;
620
+ const markerRowY = tickPanelY + tickPanelHeight;
621
+ const markerRowHeight = t.markerRow.height;
622
+ const bottomTickPanelY = t.bottomTickPanelY;
623
+ const bottomTickPanelHeight = t.bottomTickPanelHeight ?? 0;
624
+ const hasBottomTickPanel = bottomTickPanelY !== undefined && bottomTickPanelHeight > 0;
625
+
626
+ if (hasTopTickPanel) {
627
+ parts.push(
628
+ tag('rect', {
629
+ x: num(t.box.x),
630
+ y: num(tickPanelY),
631
+ width: num(t.box.width),
632
+ height: num(tickPanelHeight),
633
+ rx: 4,
634
+ ry: 4,
635
+ fill: panelFill,
636
+ stroke: borderColor,
637
+ 'stroke-width': 1,
638
+ }),
639
+ );
640
+ }
641
+ if (hasMarkerRow) {
642
+ parts.push(
643
+ tag('rect', {
644
+ x: num(t.box.x),
645
+ y: num(markerRowY),
646
+ width: num(t.box.width),
647
+ height: num(markerRowHeight),
648
+ rx: 4,
649
+ ry: 4,
650
+ fill: panelFill,
651
+ stroke: borderColor,
652
+ 'stroke-width': 1,
653
+ }),
654
+ );
655
+ }
656
+ if (hasBottomTickPanel) {
657
+ parts.push(
658
+ tag('rect', {
659
+ x: num(t.box.x),
660
+ y: num(bottomTickPanelY!),
661
+ width: num(t.box.width),
662
+ height: num(bottomTickPanelHeight),
663
+ rx: 4,
664
+ ry: 4,
665
+ fill: panelFill,
666
+ stroke: borderColor,
667
+ 'stroke-width': 1,
668
+ }),
669
+ );
670
+ }
671
+ // Header-only labels — the chart-body grid lines themselves are
672
+ // emitted by `renderGridLines` after the swimlane backgrounds so
673
+ // they actually span the chart body rather than being occluded.
674
+ for (const tick of t.ticks) {
675
+ if (!tick.major) continue;
676
+ if (!tick.label || tick.labelX === undefined) continue;
677
+ // Label sits at the COLUMN CENTER (tick.labelX), not at the
678
+ // tick boundary. The last tick has no following column → no
679
+ // label.
680
+ if (hasTopTickPanel) {
681
+ parts.push(
682
+ textTag(
683
+ {
684
+ x: num(tick.labelX),
685
+ y: num(tickPanelY + TIMELINE_TICK_LABEL_BASELINE_OFFSET_PX),
686
+ 'font-family': FONT_STACK.sans,
687
+ 'font-size': 10,
688
+ fill: labelColor,
689
+ 'text-anchor': 'middle',
690
+ },
691
+ tick.label,
692
+ ),
693
+ );
694
+ }
695
+ if (hasBottomTickPanel) {
696
+ parts.push(
697
+ textTag(
698
+ {
699
+ x: num(tick.labelX),
700
+ y: num(bottomTickPanelY! + TIMELINE_TICK_LABEL_BASELINE_OFFSET_PX),
701
+ 'font-family': FONT_STACK.sans,
702
+ 'font-size': 10,
703
+ fill: labelColor,
704
+ 'text-anchor': 'middle',
705
+ },
706
+ tick.label,
707
+ ),
708
+ );
709
+ }
710
+ }
711
+ return tag('g', { 'data-layer': 'timeline' }, parts.join(''));
712
+ }
713
+
714
+ function renderNowline(n: PositionedNowline | null, palette: Theme): string {
715
+ if (!n) return '';
716
+ const color = palette.nowline.stroke;
717
+ const labelTextColor = palette.nowline.labelText;
718
+ // Line drops from `topY` (just below the pill / top of date headers)
719
+ // through the headers into the chart, ending at `bottomY`.
720
+ const line = tag('line', {
721
+ x1: num(n.x),
722
+ y1: num(n.topY),
723
+ x2: num(n.x),
724
+ y2: num(n.bottomY),
725
+ stroke: color,
726
+ 'stroke-width': NOWLINE_STROKE_WIDTH_PX,
727
+ });
728
+ // Pill — sits above the date headers at `pillTopY`. Three modes
729
+ // (decided by layout in `buildNowline`):
730
+ // - center → rounded rect centered on the line, label `middle`
731
+ // - flag-right → squared LEFT, rounded RIGHT, line at left edge,
732
+ // label `start` past the line
733
+ // - flag-left → rounded LEFT, squared RIGHT, line at right edge,
734
+ // label `end` before the line
735
+ // The squared edge IS the line; the rounded edge points into the
736
+ // chart, so the pill always hugs the line and never overflows.
737
+ const pillBg = renderNowPillBg(n, color);
738
+ const label = renderNowPillLabel(n, labelTextColor);
739
+ return tag('g', { 'data-layer': 'nowline' }, line + pillBg + label);
740
+ }
741
+
742
+ /**
743
+ * X coordinate of the pill's squared edge in flag modes. SVG strokes
744
+ * are centered on their geometry, so a 2.25 px line at `n.x` actually
745
+ * paints from `n.x - 1.125` to `n.x + 1.125`. To make the pill's
746
+ * squared edge line up with the OUTER edge of the line stroke (so
747
+ * the line and the pill share a single continuous edge instead of
748
+ * the line peeking past the pill by half-stroke), we offset by
749
+ * `NOWLINE_STROKE_WIDTH_PX / 2` on the side the line is on.
750
+ *
751
+ * flag-right: line on the LEFT → squared edge at n.x - half-stroke
752
+ * flag-left: line on the RIGHT → squared edge at n.x + half-stroke
753
+ *
754
+ * Center mode doesn't apply — the line passes through the pill's
755
+ * vertical center, so a half-stroke offset would make things worse.
756
+ */
757
+ function squaredEdgeX(n: PositionedNowline): number {
758
+ const halfStroke = NOWLINE_STROKE_WIDTH_PX / 2;
759
+ if (n.pillMode === 'flag-right') return n.x - halfStroke;
760
+ if (n.pillMode === 'flag-left') return n.x + halfStroke;
761
+ return n.x;
762
+ }
763
+
764
+ function renderNowPillBg(n: PositionedNowline, color: string): string {
765
+ const top = n.pillTopY;
766
+ const bottom = n.pillTopY + NOW_PILL_HEIGHT_PX;
767
+ const r = NOW_PILL_CORNER_RADIUS_PX;
768
+ // Pill width comes from the layout (`n.pillWidth`) — locale-aware,
769
+ // floored at `NOW_PILL_WIDTH_PX` so en-US output stays byte-stable
770
+ // and longer locale strings (e.g. fr `'maint.'`) grow the pill
771
+ // instead of clipping. See `buildNowline` in `@nowline/layout`.
772
+ const width = n.pillWidth;
773
+ if (n.pillMode === 'center') {
774
+ return tag('rect', {
775
+ x: num(n.x - width / 2),
776
+ y: num(top),
777
+ width: num(width),
778
+ height: num(NOW_PILL_HEIGHT_PX),
779
+ rx: r,
780
+ ry: r,
781
+ fill: color,
782
+ });
783
+ }
784
+ const edgeX = squaredEdgeX(n);
785
+ if (n.pillMode === 'flag-right') {
786
+ // Squared LEFT edge aligns with line's left outer stroke edge,
787
+ // rounded corners on the RIGHT.
788
+ const right = edgeX + width;
789
+ const d = [
790
+ `M ${num(edgeX)} ${num(top)}`,
791
+ `L ${num(right - r)} ${num(top)}`,
792
+ `A ${r} ${r} 0 0 1 ${num(right)} ${num(top + r)}`,
793
+ `L ${num(right)} ${num(bottom - r)}`,
794
+ `A ${r} ${r} 0 0 1 ${num(right - r)} ${num(bottom)}`,
795
+ `L ${num(edgeX)} ${num(bottom)}`,
796
+ 'Z',
797
+ ].join(' ');
798
+ return tag('path', { d, fill: color });
799
+ }
800
+ // flag-left: squared RIGHT edge aligns with line's right outer
801
+ // stroke edge, rounded corners on the LEFT.
802
+ const left = edgeX - width;
803
+ const d = [
804
+ `M ${num(edgeX)} ${num(top)}`,
805
+ `L ${num(left + r)} ${num(top)}`,
806
+ `A ${r} ${r} 0 0 0 ${num(left)} ${num(top + r)}`,
807
+ `L ${num(left)} ${num(bottom - r)}`,
808
+ `A ${r} ${r} 0 0 0 ${num(left + r)} ${num(bottom)}`,
809
+ `L ${num(edgeX)} ${num(bottom)}`,
810
+ 'Z',
811
+ ].join(' ');
812
+ return tag('path', { d, fill: color });
813
+ }
814
+
815
+ function renderNowPillLabel(n: PositionedNowline, labelTextColor: string): string {
816
+ const baselineY = n.pillTopY + NOW_PILL_LABEL_BASELINE_OFFSET_PX;
817
+ const edgeX = squaredEdgeX(n);
818
+ let labelX: number;
819
+ let textAnchor: 'start' | 'middle' | 'end';
820
+ if (n.pillMode === 'center') {
821
+ labelX = n.x;
822
+ textAnchor = 'middle';
823
+ } else if (n.pillMode === 'flag-right') {
824
+ labelX = edgeX + NOW_PILL_LABEL_INSET_X_PX;
825
+ textAnchor = 'start';
826
+ } else {
827
+ labelX = edgeX - NOW_PILL_LABEL_INSET_X_PX;
828
+ textAnchor = 'end';
829
+ }
830
+ return textTag(
831
+ {
832
+ x: num(labelX),
833
+ y: num(baselineY),
834
+ 'font-family': FONT_STACK.sans,
835
+ 'font-size': NOW_PILL_LABEL_FONT_SIZE_PX,
836
+ 'font-weight': 700,
837
+ fill: labelTextColor,
838
+ 'text-anchor': textAnchor,
839
+ },
840
+ n.label,
841
+ );
842
+ }
843
+
844
+ function renderItem(
845
+ i: PositionedItem,
846
+ options: RenderOptions,
847
+ idPrefix: string,
848
+ palette: Theme,
849
+ ): string {
850
+ const parts: string[] = [];
851
+ const shadow = shadowFilterUrl(idPrefix, i.style.shadow);
852
+ parts.push(
853
+ rectFrame(i.box.x, i.box.y, i.box.width, i.box.height, i.style, {
854
+ filter: shadow ?? null,
855
+ }),
856
+ );
857
+ // Status-dot color — the dot communicates status via hue, but
858
+ // the bar bg can range from pale status tints (`#eff6ff`) to
859
+ // saturated mid-tones (`#1e88e5` from `bg:blue` labels) to
860
+ // dark navies (`#172554` in dark theme), so a single palette
861
+ // can't keep contrast across all bars. Two palettes — `onLight`
862
+ // (deep tints, for pale bars) and `onDark` (pale tints, for
863
+ // saturated/dark bars) — are picked from based on the bar
864
+ // bg's relative luminance.
865
+ const dotPalette = pickStatusDotPalette(i.style.bg, palette);
866
+ const statusColors: Record<string, string> = {
867
+ done: dotPalette.done,
868
+ 'in-progress': dotPalette.inProgress,
869
+ 'at-risk': dotPalette.atRisk,
870
+ blocked: dotPalette.blocked,
871
+ planned: dotPalette.planned,
872
+ neutral: dotPalette.neutral,
873
+ };
874
+ const dotColor = statusColors[i.status] ?? statusColors.neutral;
875
+ // Bottom progress strip along the bottom edge. Height comes from
876
+ // `PROGRESS_STRIP_HEIGHT_PX` so layout's chip placement and the
877
+ // milestone slack-arrow attach Y stay in sync if it's ever bumped.
878
+ if (i.progressFraction > 0) {
879
+ const pw = Math.max(0, Math.min(i.box.width, i.box.width * i.progressFraction));
880
+ parts.push(
881
+ tag('rect', {
882
+ x: num(i.box.x),
883
+ y: num(i.box.y + i.box.height - PROGRESS_STRIP_HEIGHT_PX),
884
+ width: num(pw),
885
+ height: PROGRESS_STRIP_HEIGHT_PX,
886
+ fill: i.style.fg,
887
+ opacity: 0.55,
888
+ }),
889
+ );
890
+ }
891
+ // Status dot — upper-right inset inside the bar, OR pushed into
892
+ // the spill column when the bar is too narrow to host the dot's
893
+ // full inset (`dotSpills`). Layout pre-computes `dotSpillCx` for
894
+ // the spilled case so the renderer stays geometry-dumb.
895
+ const dotCx =
896
+ i.dotSpills && i.dotSpillCx !== null
897
+ ? i.dotSpillCx
898
+ : i.box.x + i.box.width - ITEM_STATUS_DOT_INSET_RIGHT_PX;
899
+ parts.push(
900
+ tag('circle', {
901
+ cx: num(dotCx),
902
+ cy: num(i.box.y + ITEM_STATUS_DOT_INSET_TOP_PX),
903
+ r: ITEM_STATUS_DOT_RADIUS_PX,
904
+ fill: dotColor,
905
+ }),
906
+ );
907
+ // Inline-date pins (after:DATE / before:DATE). Same z-order family as
908
+ // the status dot and footnote indicators — small badge in the top
909
+ // decoration row. Color matches the bar's resolved meta (`fg`) so the
910
+ // glyph reads against the bar fill the same way the meta text does.
911
+ parts.push(renderInlineDatePins(i.inlineDatePins, i.style.fg));
912
+ // Title + meta are an atomic caption. When `textSpills` is set the
913
+ // layout has already bumped the next item to a fresh row, so we draw
914
+ // both lines BESIDE the bar (just past its right edge, stacked) at
915
+ // the same vertical positions they would occupy inside. When they
916
+ // fit, both go inside at the bar's left padding.
917
+ //
918
+ // The spilled-decoration cluster reads `[bar] [icon?] [title]
919
+ // [footnote?] [dot?]` — the only decoration to the LEFT of the
920
+ // title is the link icon (the icon→title affordance must stay
921
+ // adjacent). The dot trails the title to mirror its in-bar
922
+ // upper-right position; the footnote walks alongside the title
923
+ // (between title and dot) just like its in-bar `text-anchor: end`
924
+ // placement at the upper-right.
925
+ let captionX: number;
926
+ if (i.textSpills) {
927
+ captionX = i.box.x + i.box.width + ITEM_CAPTION_SPILL_GAP_PX;
928
+ if (i.iconSpills) {
929
+ captionX += ITEM_LINK_ICON_TILE_SIZE_PX + ITEM_DECORATION_SPILL_GAP_PX;
930
+ }
931
+ } else {
932
+ captionX = i.box.x + ITEM_CAPTION_INSET_X_PX;
933
+ }
934
+ // When the caption spills outside the bar it renders on the
935
+ // chart / group bg instead of the bar fill — `i.style.text` is
936
+ // resolved against the bar (e.g. `enterprise-style` propagates
937
+ // `text:white` from a label and audit-log's title becomes
938
+ // white-on-blue inside, but white-on-peach when spilled onto
939
+ // the orange-tinted audit-track group). Use the theme's
940
+ // default item text color (always tuned for chart bg) when
941
+ // text spills, and the per-bar color when it stays inside.
942
+ const captionInsideTextColor = i.style.text;
943
+ const captionOutsideTextColor = palette.entities.item.text;
944
+ const titleColor = i.textSpills ? captionOutsideTextColor : captionInsideTextColor;
945
+ const metaColor = i.textSpills ? captionOutsideTextColor : i.style.fg;
946
+ if (i.title) {
947
+ parts.push(
948
+ textTag(
949
+ {
950
+ x: num(captionX),
951
+ y: num(i.box.y + ITEM_CAPTION_TITLE_BASELINE_OFFSET_PX),
952
+ 'font-family': FONT_STACK[i.style.font],
953
+ 'font-size': ITEM_CAPTION_TITLE_FONT_SIZE_PX,
954
+ 'font-weight': 600,
955
+ fill: titleColor,
956
+ },
957
+ i.title,
958
+ ),
959
+ );
960
+ }
961
+ // Meta line and capacity suffix render as a unified SVG fragment.
962
+ // For pure-text suffixes (multiplier, literal glyph, no glyph) the
963
+ // unified path emits a single `<text>` element with `<tspan dx>`
964
+ // for the gap so the suffix hugs the metaText regardless of how
965
+ // wide it actually rendered. Built-in SVG icon glyphs still emit a
966
+ // sibling `<svg>` but at a tighter offset than the legacy two-text
967
+ // path. See `renderItemMetaLine` for the full case-by-case.
968
+ if (i.metaText || i.capacity) {
969
+ parts.push(
970
+ renderItemMetaLine({
971
+ metaText: i.metaText,
972
+ capacity: i.capacity,
973
+ x: captionX,
974
+ baselineY: i.box.y + ITEM_CAPTION_META_BASELINE_OFFSET_PX,
975
+ fontSize: ITEM_CAPTION_META_FONT_SIZE_PX,
976
+ fontFamily: FONT_STACK[i.style.font],
977
+ color: metaColor,
978
+ }),
979
+ );
980
+ }
981
+ // Footnote superscript indicators. Two render modes:
982
+ // - In-bar (default): glyphs walk LEFT from
983
+ // `bar.right - ITEM_FOOTNOTE_INDICATOR_INSET_RIGHT_PX`,
984
+ // anchored end. They sit on the bar fill, so use the bar's
985
+ // resolved text color for contrast (a hardcoded red was
986
+ // getting lost on saturated mid-tone bars from `bg:blue`
987
+ // labels). The "footnote = red" attention cue lives on the
988
+ // footnote PANEL's red number column at the bottom of the
989
+ // chart where red reads cleanly against white.
990
+ // - Spilled (narrow bars): the glyphs render in the spill
991
+ // column to the right of the bar, walking RIGHT from
992
+ // `footnoteSpillStartX` so they read in the same numerical
993
+ // order as the in-bar case. They sit on the chart bg, so
994
+ // use the chart-tuned default text color (same as spilled
995
+ // captions).
996
+ if (i.footnoteIndicators.length > 0) {
997
+ const footnoteY = i.box.y + ITEM_FOOTNOTE_INDICATOR_BASELINE_OFFSET_PX;
998
+ if (i.footnoteSpills && i.footnoteSpillStartX !== null) {
999
+ let fx = i.footnoteSpillStartX;
1000
+ for (let k = 0; k < i.footnoteIndicators.length; k++) {
1001
+ const n2 = i.footnoteIndicators[k];
1002
+ parts.push(
1003
+ textTag(
1004
+ {
1005
+ x: num(fx),
1006
+ y: num(footnoteY),
1007
+ 'font-family': FONT_STACK.sans,
1008
+ 'font-size': 10,
1009
+ 'font-weight': 700,
1010
+ fill: captionOutsideTextColor,
1011
+ },
1012
+ String(n2),
1013
+ ),
1014
+ );
1015
+ fx += ITEM_FOOTNOTE_INDICATOR_STEP_PX;
1016
+ }
1017
+ } else {
1018
+ let fx = i.box.x + i.box.width - ITEM_FOOTNOTE_INDICATOR_INSET_RIGHT_PX;
1019
+ for (let k = i.footnoteIndicators.length - 1; k >= 0; k--) {
1020
+ const n2 = i.footnoteIndicators[k];
1021
+ parts.push(
1022
+ textTag(
1023
+ {
1024
+ x: num(fx),
1025
+ y: num(footnoteY),
1026
+ 'font-family': FONT_STACK.sans,
1027
+ 'font-size': 10,
1028
+ 'font-weight': 700,
1029
+ fill: i.style.text,
1030
+ 'text-anchor': 'end',
1031
+ },
1032
+ String(n2),
1033
+ ),
1034
+ );
1035
+ fx -= ITEM_FOOTNOTE_INDICATOR_STEP_PX;
1036
+ }
1037
+ }
1038
+ }
1039
+ // Link icon — colored tile + white external-link glyph. Default
1040
+ // position is the bar's UPPER-LEFT corner; on a bar too narrow
1041
+ // to host both the icon and the status-dot column with a gap
1042
+ // between them, the icon spills out to the right of the bar
1043
+ // (in front of the spilled title) so the icon→title affordance
1044
+ // stays intact. The glyph is the same outbound-arrow ↗ for
1045
+ // every link kind (linear / github / jira / generic) — they
1046
+ // only differ in tile color. The include FILE-LEVEL region
1047
+ // (`include "./other.nowline"`) uses a separate stacked-sheets
1048
+ // glyph rendered by `renderIncludeRegion`, distinct from this
1049
+ // item-level link icon.
1050
+ if (!options.noLinks && i.linkIcon && i.linkIcon !== 'none') {
1051
+ const tileColor: Record<string, string> = {
1052
+ linear: '#5e6ad2',
1053
+ github: '#0f172a',
1054
+ jira: '#0052cc',
1055
+ generic: palette.item.linkIconFg,
1056
+ };
1057
+ const tile = tileColor[i.linkIcon] ?? tileColor.generic;
1058
+ const tileSize = ITEM_LINK_ICON_TILE_SIZE_PX;
1059
+ const tileX =
1060
+ i.iconSpills && i.iconSpillX !== null
1061
+ ? i.iconSpillX
1062
+ : i.box.x + ITEM_LINK_ICON_INSET_PX;
1063
+ const tileY = i.box.y + ITEM_LINK_ICON_INSET_PX;
1064
+ const tileRect = tag('rect', {
1065
+ x: num(tileX),
1066
+ y: num(tileY),
1067
+ width: tileSize,
1068
+ height: tileSize,
1069
+ rx: 2,
1070
+ ry: 2,
1071
+ fill: tile,
1072
+ });
1073
+ const gx = tileX;
1074
+ const gy = tileY;
1075
+ const glyph = tag('path', {
1076
+ d: `M${num(gx + 4)} ${num(gy + 10)} L${num(gx + 10)} ${num(gy + 4)} M${num(gx + 6)} ${num(gy + 4)} H${num(gx + 10)} V${num(gy + 8)}`,
1077
+ stroke: '#ffffff',
1078
+ fill: 'none',
1079
+ 'stroke-width': 1.1,
1080
+ 'stroke-linecap': 'round',
1081
+ 'stroke-linejoin': 'round',
1082
+ });
1083
+ const inner = tileRect + glyph;
1084
+ const link = i.linkHref
1085
+ ? tag('a', { href: i.linkHref, target: '_blank', rel: 'noopener' }, inner)
1086
+ : inner;
1087
+ parts.push(link);
1088
+ }
1089
+ // Overflow tail — red fill + stroke + caption.
1090
+ if (i.hasOverflow && i.overflowBox) {
1091
+ const tailFill = palette.item.overflowTailFill;
1092
+ const tailStroke = palette.item.overflowTailStroke;
1093
+ const captionColor = palette.item.overflowCaption;
1094
+ parts.push(
1095
+ tag('rect', {
1096
+ x: num(i.overflowBox.x),
1097
+ y: num(i.overflowBox.y),
1098
+ width: num(i.overflowBox.width),
1099
+ height: num(i.overflowBox.height),
1100
+ fill: tailFill,
1101
+ stroke: tailStroke,
1102
+ 'stroke-width': 1,
1103
+ }),
1104
+ );
1105
+ if (i.overflowAnchorId && i.overflowBox.width > 60) {
1106
+ parts.push(
1107
+ textTag(
1108
+ {
1109
+ x: num(i.overflowBox.x + i.overflowBox.width / 2),
1110
+ y: num(i.overflowBox.y + i.overflowBox.height / 2 + 3),
1111
+ 'font-family': FONT_STACK.sans,
1112
+ 'font-size': 9,
1113
+ 'font-weight': 700,
1114
+ fill: captionColor,
1115
+ 'text-anchor': 'middle',
1116
+ },
1117
+ `past ${i.overflowAnchorId}`,
1118
+ ),
1119
+ );
1120
+ }
1121
+ }
1122
+ // Label chips
1123
+ for (const chip of i.labelChips) {
1124
+ const rx = Math.min(CORNER_RADIUS_PX[chip.style.cornerRadius] ?? 8, chip.box.height / 2);
1125
+ parts.push(
1126
+ tag('rect', {
1127
+ x: num(chip.box.x),
1128
+ y: num(chip.box.y),
1129
+ width: num(chip.box.width),
1130
+ height: num(chip.box.height),
1131
+ rx: num(rx),
1132
+ ry: num(rx),
1133
+ fill: chip.style.bg === 'none' ? 'transparent' : chip.style.bg,
1134
+ stroke: chip.style.fg,
1135
+ 'stroke-width': 0.5,
1136
+ }),
1137
+ );
1138
+ parts.push(
1139
+ textTag(
1140
+ {
1141
+ x: num(chip.box.x + chip.box.width / 2),
1142
+ y: num(chip.box.y + chip.box.height / 2 + 3),
1143
+ ...fontAttrs(chip.style, TEXT_SIZE_PX.xs),
1144
+ 'text-anchor': 'middle',
1145
+ },
1146
+ chip.text,
1147
+ ),
1148
+ );
1149
+ }
1150
+ return tag('g', { 'data-layer': 'item', 'data-id': i.id ?? null }, parts.join(''));
1151
+ }
1152
+
1153
+ function renderGroup(
1154
+ g: PositionedGroup,
1155
+ options: RenderOptions,
1156
+ idPrefix: string,
1157
+ palette: Theme,
1158
+ ): string {
1159
+ const parts: string[] = [];
1160
+ const hasFill = g.style.bg !== 'none' && g.style.bg !== '#ffffff';
1161
+ if (hasFill) {
1162
+ // Filled-box style with a chiclet label flush in the upper-left
1163
+ // corner. The painted box matches the layout-reported `box` 1:1
1164
+ // (no overhang), so parents stack against the right rectangle.
1165
+ parts.push(
1166
+ tag('rect', {
1167
+ x: num(g.box.x),
1168
+ y: num(g.box.y),
1169
+ width: num(g.box.width),
1170
+ height: num(g.box.height),
1171
+ rx: 6,
1172
+ ry: 6,
1173
+ fill: g.style.bg,
1174
+ stroke: g.style.fg,
1175
+ 'stroke-width': 1,
1176
+ 'fill-opacity': 0.18,
1177
+ filter: `url(#${idPrefix}-shadow-subtle)`,
1178
+ }),
1179
+ );
1180
+ if (g.title) {
1181
+ const tabW =
1182
+ g.title.length * GROUP_TITLE_TAB_CHAR_WIDTH_PX + 2 * GROUP_TITLE_TAB_PAD_X_PX;
1183
+ const tabX = g.box.x;
1184
+ const tabY = g.box.y;
1185
+ const tabH = GROUP_TITLE_TAB_HEIGHT_PX;
1186
+ // Asymmetric corner shape: TOP-LEFT and BOTTOM-RIGHT are
1187
+ // rounded (radius 6, matching the parent group box), while
1188
+ // TOP-RIGHT and BOTTOM-LEFT are square. The TL roundness
1189
+ // continues the group box's outer corner; the squared
1190
+ // BL / TR sides "anchor" the tab into the box's left and
1191
+ // top edges so it reads as a corner-mounted label rather
1192
+ // than a floating pill.
1193
+ const r = 6;
1194
+ const tabPath =
1195
+ `M${num(tabX + r)} ${num(tabY)}` +
1196
+ `H${num(tabX + tabW)}` +
1197
+ `V${num(tabY + tabH - r)}` +
1198
+ `A${r} ${r} 0 0 1 ${num(tabX + tabW - r)} ${num(tabY + tabH)}` +
1199
+ `H${num(tabX)}` +
1200
+ `V${num(tabY + r)}` +
1201
+ `A${r} ${r} 0 0 1 ${num(tabX + r)} ${num(tabY)}` +
1202
+ `Z`;
1203
+ parts.push(
1204
+ tag('path', {
1205
+ d: tabPath,
1206
+ fill: g.style.fg,
1207
+ }),
1208
+ );
1209
+ parts.push(
1210
+ textTag(
1211
+ {
1212
+ x: num(tabX + GROUP_TITLE_TAB_PAD_X_PX),
1213
+ y: num(tabY + GROUP_TITLE_TAB_LABEL_BASELINE_OFFSET_PX),
1214
+ 'font-family': FONT_STACK[g.style.font],
1215
+ 'font-size': GROUP_TITLE_TAB_LABEL_FONT_SIZE_PX,
1216
+ 'font-weight': 600,
1217
+ fill: '#ffffff',
1218
+ },
1219
+ g.title,
1220
+ ),
1221
+ );
1222
+ }
1223
+ } else {
1224
+ const bracketColor = g.style.fg;
1225
+ if (g.style.bracket !== 'none') {
1226
+ // Bracket-style groups paint a left-side `[` glyph along
1227
+ // `box.x`. When a title is present the layout has reserved
1228
+ // `GROUP_BRACKET_LABEL_OVERHANG_PX` of vertical space ABOVE
1229
+ // `box.y` (see GroupNode.place); the bracket extends up
1230
+ // through that overhang and adds a top foot mirroring the
1231
+ // bottom foot so the `[` visually wraps the title text that
1232
+ // sits in the reserved region. Title-less bracket groups
1233
+ // keep the historical asymmetric shape (vertical bar + a
1234
+ // single bottom foot) since there's nothing above to wrap.
1235
+ const stub = 4;
1236
+ const bottom = g.box.y + g.box.height;
1237
+ const dash = g.style.bracket === 'dashed' ? '3 2' : null;
1238
+ const bracketPath = g.title
1239
+ ? `M${num(g.box.x + stub)} ${num(g.box.y - GROUP_BRACKET_LABEL_OVERHANG_PX)}` +
1240
+ ` L${num(g.box.x)} ${num(g.box.y - GROUP_BRACKET_LABEL_OVERHANG_PX)}` +
1241
+ ` L${num(g.box.x)} ${num(bottom)}` +
1242
+ ` L${num(g.box.x + stub)} ${num(bottom)}`
1243
+ : `M${num(g.box.x)} ${num(g.box.y)}` +
1244
+ ` L${num(g.box.x)} ${num(bottom)}` +
1245
+ ` L${num(g.box.x + stub)} ${num(bottom)}`;
1246
+ parts.push(
1247
+ tag('path', {
1248
+ d: bracketPath,
1249
+ fill: 'none',
1250
+ stroke: bracketColor,
1251
+ 'stroke-width': 1,
1252
+ 'stroke-dasharray': dash,
1253
+ }),
1254
+ );
1255
+ }
1256
+ if (g.title) {
1257
+ parts.push(
1258
+ textTag(
1259
+ {
1260
+ x: num(g.box.x + 6),
1261
+ y: num(g.box.y - 2),
1262
+ ...fontAttrs(g.style, TEXT_SIZE_PX.xs),
1263
+ 'fill-opacity': 0.7,
1264
+ },
1265
+ g.title,
1266
+ ),
1267
+ );
1268
+ }
1269
+ }
1270
+ // Inline-date pins on the group itself (`group ... after:DATE` /
1271
+ // `before:DATE`). Painted before the children so children's bars stay
1272
+ // visually on top — the glyphs only sit in the empty corners of the
1273
+ // bounding box.
1274
+ parts.push(renderInlineDatePins(g.inlineDatePins, g.style.fg));
1275
+ for (const c of g.children) {
1276
+ parts.push(renderTrackChild(c, options, idPrefix, palette));
1277
+ }
1278
+ void palette;
1279
+ return tag('g', { 'data-layer': 'group', 'data-id': g.id ?? null }, parts.join(''));
1280
+ }
1281
+
1282
+ function renderParallel(
1283
+ p: PositionedParallel,
1284
+ options: RenderOptions,
1285
+ idPrefix: string,
1286
+ palette: Theme,
1287
+ ): string {
1288
+ const parts: string[] = [];
1289
+ // `bracket: solid|dashed` parallels render explicit [ ] brackets framing
1290
+ // the nested tracks with 12 px vertical padding above/below.
1291
+ if (p.style.bracket === 'solid' || p.style.bracket === 'dashed') {
1292
+ const padding = 12;
1293
+ const stub = 4;
1294
+ const top = p.box.y - padding;
1295
+ const bottom = p.box.y + p.box.height + padding;
1296
+ const lx = p.box.x;
1297
+ const rx = p.box.x + p.box.width;
1298
+ const stroke = palette.parallel.bracketStroke;
1299
+ parts.push(
1300
+ tag('path', {
1301
+ d: `M${num(lx + stub)} ${num(top)} H${num(lx)} V${num(bottom)} H${num(lx + stub)}`,
1302
+ fill: 'none',
1303
+ stroke,
1304
+ 'stroke-width': 1.25,
1305
+ 'stroke-dasharray': p.style.bracket === 'dashed' ? '3 3' : null,
1306
+ 'stroke-linejoin': 'round',
1307
+ }),
1308
+ );
1309
+ parts.push(
1310
+ tag('path', {
1311
+ d: `M${num(rx - stub)} ${num(top)} H${num(rx)} V${num(bottom)} H${num(rx - stub)}`,
1312
+ fill: 'none',
1313
+ stroke,
1314
+ 'stroke-width': 1.25,
1315
+ 'stroke-dasharray': p.style.bracket === 'dashed' ? '3 3' : null,
1316
+ 'stroke-linejoin': 'round',
1317
+ }),
1318
+ );
1319
+ }
1320
+ if (p.title) {
1321
+ parts.push(
1322
+ textTag(
1323
+ {
1324
+ x: num(p.box.x + 4),
1325
+ y: num(p.box.y - 2),
1326
+ ...fontAttrs(p.style, TEXT_SIZE_PX.xs),
1327
+ 'fill-opacity': 0.7,
1328
+ },
1329
+ p.title,
1330
+ ),
1331
+ );
1332
+ }
1333
+ // Inline-date pins on the parallel itself (`parallel ... after:DATE` /
1334
+ // `before:DATE`). Painted before children so child bars sit on top.
1335
+ parts.push(renderInlineDatePins(p.inlineDatePins, p.style.fg));
1336
+ for (const c of p.children) {
1337
+ parts.push(renderTrackChild(c, options, idPrefix, palette));
1338
+ }
1339
+ return tag('g', { 'data-layer': 'parallel', 'data-id': p.id ?? null }, parts.join(''));
1340
+ }
1341
+
1342
+ function renderTrackChild(
1343
+ c: PositionedTrackChild,
1344
+ options: RenderOptions,
1345
+ idPrefix: string,
1346
+ palette: Theme,
1347
+ ): string {
1348
+ if (c.kind === 'item') return renderItem(c, options, idPrefix, palette);
1349
+ if (c.kind === 'group') return renderGroup(c, options, idPrefix, palette);
1350
+ return renderParallel(c, options, idPrefix, palette);
1351
+ }
1352
+
1353
+ // Renders only the swimlane's background tint rect. Emitted before the
1354
+ // chart-body grid lines so those lines visibly span the full chart width.
1355
+ // The frame tab (chiclet at top-left) and item content are emitted later,
1356
+ // in renderSwimlaneContent, so they appear on top of the grid.
1357
+ // Tri-state lane utilization underline. Painted along the bottom edge of
1358
+ // the band when the lane has `capacity:` AND at least one item contributing
1359
+ // load AND has not opted out of every color band via `utilization-*-at:none`.
1360
+ // Geometry per specs/rendering.md § Lane utilization underline:
1361
+ // - height: 2px (matches the milestone-line stroke weight)
1362
+ // - y: flush with the bottom edge of the band, fully inside it
1363
+ // - x: aligned to the segment boundaries the layout already pre-coalesced
1364
+ // One <rect> per coalesced segment; classification → palette token mapping
1365
+ // is the only renderer-side decision.
1366
+ const LANE_UTILIZATION_HEIGHT_PX = 2;
1367
+
1368
+ function utilizationColor(classification: 'green' | 'yellow' | 'red', palette: Theme): string {
1369
+ switch (classification) {
1370
+ case 'green':
1371
+ return palette.swimlane.utilizationOk;
1372
+ case 'yellow':
1373
+ return palette.swimlane.utilizationWarn;
1374
+ case 'red':
1375
+ return palette.swimlane.utilizationOver;
1376
+ }
1377
+ }
1378
+
1379
+ function renderLaneUtilization(s: PositionedSwimlane, palette: Theme): string {
1380
+ if (!s.utilization || s.utilization.segments.length === 0) return '';
1381
+ const y = s.box.y + s.box.height - LANE_UTILIZATION_HEIGHT_PX;
1382
+ const rects = s.utilization.segments.map((seg) => {
1383
+ const width = seg.endX - seg.startX;
1384
+ if (width <= 0) return '';
1385
+ return tag('rect', {
1386
+ x: num(seg.startX),
1387
+ y: num(y),
1388
+ width: num(width),
1389
+ height: LANE_UTILIZATION_HEIGHT_PX,
1390
+ fill: utilizationColor(seg.classification, palette),
1391
+ 'data-utilization': seg.classification,
1392
+ 'data-load': num(seg.load),
1393
+ });
1394
+ });
1395
+ return tag(
1396
+ 'g',
1397
+ {
1398
+ 'data-layer': 'lane-utilization',
1399
+ 'data-id': s.id ?? null,
1400
+ },
1401
+ rects.join(''),
1402
+ );
1403
+ }
1404
+
1405
+ function renderSwimlaneBg(s: PositionedSwimlane, palette: Theme): string {
1406
+ const tint = s.bandIndex % 2 === 0 ? palette.swimlane.rowTintEven : palette.swimlane.rowTintOdd;
1407
+ const borderColor = palette.swimlane.border;
1408
+ return tag(
1409
+ 'g',
1410
+ { 'data-layer': 'swimlane-bg', 'data-id': s.id ?? null },
1411
+ tag('rect', {
1412
+ x: num(s.box.x),
1413
+ y: num(s.box.y),
1414
+ width: num(s.box.width),
1415
+ height: num(s.box.height),
1416
+ fill: tint,
1417
+ stroke: borderColor,
1418
+ 'stroke-width': 1,
1419
+ }),
1420
+ );
1421
+ }
1422
+
1423
+ function renderSwimlane(
1424
+ s: PositionedSwimlane,
1425
+ options: RenderOptions,
1426
+ idPrefix: string,
1427
+ palette: Theme,
1428
+ ): string {
1429
+ const tabFill = palette.swimlane.tabFill;
1430
+ const tabStroke = palette.swimlane.tabStroke;
1431
+ const tabText = palette.swimlane.tabText;
1432
+ const ownerText = palette.swimlane.ownerText;
1433
+ const footnoteColor = palette.swimlane.footnoteIndicator;
1434
+ const parts: string[] = [];
1435
+ // Frame-tab chiclet at the top-left of the band — auto-sized to fit
1436
+ // title + owner. Geometry comes from the shared `frameTabGeometry`
1437
+ // helper that the layout's row-packer also calls, so the chiclet's
1438
+ // visible footprint matches the collision box layout reserved for it.
1439
+ if (s.title) {
1440
+ // Lane capacity badge + footnote indicator sit inside the frame
1441
+ // tab after the owner (or after the title if no owner). Compute
1442
+ // both widths up-front so `frameTabGeometry` can size the chiclet
1443
+ // to fit them, then read the placement positions back out — no
1444
+ // second placement pass in the renderer.
1445
+ const LANE_BADGE_FONT_SIZE_PX = 10;
1446
+ const capacityBadgeBareWidthPx = s.capacity
1447
+ ? estimateCapacitySuffixWidth(s.capacity.text, s.capacity.icon, LANE_BADGE_FONT_SIZE_PX)
1448
+ : 0;
1449
+ // Footnote indicator is a comma-joined number list painted at
1450
+ // 10 pt 700-weight; estimate via the shared caption helper.
1451
+ const footnoteIndicatorText =
1452
+ s.footnoteIndicators.length > 0 ? s.footnoteIndicators.join(',') : '';
1453
+ const footnoteIndicatorWidthPx = footnoteIndicatorText
1454
+ ? estimateCaptionWidthPx(footnoteIndicatorText, LANE_BADGE_FONT_SIZE_PX)
1455
+ : 0;
1456
+ const tab = frameTabGeometry(
1457
+ s.box.x,
1458
+ s.title,
1459
+ s.owner,
1460
+ capacityBadgeBareWidthPx,
1461
+ footnoteIndicatorWidthPx,
1462
+ );
1463
+ const tabH = FRAME_TAB_HEIGHT_PX;
1464
+ const tabY = s.box.y + 10;
1465
+ const labelY = tabY + FRAME_TAB_LABEL_BASELINE_OFFSET_PX;
1466
+ parts.push(
1467
+ tag('rect', {
1468
+ x: num(tab.tabX),
1469
+ y: num(tabY),
1470
+ width: num(tab.tabW),
1471
+ height: num(tabH),
1472
+ rx: 4,
1473
+ ry: 4,
1474
+ fill: tabFill,
1475
+ stroke: tabStroke,
1476
+ 'stroke-width': 1,
1477
+ }),
1478
+ );
1479
+ parts.push(
1480
+ textTag(
1481
+ {
1482
+ x: num(tab.titleX),
1483
+ y: num(labelY),
1484
+ 'font-family': FONT_STACK[s.style.font],
1485
+ 'font-size': 12,
1486
+ 'font-weight': 600,
1487
+ fill: tabText,
1488
+ },
1489
+ s.title,
1490
+ ),
1491
+ );
1492
+ if (s.owner) {
1493
+ parts.push(
1494
+ textTag(
1495
+ {
1496
+ x: num(tab.ownerX),
1497
+ y: num(labelY),
1498
+ 'font-family': FONT_STACK[s.style.font],
1499
+ 'font-size': 10,
1500
+ fill: ownerText,
1501
+ },
1502
+ `owner: ${s.owner}`,
1503
+ ),
1504
+ );
1505
+ }
1506
+ if (s.capacity) {
1507
+ // Re-uses the same `renderCapacitySuffix` helper that paints
1508
+ // item-level suffixes (m6) so multiplier / built-in SVG /
1509
+ // inline literal / dereferenced-custom-glyph paths stay
1510
+ // consistent across both contexts.
1511
+ parts.push(
1512
+ renderCapacitySuffix(
1513
+ s.capacity,
1514
+ undefined,
1515
+ tab.badgeX,
1516
+ labelY,
1517
+ LANE_BADGE_FONT_SIZE_PX,
1518
+ FONT_STACK[s.style.font],
1519
+ ownerText,
1520
+ ),
1521
+ );
1522
+ }
1523
+ if (footnoteIndicatorText) {
1524
+ parts.push(
1525
+ textTag(
1526
+ {
1527
+ x: num(tab.footnoteRightX),
1528
+ y: num(tabY + 14),
1529
+ 'font-family': FONT_STACK.sans,
1530
+ 'font-size': LANE_BADGE_FONT_SIZE_PX,
1531
+ 'font-weight': 700,
1532
+ fill: footnoteColor,
1533
+ 'text-anchor': 'end',
1534
+ },
1535
+ footnoteIndicatorText,
1536
+ ),
1537
+ );
1538
+ }
1539
+ }
1540
+ for (const c of s.children) {
1541
+ parts.push(renderTrackChild(c, options, idPrefix, palette));
1542
+ }
1543
+ // m13: tri-state utilization underline along the band's bottom edge.
1544
+ // Painted after items so it overlays any item that happens to extend
1545
+ // to the band's bottom; under cut-lines / now-line which run as
1546
+ // separate top-level passes.
1547
+ parts.push(renderLaneUtilization(s, palette));
1548
+ return tag('g', { 'data-layer': 'swimlane', 'data-id': s.id ?? null }, parts.join(''));
1549
+ }
1550
+
1551
+ function renderAnchor(a: PositionedAnchor, palette: Theme): string {
1552
+ const size = a.radius;
1553
+ const cx = a.center.x;
1554
+ const cy = a.center.y;
1555
+ const fill = palette.anchorDiamond.fill;
1556
+ const stroke = palette.anchorDiamond.stroke;
1557
+ const diamond = tag('path', {
1558
+ d: `M${num(cx)} ${num(cy - size)} L${num(cx + size)} ${num(cy)} L${num(cx)} ${num(cy + size)} L${num(cx - size)} ${num(cy)} Z`,
1559
+ fill,
1560
+ stroke,
1561
+ 'stroke-width': 1.25,
1562
+ });
1563
+ const labelColor = palette.anchorDiamond.label;
1564
+ // For left-flipped labels, anchor the text at its RIGHT edge using
1565
+ // `text-anchor: end`. The layout's `labelBox.width` is intentionally
1566
+ // pessimistic (0.58 em/char) so positioning by the box's left edge
1567
+ // would leave a visible gap between the actual text right edge and
1568
+ // the diamond. End-anchoring lets the browser size the glyph run
1569
+ // exactly and put the rightmost glyph 6 px from the diamond — same
1570
+ // rhythm the right-side labels already get from start-anchoring at
1571
+ // `diamondRight + 6`.
1572
+ const labelX = a.labelSide === 'left' ? a.labelBox.x + a.labelBox.width : a.labelBox.x;
1573
+ const labelAttrs: Record<string, string | number | null | undefined> = {
1574
+ x: num(labelX),
1575
+ y: num(cy + 4),
1576
+ 'font-family': FONT_STACK.sans,
1577
+ 'font-size': 10,
1578
+ fill: labelColor,
1579
+ };
1580
+ if (a.labelSide === 'left') labelAttrs['text-anchor'] = 'end';
1581
+ const label = a.title ? textTag(labelAttrs, a.title) : '';
1582
+ return tag('g', { 'data-layer': 'anchor', 'data-id': a.id ?? null }, diamond + label);
1583
+ }
1584
+
1585
+ function renderAnchorCutLine(a: PositionedAnchor, palette: Theme): string {
1586
+ const stroke = palette.anchorDiamond.cutLine;
1587
+ return tag('line', {
1588
+ x1: num(a.center.x),
1589
+ y1: num(a.center.y + a.radius + 1),
1590
+ x2: num(a.center.x),
1591
+ y2: num(a.cutBottomY),
1592
+ stroke,
1593
+ 'stroke-width': 1,
1594
+ 'stroke-dasharray': '1 3',
1595
+ });
1596
+ }
1597
+
1598
+ function renderMilestone(m: PositionedMilestone, palette: Theme): string {
1599
+ const cx = m.center.x;
1600
+ const cy = m.center.y;
1601
+ const r = m.radius;
1602
+ const fill = palette.milestoneDiamond.fill;
1603
+ const flag = tag('path', {
1604
+ d: `M${num(cx)} ${num(cy - r)} L${num(cx + r)} ${num(cy)} L${num(cx)} ${num(cy + r)} L${num(cx - r)} ${num(cy)} Z`,
1605
+ fill,
1606
+ stroke: fill,
1607
+ 'stroke-width': 1,
1608
+ });
1609
+ const labelColor = palette.milestoneDiamond.label;
1610
+ // See renderAnchor — left-flipped labels use `text-anchor: end` so
1611
+ // the visual right edge sits at `diamondLeft - 6`, matching the
1612
+ // 6 px rhythm of right-side labels.
1613
+ const labelX = m.labelSide === 'left' ? m.labelBox.x + m.labelBox.width : m.labelBox.x;
1614
+ const labelAttrs: Record<string, string | number | null | undefined> = {
1615
+ x: num(labelX),
1616
+ y: num(cy + 4),
1617
+ 'font-family': FONT_STACK.sans,
1618
+ 'font-size': 10,
1619
+ 'font-weight': 600,
1620
+ fill: labelColor,
1621
+ };
1622
+ if (m.labelSide === 'left') labelAttrs['text-anchor'] = 'end';
1623
+ const label = m.title ? textTag(labelAttrs, m.title) : '';
1624
+ return tag('g', { 'data-layer': 'milestone', 'data-id': m.id ?? null }, flag + label);
1625
+ }
1626
+
1627
+ function renderMilestoneCutLine(m: PositionedMilestone, palette: Theme): string {
1628
+ const stroke = m.isOverrun
1629
+ ? palette.milestoneDiamond.cutLineOverrun
1630
+ : palette.milestoneDiamond.cutLineNormal;
1631
+ const parts: string[] = [];
1632
+ parts.push(
1633
+ tag('line', {
1634
+ x1: num(m.center.x),
1635
+ y1: num(m.center.y + m.radius + 1),
1636
+ x2: num(m.center.x),
1637
+ y2: num(m.cutBottomY),
1638
+ stroke,
1639
+ 'stroke-width': 2,
1640
+ 'stroke-dasharray': ACCENT_DASH_PATTERN,
1641
+ 'stroke-linecap': 'round',
1642
+ }),
1643
+ );
1644
+ if (m.slackArrows && m.slackArrows.length > 0) {
1645
+ const slackColor = palette.milestoneDiamond.slack;
1646
+ for (const arrow of m.slackArrows) {
1647
+ parts.push(
1648
+ tag('path', {
1649
+ d: `M${num(arrow.x)} ${num(arrow.y)} H${num(m.center.x - 6)}`,
1650
+ fill: 'none',
1651
+ stroke: slackColor,
1652
+ 'stroke-width': 1.1,
1653
+ 'stroke-dasharray': '3 3',
1654
+ 'stroke-linecap': 'round',
1655
+ 'marker-end': 'url(#nl-arrow-dark)',
1656
+ }),
1657
+ );
1658
+ }
1659
+ }
1660
+ return parts.join('');
1661
+ }
1662
+
1663
+ function renderEdge(e: PositionedDependencyEdge, palette: Theme): string {
1664
+ const color =
1665
+ e.kind === 'overflow' ? palette.dependency.overflowStroke : palette.dependency.edgeStroke;
1666
+ const points = e.waypoints;
1667
+ if (points.length < 2) return '';
1668
+ // Under-bar edges paint BEFORE item fills (see `renderRoadmap`)
1669
+ // and use a thinner stroke so the bar stays foreground. Normal /
1670
+ // overflow edges sit on top of items and use the standard 1.1 px
1671
+ // stroke.
1672
+ const strokeWidth = e.kind === 'underBar' ? 0.8 : 1.1;
1673
+ return tag('path', {
1674
+ d: roundedOrthogonalPath(points, EDGE_CORNER_RADIUS),
1675
+ fill: 'none',
1676
+ stroke: color,
1677
+ 'stroke-width': strokeWidth,
1678
+ 'stroke-dasharray': e.kind === 'overflow' ? '4 2' : null,
1679
+ 'stroke-linejoin': 'round',
1680
+ 'marker-end': 'url(#nl-arrow)',
1681
+ });
1682
+ }
1683
+
1684
+ // Build an SVG path for a sequence of orthogonal waypoints, inserting a
1685
+ // quarter-arc at every interior bend. Falls back to straight segments when
1686
+ // adjacent points aren't axis-aligned (defensive — the layout always emits
1687
+ // orthogonal segments).
1688
+ function roundedOrthogonalPath(points: Point[], radius: number): string {
1689
+ if (points.length < 2) return '';
1690
+ const parts: string[] = [`M${num(points[0].x)} ${num(points[0].y)}`];
1691
+ for (let i = 1; i < points.length; i++) {
1692
+ const prev = points[i - 1];
1693
+ const cur = points[i];
1694
+ if (i === points.length - 1) {
1695
+ parts.push(`L${num(cur.x)} ${num(cur.y)}`);
1696
+ continue;
1697
+ }
1698
+ const next = points[i + 1];
1699
+ const dxIn = Math.sign(cur.x - prev.x);
1700
+ const dyIn = Math.sign(cur.y - prev.y);
1701
+ const dxOut = Math.sign(next.x - cur.x);
1702
+ const dyOut = Math.sign(next.y - cur.y);
1703
+ const inLen = Math.hypot(cur.x - prev.x, cur.y - prev.y);
1704
+ const outLen = Math.hypot(next.x - cur.x, next.y - cur.y);
1705
+ const r = Math.min(radius, inLen / 2, outLen / 2);
1706
+ if (r <= 0 || (dxIn !== 0 && dxOut !== 0) || (dyIn !== 0 && dyOut !== 0)) {
1707
+ parts.push(`L${num(cur.x)} ${num(cur.y)}`);
1708
+ continue;
1709
+ }
1710
+ const beforeBend = { x: cur.x - dxIn * r, y: cur.y - dyIn * r };
1711
+ const afterBend = { x: cur.x + dxOut * r, y: cur.y + dyOut * r };
1712
+ parts.push(`L${num(beforeBend.x)} ${num(beforeBend.y)}`);
1713
+ parts.push(`Q${num(cur.x)} ${num(cur.y)} ${num(afterBend.x)} ${num(afterBend.y)}`);
1714
+ }
1715
+ return parts.join(' ');
1716
+ }
1717
+
1718
+ function renderFootnotes(f: PositionedFootnoteArea, idPrefix: string, palette: Theme): string {
1719
+ if (f.entries.length === 0) return '';
1720
+ const panelFill = palette.footnotePanel.fill;
1721
+ const borderColor = palette.footnotePanel.border;
1722
+ const headerColor = palette.footnotePanel.header;
1723
+ const titleColor = palette.footnotePanel.title;
1724
+ const descColor = palette.footnotePanel.description;
1725
+ const numberColor = palette.footnotePanel.number;
1726
+ const parts: string[] = [];
1727
+ parts.push(
1728
+ tag('rect', {
1729
+ x: num(f.box.x),
1730
+ y: num(f.box.y),
1731
+ width: num(f.box.width),
1732
+ height: num(f.box.height),
1733
+ rx: 6,
1734
+ ry: 6,
1735
+ fill: panelFill,
1736
+ stroke: borderColor,
1737
+ 'stroke-width': 1,
1738
+ filter: `url(#${idPrefix}-shadow-subtle)`,
1739
+ }),
1740
+ );
1741
+ parts.push(
1742
+ textTag(
1743
+ {
1744
+ x: num(f.box.x + FOOTNOTE_PANEL_PADDING_PX),
1745
+ y: num(f.box.y + FOOTNOTE_HEADER_BASELINE_OFFSET_PX),
1746
+ 'font-family': FONT_STACK.sans,
1747
+ 'font-size': 12,
1748
+ 'font-weight': 700,
1749
+ fill: headerColor,
1750
+ },
1751
+ 'Footnotes',
1752
+ ),
1753
+ );
1754
+ // First entry baseline = panel-top + header band + one panel padding
1755
+ // (the gap between the header band and the first row).
1756
+ const firstEntryBaselineY = f.box.y + FOOTNOTE_HEADER_HEIGHT_PX + FOOTNOTE_PANEL_PADDING_PX;
1757
+ const numberX = f.box.x + FOOTNOTE_PANEL_PADDING_PX;
1758
+ const titleX = numberX + FOOTNOTE_PANEL_PADDING_PX;
1759
+ f.entries.forEach((e, i) => {
1760
+ const y = firstEntryBaselineY + i * FOOTNOTE_ROW_HEIGHT;
1761
+ parts.push(
1762
+ textTag(
1763
+ {
1764
+ x: num(numberX),
1765
+ y: num(y),
1766
+ 'font-family': FONT_STACK.sans,
1767
+ 'font-size': 10,
1768
+ 'font-weight': 700,
1769
+ fill: numberColor,
1770
+ },
1771
+ String(e.number),
1772
+ ),
1773
+ );
1774
+ parts.push(
1775
+ textTag(
1776
+ {
1777
+ x: num(titleX),
1778
+ y: num(y),
1779
+ 'font-family': FONT_STACK.sans,
1780
+ 'font-size': 11,
1781
+ 'font-weight': 600,
1782
+ fill: titleColor,
1783
+ },
1784
+ e.title,
1785
+ ),
1786
+ );
1787
+ if (e.description) {
1788
+ parts.push(
1789
+ textTag(
1790
+ {
1791
+ x: num(titleX + Math.max(120, e.title.length * 6)),
1792
+ y: num(y),
1793
+ 'font-family': FONT_STACK.sans,
1794
+ 'font-size': 11,
1795
+ fill: descColor,
1796
+ },
1797
+ `— ${e.description}`,
1798
+ ),
1799
+ );
1800
+ }
1801
+ });
1802
+ return tag('g', { 'data-layer': 'footnotes' }, parts.join(''));
1803
+ }
1804
+
1805
+ function renderIncludeRegion(
1806
+ r: PositionedIncludeRegion,
1807
+ options: RenderOptions,
1808
+ idPrefix: string,
1809
+ palette: Theme,
1810
+ ): string {
1811
+ const border = palette.includeRegion.border;
1812
+ const fill = palette.includeRegion.fill;
1813
+ const tabFill = palette.includeRegion.tabFill;
1814
+ const tabStroke = palette.includeRegion.tabStroke;
1815
+ const tabText = palette.includeRegion.tabText;
1816
+ const badgeFill = palette.includeRegion.badgeFill;
1817
+ const badgeStroke = palette.includeRegion.badgeStroke;
1818
+ const badgeText = palette.includeRegion.badgeText;
1819
+
1820
+ const rx = r.box.x + 8;
1821
+ const ry = r.box.y;
1822
+ const rw = r.box.width - 16;
1823
+ const rh = r.box.height;
1824
+
1825
+ const region = tag('rect', {
1826
+ x: num(rx),
1827
+ y: num(ry),
1828
+ width: num(rw),
1829
+ height: num(rh),
1830
+ rx: 8,
1831
+ ry: 8,
1832
+ fill,
1833
+ stroke: border,
1834
+ 'stroke-width': 1,
1835
+ 'stroke-dasharray': ACCENT_DASH_PATTERN,
1836
+ });
1837
+
1838
+ // Chrome geometry — single source of truth shared with the layout
1839
+ // (`buildIncludeRegions` calls the same helper to size the dashed
1840
+ // bracket so the chrome always fits inside it). All placement Xs
1841
+ // come from the helper directly so the renderer stays declarative.
1842
+ const tabHeight = FRAME_TAB_HEIGHT_PX;
1843
+ const chrome = includeChromeGeometry(r.box.x, r.label, r.sourcePath);
1844
+ const tabY = ry - tabHeight / 2;
1845
+ const tab = tag('rect', {
1846
+ x: num(chrome.tabX),
1847
+ y: num(tabY),
1848
+ width: num(chrome.tabWidth),
1849
+ height: tabHeight,
1850
+ rx: 4,
1851
+ ry: 4,
1852
+ fill: tabFill,
1853
+ stroke: tabStroke,
1854
+ 'stroke-width': 1,
1855
+ });
1856
+ const tabLabel = textTag(
1857
+ {
1858
+ x: num(chrome.tabLabelX),
1859
+ y: num(tabY + FRAME_TAB_LABEL_BASELINE_OFFSET_PX),
1860
+ 'font-family': FONT_STACK.sans,
1861
+ 'font-size': 11,
1862
+ 'font-weight': 600,
1863
+ fill: tabText,
1864
+ },
1865
+ r.label,
1866
+ );
1867
+
1868
+ // Content badge to the right of the tab. The glyph here is the
1869
+ // stacked-sheets icon, distinct from the item-level link-icon
1870
+ // outbound-arrow: an `include` is a content pull (one document
1871
+ // brings in another), conceptually different from a `link:` that
1872
+ // navigates somewhere.
1873
+ const badgeX = chrome.badgeX;
1874
+ const badgeSize = chrome.badgeSize;
1875
+ const badgeY = ry - badgeSize / 2;
1876
+ const badge = tag('rect', {
1877
+ x: num(badgeX),
1878
+ y: num(badgeY),
1879
+ width: badgeSize,
1880
+ height: badgeSize,
1881
+ rx: 4,
1882
+ ry: 4,
1883
+ fill: badgeFill,
1884
+ stroke: badgeStroke,
1885
+ 'stroke-width': 1,
1886
+ });
1887
+ // Glyph: stacked sheets — back rectangle peeking behind front
1888
+ // rectangle. Sized for the 18×18 badge tile.
1889
+ const glyph = tag('path', {
1890
+ d:
1891
+ `M${num(badgeX + 7)} ${num(badgeY + 4)} H${num(badgeX + 14)} V${num(badgeY + 11)}` +
1892
+ ` M${num(badgeX + 4)} ${num(badgeY + 7)} H${num(badgeX + 11)} V${num(badgeY + 14)} H${num(badgeX + 4)} Z`,
1893
+ stroke: badgeText,
1894
+ 'stroke-width': 1.4,
1895
+ fill: 'none',
1896
+ 'stroke-linecap': 'round',
1897
+ 'stroke-linejoin': 'round',
1898
+ });
1899
+ // Halo behind the source-path text. The text's baseline at `ry + 4`
1900
+ // straddles the dashed region border (drawn at y = ry); without a
1901
+ // backing rect the dashed stroke cuts through the text body. Matches
1902
+ // how the tab and badge already mask the border where they cross it.
1903
+ // Fill is `includeRegion.fill` — same cream/tint as the region, near
1904
+ // the canvas surface above, so the halo disappears into both.
1905
+ const sourceFontSize = 9;
1906
+ const sourceTextY = ry + 4;
1907
+ const sourceHalo = tag('rect', {
1908
+ x: num(chrome.sourceHaloX),
1909
+ y: num(sourceTextY - sourceFontSize),
1910
+ width: num(chrome.sourceHaloWidth),
1911
+ height: sourceFontSize + 6,
1912
+ fill,
1913
+ });
1914
+ const sourceText = textTag(
1915
+ {
1916
+ x: num(chrome.sourceTextX),
1917
+ y: num(sourceTextY),
1918
+ 'font-family': FONT_STACK.mono,
1919
+ 'font-size': sourceFontSize,
1920
+ fill: badgeText,
1921
+ },
1922
+ r.sourcePath,
1923
+ );
1924
+
1925
+ // Nested swimlanes (laid out by buildIncludeRegions against the parent's timeline).
1926
+ const nested = r.nestedSwimlanes
1927
+ .map((s) => renderSwimlane(s, options, idPrefix, palette))
1928
+ .join('');
1929
+
1930
+ return tag(
1931
+ 'g',
1932
+ { 'data-layer': 'include' },
1933
+ region + nested + tab + tabLabel + badge + glyph + sourceHalo + sourceText,
1934
+ );
1935
+ }
1936
+
1937
+ // Paint the "Powered by nowline" attribution mark inside the
1938
+ // layout-supplied `attributionBox`. The whole mark — prefix text,
1939
+ // "now", red "l" bar, and "ine" — sits inside one <a href> so the
1940
+ // entire string is clickable. Glyph anatomy (positions, widths, scale)
1941
+ // lives in `themes/shared.ts` (`ATTRIBUTION_*`); the layout reserves a
1942
+ // box of exactly that size at canvas-bottom-right.
1943
+ function renderAttributionMark(model: PositionedRoadmap): string {
1944
+ const muted = model.palette.attribution.mark;
1945
+ const accent = model.palette.attribution.link;
1946
+ if (model.swimlanes.length === 0) return '';
1947
+ const tx = model.header.attributionBox.x;
1948
+ const ty = model.header.attributionBox.y;
1949
+ // Both texts share the wordmark's baseline (y = wordmark font size)
1950
+ // so the smaller "Powered by" sits visually above the wordmark's
1951
+ // baseline without bumping the bar's bottom up.
1952
+ const baselineY = ATTRIBUTION_WORDMARK_FONT_SIZE;
1953
+ const inner =
1954
+ textTag(
1955
+ {
1956
+ x: '0',
1957
+ y: baselineY,
1958
+ 'font-family': FONT_STACK.sans,
1959
+ 'font-size': ATTRIBUTION_PREFIX_FONT_SIZE,
1960
+ 'font-weight': 400,
1961
+ fill: muted,
1962
+ },
1963
+ ATTRIBUTION_TEXT,
1964
+ ) +
1965
+ textTag(
1966
+ {
1967
+ x: ATTRIBUTION_NOW_LOGICAL_X,
1968
+ y: baselineY,
1969
+ 'font-family': FONT_STACK.sans,
1970
+ 'font-size': ATTRIBUTION_WORDMARK_FONT_SIZE,
1971
+ 'font-weight': 700,
1972
+ fill: muted,
1973
+ },
1974
+ 'now',
1975
+ ) +
1976
+ tag('rect', {
1977
+ x: ATTRIBUTION_BAR_LOGICAL_X,
1978
+ y: 12,
1979
+ width: ATTRIBUTION_BAR_LOGICAL_WIDTH,
1980
+ height: ATTRIBUTION_WORDMARK_FONT_SIZE,
1981
+ fill: accent,
1982
+ }) +
1983
+ textTag(
1984
+ {
1985
+ x: ATTRIBUTION_INE_LOGICAL_X,
1986
+ y: baselineY,
1987
+ 'font-family': FONT_STACK.sans,
1988
+ 'font-size': ATTRIBUTION_WORDMARK_FONT_SIZE,
1989
+ 'font-weight': 400,
1990
+ fill: muted,
1991
+ },
1992
+ 'ine',
1993
+ );
1994
+ const group = tag(
1995
+ 'g',
1996
+ { transform: `translate(${num(tx)} ${num(ty)}) scale(${num(ATTRIBUTION_SCALE)})` },
1997
+ inner,
1998
+ );
1999
+ return tag(
2000
+ 'a',
2001
+ {
2002
+ href: ATTRIBUTION_LINK,
2003
+ target: '_blank',
2004
+ rel: 'noopener',
2005
+ 'aria-label': 'Powered by nowline',
2006
+ },
2007
+ tag('g', { 'data-layer': 'attribution' }, group),
2008
+ );
2009
+ }
2010
+
2011
+ async function embedLogo(
2012
+ logoRef: string,
2013
+ resolver: AssetResolver | undefined,
2014
+ idPrefix: string,
2015
+ options: RenderOptions,
2016
+ x: number,
2017
+ y: number,
2018
+ size: number,
2019
+ ): Promise<string> {
2020
+ if (!resolver) return '';
2021
+ let asset: AssetBytes;
2022
+ try {
2023
+ asset = await resolver(logoRef);
2024
+ } catch (err) {
2025
+ const msg = `logo: failed to load ${logoRef}: ${err instanceof Error ? err.message : String(err)}`;
2026
+ if (options.strict) throw err;
2027
+ options.warn?.(msg);
2028
+ return '';
2029
+ }
2030
+ const mime = (asset.mime ?? '').toLowerCase();
2031
+ if (mime === 'image/svg+xml') {
2032
+ const raw = new TextDecoder().decode(asset.bytes);
2033
+ const cleaned = sanitizeSvg(raw, { idPrefix: `${idPrefix}-logo`, onWarn: options.warn });
2034
+ return tag('g', { transform: `translate(${num(x)} ${num(y)})` }, cleaned);
2035
+ }
2036
+ if (
2037
+ mime === 'image/png' ||
2038
+ mime === 'image/jpeg' ||
2039
+ mime === 'image/jpg' ||
2040
+ mime === 'image/webp'
2041
+ ) {
2042
+ const b64 = bytesToBase64(asset.bytes);
2043
+ return tag('image', {
2044
+ href: `data:${mime};base64,${b64}`,
2045
+ x: num(x),
2046
+ y: num(y),
2047
+ width: num(size),
2048
+ height: num(size),
2049
+ preserveAspectRatio: 'xMidYMid meet',
2050
+ });
2051
+ }
2052
+ const msg = `logo: unsupported mime ${mime} for ${logoRef}`;
2053
+ if (options.strict) throw new Error(msg);
2054
+ options.warn?.(msg);
2055
+ return '';
2056
+ }
2057
+
2058
+ // Base64 without depending on Node's Buffer (renderer stays browser-safe).
2059
+ function bytesToBase64(bytes: Uint8Array): string {
2060
+ if (typeof btoa !== 'undefined') {
2061
+ let bin = '';
2062
+ for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
2063
+ return btoa(bin);
2064
+ }
2065
+ // Node fallback without importing Buffer directly; use lazy dynamic require.
2066
+ const g = globalThis as {
2067
+ Buffer?: { from: (b: Uint8Array) => { toString: (enc: string) => string } };
2068
+ };
2069
+ if (g.Buffer) return g.Buffer.from(bytes).toString('base64');
2070
+ throw new Error('renderer: no base64 encoder available');
2071
+ }
2072
+
2073
+ export async function renderSvg(
2074
+ model: PositionedRoadmap,
2075
+ options: RenderOptions = {},
2076
+ ): Promise<string> {
2077
+ const ids = new IdGenerator(options.idPrefix ?? 'nl');
2078
+ const idPrefix = ids.next('root');
2079
+
2080
+ const palette = model.palette;
2081
+ const parts: string[] = [];
2082
+
2083
+ // <defs> — shadows + arrowhead markers (palette-driven fills baked in).
2084
+ const arrowFillNeutral = palette.arrowhead.neutral;
2085
+ const arrowFillLight = palette.arrowhead.light;
2086
+ const arrowFillDark = palette.arrowhead.dark;
2087
+ const arrowDef = (id: string, fill: string): string =>
2088
+ `<marker id="${id}" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" fill="${fill}"/></marker>`;
2089
+ const defs =
2090
+ `<defs>${allShadowDefs(idPrefix)}` +
2091
+ arrowDef('nl-arrow', arrowFillNeutral) +
2092
+ arrowDef('nl-arrow-light', arrowFillLight) +
2093
+ arrowDef('nl-arrow-dark', arrowFillDark) +
2094
+ `</defs>`;
2095
+ parts.push(defs);
2096
+
2097
+ // Background
2098
+ parts.push(
2099
+ tag('rect', {
2100
+ x: 0,
2101
+ y: 0,
2102
+ width: num(model.width),
2103
+ height: num(model.height),
2104
+ fill: model.backgroundColor,
2105
+ }),
2106
+ );
2107
+
2108
+ // Timeline header strip (panels + date labels). Chart-body grid
2109
+ // lines used to live here too, but they were occluded by the
2110
+ // swimlane background rects emitted later — so the major dotted
2111
+ // and minor grid lines never actually rendered in the chart body.
2112
+ // They now ship as their own layer below.
2113
+ parts.push(renderTimeline(model.timeline, palette));
2114
+
2115
+ // Swimlane backgrounds — emitted as their own pass so the grid
2116
+ // lines can be drawn on top of them, then the swimlane content
2117
+ // (frame tab + items) sits on top of the grid.
2118
+ for (const s of model.swimlanes) parts.push(renderSwimlaneBg(s, palette));
2119
+
2120
+ // Chart-body grid lines (major dotted at every labeled tick, plus
2121
+ // optional faint minor lines when minor-grid is set). Drawn after
2122
+ // swimlane backgrounds so they actually span the chart body, but
2123
+ // before items and overlays so item bars sit cleanly on top.
2124
+ parts.push(renderGridLines(model.timeline, model.chartBox.y, palette));
2125
+
2126
+ // m2g+: under-bar dependency edges go BEFORE swimlane / item
2127
+ // content. The channel router falls back to under-bar routing when
2128
+ // it can't find a clear vertical gutter between source and target;
2129
+ // these edges intentionally cross item bars and need the item fills
2130
+ // painted ON TOP so the bars stay the visual foreground. Renderer
2131
+ // applies a thinner stroke (see `renderEdge`) to further de-emphasise
2132
+ // the arrow body — only the head and stub at the target end stay
2133
+ // crisply visible.
2134
+ for (const e of model.edges) {
2135
+ if (e.kind === 'underBar') parts.push(renderEdge(e, palette));
2136
+ }
2137
+
2138
+ // Swimlane content (frame tabs + items) on top of the grid lines.
2139
+ for (const s of model.swimlanes) parts.push(renderSwimlane(s, options, idPrefix, palette));
2140
+
2141
+ // Include regions (drawn after own swimlanes so the dashed border + tab
2142
+ // overlay the chart, with their own nested swimlanes inside).
2143
+ for (const r of model.includes) parts.push(renderIncludeRegion(r, options, idPrefix, palette));
2144
+
2145
+ // Normal / overflow dependency edges on top of items but below
2146
+ // cut-lines / nowline. Under-bar edges already painted above.
2147
+ for (const e of model.edges) {
2148
+ if (e.kind !== 'underBar') parts.push(renderEdge(e, palette));
2149
+ }
2150
+
2151
+ // Anchor + milestone cut lines drawn AFTER items so they overlay the
2152
+ // swimlane fills.
2153
+ for (const a of model.anchors) parts.push(renderAnchorCutLine(a, palette));
2154
+ for (const m of model.milestones) parts.push(renderMilestoneCutLine(m, palette));
2155
+
2156
+ // Marker-row diamonds + labels.
2157
+ for (const a of model.anchors) parts.push(renderAnchor(a, palette));
2158
+ for (const m of model.milestones) parts.push(renderMilestone(m, palette));
2159
+
2160
+ // Now-line
2161
+ parts.push(renderNowline(model.nowline, palette));
2162
+
2163
+ // Footnotes + header last (always on top)
2164
+ parts.push(renderFootnotes(model.footnotes, idPrefix, palette));
2165
+ parts.push(renderHeader(model.header, idPrefix, palette));
2166
+ parts.push(renderAttributionMark(model));
2167
+
2168
+ // Logo (if header carries one)
2169
+ if (model.header.logo && options.assetResolver) {
2170
+ const logoSvg = await embedLogo(
2171
+ model.header.logo.assetRef ?? '',
2172
+ options.assetResolver,
2173
+ idPrefix,
2174
+ options,
2175
+ model.header.logo.box.x,
2176
+ model.header.logo.box.y,
2177
+ Math.max(model.header.logo.box.width, model.header.logo.box.height),
2178
+ );
2179
+ if (logoSvg) parts.push(logoSvg);
2180
+ }
2181
+
2182
+ const svgAttrs = attrs({
2183
+ xmlns: 'http://www.w3.org/2000/svg',
2184
+ viewBox: `0 0 ${num(model.width)} ${num(model.height)}`,
2185
+ width: num(model.width),
2186
+ height: num(model.height),
2187
+ 'data-theme': model.theme,
2188
+ 'data-generator': 'nowline',
2189
+ });
2190
+ return `<svg${svgAttrs}>${parts.join('')}</svg>`;
2191
+ }
2192
+
2193
+ // Exported for tests.
2194
+ export const __internal = {
2195
+ renderItem,
2196
+ renderSwimlane,
2197
+ renderTimeline,
2198
+ renderHeader,
2199
+ renderEdge,
2200
+ };
2201
+
2202
+ // These helpers are kept in the exports table so tsc doesn't prune them.
2203
+ void escAttr;
2204
+ void escText;