lini 0.20.0

Pretty diagrams, charts, and technical drawings from plain text, with fine-grained control. Compiles to clean, themeable SVG.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! `PlacedNode` **builders** for a layout engine's lowered primitives — shared by
//! charts [SPEC 14.9] and sequences [SPEC 13]. Every bar, gridline, lifeline,
//! arrow, label, frame, and note is built through these — never an open-coded
//! `PlacedNode` — so lowering stays one mechanism and the render emitters
//! (`emit_rect` / `emit_line` / the text path) draw them unchanged. (Distinct from
//! `layout::primitives`, which *sizes* primitives.)

use crate::layout::{Bbox, PlacedNode, approx_height, approx_width};
use crate::resolve::{AttrMap, MarkerKind, Markers, NodeKind, ResolvedInst, ResolvedValue};
use crate::span::Span;
use std::f64::consts::TAU;

fn node(kind: NodeKind, bbox: Bbox) -> PlacedNode {
    PlacedNode {
        id: None,
        kind,
        type_chain: Vec::new(),
        applied_styles: Vec::new(),
        label: None,
        attrs: AttrMap::new(),
        own_style: AttrMap::new(),
        markers: Markers::default(),
        cx: 0.0,
        cy: 0.0,
        bbox,
        rotation: 0.0,
        children: Vec::new(),
        gutters: Vec::new(),
        links: Vec::new(),
        sketch: None,
        origin: (0.0, 0.0),
        span: Span::empty(),
    }
}

fn ident(s: &str) -> ResolvedValue {
    ResolvedValue::Ident(s.to_string())
}

/// A filled rectangle (a bar, a band shade, a legend swatch) centred at (cx, cy).
/// Stroke off and width 0 so the drawn rect matches the bbox exactly; `opacity` lets a
/// band wash or an overlay bar read through (omitted at 1, so opaque rects don't churn).
pub fn rect(cx: f64, cy: f64, w: f64, h: f64, fill: ResolvedValue, opacity: f64) -> PlacedNode {
    let mut n = node(NodeKind::Block, Bbox::centered(w, h));
    n.cx = cx;
    n.cy = cy;
    n.attrs.insert("fill", fill);
    n.attrs.insert("stroke", ident("none"));
    n.attrs.insert("stroke-width", ResolvedValue::Number(0.0));
    if (opacity - 1.0).abs() > 1e-9 {
        n.attrs.insert("opacity", ResolvedValue::Number(opacity));
    }
    n
}

/// The bounding box of a point list (empty for no points).
fn bounds(points: &[(f64, f64)]) -> Bbox {
    if points.is_empty() {
        return Bbox::empty();
    }
    points.iter().fold(
        Bbox {
            min_x: f64::INFINITY,
            min_y: f64::INFINITY,
            max_x: f64::NEG_INFINITY,
            max_y: f64::NEG_INFINITY,
        },
        |b, &(x, y)| Bbox {
            min_x: b.min_x.min(x),
            min_y: b.min_y.min(y),
            max_x: b.max_x.max(x),
            max_y: b.max_y.max(y),
        },
    )
}

/// A `w`×`h` filled primitive centred at (cx, cy), stroke off and width 0 so the drawn
/// shape matches the bbox exactly. The shared body of [`oval`] and [`marker`].
fn filled(kind: NodeKind, cx: f64, cy: f64, w: f64, h: f64, fill: ResolvedValue) -> PlacedNode {
    let mut n = node(kind, Bbox::centered(w, h));
    n.cx = cx;
    n.cy = cy;
    n.attrs.insert("fill", fill);
    n.attrs.insert("stroke", ident("none"));
    n.attrs.insert("stroke-width", ResolvedValue::Number(0.0));
    n
}

/// A filled ellipse (a dot, a bubble) centred at (cx, cy).
pub fn oval(cx: f64, cy: f64, w: f64, h: f64, fill: ResolvedValue) -> PlacedNode {
    filled(NodeKind::Oval, cx, cy, w, h, fill)
}

