@nowline/layout 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.
Files changed (193) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +103 -0
  3. package/dist/band-scale.d.ts +56 -0
  4. package/dist/band-scale.d.ts.map +1 -0
  5. package/dist/band-scale.js +86 -0
  6. package/dist/band-scale.js.map +1 -0
  7. package/dist/calendar.d.ts +79 -0
  8. package/dist/calendar.d.ts.map +1 -0
  9. package/dist/calendar.js +210 -0
  10. package/dist/calendar.js.map +1 -0
  11. package/dist/capacity.d.ts +72 -0
  12. package/dist/capacity.d.ts.map +1 -0
  13. package/dist/capacity.js +163 -0
  14. package/dist/capacity.js.map +1 -0
  15. package/dist/dsl-utils.d.ts +5 -0
  16. package/dist/dsl-utils.d.ts.map +1 -0
  17. package/dist/dsl-utils.js +28 -0
  18. package/dist/dsl-utils.js.map +1 -0
  19. package/dist/edge-routing.d.ts +89 -0
  20. package/dist/edge-routing.d.ts.map +1 -0
  21. package/dist/edge-routing.js +435 -0
  22. package/dist/edge-routing.js.map +1 -0
  23. package/dist/frame-tab-geometry.d.ts +78 -0
  24. package/dist/frame-tab-geometry.d.ts.map +1 -0
  25. package/dist/frame-tab-geometry.js +115 -0
  26. package/dist/frame-tab-geometry.js.map +1 -0
  27. package/dist/header-card-geometry.d.ts +29 -0
  28. package/dist/header-card-geometry.d.ts.map +1 -0
  29. package/dist/header-card-geometry.js +41 -0
  30. package/dist/header-card-geometry.js.map +1 -0
  31. package/dist/i18n.d.ts +48 -0
  32. package/dist/i18n.d.ts.map +1 -0
  33. package/dist/i18n.js +114 -0
  34. package/dist/i18n.js.map +1 -0
  35. package/dist/include-chrome-geometry.d.ts +86 -0
  36. package/dist/include-chrome-geometry.d.ts.map +1 -0
  37. package/dist/include-chrome-geometry.js +104 -0
  38. package/dist/include-chrome-geometry.js.map +1 -0
  39. package/dist/index.d.ts +11 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +10 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/inline-date-pin-geometry.d.ts +46 -0
  44. package/dist/inline-date-pin-geometry.d.ts.map +1 -0
  45. package/dist/inline-date-pin-geometry.js +149 -0
  46. package/dist/inline-date-pin-geometry.js.map +1 -0
  47. package/dist/item-bar-geometry.d.ts +157 -0
  48. package/dist/item-bar-geometry.d.ts.map +1 -0
  49. package/dist/item-bar-geometry.js +214 -0
  50. package/dist/item-bar-geometry.js.map +1 -0
  51. package/dist/lane-utilization.d.ts +90 -0
  52. package/dist/lane-utilization.d.ts.map +1 -0
  53. package/dist/lane-utilization.js +206 -0
  54. package/dist/lane-utilization.js.map +1 -0
  55. package/dist/layout-context.d.ts +143 -0
  56. package/dist/layout-context.d.ts.map +1 -0
  57. package/dist/layout-context.js +8 -0
  58. package/dist/layout-context.js.map +1 -0
  59. package/dist/layout.d.ts +18 -0
  60. package/dist/layout.d.ts.map +1 -0
  61. package/dist/layout.js +1298 -0
  62. package/dist/layout.js.map +1 -0
  63. package/dist/nodes/anchor-node.d.ts +16 -0
  64. package/dist/nodes/anchor-node.d.ts.map +1 -0
  65. package/dist/nodes/anchor-node.js +68 -0
  66. package/dist/nodes/anchor-node.js.map +1 -0
  67. package/dist/nodes/footnote-node.d.ts +10 -0
  68. package/dist/nodes/footnote-node.d.ts.map +1 -0
  69. package/dist/nodes/footnote-node.js +41 -0
  70. package/dist/nodes/footnote-node.js.map +1 -0
  71. package/dist/nodes/group-node.d.ts +29 -0
  72. package/dist/nodes/group-node.d.ts.map +1 -0
  73. package/dist/nodes/group-node.js +195 -0
  74. package/dist/nodes/group-node.js.map +1 -0
  75. package/dist/nodes/include-node.d.ts +16 -0
  76. package/dist/nodes/include-node.d.ts.map +1 -0
  77. package/dist/nodes/include-node.js +117 -0
  78. package/dist/nodes/include-node.js.map +1 -0
  79. package/dist/nodes/item-node.d.ts +51 -0
  80. package/dist/nodes/item-node.d.ts.map +1 -0
  81. package/dist/nodes/item-node.js +108 -0
  82. package/dist/nodes/item-node.js.map +1 -0
  83. package/dist/nodes/marker-geometry.d.ts +22 -0
  84. package/dist/nodes/marker-geometry.d.ts.map +1 -0
  85. package/dist/nodes/marker-geometry.js +38 -0
  86. package/dist/nodes/marker-geometry.js.map +1 -0
  87. package/dist/nodes/milestone-node.d.ts +48 -0
  88. package/dist/nodes/milestone-node.d.ts.map +1 -0
  89. package/dist/nodes/milestone-node.js +210 -0
  90. package/dist/nodes/milestone-node.js.map +1 -0
  91. package/dist/nodes/parallel-node.d.ts +21 -0
  92. package/dist/nodes/parallel-node.d.ts.map +1 -0
  93. package/dist/nodes/parallel-node.js +88 -0
  94. package/dist/nodes/parallel-node.js.map +1 -0
  95. package/dist/nodes/roadmap-node.d.ts +76 -0
  96. package/dist/nodes/roadmap-node.d.ts.map +1 -0
  97. package/dist/nodes/roadmap-node.js +788 -0
  98. package/dist/nodes/roadmap-node.js.map +1 -0
  99. package/dist/nodes/swimlane-node.d.ts +38 -0
  100. package/dist/nodes/swimlane-node.d.ts.map +1 -0
  101. package/dist/nodes/swimlane-node.js +308 -0
  102. package/dist/nodes/swimlane-node.js.map +1 -0
  103. package/dist/renderable.d.ts +44 -0
  104. package/dist/renderable.d.ts.map +1 -0
  105. package/dist/renderable.js +21 -0
  106. package/dist/renderable.js.map +1 -0
  107. package/dist/row-packer.d.ts +125 -0
  108. package/dist/row-packer.d.ts.map +1 -0
  109. package/dist/row-packer.js +169 -0
  110. package/dist/row-packer.js.map +1 -0
  111. package/dist/style-resolution.d.ts +14 -0
  112. package/dist/style-resolution.d.ts.map +1 -0
  113. package/dist/style-resolution.js +191 -0
  114. package/dist/style-resolution.js.map +1 -0
  115. package/dist/themes/dark.d.ts +4 -0
  116. package/dist/themes/dark.d.ts.map +1 -0
  117. package/dist/themes/dark.js +241 -0
  118. package/dist/themes/dark.js.map +1 -0
  119. package/dist/themes/grayscale.d.ts +4 -0
  120. package/dist/themes/grayscale.d.ts.map +1 -0
  121. package/dist/themes/grayscale.js +237 -0
  122. package/dist/themes/grayscale.js.map +1 -0
  123. package/dist/themes/index.d.ts +19 -0
  124. package/dist/themes/index.d.ts.map +1 -0
  125. package/dist/themes/index.js +57 -0
  126. package/dist/themes/index.js.map +1 -0
  127. package/dist/themes/light.d.ts +4 -0
  128. package/dist/themes/light.d.ts.map +1 -0
  129. package/dist/themes/light.js +248 -0
  130. package/dist/themes/light.js.map +1 -0
  131. package/dist/themes/shape.d.ts +194 -0
  132. package/dist/themes/shape.d.ts.map +1 -0
  133. package/dist/themes/shape.js +6 -0
  134. package/dist/themes/shape.js.map +1 -0
  135. package/dist/themes/shared.d.ts +145 -0
  136. package/dist/themes/shared.d.ts.map +1 -0
  137. package/dist/themes/shared.js +310 -0
  138. package/dist/themes/shared.js.map +1 -0
  139. package/dist/time-scale.d.ts +39 -0
  140. package/dist/time-scale.d.ts.map +1 -0
  141. package/dist/time-scale.js +62 -0
  142. package/dist/time-scale.js.map +1 -0
  143. package/dist/types.d.ts +516 -0
  144. package/dist/types.d.ts.map +1 -0
  145. package/dist/types.js +6 -0
  146. package/dist/types.js.map +1 -0
  147. package/dist/view-preset.d.ts +23 -0
  148. package/dist/view-preset.d.ts.map +1 -0
  149. package/dist/view-preset.js +146 -0
  150. package/dist/view-preset.js.map +1 -0
  151. package/dist/working-calendar.d.ts +14 -0
  152. package/dist/working-calendar.d.ts.map +1 -0
  153. package/dist/working-calendar.js +74 -0
  154. package/dist/working-calendar.js.map +1 -0
  155. package/package.json +43 -0
  156. package/src/band-scale.ts +115 -0
  157. package/src/calendar.ts +244 -0
  158. package/src/capacity.ts +191 -0
  159. package/src/dsl-utils.ts +30 -0
  160. package/src/edge-routing.ts +550 -0
  161. package/src/frame-tab-geometry.ts +165 -0
  162. package/src/header-card-geometry.ts +48 -0
  163. package/src/i18n.ts +124 -0
  164. package/src/include-chrome-geometry.ts +156 -0
  165. package/src/index.ts +118 -0
  166. package/src/inline-date-pin-geometry.ts +196 -0
  167. package/src/item-bar-geometry.ts +271 -0
  168. package/src/lane-utilization.ts +259 -0
  169. package/src/layout-context.ts +182 -0
  170. package/src/layout.ts +1530 -0
  171. package/src/nodes/anchor-node.ts +77 -0
  172. package/src/nodes/footnote-node.ts +60 -0
  173. package/src/nodes/group-node.ts +260 -0
  174. package/src/nodes/include-node.ts +168 -0
  175. package/src/nodes/item-node.ts +171 -0
  176. package/src/nodes/marker-geometry.ts +43 -0
  177. package/src/nodes/milestone-node.ts +263 -0
  178. package/src/nodes/parallel-node.ts +110 -0
  179. package/src/nodes/roadmap-node.ts +957 -0
  180. package/src/nodes/swimlane-node.ts +423 -0
  181. package/src/renderable.ts +68 -0
  182. package/src/row-packer.ts +271 -0
  183. package/src/style-resolution.ts +243 -0
  184. package/src/themes/dark.ts +244 -0
  185. package/src/themes/grayscale.ts +240 -0
  186. package/src/themes/index.ts +66 -0
  187. package/src/themes/light.ts +251 -0
  188. package/src/themes/shape.ts +230 -0
  189. package/src/themes/shared.ts +369 -0
  190. package/src/time-scale.ts +78 -0
  191. package/src/types.ts +641 -0
  192. package/src/view-preset.ts +180 -0
  193. package/src/working-calendar.ts +91 -0
