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