/// A centred chart point marker [SPEC 14.2]: a filled round point (`dot` / `circle`)
/// or a rhombus (`diamond`), `w`×`h` at (cx, cy). The kind picks the shape; the caller
/// sizes it (a line vertex by the kind, a `|dots|` by its `width`). The **one** place a
/// chart point marker is built — line/area vertices, `|dots|`, and `|mark|` points all
/// route through it, so dot/circle/diamond never diverge. `arrow` / `crow` never reach
/// here (rejected at parse, [SPEC 20]); any non-diamond draws round.
pub fn marker(
    kind: MarkerKind,
    cx: f64,
    cy: f64,
    w: f64,
    h: f64,
    fill: ResolvedValue,
) -> PlacedNode {
    let shape = if matches!(kind, MarkerKind::Diamond) {
        NodeKind::Diamond
    } else {
        NodeKind::Oval
    };
    filled(shape, cx, cy, w, h, fill)
}

/// A drawing annotation's filled marker head [SPEC 15.6/15.7] — the slender
/// dimension arrow, the surface-seated datum triangle — classed
/// `lini-marker lini-marker-{variant}`, so the shared `.lini-marker` rule
/// paints it (fill = the link stroke, stroke off) and only a recoloured
/// statement inlines anything — exactly how link markers ride the sheet
/// [SPEC 17].
pub fn dim_marker(variant: &str, points: Vec<(f64, f64)>, fill: ResolvedValue) -> PlacedNode {
    let bbox = bounds(&points);
    let pts = points
        .into_iter()
        .map(|(x, y)| {
            ResolvedValue::Tuple(vec![ResolvedValue::Number(x), ResolvedValue::Number(y)])
        })
        .collect();
    let mut n = node(NodeKind::Poly, bbox);
    n.type_chain = vec!["marker".into(), format!("marker-{variant}")];
    n.attrs.insert("points", ResolvedValue::List(pts));
    n.attrs.insert("fill", fill);
    n.attrs.insert("stroke", ident("none"));
    n
}

/// A filled polygon (an area's body) through `points`. Stroke off; `opacity` lets
/// overlapping areas read through.
pub fn poly(points: Vec<(f64, f64)>, fill: ResolvedValue, opacity: f64) -> PlacedNode {
    let bbox = bounds(&points);
    let pts = points
        .into_iter()
        .map(|(x, y)| {
            ResolvedValue::Tuple(vec![ResolvedValue::Number(x), ResolvedValue::Number(y)])
        })
        .collect();
    let mut n = node(NodeKind::Poly, bbox);
    n.attrs.insert("points", ResolvedValue::List(pts));
    n.attrs.insert("fill", fill);
    n.attrs.insert("stroke", ident("none"));
    n.attrs.insert("stroke-width", ResolvedValue::Number(0.0));
    n.attrs.insert("opacity", ResolvedValue::Number(opacity));
    n
}

/// An annular-sector filled polygon (a pie / donut slice — [SPEC 14.7] — or a
/// radial bar, [SPEC 12]): radius `r0`→`r1` over angles `[a_lo, a_hi]` (0 straight up,
/// increasing clockwise). The arcs are segmented finely enough to read smooth at any
/// size; `r0 ≈ 0` collapses the inner edge to the centre (a full wedge). `opacity` lets
/// overlapping wedges read through.
#[allow(clippy::too_many_arguments)]
pub fn wedge(
    cx: f64,
    cy: f64,
    r0: f64,
    r1: f64,
    a_lo: f64,
    a_hi: f64,
    fill: ResolvedValue,
    opacity: f64,
) -> PlacedNode {
    let span = a_hi - a_lo;
    let steps = (span.abs() / TAU * 64.0).ceil().max(2.0) as usize; // ~one segment / 6°
    let at = |r: f64, a: f64| (cx + r * a.sin(), cy - r * a.cos());
    let mut pts: Vec<(f64, f64)> = (0..=steps)
        .map(|k| at(r1, a_lo + span * k as f64 / steps as f64))
        .collect();
    if r0 <= 0.5 {
        pts.push((cx, cy));
    } else {
        pts.extend((0..=steps).map(|k| at(r0, a_hi - span * k as f64 / steps as f64)));
    }
    poly(pts, fill, opacity)
}

/// A polyline (a gridline or a line series) through `points`, with the given stroke
/// colour and width.
pub fn line(points: Vec<(f64, f64)>, stroke: ResolvedValue, width: f64) -> PlacedNode {
    let bbox = bounds(&points);
    let pts = points
        .into_iter()
        .map(|(x, y)| {
            ResolvedValue::Tuple(vec![ResolvedValue::Number(x), ResolvedValue::Number(y)])
        })
        .collect();
    let mut n = node(NodeKind::Line, bbox);
    n.attrs.insert("points", ResolvedValue::List(pts));
    n.attrs.insert("fill", ident("none"));
    n.attrs.insert("stroke", stroke);
    n.attrs.insert("stroke-width", ResolvedValue::Number(width));
    n
}