package/src/layout.ts ADDED
@@ -0,0 +1,1530 @@
1
+ import type {
2
+ EntityProperty,
3
+ GroupBlock,
4
+ ItemDeclaration,
5
+ LabelDeclaration,
6
+ NowlineFile,
7
+ ParallelBlock,
8
+ ResolveResult,
9
+ SwimlaneDeclaration,
10
+ } from '@nowline/core';
11
+ import { isGroupBlock, isItemDeclaration, isParallelBlock } from '@nowline/core';
12
+ import {
13
+ addDays,
14
+ daysBetween,
15
+ deriveItemDurationDays,
16
+ deriveTotalEffortDays,
17
+ resolveDuration,
18
+ } from './calendar.js';
19
+ import {
20
+ estimateCapacitySuffixWidth,
21
+ formatCapacityNumber,
22
+ parseCapacityValue,
23
+ resolveCapacityIcon,
24
+ } from './capacity.js';
25
+ import { parseDate, propValue, propValues } from './dsl-utils.js';
26
+ import {
27
+ ChannelGrid,
28
+ collectRoutingObstacles,
29
+ type EdgeRouteRequest,
30
+ routeChannelEdges,
31
+ } from './edge-routing.js';
32
+ import {
33
+ HEADER_AUTHOR_FONT_SIZE_PX,
34
+ HEADER_AUTHOR_LINE_HEIGHT_PX,
35
+ HEADER_CARD_PADDING_BOTTOM,
36
+ HEADER_CARD_PADDING_TOP,
37
+ HEADER_CARD_PADDING_X,
38
+ HEADER_TITLE_FONT_SIZE_PX,
39
+ HEADER_TITLE_LINE_HEIGHT_PX,
40
+ HEADER_TITLE_TO_AUTHOR_GAP_PX,
41
+ } from './header-card-geometry.js';
42
+ import { localeStrings } from './i18n.js';
43
+ import { computeItemInlineDatePins, pickInlineDate } from './inline-date-pin-geometry.js';
44
+ import {
45
+ ITEM_CAPTION_INSET_X_PX,
46
+ ITEM_CAPTION_META_BASELINE_OFFSET_PX,
47
+ ITEM_CAPTION_SPILL_GAP_PX,
48
+ ITEM_CAPTION_TITLE_FONT_SIZE_PX,
49
+ ITEM_DECORATION_SPILL_GAP_PX,
50
+ ITEM_FOOTNOTE_INDICATOR_STEP_PX,
51
+ ITEM_LINK_ICON_TILE_SIZE_PX,
52
+ ITEM_STATUS_DOT_RADIUS_PX,
53
+ LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX,
54
+ LABEL_CHIP_GAP_BETWEEN_PX,
55
+ LABEL_CHIP_HEIGHT_PX,
56
+ LABEL_CHIP_ROW_STEP_PX,
57
+ MIN_BAR_WIDTH_FOR_DOT_PX,
58
+ MIN_BAR_WIDTH_FOR_FOOTNOTE_PX,
59
+ MIN_BAR_WIDTH_FOR_LINK_AND_DOT_PX,
60
+ packSpillChips,
61
+ } from './item-bar-geometry.js';
62
+ import { type LayoutContext, newCursor, type TrackCursor } from './layout-context.js';
63
+ import { GroupNode } from './nodes/group-node.js';
64
+ import { ItemNode } from './nodes/item-node.js';
65
+ import { ParallelNode } from './nodes/parallel-node.js';
66
+ import { RoadmapNode } from './nodes/roadmap-node.js';
67
+ import { SwimlaneNode } from './nodes/swimlane-node.js';
68
+ import { resolveLabelChipStyle, resolveStyle, type StyleContext } from './style-resolution.js';
69
+ import type { ThemeName } from './themes/index.js';
70
+ import {
71
+ GUTTER_PX,
72
+ HEADER_BESIDE_MAX_WIDTH_PX,
73
+ HEADER_BESIDE_MIN_WIDTH_PX,
74
+ ITEM_INSET_PX,
75
+ MIN_ITEM_WIDTH,
76
+ NOW_PILL_LABEL_FONT_SIZE_PX,
77
+ NOW_PILL_LABEL_INSET_X_PX,
78
+ NOW_PILL_WIDTH_PX,
79
+ PADDING_PX,
80
+ PROGRESS_STRIP_HEIGHT_PX,
81
+ } from './themes/shared.js';
82
+ import type {
83
+ BoundingBox,
84
+ LinkIconKind,
85
+ Point,
86
+ PositionedCapacity,
87
+ PositionedDependencyEdge,
88
+ PositionedGroup,
89
+ PositionedIncludeRegion,
90
+ PositionedItem,
91
+ PositionedLabelChip,
92
+ PositionedNowline,
93
+ PositionedParallel,
94
+ PositionedRoadmap,
95
+ PositionedSwimlane,
96
+ PositionedTrackChild,
97
+ StatusKind,
98
+ } from './types.js';
99
+ import type { ViewPreset } from './view-preset.js';
100
+ import { daysPerUnit } from './working-calendar.js';
101
+
102
+ export interface LayoutOptions {
103
+ theme?: ThemeName;
104
+ today?: Date;
105
+ width?: number; // total SVG width in px; default 1280
106
+ /**
107
+ * BCP-47 tag controlling axis labels, the now-pill string, and the
108
+ * quarter prefix. Resolved by the caller (CLI flag → env vars). When
109
+ * undefined, layout falls back to the file's `nowline v1 locale:` and
110
+ * then to `en-US`. See `specs/localization.md`.
111
+ */
112
+ locale?: string;
113
+ }
114
+
115
+ export type LayoutResult = PositionedRoadmap;
116
+
117
+ function statusFromProp(raw: string | undefined): StatusKind {
118
+ switch (raw) {
119
+ case 'done':
120
+ case 'completed':
121
+ return 'done';
122
+ case 'in-progress':
123
+ case 'active':
124
+ return 'in-progress';
125
+ case 'at-risk':
126
+ case 'blocked':
127
+ case 'planned':
128
+ return raw;
129
+ case undefined:
130
+ return 'planned';
131
+ default:
132
+ return 'neutral';
133
+ }
134
+ }
135
+
136
+ function parseProgressFraction(raw: string | undefined): number {
137
+ if (!raw) return 0;
138
+ const m = /^(\d{1,3})%$/.exec(raw);
139
+ if (!m) return 0;
140
+ return Math.max(0, Math.min(100, parseInt(m[1], 10))) / 100;
141
+ }
142
+
143
+ // Resolve a `size:NAME` value to its size declaration's effort literal, used
144
+ // by displays that want the literal duration string (not days). Returns the
145
+ // original string for raw literals (`1w`, `3d`) and undefined for missing
146
+ // values. m5 will adjust callers to apply capacity-aware derivation when the
147
+ // caller wants the calendar duration; this helper continues to expose the
148
+ // raw effort/duration literal for chips, captions, and tooltips.
149
+ function resolveDurationLiteral(
150
+ raw: string | undefined,
151
+ ctx: { sizes: Map<string, import('./types.js').ResolvedSize> },
152
+ ): string | undefined {
153
+ if (!raw) return undefined;
154
+ if (/^\d+(?:\.\d+)?[dwmqy]$/.test(raw) || /^\d+%$/.test(raw)) return raw;
155
+ return ctx.sizes.get(raw)?.effortLiteral ?? raw;
156
+ }
157
+
158
+ // Resolve a person/team id to its declared title when present (id otherwise).
159
+ function resolveActorDisplay(
160
+ raw: string | undefined,
161
+ ctx: {
162
+ teams: Map<string, import('@nowline/core').TeamDeclaration>;
163
+ persons: Map<string, import('@nowline/core').PersonDeclaration>;
164
+ },
165
+ ): string | undefined {
166
+ if (!raw) return undefined;
167
+ return ctx.teams.get(raw)?.title ?? ctx.persons.get(raw)?.title ?? raw;
168
+ }
169
+
170
+ function parseLinkIcon(link: string | undefined): { icon: LinkIconKind; href?: string } {
171
+ if (!link) return { icon: 'none' };
172
+ const lower = link.toLowerCase();
173
+ if (lower.includes('linear.app')) return { icon: 'linear', href: link };
174
+ if (lower.includes('github.com')) return { icon: 'github', href: link };
175
+ if (lower.includes('atlassian.net') || lower.includes('jira.')) {
176
+ return { icon: 'jira', href: link };
177
+ }
178
+ return { icon: 'generic', href: link };
179
+ }
180
+
181
+ // Bake a named/hex color from a label's `bg:` or `fg:` into the chip style.
182
+ function buildLabelChip(
183
+ label: LabelDeclaration,
184
+ ctx: StyleContext,
185
+ x: number,
186
+ y: number,
187
+ maxWidth?: number,
188
+ ): PositionedLabelChip {
189
+ const style = resolveLabelChipStyle(label, ctx);
190
+ // Prefer the short name (id) when it exists — chips inside an item bar
191
+ // are tight; the long title risks overflowing.
192
+ const text = label.name ?? label.title ?? '';
193
+ const padKey = style.padding === 'none' ? 'xs' : style.padding;
194
+ const pad = PADDING_PX[padKey as keyof typeof PADDING_PX];
195
+ let width = Math.max(20, Math.round(text.length * 5.5 + pad * 2));
196
+ if (maxWidth !== undefined) width = Math.min(width, maxWidth);
197
+ return {
198
+ text,
199
+ style,
200
+ box: { x, y, width, height: LABEL_CHIP_HEIGHT_PX },
201
+ };
202
+ }
203
+
204
+ // Sequence a set of nodes into a single horizontal track. `parallelInside`
205
+ // indicates the caller is inside a ParallelBlock and each child occupies a
206
+ // fresh sub-track (caller passes a new cursor per call).
207
+ function sequenceItem(
208
+ node: ItemDeclaration,
209
+ cursor: TrackCursor,
210
+ ctx: LayoutContext,
211
+ ownerOverride?: string,
212
+ ): PositionedItem {
213
+ const props = node.properties;
214
+ const style = resolveStyle('item', props, ctx.styleCtx);
215
+ // Sizing precedence (specs/dsl.md § "Sizing precedence"):
216
+ // 1. `duration:LITERAL` wins as the calendar duration — `size:NAME`
217
+ // becomes a pure annotation (chip only).
218
+ // 2. Otherwise, `size:NAME` derives `effort ÷ capacity` (default
219
+ // capacity = 1).
220
+ // The validator requires one of the two on every item.
221
+ const sizeRef = propValue(props, 'size');
222
+ const sizeResolved = sizeRef ? (ctx.sizes.get(sizeRef) ?? null) : null;
223
+ const durationDays = deriveItemDurationDays(props, ctx.sizes, ctx.cal);
224
+ // Total work in single-engineer days, used below to normalize a literal
225
+ // `remaining:` value into a progress fraction. Stays per-engineer
226
+ // regardless of the item's `capacity:` so a `remaining:1w` always means
227
+ // "one engineer-week of work left".
228
+ const totalEffortDays = deriveTotalEffortDays(props, ctx.sizes, ctx.cal);
229
+ const afterRaw = propValues(props, 'after');
230
+ const beforeRaw = propValues(props, 'before');
231
+ const dateRaw = propValue(props, 'date');
232
+ const remainingDays = resolveDuration(propValue(props, 'remaining'), ctx.sizes, ctx.cal);
233
+
234
+ // Resolve start x: explicit date > after-chain > cursor position.
235
+ // `after:` accepts both entity ids (looked up in entityRightEdges) and
236
+ // inline ISO date literals (resolved through the time scale). The
237
+ // validator enforces "at most one inline date per direction" so at most
238
+ // one element will hit the date path per item.
239
+ let startX = cursor.x;
240
+ const explicitDate = parseDate(dateRaw);
241
+ if (explicitDate) {
242
+ const xd = ctx.scale.forwardWithinDomain(explicitDate);
243
+ if (xd !== null) startX = xd;
244
+ } else if (afterRaw.length > 0) {
245
+ let maxEnd = cursor.x;
246
+ for (const ref of afterRaw) {
247
+ const inlineDate = parseDate(ref);
248
+ if (inlineDate) {
249
+ const xd = ctx.scale.forwardWithinDomain(inlineDate);
250
+ if (xd !== null) maxEnd = Math.max(maxEnd, xd);
251
+ continue;
252
+ }
253
+ const endX = ctx.entityRightEdges.get(ref);
254
+ if (endX !== undefined) maxEnd = Math.max(maxEnd, endX);
255
+ }
256
+ startX = Math.max(cursor.x, maxEnd);
257
+ }
258
+
259
+ const naturalWidth = Math.max(MIN_ITEM_WIDTH, durationDays * ctx.timeline.pixelsPerDay);
260
+ // Logical extent — what the item "owns" in time (used for chaining,
261
+ // `after:` lookups, dependency-arrow attach points).
262
+ const logicalLeft = startX;
263
+ const logicalRight = startX + naturalWidth;
264
+
265
+ const linkRaw = propValue(props, 'link');
266
+ const linkInfo = parseLinkIcon(linkRaw);
267
+ const hasLinkIcon = linkInfo.icon !== 'none';
268
+
269
+ // Pre-compute the chip row geometry — every chip renders at its
270
+ // NATURAL text-fit width on a single horizontal row, never
271
+ // truncated. We only need the total row width here so we can
272
+ // decide whether the row fits inside the bar; concrete chip
273
+ // (x, y) placement comes after ItemNode resolves the visible
274
+ // bar box below.
275
+ //
276
+ // Chips sit at the bar's bottom (just above the progress strip)
277
+ // and the link icon (when present) sits in the bar's UPPER-LEFT
278
+ // corner, so they no longer share a vertical band — chips have
279
+ // the full caption-inset-bounded inner width regardless of
280
+ // whether a link icon is rendered. The link-icon column's
281
+ // horizontal cost is borne by the caption (title/meta) inset
282
+ // instead, see `ItemNode`.
283
+ const visualWidthPredict = Math.max(MIN_ITEM_WIDTH, naturalWidth - 2 * ITEM_INSET_PX);
284
+ const labelIds = propValues(props, 'labels');
285
+ const chipSamples: { id: LabelDeclaration; width: number }[] = [];
286
+ for (const labelId of labelIds) {
287
+ const label = ctx.labels.get(labelId);
288
+ if (!label) continue;
289
+ const sample = buildLabelChip(label, ctx.styleCtx, 0, 0);
290
+ chipSamples.push({ id: label, width: sample.box.width });
291
+ }
292
+ let chipRowWidth = 0;
293
+ for (let i = 0; i < chipSamples.length; i += 1) {
294
+ if (i > 0) chipRowWidth += LABEL_CHIP_GAP_BETWEEN_PX;
295
+ chipRowWidth += chipSamples[i].width;
296
+ }
297
+ const chipInsideAvailWidth = Math.max(0, visualWidthPredict - 2 * ITEM_CAPTION_INSET_X_PX);
298
+ const chipsOutside = chipSamples.length > 0 && chipRowWidth > chipInsideAvailWidth;
299
+
300
+ // Handle `before:` — item must end by the earliest cap. Each cap is
301
+ // either an entity id (looked up in entityLeftEdges) or an inline ISO
302
+ // date literal (resolved through the time scale). The validator enforces
303
+ // "at most one inline date per direction" so at most one element per
304
+ // before-list will hit the date path.
305
+ let hasOverflow = false;
306
+ let overflowBox: BoundingBox | undefined;
307
+ let overflowAnchorId: string | undefined;
308
+ if (beforeRaw.length > 0) {
309
+ let earliestCapX: number | undefined;
310
+ let earliestCapRef: string | undefined;
311
+ for (const ref of beforeRaw) {
312
+ const inlineDate = parseDate(ref);
313
+ const capX = inlineDate
314
+ ? (ctx.scale.forwardWithinDomain(inlineDate) ?? undefined)
315
+ : ctx.entityLeftEdges.get(ref);
316
+ if (capX === undefined) continue;
317
+ if (earliestCapX === undefined || capX < earliestCapX) {
318
+ earliestCapX = capX;
319
+ earliestCapRef = ref;
320
+ }
321
+ }
322
+ if (earliestCapX !== undefined && logicalRight > earliestCapX) {
323
+ hasOverflow = true;
324
+ overflowBox = {
325
+ x: earliestCapX,
326
+ y: cursor.y,
327
+ width: logicalRight - earliestCapX,
328
+ height: ctx.bandScale.bandwidth(),
329
+ };
330
+ overflowAnchorId = earliestCapRef;
331
+ }
332
+ }
333
+
334
+ // Progress fraction
335
+ const statusRaw = propValue(props, 'status');
336
+ const status = statusFromProp(statusRaw);
337
+ let progress = parseProgressFraction(statusRaw);
338
+ if (progress === 0 && status === 'done') progress = 1;
339
+ const remainingPctMatch = /^(\d{1,3})%$/.exec(propValue(props, 'remaining') ?? '');
340
+ if (progress === 0 && status === 'in-progress' && remainingPctMatch) {
341
+ const pct = Math.max(0, Math.min(100, parseInt(remainingPctMatch[1], 10))) / 100;
342
+ progress = 1 - pct;
343
+ }
344
+ if (progress === 0 && status === 'in-progress' && remainingDays > 0 && totalEffortDays > 0) {
345
+ // `remaining:` literal is single-engineer days; `totalEffortDays`
346
+ // is also single-engineer days, so the ratio is unit-correct.
347
+ // Clamp to [0, 1] — the renderer paints 100% remaining when the
348
+ // author overshot, matching the spec's "warn-and-clamp" overflow
349
+ // behavior. (Validation defers the warn to layout-time today; a
350
+ // future diagnostics channel can surface it back to the user.)
351
+ progress = Math.max(0, Math.min(1, 1 - remainingDays / totalEffortDays));
352
+ }
353
+
354
+ // Apply the status-tinted item background when the resolved bg is still
355
+ // theme-default. Authors who set explicit `bg:` keep their override.
356
+ // Per m2d handoff Resolution 3: layout owns this so the renderer stays
357
+ // palette-dumb.
358
+ const STATUS_TINT_LIGHT: Record<StatusKind, string> = {
359
+ done: '#ecfdf5',
360
+ 'in-progress': '#eff6ff',
361
+ 'at-risk': '#fffbeb',
362
+ blocked: '#fee2e2',
363
+ planned: '#f8fafc',
364
+ neutral: '#f8fafc',
365
+ };
366
+ const STATUS_TINT_DARK: Record<StatusKind, string> = {
367
+ done: '#052e16',
368
+ 'in-progress': '#172554',
369
+ 'at-risk': '#422006',
370
+ blocked: '#7f1d1d',
371
+ planned: '#1e293b',
372
+ neutral: '#1e293b',
373
+ };
374
+ const STATUS_TINT_GREY: Record<StatusKind, string> = {
375
+ done: '#ebebeb',
376
+ 'in-progress': '#e4e4e4',
377
+ 'at-risk': '#eeeeee',
378
+ blocked: '#dcdcdc',
379
+ planned: '#f5f5f5',
380
+ neutral: '#f5f5f5',
381
+ };
382
+ const STATUS_BORDER: Record<StatusKind, string> = {
383
+ done: ctx.styleCtx.theme.status.done,
384
+ 'in-progress': ctx.styleCtx.theme.status.inProgress,
385
+ 'at-risk': ctx.styleCtx.theme.status.atRisk,
386
+ blocked: ctx.styleCtx.theme.status.blocked,
387
+ planned: ctx.styleCtx.theme.status.planned,
388
+ neutral: ctx.styleCtx.theme.status.neutral,
389
+ };
390
+ const themeName = ctx.styleCtx.theme.name;
391
+ const isDark = themeName === 'dark';
392
+ const themeDefaultBg = isDark ? '#0f172a' : '#ffffff';
393
+ const themeDefaultFg = themeName === 'grayscale' ? '#9e9e9e' : '#94a3b8';
394
+ if (style.bg === themeDefaultBg) {
395
+ style.bg =
396
+ themeName === 'grayscale'
397
+ ? STATUS_TINT_GREY[status]
398
+ : isDark
399
+ ? STATUS_TINT_DARK[status]
400
+ : STATUS_TINT_LIGHT[status];
401
+ }
402
+ if (style.fg === themeDefaultFg) {
403
+ style.fg = STATUS_BORDER[status];
404
+ }
405
+
406
+ // Pre-format the secondary line shown inside the item bar.
407
+ //
408
+ // Driver-only meta line (`rendering.md` § Item size chip): exactly one
409
+ // leading token — the explicit non-empty `duration:LITERAL` when set,
410
+ // otherwise the size chip when `size:` drives. Never both; bar width
411
+ // already encodes derived calendar span for sized items.
412
+ const explicitDurationLiteral = propValue(props, 'duration');
413
+ const durationDrives =
414
+ !!explicitDurationLiteral && /^\d+(?:\.\d+)?[dwmqy]$/.test(explicitDurationLiteral);
415
+ const sizeChipText = sizeResolved ? (sizeResolved.title ?? sizeResolved.name) : '';
416
+ const driverToken: string | undefined = durationDrives
417
+ ? explicitDurationLiteral
418
+ : sizeChipText || undefined;
419
+ const remainingRaw = propValue(props, 'remaining');
420
+ const remainingLiteral = resolveDurationLiteral(remainingRaw, ctx);
421
+ const ownerDisplay = resolveActorDisplay(ownerOverride ?? propValue(props, 'owner'), ctx);
422
+ const metaHead = (): string => [driverToken, ownerDisplay].filter(Boolean).join(' ');
423
+ let metaText: string | undefined;
424
+ if (status === 'in-progress' && remainingLiteral) {
425
+ const head = metaHead();
426
+ metaText = head
427
+ ? `${head} — ${remainingLiteral} remaining`
428
+ : `${remainingLiteral} remaining`;
429
+ } else if (status === 'in-progress' && progress > 0 && progress < 1) {
430
+ const pct = Math.round((1 - progress) * 100);
431
+ const head = metaHead();
432
+ metaText = head ? `${head} — ${pct}% remaining` : `${pct}% remaining`;
433
+ } else if (ownerDisplay || driverToken) {
434
+ metaText = metaHead() || undefined;
435
+ }
436
+
437
+ // Capacity suffix — appended after metaText at render time. Layout's
438
+ // job here is to (a) parse the value out of `capacity:`, (b) format
439
+ // the display number, (c) resolve `capacity-icon` to either a
440
+ // built-in name or a literal string the renderer can paint directly,
441
+ // and (d) feed the suffix's estimated width into ItemNode so spill
442
+ // detection accounts for `2w 5×` rather than just `2w`. The suffix
443
+ // disappears entirely when capacity is missing or non-positive.
444
+ const capacityRaw = propValue(props, 'capacity');
445
+ const capacityValue = parseCapacityValue(capacityRaw);
446
+ let capacity: PositionedCapacity | null = null;
447
+ let capacityTrailingWidth = 0;
448
+ if (capacityValue !== null) {
449
+ const capacityText = formatCapacityNumber(capacityValue);
450
+ const capacityIcon = resolveCapacityIcon(style.capacityIcon, ctx.symbols);
451
+ capacity = { value: capacityValue, text: capacityText, icon: capacityIcon };
452
+ const META_FONT_SIZE_PX_LOCAL = 11;
453
+ // Add a small leading separator (a single space's worth) only when
454
+ // the suffix sits next to existing meta text, so `m 5×` has air
455
+ // between the driver token and the count. Standalone suffix needs no
456
+ // leading separator.
457
+ const separatorWidth = metaText ? estimateTextWidth(' ', META_FONT_SIZE_PX_LOCAL) : 0;
458
+ capacityTrailingWidth =
459
+ separatorWidth +
460
+ estimateCapacitySuffixWidth(capacityText, capacityIcon, META_FONT_SIZE_PX_LOCAL);
461
+ }
462
+
463
+ // Visual bar + caption-spill decision delegated to ItemNode. Logical
464
+ // extent (used by chaining and `after:` lookups) stays on
465
+ // logicalLeft/logicalRight; ItemNode computes the inset visual box and
466
+ // whether the title+meta line overflows the bar's inner padded width.
467
+ const titleStr = node.title ?? node.name ?? '';
468
+ const placed = new ItemNode({
469
+ id: node.name ?? '',
470
+ title: titleStr,
471
+ logicalLeftX: logicalLeft,
472
+ logicalRightX: logicalRight,
473
+ metaText,
474
+ metaTrailingWidth: capacityTrailingWidth,
475
+ hasLinkIcon,
476
+ }).place({ x: logicalLeft, y: cursor.y }, { time: ctx.scale, bands: ctx.bandScale, style });
477
+ const itemBox = placed.box;
478
+ const bandwidth = ctx.bandScale.bandwidth();
479
+
480
+ // Narrow-bar decoration spill — when a bar is too narrow to host
481
+ // the dot, link icon, or footnote with its full inset, those
482
+ // glyphs render in the same spill column as the (already-
483
+ // spilling) caption text, in reading order
484
+ // `[bar][dot][icon][title][footnote#…][meta]`. Each decoration's
485
+ // threshold is independent (a 20-px-wide bar can host the dot
486
+ // but not the icon, etc.); see the `MIN_BAR_WIDTH_FOR_*`
487
+ // constants in `item-bar-geometry`.
488
+ //
489
+ // Forcing `textSpills` when `iconSpills` keeps the icon and
490
+ // title visually adjacent — otherwise a spilled icon would
491
+ // float at `bar.right + 6` while the title stayed inside the
492
+ // bar, breaking the icon→title affordance.
493
+ const dotSpills = itemBox.width < MIN_BAR_WIDTH_FOR_DOT_PX;
494
+ const iconSpills = hasLinkIcon && itemBox.width < MIN_BAR_WIDTH_FOR_LINK_AND_DOT_PX;
495
+ const footnoteSpillsForNarrow = itemBox.width < MIN_BAR_WIDTH_FOR_FOOTNOTE_PX;
496
+ const textSpills = placed.textSpills || iconSpills;
497
+
498
+ // Label chips lay out left → right at natural text width.
499
+ //
500
+ // INSIDE the bar (chipsOutside === false): single row,
501
+ // left-aligned at the caption inset, anchored just above the
502
+ // bottom progress strip.
503
+ //
504
+ // OUTSIDE the bar (chipsOutside === true): the whole chip set
505
+ // moves to the spill column at `bar.right + 6`. Within the
506
+ // column, chips pack into rows capped at the bar's visual
507
+ // width — see `packSpillChips`. Row 0 sits at the same y the
508
+ // single-row would have used; subsequent rows stack DOWNWARD by
509
+ // one `LABEL_CHIP_ROW_STEP_PX`.
510
+ //
511
+ // When chips spill, the BAR ITSELF GROWS DOWNWARD so the chip
512
+ // column reads as enclosed by the bar — the painted footprint
513
+ // of the bar is `bandwidth + chipBarExtra` and the bottom
514
+ // progress strip moves with the new bottom edge. Chip Y is
515
+ // anchored to the ORIGINAL bandwidth (relative to the bar's
516
+ // top), not to the grown box.height, so row 0 stays where a
517
+ // single-row chip would naturally render and rows 1..N grow
518
+ // downward into the new bar area.
519
+ //
520
+ // When the caption ALSO spills (`textSpills && chipsOutside`),
521
+ // row 0's y drops below the meta baseline so the spilled stack
522
+ // reads `title → meta → chip-row-0 → chip-row-1 → ...` at a
523
+ // single column inside the (now-taller) bar.
524
+ let chipPack: ReturnType<typeof packSpillChips<LabelDeclaration>> | null = null;
525
+ if (chipsOutside) {
526
+ chipPack = packSpillChips(chipSamples, itemBox.width);
527
+ }
528
+ const chipRowCount = chipPack ? chipPack.rows.length : chipSamples.length > 0 ? 1 : 0;
529
+ // Capacity suffix renders on the same line as metaText. Treat the meta
530
+ // line as present whenever EITHER metaText OR a capacity suffix will
531
+ // paint, so chip-row pitch reserves the right amount of vertical space.
532
+ const hasMeta = metaText !== undefined || capacity !== null;
533
+ const chipBarExtra = computeChipBarExtra(
534
+ chipsOutside,
535
+ textSpills,
536
+ chipRowCount,
537
+ bandwidth,
538
+ hasMeta,
539
+ );
540
+ if (chipBarExtra > 0) {
541
+ itemBox.height = bandwidth + chipBarExtra;
542
+ }
543
+
544
+ const labelChips: PositionedLabelChip[] = [];
545
+ const baseChipY =
546
+ itemBox.y +
547
+ bandwidth -
548
+ PROGRESS_STRIP_HEIGHT_PX -
549
+ LABEL_CHIP_HEIGHT_PX -
550
+ LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX;
551
+ const captionStackChipY =
552
+ itemBox.y + ITEM_CAPTION_META_BASELINE_OFFSET_PX + LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX;
553
+ // Inside-bar chips with meta need to clear the meta baseline —
554
+ // the natural `baseChipY` (anchored to bar bottom) sits above
555
+ // the meta line at typical bandwidths, so the chip rect would
556
+ // overlap the meta text vertically. Use whichever Y is lower.
557
+ // Outside-bar chips already reuse `captionStackChipY` when the
558
+ // caption ALSO spills; with caption inside we don't need to
559
+ // shift them since they're horizontally separated from the meta.
560
+ const stackBelowMeta =
561
+ (chipsOutside && textSpills) || (!chipsOutside && hasMeta && chipSamples.length > 0);
562
+ const chipRow0Y = stackBelowMeta ? Math.max(baseChipY, captionStackChipY) : baseChipY;
563
+ const chipStartX = chipsOutside
564
+ ? itemBox.x + itemBox.width + ITEM_CAPTION_SPILL_GAP_PX
565
+ : itemBox.x + ITEM_CAPTION_INSET_X_PX;
566
+
567
+ let chipsRightX = chipStartX;
568
+ if (chipPack) {
569
+ for (let r = 0; r < chipPack.rows.length; r += 1) {
570
+ const rowY = chipRow0Y + r * LABEL_CHIP_ROW_STEP_PX;
571
+ let rowCursorX = chipStartX;
572
+ for (const sample of chipPack.rows[r]) {
573
+ const chip = buildLabelChip(sample.id, ctx.styleCtx, rowCursorX, rowY);
574
+ labelChips.push(chip);
575
+ rowCursorX += chip.box.width + LABEL_CHIP_GAP_BETWEEN_PX;
576
+ }
577
+ const rowRight = rowCursorX - LABEL_CHIP_GAP_BETWEEN_PX;
578
+ if (rowRight > chipsRightX) chipsRightX = rowRight;
579
+ }
580
+ } else {
581
+ let rowCursorX = chipStartX;
582
+ for (const sample of chipSamples) {
583
+ const chip = buildLabelChip(sample.id, ctx.styleCtx, rowCursorX, chipRow0Y);
584
+ labelChips.push(chip);
585
+ rowCursorX += chip.box.width + LABEL_CHIP_GAP_BETWEEN_PX;
586
+ }
587
+ chipsRightX = chipSamples.length > 0 ? rowCursorX - LABEL_CHIP_GAP_BETWEEN_PX : chipStartX;
588
+ }
589
+
590
+ // Footnote superscript indicators. Per `specs/dsl.md`, footnotes
591
+ // attach via the `on:` property on the footnote declaration only —
592
+ // there is no forward `footnote:` property on the host entity. Walk
593
+ // `footnoteHosts` (built from each footnote's `on:` list) and emit
594
+ // a superscript for every footnote that names this item.
595
+ const footnoteIndicatorSet = new Set<number>();
596
+ if (node.name) {
597
+ for (const [fid, hosts] of ctx.footnoteHosts.entries()) {
598
+ if (hosts.includes(node.name)) {
599
+ const n = ctx.footnoteIndex.get(fid);
600
+ if (n !== undefined) footnoteIndicatorSet.add(n);
601
+ }
602
+ }
603
+ }
604
+ const footnoteIndicators = [...footnoteIndicatorSet].sort((a, b) => a - b);
605
+
606
+ const owner = ownerDisplay ?? ownerOverride ?? propValue(props, 'owner');
607
+ const description = node.description?.text;
608
+
609
+ // Footnote glyphs only need to spill when there's at least one
610
+ // indicator AND the bar is too narrow to host them at the inset-
611
+ // right anchor. Compute the final boolean here once we know the
612
+ // indicator count.
613
+ const footnoteSpills = footnoteIndicators.length > 0 && footnoteSpillsForNarrow;
614
+
615
+ // Spill-column x positions for the decorations. The cluster
616
+ // mirrors the in-bar reading order so users see the same visual
617
+ // hierarchy whether everything fits inside or trails off to the
618
+ // right:
619
+ //
620
+ // In-bar (default): [icon] [title] [¹²] [dot]
621
+ // Spilled (narrow): [bar] [icon?] [title][¹²?] [dot?]
622
+ //
623
+ // The dot lives at the trailing edge in BOTH cases — pushing it
624
+ // to the LEFT of the title (with the title trailing it) read as
625
+ // the dot belonging to the next item, not this one. A missing
626
+ // decoration just collapses out of the row; e.g. an item with
627
+ // no link AND a too-narrow bar gives `[bar] [title] [dot]`.
628
+ //
629
+ // `decorationsRightX` is the furthest right edge any spilled
630
+ // glyph reaches; the row-packer uses it (alongside spilled-chip
631
+ // width) to reserve x-extent so the next chained item bumps to
632
+ // a fresh row instead of landing under the spilled cluster.
633
+ const SPILL_COLUMN_X0 = itemBox.x + itemBox.width + ITEM_CAPTION_SPILL_GAP_PX;
634
+ let spillCursor = SPILL_COLUMN_X0;
635
+ // Advance the cursor by `gap` IFF something has already been
636
+ // placed in the column — keeps the cluster from leaving a
637
+ // dangling gap past its final glyph (which would over-reserve
638
+ // x-extent and shift downstream items).
639
+ let needGap = false;
640
+ let iconSpillX: number | null = null;
641
+ if (iconSpills) {
642
+ if (needGap) spillCursor += ITEM_DECORATION_SPILL_GAP_PX;
643
+ iconSpillX = spillCursor;
644
+ spillCursor = iconSpillX + ITEM_LINK_ICON_TILE_SIZE_PX;
645
+ needGap = true;
646
+ }
647
+ let captionSpillWidth = 0;
648
+ if (textSpills) {
649
+ if (needGap) spillCursor += ITEM_DECORATION_SPILL_GAP_PX;
650
+ const titleW = estimateTextWidth(titleStr, ITEM_CAPTION_TITLE_FONT_SIZE_PX);
651
+ // Spill column width is the wider of the title and the *full* meta
652
+ // line (text + capacity suffix). `capacityTrailingWidth` is 0 when
653
+ // no capacity suffix is rendered, so this stays a no-op for items
654
+ // without `capacity:`.
655
+ const metaW = (metaText ? estimateTextWidth(metaText, 11) : 0) + capacityTrailingWidth;
656
+ captionSpillWidth = Math.max(titleW, metaW);
657
+ spillCursor += captionSpillWidth;
658
+ needGap = true;
659
+ }
660
+ let footnoteSpillStartX: number | null = null;
661
+ if (footnoteSpills) {
662
+ if (needGap) spillCursor += ITEM_DECORATION_SPILL_GAP_PX;
663
+ footnoteSpillStartX = spillCursor;
664
+ spillCursor += footnoteIndicators.length * ITEM_FOOTNOTE_INDICATOR_STEP_PX;
665
+ needGap = true;
666
+ }
667
+ let dotSpillCx: number | null = null;
668
+ if (dotSpills) {
669
+ if (needGap) spillCursor += ITEM_DECORATION_SPILL_GAP_PX;
670
+ dotSpillCx = spillCursor + ITEM_STATUS_DOT_RADIUS_PX;
671
+ spillCursor = dotSpillCx + ITEM_STATUS_DOT_RADIUS_PX;
672
+ needGap = true;
673
+ }
674
+ const decorationsRightX = Math.max(itemBox.x + itemBox.width, spillCursor);
675
+
676
+ const id = node.name;
677
+ if (id) {
678
+ // Entity edges live in LOGICAL space so chained items / `after:`
679
+ // references / dependency-arrow attach points sit on the column
680
+ // boundary, not on the visually inset bar edge. The visible 12 px
681
+ // gutter between bars then becomes a clean attach corridor.
682
+ ctx.entityLeftEdges.set(id, logicalLeft);
683
+ ctx.entityRightEdges.set(id, logicalRight);
684
+ ctx.entityMidpoints.set(id, {
685
+ x: (logicalLeft + logicalRight) / 2,
686
+ y: itemBox.y + itemBox.height / 2,
687
+ });
688
+ // Visual edges — where dependency arrows actually attach. These
689
+ // sit ITEM_INSET_PX inside the column boundaries so the arrows
690
+ // emerge from the painted bar edge instead of the inter-column
691
+ // gutter. See LayoutContext.entityVisualLeftX/RightX.
692
+ ctx.entityVisualLeftX.set(id, itemBox.x);
693
+ ctx.entityVisualRightX.set(id, itemBox.x + itemBox.width);
694
+ // Dependency-arrow source point. Default = the bar's right
695
+ // edge at row midpoint. When the caption spills past the
696
+ // bar's right edge (`textSpills`), the spilled title /
697
+ // meta occupy the area immediately right of the bar at
698
+ // row midline. Keep X on the bar's right edge so the
699
+ // arrow visually leaves the bar's side, but drop Y to the
700
+ // vertical center of the bottom progress strip so the
701
+ // arrow runs UNDERNEATH the spilled text rather than
702
+ // through it. Mirrors the slack-arrow attach below.
703
+ const arrowSource: Point = textSpills
704
+ ? {
705
+ x: itemBox.x + itemBox.width,
706
+ y: itemBox.y + itemBox.height - PROGRESS_STRIP_HEIGHT_PX / 2,
707
+ }
708
+ : {
709
+ x: itemBox.x + itemBox.width,
710
+ y: itemBox.y + itemBox.height / 2,
711
+ };
712
+ ctx.itemArrowSource.set(id, arrowSource);
713
+ ctx.itemFlowKey.set(id, ctx.currentFlowKey);
714
+ // Slack-arrow attach Y. Defaults to the bar's row midpoint; when
715
+ // the caption spills past the bar's right edge, drop to the
716
+ // progress-strip's vertical center so the arrow aligns with the
717
+ // bottom-edge progress bar instead of running through the
718
+ // adjacent title/meta text. The `/ 2` keeps the attach point on
719
+ // the strip's vertical center if `PROGRESS_STRIP_HEIGHT_PX` is
720
+ // ever bumped.
721
+ const slackAttachY = textSpills
722
+ ? itemBox.y + itemBox.height - PROGRESS_STRIP_HEIGHT_PX / 2
723
+ : itemBox.y + itemBox.height / 2;
724
+ ctx.itemSlackAttachY.set(id, slackAttachY);
725
+ }
726
+
727
+ cursor.x = logicalRight;
728
+ cursor.maxX = Math.max(cursor.maxX, cursor.x);
729
+ // The next row in a parallel/group/lane starts at
730
+ // `cursor.y + cursor.height`. Default pitch is `bandScale.step()`
731
+ // (bandwidth + inter-row gap). When the bar grew to enclose a
732
+ // spilled chip column, the pitch grows by the SAME amount so the
733
+ // inter-row gap stays constant — the next row's bar starts
734
+ // `step − bandwidth` px below the (now-taller) bar bottom.
735
+ cursor.height = Math.max(cursor.height, ctx.bandScale.step() + chipBarExtra);
736
+
737
+ const inlineDatePins = computeItemInlineDatePins({
738
+ box: itemBox,
739
+ afterDate: pickInlineDate(afterRaw),
740
+ beforeDate: pickInlineDate(beforeRaw),
741
+ hasLinkIcon,
742
+ footnoteCount: footnoteIndicators.length,
743
+ });
744
+
745
+ const result: PositionedItem = {
746
+ kind: 'item',
747
+ id,
748
+ title: titleStr,
749
+ box: itemBox,
750
+ status,
751
+ progressFraction: progress,
752
+ footnoteIndicators,
753
+ labelChips,
754
+ chipsOutside,
755
+ chipsRightX,
756
+ linkIcon: linkInfo.icon,
757
+ linkHref: linkInfo.href,
758
+ hasOverflow,
759
+ overflowBox,
760
+ overflowAnchorId,
761
+ owner,
762
+ description,
763
+ metaText,
764
+ textSpills,
765
+ dotSpills,
766
+ iconSpills,
767
+ footnoteSpills,
768
+ dotSpillCx,
769
+ iconSpillX,
770
+ footnoteSpillStartX,
771
+ decorationsRightX,
772
+ capacity,
773
+ size: sizeResolved,
774
+ style,
775
+ inlineDatePins: inlineDatePins.length > 0 ? inlineDatePins : undefined,
776
+ };
777
+ return result;
778
+ }
779
+
780
+ function sequenceParallel(
781
+ node: ParallelBlock,
782
+ cursor: TrackCursor,
783
+ ctx: LayoutContext,
784
+ ): PositionedParallel {
785
+ return new ParallelNode(node, { sequenceOne, newCursor }).place(cursor, ctx);
786
+ }
787
+
788
+ function sequenceGroup(node: GroupBlock, cursor: TrackCursor, ctx: LayoutContext): PositionedGroup {
789
+ return new GroupNode(node, {
790
+ sequenceItem,
791
+ sequenceOne,
792
+ resolveChildStart,
793
+ newCursor,
794
+ estimateTextWidth,
795
+ predictItemChipExtraHeight,
796
+ }).place(cursor, ctx);
797
+ }
798
+
799
+ function sequenceOne(
800
+ node: ItemDeclaration | GroupBlock | ParallelBlock,
801
+ cursor: TrackCursor,
802
+ ctx: LayoutContext,
803
+ ): PositionedTrackChild {
804
+ if (isItemDeclaration(node)) return sequenceItem(node, cursor, ctx);
805
+ if (isParallelBlock(node)) return sequenceParallel(node, cursor, ctx);
806
+ if (isGroupBlock(node)) return sequenceGroup(node, cursor, ctx);
807
+ throw new Error(
808
+ `Unknown swimlane child type: ${(node as { $type?: string }).$type ?? 'unknown'}`,
809
+ );
810
+ }
811
+
812
+ // Rough px-width estimate for sans-serif text. Intentionally pessimistic
813
+ // (uses ~0.58 em per char) so we err toward "doesn't fit" and trigger a
814
+ // row bump rather than draw an item with a clipped title.
815
+ function estimateTextWidth(text: string, fontSize: number): number {
816
+ return text.length * fontSize * 0.58;
817
+ }
818
+
819
+ /**
820
+ * Compute the extra vertical px the bar grows when its spilled chip
821
+ * column would otherwise extend below the (single-row) bottom. The
822
+ * bar's painted footprint becomes `bandwidth + chipBarExtra`, the
823
+ * progress strip rides the new bottom, and chip rows pack inside
824
+ * the taller bar (anchored from the bar TOP so row 0 doesn't shift
825
+ * when the bar grows).
826
+ *
827
+ * Returns 0 when chips fit inside the bar, when there are no chips,
828
+ * or when the spilled column happens to fit inside `bandwidth` (a
829
+ * single row with the caption inside, for instance).
830
+ *
831
+ * The same number is the row-pitch increase the swimlane / group
832
+ * row-packer needs to reserve so the next row clears the taller
833
+ * bar — `cursor.height = step + chipBarExtra` and the predict
834
+ * helper returns this verbatim.
835
+ */
836
+ function computeChipBarExtra(
837
+ chipsOutside: boolean,
838
+ captionSpills: boolean,
839
+ chipRowCount: number,
840
+ bandwidth: number,
841
+ hasMeta: boolean,
842
+ ): number {
843
+ if (chipRowCount === 0) return 0;
844
+ // Row 0 anchor relative to the bar's TOP — three regimes:
845
+ //
846
+ // 1. chipsOutside + captionSpills → chips stack below the
847
+ // spilled meta line (`captionStackTop`).
848
+ // 2. chips INSIDE the bar AND meta is present → chip top must
849
+ // clear the meta baseline; the natural `baseTop` sits
850
+ // ABOVE the meta line at default bandwidth (=56), so we
851
+ // take whichever is lower of base/captionStack.
852
+ // 3. otherwise (in-bar w/o meta, or chipsOutside w/o caption
853
+ // spill) → row 0 hugs the bar bottom at `baseTop`.
854
+ //
855
+ // Cases (2) and (3-with-multi-row-spill) can both grow the bar;
856
+ // case (3-with-single-row-inside-no-meta) never grows.
857
+ const baseTop =
858
+ bandwidth -
859
+ PROGRESS_STRIP_HEIGHT_PX -
860
+ LABEL_CHIP_HEIGHT_PX -
861
+ LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX;
862
+ const captionStackTop =
863
+ ITEM_CAPTION_META_BASELINE_OFFSET_PX + LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX;
864
+ let chipRow0Top: number;
865
+ if (chipsOutside && captionSpills) {
866
+ chipRow0Top = captionStackTop;
867
+ } else if (!chipsOutside && hasMeta) {
868
+ chipRow0Top = Math.max(baseTop, captionStackTop);
869
+ } else {
870
+ chipRow0Top = baseTop;
871
+ }
872
+ const lastRowBottomTop =
873
+ chipRow0Top + (chipRowCount - 1) * LABEL_CHIP_ROW_STEP_PX + LABEL_CHIP_HEIGHT_PX;
874
+ // The bar must be tall enough to fit `lastRowBottomTop` plus a
875
+ // GAP above the progress strip plus the progress strip itself.
876
+ const requiredHeight =
877
+ lastRowBottomTop + LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX + PROGRESS_STRIP_HEIGHT_PX;
878
+ return Math.max(0, requiredHeight - bandwidth);
879
+ }
880
+
881
+ /**
882
+ * Predict an item's bar growth (and therefore row-pitch growth) for
883
+ * a multi-row spilled chip column BEFORE the bar is sequenced. Used
884
+ * by the swimlane / group row-packer so neighboring rows on later
885
+ * rows are positioned correctly without a retroactive shift.
886
+ *
887
+ * Mirrors the chip-pack + caption-spill arithmetic in
888
+ * `sequenceItem` so prediction and placement agree byte-for-byte.
889
+ */
890
+ function predictItemChipExtraHeight(item: ItemDeclaration, ctx: LayoutContext): number {
891
+ const props = item.properties;
892
+ const labelIds = propValues(props, 'labels');
893
+ if (labelIds.length === 0) return 0;
894
+ const durationDays = deriveItemDurationDays(props, ctx.sizes, ctx.cal);
895
+ const naturalWidth = Math.max(MIN_ITEM_WIDTH, durationDays * ctx.timeline.pixelsPerDay);
896
+ const visualWidth = Math.max(MIN_ITEM_WIDTH, naturalWidth - 2 * ITEM_INSET_PX);
897
+ const samples: { id: LabelDeclaration; width: number }[] = [];
898
+ for (const labelId of labelIds) {
899
+ const label = ctx.labels.get(labelId);
900
+ if (!label) continue;
901
+ const sample = buildLabelChip(label, ctx.styleCtx, 0, 0);
902
+ samples.push({ id: label, width: sample.box.width });
903
+ }
904
+ if (samples.length === 0) return 0;
905
+ let chipRowWidth = 0;
906
+ for (let i = 0; i < samples.length; i += 1) {
907
+ if (i > 0) chipRowWidth += LABEL_CHIP_GAP_BETWEEN_PX;
908
+ chipRowWidth += samples[i].width;
909
+ }
910
+ const insideAvail = Math.max(0, visualWidth - 2 * ITEM_CAPTION_INSET_X_PX);
911
+ const chipsOutside = chipRowWidth > insideAvail;
912
+
913
+ // `hasMeta` mirrors `metaText !== undefined` in `sequenceItem`,
914
+ // plus the capacity suffix (which renders on the same meta line).
915
+ // metaText is set whenever an item declares a duration, owner,
916
+ // or remaining — so we just check those four props. Status
917
+ // strings (in-progress) only matter when paired with one of
918
+ // these, so this is an upper bound (false-positives still grow
919
+ // the bar by exactly the same amount as the renderer would, so
920
+ // they stay byte-stable).
921
+ const hasMeta =
922
+ propValue(props, 'duration') !== undefined ||
923
+ propValue(props, 'size') !== undefined ||
924
+ propValue(props, 'owner') !== undefined ||
925
+ propValue(props, 'remaining') !== undefined ||
926
+ propValue(props, 'capacity') !== undefined;
927
+
928
+ const titleStr = item.title ?? item.name ?? '';
929
+ const titleW = titleStr ? estimateTextWidth(titleStr, ITEM_CAPTION_TITLE_FONT_SIZE_PX) : 0;
930
+ const captionSpills = titleW > insideAvail;
931
+ const pack = chipsOutside ? packSpillChips(samples, visualWidth) : null;
932
+ const chipRowCount = pack ? pack.rows.length : samples.length > 0 ? 1 : 0;
933
+ return computeChipBarExtra(
934
+ chipsOutside,
935
+ captionSpills,
936
+ chipRowCount,
937
+ ctx.bandScale.bandwidth(),
938
+ hasMeta,
939
+ );
940
+ }
941
+
942
+ // Resolve the desired startX for a swimlane child, honoring `date:` (fixed
943
+ // pin) > `start:` (fixed pin) > `after:` (chain after refs) > sequential
944
+ // default (continue from `seqDefault`, which is the lane's rightmost time
945
+ // cursor across all rows).
946
+ //
947
+ // `after:` accepts both entity ids (looked up in `ctx.entityRightEdges`) and
948
+ // inline ISO date literals (looked up via `ctx.scale.forwardWithinDomain`).
949
+ // The validator already enforces "at most one inline date per direction" so
950
+ // at most one element in the list will hit the date path.
951
+ function resolveChildStart(
952
+ props: EntityProperty[],
953
+ seqDefault: number,
954
+ laneLeftX: number,
955
+ ctx: LayoutContext,
956
+ ): number {
957
+ const explicitDate =
958
+ parseDate(propValue(props, 'date')) ?? parseDate(propValue(props, 'start'));
959
+ if (explicitDate) {
960
+ const xd = ctx.scale.forwardWithinDomain(explicitDate);
961
+ if (xd !== null) return xd;
962
+ }
963
+ const afterRefs = propValues(props, 'after');
964
+ if (afterRefs.length > 0) {
965
+ let maxEnd = laneLeftX;
966
+ for (const ref of afterRefs) {
967
+ const inlineDate = parseDate(ref);
968
+ if (inlineDate) {
969
+ const xd = ctx.scale.forwardWithinDomain(inlineDate);
970
+ if (xd !== null) maxEnd = Math.max(maxEnd, xd);
971
+ continue;
972
+ }
973
+ const endX = ctx.entityRightEdges.get(ref);
974
+ if (endX !== undefined) maxEnd = Math.max(maxEnd, endX);
975
+ }
976
+ return Math.max(laneLeftX, maxEnd);
977
+ }
978
+ return seqDefault;
979
+ }
980
+
981
+ function _buildSwimlane(
982
+ lane: SwimlaneDeclaration,
983
+ y: number,
984
+ bandIndex: number,
985
+ ctx: LayoutContext,
986
+ ): { positioned: PositionedSwimlane; usedHeight: number } {
987
+ return new SwimlaneNode(
988
+ { lane, bandIndex },
989
+ {
990
+ sequenceItem,
991
+ sequenceOne,
992
+ resolveChildStart,
993
+ newCursor,
994
+ estimateTextWidth,
995
+ predictItemChipExtraHeight,
996
+ },
997
+ ).place({ x: ctx.timeline.originX, y }, ctx);
998
+ }
999
+
1000
+ // Card-sizing constants for beside-mode headers live in
1001
+ // `header-card-geometry.ts` so the renderer can paint with the same
1002
+ // numbers the layout sized against. Title and author both wrap at
1003
+ // MAX_CONTENT_WIDTH (= MAX header width minus 2 * padding). The card
1004
+ // hugs its content in the MIN..MAX range and grows vertically when
1005
+ // wrapping is needed.
1006
+ //
1007
+ // Left margin between the canvas's left edge and the visible card. The
1008
+ // matching right-side breathing room is owned by `GUTTER_PX` (the canonical
1009
+ // content gutter, applied between `chartLeftX` and `originX`), so the gap
1010
+ // from the card's right edge to the timeline strip is the same as the gap
1011
+ // between two adjacent items.
1012
+ const HEADER_CARD_OUTER_PAD = 6;
1013
+
1014
+ interface SizedHeader {
1015
+ titleLines: string[];
1016
+ authorLines: string[];
1017
+ cardWidth: number;
1018
+ cardHeight: number;
1019
+ boxWidth: number;
1020
+ }
1021
+
1022
+ // Word-wrap `text` so that no line wider than `maxWidth` (in px). Long single
1023
+ // words are kept on their own line even if they overflow — we never split a
1024
+ // word in the middle.
1025
+ function wrapText(text: string, maxWidth: number, fontSize: number): string[] {
1026
+ if (!text) return [];
1027
+ const words = text.split(/\s+/).filter((w) => w.length > 0);
1028
+ if (words.length === 0) return [];
1029
+ const lines: string[] = [];
1030
+ let cur = '';
1031
+ for (const word of words) {
1032
+ const trial = cur ? `${cur} ${word}` : word;
1033
+ if (cur && estimateTextWidth(trial, fontSize) > maxWidth) {
1034
+ lines.push(cur);
1035
+ cur = word;
1036
+ } else {
1037
+ cur = trial;
1038
+ }
1039
+ }
1040
+ if (cur) lines.push(cur);
1041
+ return lines;
1042
+ }
1043
+
1044
+ function sizeBesideHeader(title: string, author: string | undefined): SizedHeader {
1045
+ const maxContentWidth = HEADER_BESIDE_MAX_WIDTH_PX - 2 * HEADER_CARD_PADDING_X;
1046
+ const titleLines = wrapText(title, maxContentWidth, HEADER_TITLE_FONT_SIZE_PX);
1047
+ const authorLines = wrapText(author ?? '', maxContentWidth, HEADER_AUTHOR_FONT_SIZE_PX);
1048
+
1049
+ let widest = 0;
1050
+ for (const line of titleLines)
1051
+ widest = Math.max(widest, estimateTextWidth(line, HEADER_TITLE_FONT_SIZE_PX));
1052
+ for (const line of authorLines)
1053
+ widest = Math.max(widest, estimateTextWidth(line, HEADER_AUTHOR_FONT_SIZE_PX));
1054
+
1055
+ const naturalCardWidth = widest + 2 * HEADER_CARD_PADDING_X;
1056
+ // `HEADER_BESIDE_{MIN,MAX}_WIDTH_PX` bound the **boxWidth** (= cardWidth
1057
+ // + left outer pad). Subtract one outer pad to derive the cardWidth
1058
+ // bounds.
1059
+ const cardWidth = Math.max(
1060
+ HEADER_BESIDE_MIN_WIDTH_PX - HEADER_CARD_OUTER_PAD,
1061
+ Math.min(HEADER_BESIDE_MAX_WIDTH_PX - HEADER_CARD_OUTER_PAD, naturalCardWidth),
1062
+ );
1063
+
1064
+ const titleBlockHeight =
1065
+ titleLines.length > 0 ? (titleLines.length - 1) * HEADER_TITLE_LINE_HEIGHT_PX : 0;
1066
+ const authorBlockHeight =
1067
+ authorLines.length > 0
1068
+ ? HEADER_TITLE_TO_AUTHOR_GAP_PX +
1069
+ (authorLines.length - 1) * HEADER_AUTHOR_LINE_HEIGHT_PX
1070
+ : 0;
1071
+ const cardHeight =
1072
+ HEADER_CARD_PADDING_TOP + titleBlockHeight + authorBlockHeight + HEADER_CARD_PADDING_BOTTOM;
1073
+
1074
+ // `boxWidth` only includes the LEFT outer pad — the right-side breathing
1075
+ // room between the card and the chart is owned by `GUTTER_PX` in
1076
+ // `RoadmapNode`. So `boxWidth` doubles as `chartLeftX` (the card's right
1077
+ // edge in canvas coordinates).
1078
+ const boxWidth = cardWidth + HEADER_CARD_OUTER_PAD;
1079
+ return { titleLines, authorLines, cardWidth, cardHeight, boxWidth };
1080
+ }
1081
+
1082
+ // Compute a sensible [startDate, endDate] window.
1083
+ //
1084
+ // Precedence:
1085
+ // 1. Explicit `length:` on the roadmap declaration wins.
1086
+ // 2. Otherwise we derive the end day from the actual content extent
1087
+ // (latest item end, anchor date, milestone date/after, and today's
1088
+ // now-line if it falls past the content). This keeps the rendered
1089
+ // chart from defaulting to a 180-day desert when the content only
1090
+ // spans a few weeks.
1091
+ // 3. As a last resort (no content + no length), fall back to a small
1092
+ // 4-week placeholder so an empty roadmap still draws a sensible axis.
1093
+ function computeDateWindow(
1094
+ file: NowlineFile,
1095
+ ctx: {
1096
+ cal: import('./calendar.js').CalendarConfig;
1097
+ sizes: Map<string, import('./types.js').ResolvedSize>;
1098
+ },
1099
+ resolved: ResolveResult,
1100
+ today: Date | undefined,
1101
+ scale: ViewPreset,
1102
+ ): { startDate: Date; endDate: Date } {
1103
+ const roadmap = file.roadmapDecl;
1104
+ const props = roadmap?.properties ?? [];
1105
+ const startRaw = propValue(props, 'start');
1106
+ // Spec (`specs/dsl.md`): "A roadmap with no `start:` and no dates is
1107
+ // purely relative — renderers choose their own reference date (e.g.
1108
+ // the day of rendering)." Use the caller's `today` (the resolved
1109
+ // `--now`) when present so the start lines up with the now-line, and
1110
+ // fall back to actual today's UTC midnight otherwise. Either default
1111
+ // is dangerous — output drifts day-to-day — but it's strictly better
1112
+ // than the legacy "Jan 1 of the current year" fallback that drifted
1113
+ // every January 1 by 365 days at once. Authors should set `start:`
1114
+ // for any roadmap they want to be reproducible.
1115
+ const startDate = parseDate(startRaw) ?? defaultStartDate(today);
1116
+ const lengthRaw = propValue(props, 'length');
1117
+ if (lengthRaw) {
1118
+ const days = literalDays(lengthRaw, ctx.cal);
1119
+ if (days > 0) {
1120
+ return { startDate, endDate: addDays(startDate, days) };
1121
+ }
1122
+ }
1123
+ const contentDays = computeContentEndDay(resolved, ctx, startDate, today);
1124
+ const tickDays = daysPerUnit(scale.unit, ctx.cal);
1125
+ // Round up to the smallest tick boundary that is `>= contentDays`. When
1126
+ // the latest content lands exactly on a tick boundary the chart ends
1127
+ // exactly there (no extra trailing tick); otherwise we extend to the
1128
+ // next tick so the right edge always sits on a labelled column.
1129
+ const padded =
1130
+ contentDays > 0 ? Math.ceil(contentDays / tickDays) * tickDays : 4 * ctx.cal.daysPerWeek;
1131
+ return { startDate, endDate: addDays(startDate, Math.max(1, padded)) };
1132
+ }
1133
+
1134
+ /**
1135
+ * Reference date used when a roadmap omits `start:`. Prefers the
1136
+ * caller-supplied `today` (UTC midnight already, when it comes from
1137
+ * the CLI); falls back to actual today's UTC midnight so the layout
1138
+ * still produces a valid window for direct API callers that don't pass
1139
+ * a `today`. Date components are taken in UTC to match `parseDate`.
1140
+ */
1141
+ function defaultStartDate(today: Date | undefined): Date {
1142
+ const ref = today ?? new Date();
1143
+ return new Date(Date.UTC(ref.getUTCFullYear(), ref.getUTCMonth(), ref.getUTCDate()));
1144
+ }
1145
+
1146
+ function literalDays(literal: string, cal: import('./calendar.js').CalendarConfig): number {
1147
+ const m = /^(\d+(?:\.\d+)?)([dwmqy])$/.exec(literal);
1148
+ if (!m) return 0;
1149
+ const n = parseFloat(m[1]);
1150
+ switch (m[2]) {
1151
+ case 'd':
1152
+ return n;
1153
+ case 'w':
1154
+ return n * cal.daysPerWeek;
1155
+ case 'm':
1156
+ return n * cal.daysPerMonth;
1157
+ case 'q':
1158
+ return n * cal.daysPerQuarter;
1159
+ case 'y':
1160
+ return n * cal.daysPerYear;
1161
+ default:
1162
+ return 0;
1163
+ }
1164
+ }
1165
+
1166
+ // Walk every dated/sequenced entity in the resolved content and return the
1167
+ // latest day-offset from `startDate`. Mirrors the sequencer's start-rules
1168
+ // (date: > start: > after: > previous-in-lane) without producing positions.
1169
+ function computeContentEndDay(
1170
+ resolved: ResolveResult,
1171
+ ctx: {
1172
+ cal: import('./calendar.js').CalendarConfig;
1173
+ sizes: Map<string, import('./types.js').ResolvedSize>;
1174
+ },
1175
+ startDate: Date,
1176
+ today: Date | undefined,
1177
+ ): number {
1178
+ const itemEnd = new Map<string, number>();
1179
+ const anchorEnd = new Map<string, number>();
1180
+ const milestoneEnd = new Map<string, number>();
1181
+ let maxDay = 0;
1182
+
1183
+ // Resolve a single `after:` element to a day-offset from `startDate`.
1184
+ // The element is either an entity id (looked up in itemEnd / anchorEnd /
1185
+ // milestoneEnd) or an inline ISO date literal (converted directly via
1186
+ // `daysBetween`). The validator already enforces "at most one inline date
1187
+ // per direction", so at most one element per list will hit the date path.
1188
+ const resolveAfterDay = (ref: string): number => {
1189
+ const inlineDate = parseDate(ref);
1190
+ if (inlineDate) return daysBetween(startDate, inlineDate);
1191
+ if (itemEnd.has(ref)) return itemEnd.get(ref)!;
1192
+ if (anchorEnd.has(ref)) return anchorEnd.get(ref)!;
1193
+ if (milestoneEnd.has(ref)) return milestoneEnd.get(ref)!;
1194
+ return 0;
1195
+ };
1196
+
1197
+ // Pre-seed anchors fixed by `date:` so items that reference them get a
1198
+ // valid end-day during the lane walk.
1199
+ for (const anchor of resolved.content.anchors.values()) {
1200
+ const d = parseDate(propValue(anchor.properties, 'date'));
1201
+ if (d && anchor.name) {
1202
+ const day = daysBetween(startDate, d);
1203
+ anchorEnd.set(anchor.name, day);
1204
+ maxDay = Math.max(maxDay, day);
1205
+ }
1206
+ }
1207
+
1208
+ const walkLane = (children: SwimlaneDeclaration['content'], baselineEnd: number): number => {
1209
+ let prevEnd = baselineEnd;
1210
+ for (const child of children) {
1211
+ if (child.$type === 'DescriptionDirective') continue;
1212
+ prevEnd = walkNode(child as ItemDeclaration | GroupBlock | ParallelBlock, prevEnd);
1213
+ maxDay = Math.max(maxDay, prevEnd);
1214
+ }
1215
+ return prevEnd;
1216
+ };
1217
+
1218
+ const walkNode = (
1219
+ node: ItemDeclaration | GroupBlock | ParallelBlock,
1220
+ prevEnd: number,
1221
+ ): number => {
1222
+ if (isItemDeclaration(node)) {
1223
+ const dur = deriveItemDurationDays(node.properties, ctx.sizes, ctx.cal);
1224
+ const dateProp = parseDate(propValue(node.properties, 'date'));
1225
+ const startProp = parseDate(propValue(node.properties, 'start'));
1226
+ const afterRefs = propValues(node.properties, 'after');
1227
+ let start = prevEnd;
1228
+ if (dateProp) {
1229
+ start = daysBetween(startDate, dateProp);
1230
+ } else if (startProp) {
1231
+ start = daysBetween(startDate, startProp);
1232
+ } else if (afterRefs.length > 0) {
1233
+ start = Math.max(prevEnd, ...afterRefs.map(resolveAfterDay));
1234
+ }
1235
+ const end = start + dur;
1236
+ if (node.name) itemEnd.set(node.name, end);
1237
+ return end;
1238
+ }
1239
+ if (isParallelBlock(node)) {
1240
+ // All children share the parallel's start; the block's effective
1241
+ // end is the maximum child end. The parallel's own `after:`
1242
+ // (including inline-date pins) widens that shared start.
1243
+ const afterRefs = propValues(node.properties, 'after');
1244
+ const containerStart =
1245
+ afterRefs.length > 0
1246
+ ? Math.max(prevEnd, ...afterRefs.map(resolveAfterDay))
1247
+ : prevEnd;
1248
+ let parallelEnd = containerStart;
1249
+ for (const child of node.content) {
1250
+ if (child.$type === 'DescriptionDirective') continue;
1251
+ const childEnd = walkNode(child as ItemDeclaration | GroupBlock, containerStart);
1252
+ parallelEnd = Math.max(parallelEnd, childEnd);
1253
+ }
1254
+ return parallelEnd;
1255
+ }
1256
+ if (isGroupBlock(node)) {
1257
+ // The group's own `after:` (including inline-date pins) widens
1258
+ // the baseline before walking the inner sequential lane.
1259
+ const afterRefs = propValues(node.properties, 'after');
1260
+ const containerStart =
1261
+ afterRefs.length > 0
1262
+ ? Math.max(prevEnd, ...afterRefs.map(resolveAfterDay))
1263
+ : prevEnd;
1264
+ return walkLane(node.content as SwimlaneDeclaration['content'], containerStart);
1265
+ }
1266
+ return prevEnd;
1267
+ };
1268
+
1269
+ for (const lane of resolved.content.swimlanes.values()) {
1270
+ walkLane(lane.content, 0);
1271
+ }
1272
+
1273
+ // Milestones (after items so `after:` references can resolve).
1274
+ for (const ms of resolved.content.milestones.values()) {
1275
+ const d = parseDate(propValue(ms.properties, 'date'));
1276
+ if (d) {
1277
+ const day = daysBetween(startDate, d);
1278
+ if (ms.name) milestoneEnd.set(ms.name, day);
1279
+ maxDay = Math.max(maxDay, day);
1280
+ continue;
1281
+ }
1282
+ const after = propValues(ms.properties, 'after');
1283
+ if (after.length > 0) {
1284
+ // Milestones disallow inline date literals at the validator
1285
+ // level (rule 24a — milestones already have `date:`). The
1286
+ // shared `resolveAfterDay` helper still handles such input
1287
+ // gracefully if it slips past, treating the date as the
1288
+ // milestone's effective day.
1289
+ const day = Math.max(0, ...after.map(resolveAfterDay));
1290
+ if (ms.name) milestoneEnd.set(ms.name, day);
1291
+ maxDay = Math.max(maxDay, day);
1292
+ }
1293
+ }
1294
+
1295
+ // Isolated includes contribute their own content extent against the
1296
+ // shared timeline.
1297
+ for (const region of resolved.content.isolatedRegions) {
1298
+ const nestedMax = computeContentEndDay(
1299
+ {
1300
+ config: region.config,
1301
+ content: region.content,
1302
+ diagnostics: [],
1303
+ processedFiles: new Set(),
1304
+ },
1305
+ ctx,
1306
+ startDate,
1307
+ undefined,
1308
+ );
1309
+ maxDay = Math.max(maxDay, nestedMax);
1310
+ }
1311
+
1312
+ if (today) {
1313
+ const t = daysBetween(startDate, today);
1314
+ if (t > 0) maxDay = Math.max(maxDay, t);
1315
+ }
1316
+
1317
+ return maxDay;
1318
+ }
1319
+
1320
+ function buildDependencies(
1321
+ items: Map<string, ItemDeclaration>,
1322
+ swimlanes: PositionedSwimlane[],
1323
+ includes: PositionedIncludeRegion[],
1324
+ ctx: LayoutContext,
1325
+ ): PositionedDependencyEdge[] {
1326
+ // m2g+: collect every painted item bar + every visible parallel /
1327
+ // group bracket once, hand to the channel router so it can drop
1328
+ // vertical legs in clean inter-column gutters and nudge away from
1329
+ // bracket strokes that would otherwise be hugged.
1330
+ const grid = new ChannelGrid(collectRoutingObstacles(swimlanes, includes));
1331
+
1332
+ interface Pending {
1333
+ fromId: string;
1334
+ toId: string;
1335
+ }
1336
+ const requests: EdgeRouteRequest[] = [];
1337
+ const pending: Pending[] = [];
1338
+
1339
+ for (const [id, item] of items) {
1340
+ const afters = propValues(item.properties, 'after');
1341
+ const targetMid = ctx.entityMidpoints.get(id);
1342
+ const targetVisualLeftX = ctx.entityVisualLeftX.get(id);
1343
+ if (!targetMid || targetVisualLeftX === undefined) continue;
1344
+ // The arrow always TERMINATES at the target item's left
1345
+ // visual edge, vertically centered on the bar.
1346
+ const targetPoint: Point = { x: targetVisualLeftX, y: targetMid.y };
1347
+ for (const pred of afters) {
1348
+ // Source point depends on what kind of predecessor `pred`
1349
+ // is. For ITEMS we use the per-item arrow source point
1350
+ // — (visualRight, midY) by default, dropping to
1351
+ // (visualRight, bar.bottom - PROGRESS_STRIP_HEIGHT/2) when
1352
+ // the caption spilled past the right edge so the arrow
1353
+ // exits below the spilled title / meta text. For ANCHORS
1354
+ // / MILESTONES we attach to the marker's vertical CUT
1355
+ // LINE at the TARGET item's row mid-Y — the dashed/solid
1356
+ // cut line already drops through the chart and reads as
1357
+ // the arrow's stem, so a short horizontal stub from the
1358
+ // line into the bar's left edge is the cleanest
1359
+ // connection.
1360
+ const itemSource = ctx.itemArrowSource.get(pred);
1361
+ let from: Point;
1362
+ const isMarkerPred = itemSource === undefined;
1363
+ if (itemSource) {
1364
+ from = itemSource;
1365
+ } else {
1366
+ const markerMid = ctx.entityMidpoints.get(pred);
1367
+ if (!markerMid) continue;
1368
+ from = { x: markerMid.x, y: targetMid.y };
1369
+ }
1370
+ // Skip same-row contiguous chains for ITEM → ITEM only:
1371
+ // when the target sits immediately to the right of the
1372
+ // source on the same row, the spatial flow already
1373
+ // conveys ordering and an arrow is redundant noise.
1374
+ // MARKER → ITEM stubs always draw (the cut line is the
1375
+ // stem; the short stub completes the visual connection
1376
+ // even when the bar is right next to the cut line).
1377
+ if (
1378
+ !isMarkerPred &&
1379
+ Math.abs(from.y - targetPoint.y) < 0.5 &&
1380
+ targetPoint.x - from.x < 20
1381
+ ) {
1382
+ continue;
1383
+ }
1384
+ requests.push({
1385
+ fromId: pred,
1386
+ toId: id,
1387
+ from,
1388
+ to: targetPoint,
1389
+ isMarkerSource: isMarkerPred,
1390
+ });
1391
+ pending.push({ fromId: pred, toId: id });
1392
+ }
1393
+ }
1394
+
1395
+ const routed = routeChannelEdges(requests, grid);
1396
+ const out: PositionedDependencyEdge[] = [];
1397
+ for (let i = 0; i < routed.length; i++) {
1398
+ const r = routed[i];
1399
+ out.push({
1400
+ fromId: pending[i].fromId,
1401
+ toId: pending[i].toId,
1402
+ waypoints: r.waypoints,
1403
+ kind: r.underBar ? 'underBar' : 'normal',
1404
+ style: resolveStyle('item', [], ctx.styleCtx),
1405
+ });
1406
+ }
1407
+ return out;
1408
+ }
1409
+
1410
+ function buildNowline(
1411
+ today: Date | undefined,
1412
+ ctx: LayoutContext,
1413
+ locale: string,
1414
+ ): PositionedNowline | null {
1415
+ if (!today) return null;
1416
+ const x = ctx.scale.forwardWithinDomain(today);
1417
+ if (x === null) return null;
1418
+ // Pill row is the band reserved at the very top of the timeline area.
1419
+ // The line drops from the bottom of the pill (top of the date headers)
1420
+ // through any marker row and into the chart, so the pill and line stay
1421
+ // visually connected.
1422
+ const pillTopY = ctx.timeline.box.y;
1423
+ const lineTopY = ctx.timeline.tickPanelY;
1424
+ // Pill width is locale-aware: en-US's `'now'` (3 chars) fits the
1425
+ // 36 px default trivially; longer strings like fr's `'maint.'`
1426
+ // (6 chars) need a wider pill to avoid clipping. Floor at the
1427
+ // default so en-US output stays byte-stable; grow when the
1428
+ // measured label needs it. The 2× inset matches the renderer's
1429
+ // flag-mode label inset (`NOW_PILL_LABEL_INSET_X_PX`) and gives
1430
+ // the same visual padding to centered-mode strings.
1431
+ const label = localeStrings(locale).nowLabel;
1432
+ const labelTextWidth = estimateTextWidth(label, NOW_PILL_LABEL_FONT_SIZE_PX);
1433
+ const pillWidth = Math.max(
1434
+ NOW_PILL_WIDTH_PX,
1435
+ Math.ceil(labelTextWidth + 2 * NOW_PILL_LABEL_INSET_X_PX),
1436
+ );
1437
+ // The "chart's left edge" the pill must clear is `chartLeftX` (in
1438
+ // beside-mode, the right edge of the header card; in above-mode,
1439
+ // the canvas left edge at x=0). originX = chartLeftX + GUTTER_PX,
1440
+ // so we recover chartLeftX as `originX - GUTTER_PX`.
1441
+ const chartLeftX = ctx.timeline.originX - GUTTER_PX;
1442
+ const halfPill = pillWidth / 2;
1443
+ let pillMode: 'center' | 'flag-right' | 'flag-left';
1444
+ if (x - halfPill < chartLeftX) {
1445
+ // Centered pill would intrude into the header card / past the
1446
+ // canvas left edge — anchor the pill's LEFT side to the line
1447
+ // and let it extend right into the chart.
1448
+ pillMode = 'flag-right';
1449
+ } else if (x + halfPill > ctx.chartRightX) {
1450
+ // Centered pill would clip past the canvas right edge — anchor
1451
+ // the pill's RIGHT side to the line and let it extend left.
1452
+ pillMode = 'flag-left';
1453
+ } else {
1454
+ pillMode = 'center';
1455
+ }
1456
+ // Bottom-most Y the now-line should reach. When a mirrored bottom
1457
+ // tick panel exists, thread the line through it (so the line ties
1458
+ // the two date strips together visually). Otherwise stop at the
1459
+ // last swimlane — never extend into the footnote area below.
1460
+ const bottomTickPanelY = ctx.timeline.bottomTickPanelY;
1461
+ const bottomTickPanelHeight = ctx.timeline.bottomTickPanelHeight ?? 0;
1462
+ const lineBottomY =
1463
+ bottomTickPanelY !== undefined && bottomTickPanelHeight > 0
1464
+ ? bottomTickPanelY + bottomTickPanelHeight
1465
+ : ctx.swimlaneBottomY;
1466
+ return {
1467
+ x,
1468
+ topY: lineTopY,
1469
+ bottomY: lineBottomY,
1470
+ pillTopY,
1471
+ pillMode,
1472
+ label,
1473
+ pillWidth,
1474
+ style: resolveStyle('item', [], ctx.styleCtx),
1475
+ };
1476
+ }
1477
+
1478
+ // Mutable layout-time context shared across helpers.
1479
+
1480
+ // Traverse the full content tree to build an `items` map keyed by id.
1481
+ function collectItems(swimlanes: SwimlaneDeclaration[]): Map<string, ItemDeclaration> {
1482
+ const out = new Map<string, ItemDeclaration>();
1483
+ const walk = (node: ItemDeclaration | GroupBlock | ParallelBlock): void => {
1484
+ if (isItemDeclaration(node)) {
1485
+ if (node.name) out.set(node.name, node);
1486
+ return;
1487
+ }
1488
+ if (isParallelBlock(node)) {
1489
+ for (const child of node.content) {
1490
+ if (child.$type === 'DescriptionDirective') continue;
1491
+ walk(child as ItemDeclaration | GroupBlock);
1492
+ }
1493
+ return;
1494
+ }
1495
+ if (isGroupBlock(node)) {
1496
+ for (const child of node.content) {
1497
+ if (child.$type === 'DescriptionDirective') continue;
1498
+ walk(child as ItemDeclaration | GroupBlock | ParallelBlock);
1499
+ }
1500
+ return;
1501
+ }
1502
+ };
1503
+ for (const lane of swimlanes) {
1504
+ for (const child of lane.content) {
1505
+ if (child.$type === 'DescriptionDirective') continue;
1506
+ walk(child as ItemDeclaration | GroupBlock | ParallelBlock);
1507
+ }
1508
+ }
1509
+ return out;
1510
+ }
1511
+
1512
+ export function layoutRoadmap(
1513
+ file: NowlineFile,
1514
+ resolved: ResolveResult,
1515
+ options: LayoutOptions = {},
1516
+ ): LayoutResult {
1517
+ return new RoadmapNode().place(file, resolved, options, {
1518
+ sequenceItem,
1519
+ sequenceOne,
1520
+ resolveChildStart,
1521
+ newCursor,
1522
+ estimateTextWidth,
1523
+ predictItemChipExtraHeight,
1524
+ computeDateWindow,
1525
+ sizeBesideHeader,
1526
+ collectItems,
1527
+ buildDependencies,
1528
+ buildNowline,
1529
+ });
1530
+ }