@nowline/renderer 0.2.0

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