/// A filled shape through a raw SVG path `d` (absolute coords baked in, like [`line`] /
/// [`poly`]; the node stays at the origin). For a composed outline a `rect` / `poly`
/// can't state — a frame's banner tab or a note's folded corner [SPEC 13]. `bbox` is
/// its absolute bounds, so the enclosing engine sizes correctly.
pub fn path(d: String, fill: ResolvedValue, bbox: Bbox) -> PlacedNode {
    let mut n = node(NodeKind::Path, bbox);
    n.attrs.insert("path", ResolvedValue::String(d));
    n.attrs.insert("fill", fill);
    n.attrs.insert("stroke", ident("none"));
    n.attrs.insert("stroke-width", ResolvedValue::Number(0.0));
    n
}

/// A transparent group wrapping `children` (e.g. a tooltip card's box + text), tagged
/// with `type_chain` so it carries `.lini-{name}` classes. `bbox` is its absolute bounds
/// (the children are positioned absolutely; the group keeps `cx`/`cy` at the origin).
pub fn group(children: Vec<PlacedNode>, type_chain: Vec<String>, bbox: Bbox) -> PlacedNode {
    let mut n = node(NodeKind::Block, bbox);
    n.type_chain = type_chain;
    n.children = children;
    n
}

/// The container shell for a **layout-owning engine** (chart, pie, sequence): a `Block`
/// carrying the node's identity, classes, and paint, with the lowered primitives as its
/// pre-positioned `children`. The one place that shell is built, so the engines don't each
/// open-code a `PlacedNode`.
pub fn container(inst: &ResolvedInst, bbox: Bbox, children: Vec<PlacedNode>) -> PlacedNode {
    PlacedNode {
        id: inst.id.clone(),
        kind: NodeKind::Block,
        type_chain: inst.type_chain.clone(),
        applied_styles: inst.applied_styles.clone(),
        label: None,
        attrs: inst.attrs.clone(),
        own_style: AttrMap::new(),
        markers: inst.markers.clone(),
        cx: 0.0,
        cy: 0.0,
        bbox,
        rotation: inst.attrs.number("rotate").unwrap_or(0.0),
        children,
        gutters: Vec::new(),
        links: Vec::new(),
        sketch: None,
        origin: (0.0, 0.0),
        span: inst.span,
    }
}

/// Centred text at (cx, cy) — anchor middle, the `.lini-text` default. `size` (and
/// `bold` / `color`) ride `own_style`, so they appear in the output, overriding the
/// chart `<g>`'s inherited root font.
pub fn text(
    content: &str,
    cx: f64,
    cy: f64,
    size: f64,
    color: Option<ResolvedValue>,
    bold: bool,
) -> PlacedNode {
    let bbox = Bbox::centered(
        approx_width(content, size, 0.0),
        approx_height(content, size, 0.0),
    );
    let mut n = node(NodeKind::Text, bbox);
    n.cx = cx;
    n.cy = cy;
    n.label = Some(content.to_string());
    set(&mut n, "font-size", ResolvedValue::Number(size));
    // The diagram-wide default weight is bold (`--lini-font-weight`); a chart keeps that
    // for the title and legend (`bold`) but states `normal` for its data text — axis
    // ticks, tags, annotations — so the numbers and labels don't shout [SPEC 14.6].
    set(
        &mut n,
        "font-weight",
        ident(if bold { "bold" } else { "normal" }),
    );
    if let Some(c) = color {
        set(&mut n, "color", c);
    }
    n
}

/// Text whose size / weight come from a `.lini-{class}` **stylesheet rule** rather than
/// inline `style=` — for a sequence message label and the like, mirroring how a link label
/// rides `.lini-link-label`. `size` only bounds the bbox (so the engine measures the label);
/// the class states the rendered font. Centred at (cx, cy); `.lini-text` gives the anchors.
pub fn text_classed(content: &str, cx: f64, cy: f64, size: f64, class: &str) -> PlacedNode {
    let bbox = Bbox::centered(
        approx_width(content, size, 0.0),
        approx_height(content, size, 0.0),
    );
    let mut n = node(NodeKind::Text, bbox);
    n.cx = cx;
    n.cy = cy;
    n.label = Some(content.to_string());
    n.type_chain = vec![class.to_string()];
    n
}

/// The caption size a drawing's annotation text reads at [SPEC 15.1] — the
/// `.lini-dim-text` class states it, so no dim / leader / callout leaf inlines it.
const DIM_TEXT_SIZE: f64 = 12.0;

/// Dimension / leader / callout text [SPEC 15.6/17]: a `.lini-dim-text` leaf.
/// The class states the font (12 px, normal weight), so a leaf at that default
/// inlines nothing; only a size that differs — a `tol:` deviation stack, a
/// restyled link — carries an inline `font-size` override (a statement's own
/// text styling still inlines, [SPEC 17]).
pub fn dim_text(content: &str, cx: f64, cy: f64, size: f64) -> PlacedNode {
    let mut n = text_classed(content, cx, cy, size, "dim-text");
    if (size - DIM_TEXT_SIZE).abs() > 1e-9 {
        set(&mut n, "font-size", ResolvedValue::Number(size));
    }
    n
}

/// A plain text leaf that **inherits** its font from the enclosing `<g>` — no
/// class, no inline [SPEC 17]. For text under a box that already states the font
/// (a title `|footnote|`), so nothing is stated twice. `size` bounds the bbox.
pub fn text_plain(content: &str, cx: f64, cy: f64, size: f64) -> PlacedNode {
    let bbox = Bbox::centered(
        approx_width(content, size, 0.0),
        approx_height(content, size, 0.0),
    );
    let mut n = node(NodeKind::Text, bbox);
    n.cx = cx;
    n.cy = cy;
    n.label = Some(content.to_string());
    n
}

/// Text whose **right edge** sits at `right_x` (for value-axis labels): the node is
/// anchored middle, so shift its centre left by half the measured width.
pub fn text_right(
    content: &str,
    right_x: f64,
    cy: f64,
    size: f64,
    color: Option<ResolvedValue>,
) -> PlacedNode {
    let cx = right_x - text_width(content, size) / 2.0;
    text(content, cx, cy, size, color, false)
}

/// Text whose **left edge** sits at `left_x` (for a right-side value axis).
pub fn text_left(
    content: &str,
    left_x: f64,
    cy: f64,
    size: f64,
    color: Option<ResolvedValue>,
) -> PlacedNode {
    let cx = left_x + text_width(content, size) / 2.0;
    text(content, cx, cy, size, color, false)
}

/// The drawn width of a centred label, for laying out legends and right-aligned
/// ticks (compile-time text measurement, [SPEC 5]).
pub fn text_width(content: &str, size: f64) -> f64 {
    approx_width(content, size, 0.0)
}

/// The drawn height of a label, for collision-testing inline labels [SPEC 14.8].
pub fn text_height(content: &str, size: f64) -> f64 {
    approx_height(content, size, 0.0)
}

/// Attach a native `<title>` — the baked-safe tooltip floor [SPEC 14.8],
/// emitted by `render_node` on any node carrying a `hint:`.
pub fn set_hint(n: &mut PlacedNode, hint: String) {
    n.attrs.insert("hint", ResolvedValue::String(hint));
}

/// Draw a `stroke:` outline on a fill shape [SPEC 14.6]: replace the builder's
/// `stroke: none` / width-0 default with `color` at `width`. The one place a chart
/// shape gains an outline — reused by bars, slices, and bubbles (an area's outline is
/// its own top edge, a `prim::line`), so `stroke:` never bleeds into the fill.
pub fn outline(n: &mut PlacedNode, color: ResolvedValue, width: f64) {
    n.attrs.insert("stroke", color);
    n.attrs.insert("stroke-width", ResolvedValue::Number(width));
}

/// Round a rect's corners [SPEC 14.2]: set the `radius` the `Block` renderer reads
/// (`emit_rect` → `rx`/`ry`). Skipped at 0 so a square shape's attrs don't churn. Shared
/// by bars, the legend swatches, and the tooltip card.
pub fn round(n: &mut PlacedNode, radius: f64) {
    if radius > 0.0 {
        n.attrs.insert("radius", ResolvedValue::Number(radius));
    }
}

/// Set a text prop on both `attrs` (so layout measures with it) and `own_style` (so
/// render emits it, beating the inherited `.lini` value).
fn set(n: &mut PlacedNode, name: &str, v: ResolvedValue) {
    n.attrs.insert(name, v.clone());
    n.own_style.insert(name, v);
}