zenith-scene 0.0.5

Zenith backend-neutral scene IR and compilation (geometry, text wrap, anchors, opacity, clip).
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! `shape` compound-node compilation — background primitive + owned centered
//! label, reusing the production text path for the label.

use std::collections::BTreeMap;

use zenith_core::{
    Diagnostic, FontProvider, PropertyValue, ResolvedToken, ShapeNode, Style, TextNode,
};
use zenith_layout::RustybuzzEngine;

use crate::ir::{Paint, SceneCommand, StrokeAlign};

use super::super::RenderCtx;
use super::super::anchor::AnchorMap;
use super::super::chain::ChainAssignments;
use super::super::paint::resolve_property_color;
use super::super::style_prop;
use super::super::text::{
    MeasureEnv, TextCompileEnv, compile_text, empty_md_blocks, measure_text_wrapped_height,
    resolve_text_families,
};
use super::super::util::{
    AxisTarget, missing_geometry_diag, px_prop, resolve_anchored_axis, resolve_geometry_px,
    resolve_property_dimension_px, rotation_degrees, unsupported_unit_diag,
};

/// Read-only borrow + scalar context for [`compile_shape`] and its label
/// emitter.
///
/// Bundles every map/provider borrow plus the per-subtree [`RenderCtx`] so the
/// shape compiler and its label helper stay under the argument-count lint
/// without an `#[allow]`. All fields are borrows/`Copy` scalars held for the
/// duration of a single compile call.
#[derive(Clone, Copy)]
pub(in crate::compile) struct ShapeCompileEnv<'a> {
    pub(in crate::compile) resolved: &'a BTreeMap<String, ResolvedToken>,
    pub(in crate::compile) style_map: &'a BTreeMap<&'a str, &'a Style>,
    pub(in crate::compile) fonts: &'a dyn FontProvider,
    pub(in crate::compile) engine: &'a RustybuzzEngine,
    pub(in crate::compile) chains: &'a ChainAssignments,
    pub(in crate::compile) footnote_markers: &'a BTreeMap<String, String>,
    pub(in crate::compile) node_boxes: &'a BTreeMap<String, (f64, f64, f64, f64)>,
    pub(in crate::compile) anchors: &'a AnchorMap,
    pub(in crate::compile) ctx: RenderCtx,
}

/// Page-absolute bounding box of a resolved shape, shared by the background
/// emitters and the label placer.
#[derive(Clone, Copy)]
struct ShapeBox {
    x: f64,
    y: f64,
    w: f64,
    h: f64,
}

/// Geometry + paint inputs for the background-primitive emitters, bundled so each
/// emitter stays under the argument-count lint without an `#[allow]`.
#[derive(Clone, Copy)]
struct ShapeBg<'a> {
    x: f64,
    y: f64,
    w: f64,
    h: f64,
    color_op: f64,
    fill_prop: Option<&'a PropertyValue>,
    stroke_prop: Option<&'a PropertyValue>,
    stroke_width: f64,
}

/// Compile a `shape` compound node — background + owned centered label.
///
/// Emits the background primitive selected by [`ShapeNode::kind`] (default
/// `"process"` when absent or unrecognized):
/// - `process`   → rounded rect (`FillRoundedRect` + `StrokeRoundedRect`), using
///   `radius`; a plain `FillRect`/`StrokeRect` when `radius` is absent/0.
/// - `terminator`→ rounded rect with corner radius = `h/2` (pill).
/// - `ellipse`   → `FillEllipse` + `StrokeEllipse`.
/// - `decision`  → 4-point diamond polygon (`FillPolygon` + closed
///   `StrokePolyline`) built from the bbox mid-edges.
///
/// AFTER the background (so the label paints ON TOP of the fill), the owned
/// label [`ShapeNode::spans`] are rendered as a synthesized [`TextNode`] laid
/// into the shape's padded content box, REUSING the production
/// [`compile_text`] path. The label is horizontally aligned by `h_align`
/// (default `center`) and vertically aligned by `v_align` (default `middle`,
/// via a measured pre-offset like the table cell), and it shares the SAME
/// `ctx` as the background — so the shape's opacity, rotation, and clip
/// propagate to the label and the two stay locked together.
///
/// `opacity`/`visible`/`rotate` are honored exactly as `compile_rect` does;
/// `stroke_alignment` insets the rounded-rect/ellipse box the same way
/// `compile_rect` handles it. For the decision diamond the stroke is left
/// centered (v0).
pub(in crate::compile) fn compile_shape(
    shape: &ShapeNode,
    commands: &mut Vec<SceneCommand>,
    diagnostics: &mut Vec<Diagnostic>,
    env: ShapeCompileEnv,
) {
    let resolved = env.resolved;
    let style_map = env.style_map;
    let ctx = env.ctx;

    // Skip invisible shapes.
    if shape.visible == Some(false) {
        return;
    }

    // Resolve geometry — w and h are always required. x and y may be
    // derived from a page-relative anchor when absent.
    let (Some(w_dim), Some(h_dim)) = (&shape.w, &shape.h) else {
        diagnostics.push(missing_geometry_diag("shape", &shape.id, shape.source_span));
        return;
    };
    let Some(w) = resolve_geometry_px(Some(w_dim), resolved) else {
        diagnostics.push(unsupported_unit_diag(
            "shape",
            &shape.id,
            "w",
            shape.source_span,
        ));
        return;
    };
    let Some(h) = resolve_geometry_px(Some(h_dim), resolved) else {
        diagnostics.push(unsupported_unit_diag(
            "shape",
            &shape.id,
            "h",
            shape.source_span,
        ));
        return;
    };

    // Anchor-derived (x, y): look up the pre-pass map when x or y is absent.
    let anchor_xy = env.anchors.get(&shape.id).copied();

    let Some(x_raw) = resolve_anchored_axis(
        AxisTarget {
            kind: "shape",
            node_id: &shape.id,
            axis: "x",
        },
        shape.x.as_ref(),
        resolved,
        anchor_xy.map(|(ax, _)| ax),
        shape.source_span,
        diagnostics,
    ) else {
        return;
    };
    let Some(y_raw) = resolve_anchored_axis(
        AxisTarget {
            kind: "shape",
            node_id: &shape.id,
            axis: "y",
        },
        shape.y.as_ref(),
        resolved,
        anchor_xy.map(|(_, ay)| ay),
        shape.source_span,
        diagnostics,
    ) else {
        return;
    };

    // Apply group translation offset.
    let x = x_raw + ctx.dx;
    let y = y_raw + ctx.dy;
    let geom = ShapeBox { x, y, w, h };

    // Apply node opacity then cascade ctx.opacity on top (matches compile_rect).
    let node_opacity = shape.opacity.unwrap_or(1.0).clamp(0.0, 1.0);
    let color_op = node_opacity * ctx.opacity;

    // Resolve fill / stroke once (node-local prop overrides style cascade).
    // Any `(data)` ref was already substituted to a `Literal` by the pre-pass.
    let fill_prop: Option<&PropertyValue> = shape
        .fill
        .as_ref()
        .or_else(|| style_prop(&shape.style, style_map, "fill"));
    let stroke_prop = shape
        .stroke
        .as_ref()
        .or_else(|| style_prop(&shape.style, style_map, "stroke"));
    let stroke_width = {
        let sw = shape
            .stroke_width
            .clone()
            .or_else(|| style_prop(&shape.style, style_map, "stroke-width").cloned());
        resolve_property_dimension_px(sw.as_ref(), resolved, 1.0)
    };

    let bg = ShapeBg {
        x,
        y,
        w,
        h,
        color_op,
        fill_prop,
        stroke_prop,
        stroke_width,
    };

    // Rotation bracket (outermost). PushTransform only when rotate ≠ 0.
    let rot = rotation_degrees(shape.rotate.as_ref());
    if let Some(angle) = rot {
        let cx = x + w / 2.0;
        let cy = y + h / 2.0;
        commands.push(SceneCommand::PushTransform {
            angle_deg: angle,
            cx,
            cy,
        });
    }

    // Background primitive by kind (default "process").
    match shape.kind.as_deref() {
        Some("ellipse") => {
            emit_shape_ellipse(shape, resolved, diagnostics, commands, bg);
        }
        Some("decision") => {
            emit_shape_decision(shape, resolved, diagnostics, commands, bg);
        }
        Some("terminator") => {
            // Pill: corner radius = h/2.
            emit_shape_rounded_rect(shape, resolved, diagnostics, commands, h / 2.0, bg);
        }
        // "process" (default) and any unrecognized value: rounded rect using
        // `radius` (0 → plain rect).
        _ => {
            let radius_prop = shape
                .radius
                .clone()
                .or_else(|| style_prop(&shape.style, style_map, "radius").cloned());
            let radius = resolve_property_dimension_px(radius_prop.as_ref(), resolved, 0.0);
            emit_shape_rounded_rect(shape, resolved, diagnostics, commands, radius, bg);
        }
    }

    // OWNED LABEL (painted ON TOP of the background). Emitted INSIDE the
    // rotation bracket so the label rotates with the shape, and using the SAME
    // `ctx` so the shape's opacity/clip cascade onto the glyphs too.
    emit_shape_label(shape, commands, diagnostics, env, geom);

    if rot.is_some() {
        commands.push(SceneCommand::PopTransform);
    }
}

/// Synthesize a [`TextNode`] for the shape's owned label and render it via the
/// production [`compile_text`] path into the shape's padded content box.
///
/// The label inherits the shape's `ctx` (opacity / rotation / clip). Horizontal
/// alignment maps `h_align` → the text node's `align` (default `center`);
/// vertical alignment is applied by PRE-OFFSETTING the synthetic node's `y`
/// (measured via [`measure_text_wrapped_height`]), exactly like the table cell
/// — `TextNode` has no native v-align. The label defaults to centered both ways.
///
/// v0 simplification: for ALL kinds the content box is the bbox inset by
/// `padding`. The decision diamond inscribes its label in the bbox; an author
/// adds `padding` to keep the text inside the rhombus.
fn emit_shape_label(
    shape: &ShapeNode,
    commands: &mut Vec<SceneCommand>,
    diagnostics: &mut Vec<Diagnostic>,
    env: ShapeCompileEnv,
    geom: ShapeBox,
) {
    let ShapeCompileEnv {
        resolved,
        style_map,
        fonts,
        engine,
        chains,
        footnote_markers,
        node_boxes,
        anchors,
        ctx,
    } = env;
    let ShapeBox { x, y, w, h } = geom;

    // Nothing to render when the label has no spans.
    if shape.spans.is_empty() {
        return;
    }

    // Padded content box: bbox inset by `padding` (token → px; 0 when absent).
    let pad = resolve_property_dimension_px(shape.padding.as_ref(), resolved, 0.0);
    let content_x = x + pad;
    let content_y = y + pad;
    let content_w = (w - 2.0 * pad).max(0.0);
    let content_h = (h - 2.0 * pad).max(0.0);

    // Padding larger than the box collapses the content area; skip rather than
    // emit a degenerate (zero/negative) text box.
    if content_w <= 0.0 || content_h <= 0.0 {
        return;
    }

    // Map the shape's `h_align` to the text node's `align` (default center).
    let align = match shape.h_align.as_deref() {
        Some("end") => Some("end".to_owned()),
        Some("start") => Some("start".to_owned()),
        // "center", any unrecognized value, and absent all center the label.
        _ => Some("center".to_owned()),
    };

    // Synthesize the label as a fresh TextNode laid into the content box. A
    // synthetic id derived from the shape id keeps it unique (never collides).
    let mut synth = TextNode {
        id: format!("{}/label", shape.id),
        name: None,
        role: None,
        x: Some(px_prop(content_x)),
        y: Some(px_prop(content_y)),
        w: Some(px_prop(content_w)),
        h: Some(px_prop(content_h)),
        align,
        v_align: None,
        direction: None,
        overflow: None,
        overflow_wrap: None,
        style: shape.text_style.clone(),
        fill: None,
        stroke: None,
        stroke_width: None,
        contrast_bg: None,
        font_family: None,
        font_size: None,
        font_size_min: None,
        font_weight: None,
        shadow: None,
        filter: None,
        mask: None,
        blend_mode: None,
        blur: None,
        opacity: None,
        visible: None,
        locked: None,
        selectable: None,
        rotate: None,
        chain: None,
        drop_cap_lines: None,
        hyphenate: None,
        widow_orphan: None,
        tab_leader: None,
        text_exclusion: None,
        padding_left: None,
        text_indent: None,
        content_format: None,
        src: None,
        bullet: None,
        bullet_gap: None,
        anchor: None,
        anchor_zone: None,
        anchor_sibling: None,
        anchor_edge: None,
        anchor_gap: None,
        anchor_parent: None,
        spans: shape.spans.clone(),
        block_styles: Vec::new(),
        source_span: shape.source_span,
        unknown_props: BTreeMap::new(),
    };

    // VERTICAL ALIGNMENT: TextNode has no native v-align, so pre-offset `y` by
    // the measured wrapped height (same approach as the table cell). Default is
    // middle.
    let families = resolve_text_families(&synth, resolved, style_map, fonts, diagnostics);
    let wrapped_h = measure_text_wrapped_height(
        &synth,
        content_w,
        &families,
        MeasureEnv {
            resolved,
            style_map,
            fonts,
            engine,
        },
        diagnostics,
    )
    .unwrap_or(0.0);
    let v_offset = match shape.v_align.as_deref() {
        Some("top") => 0.0,
        Some("bottom") => (content_h - wrapped_h).max(0.0),
        // "middle", any unrecognized value, and absent center vertically.
        _ => ((content_h - wrapped_h) / 2.0).max(0.0),
    };
    synth.y = Some(px_prop(content_y + v_offset));

    // Emit the label via the production text path. The synth's x/y are ALREADY
    // absolute (the caller resolved `x_raw + ctx.dx`), so the translation must
    // NOT be applied again — `compile_text` adds `ctx.dx/dy` itself. Zero the
    // translation while preserving opacity/baseline-grid so the label still
    // cascades correctly. Without this, a shape inside a group/instance has its
    // label double-translated by the container offset.
    let label_ctx = RenderCtx {
        dx: 0.0,
        dy: 0.0,
        ..ctx
    };
    let _ = compile_text(
        &synth,
        TextCompileEnv {
            resolved,
            style_map,
            fonts,
            engine,
            chains,
            footnote_markers,
            node_boxes,
            anchors,
            md_blocks: empty_md_blocks(),
            page_block_styles: &[],
            doc_block_styles: &[],
        },
        commands,
        diagnostics,
        label_ctx,
    );
}

/// Emit the rounded-rect (or plain-rect when `radius <= 0`) background for a
/// `process`/`terminator` shape. Stroke alignment insets the stroked box like
/// `compile_rect`.
fn emit_shape_rounded_rect(
    shape: &ShapeNode,
    resolved: &BTreeMap<String, ResolvedToken>,
    diagnostics: &mut Vec<Diagnostic>,
    commands: &mut Vec<SceneCommand>,
    radius: f64,
    bg: ShapeBg,
) {
    let ShapeBg {
        x,
        y,
        w,
        h,
        color_op,
        fill_prop,
        stroke_prop,
        stroke_width,
    } = bg;

    let is_rounded = radius > 0.0;

    // FILL (emitted first, under the stroke).
    if let Some(fill_prop) = fill_prop
        && let Some(mut color) = resolve_property_color(fill_prop, resolved, diagnostics, &shape.id)
    {
        color.a = (color.a as f64 * color_op).round() as u8;
        if is_rounded {
            commands.push(SceneCommand::FillRoundedRect {
                x,
                y,
                w,
                h,
                radius,
                radii: None,
                paint: Paint::solid(color),
            });
        } else {
            commands.push(SceneCommand::FillRect {
                x,
                y,
                w,
                h,
                paint: Paint::solid(color),
            });
        }
    }

    // STROKE (emitted on top of fill). Only when both stroke and width apply.
    if let Some(stroke_prop) = stroke_prop
        && let Some(mut color) =
            resolve_property_color(stroke_prop, resolved, diagnostics, &shape.id)
    {
        color.a = (color.a as f64 * color_op).round() as u8;
        let half = stroke_width / 2.0;
        let adjust_inside = |v: f64| if v > 0.0 { (v - half).max(0.0) } else { 0.0 };
        let adjust_outside = |v: f64| if v > 0.0 { v + half } else { 0.0 };
        let (sx, sy, sw_geom, sh_geom, sradius) = match shape.stroke_alignment.as_deref() {
            Some("inside") => (
                x + half,
                y + half,
                w - stroke_width,
                h - stroke_width,
                adjust_inside(radius),
            ),
            Some("outside") => (
                x - half,
                y - half,
                w + stroke_width,
                h + stroke_width,
                adjust_outside(radius),
            ),
            // "center" (default) and any unrecognized value.
            _ => (x, y, w, h, radius),
        };
        let stroke_is_rounded = sradius > 0.0;
        if sw_geom > 0.0 && sh_geom > 0.0 {
            if stroke_is_rounded {
                commands.push(SceneCommand::StrokeRoundedRect {
                    x: sx,
                    y: sy,
                    w: sw_geom,
                    h: sh_geom,
                    radius: sradius,
                    radii: None,
                    color,
                    stroke_width,
                    stroke_dash: None,
                    stroke_gap: None,
                    stroke_linecap: None,
                });
            } else {
                commands.push(SceneCommand::StrokeRect {
                    x: sx,
                    y: sy,
                    w: sw_geom,
                    h: sh_geom,
                    color,
                    stroke_width,
                    stroke_dash: None,
                    stroke_gap: None,
                    stroke_linecap: None,
                });
            }
        }
    }
}

/// Emit an ellipse background (mirrors `compile_ellipse`'s fill/stroke emit).
/// Stroke alignment is not modeled for the ellipse (centered, like `ellipse`).
fn emit_shape_ellipse(
    shape: &ShapeNode,
    resolved: &BTreeMap<String, ResolvedToken>,
    diagnostics: &mut Vec<Diagnostic>,
    commands: &mut Vec<SceneCommand>,
    bg: ShapeBg,
) {
    let ShapeBg {
        x,
        y,
        w,
        h,
        color_op,
        fill_prop,
        stroke_prop,
        stroke_width,
    } = bg;

    if let Some(fill_prop) = fill_prop
        && let Some(mut color) = resolve_property_color(fill_prop, resolved, diagnostics, &shape.id)
    {
        color.a = (color.a as f64 * color_op).round() as u8;
        commands.push(SceneCommand::FillEllipse {
            x,
            y,
            w,
            h,
            rx: None,
            ry: None,
            paint: Paint::solid(color),
        });
    }

    if let Some(stroke_prop) = stroke_prop
        && let Some(mut color) =
            resolve_property_color(stroke_prop, resolved, diagnostics, &shape.id)
    {
        color.a = (color.a as f64 * color_op).round() as u8;
        commands.push(SceneCommand::StrokeEllipse {
            x,
            y,
            w,
            h,
            rx: None,
            ry: None,
            color,
            stroke_width,
            stroke_dash: None,
            stroke_gap: None,
            stroke_linecap: None,
        });
    }
}

/// Emit a 4-point diamond polygon background for a `decision` shape (mirrors
/// `compile_polygon`'s emit). The diamond vertices are the bbox mid-edges:
/// top-mid, right-mid, bottom-mid, left-mid. Stroke is centered in U1.
fn emit_shape_decision(
    shape: &ShapeNode,
    resolved: &BTreeMap<String, ResolvedToken>,
    diagnostics: &mut Vec<Diagnostic>,
    commands: &mut Vec<SceneCommand>,
    bg: ShapeBg,
) {
    let ShapeBg {
        x,
        y,
        w,
        h,
        color_op,
        fill_prop,
        stroke_prop,
        stroke_width,
    } = bg;

    let flat_points = vec![
        x + w / 2.0,
        y, // top-mid
        x + w,
        y + h / 2.0, // right-mid
        x + w / 2.0,
        y + h, // bottom-mid
        x,
        y + h / 2.0, // left-mid
    ];

    if let Some(fill_prop) = fill_prop
        && let Some(mut color) = resolve_property_color(fill_prop, resolved, diagnostics, &shape.id)
    {
        color.a = (color.a as f64 * color_op).round() as u8;
        commands.push(SceneCommand::FillPolygon {
            points: flat_points.clone(),
            paint: Paint::solid(color),
            even_odd: false,
        });
    }

    if let Some(stroke_prop) = stroke_prop
        && let Some(mut color) =
            resolve_property_color(stroke_prop, resolved, diagnostics, &shape.id)
    {
        color.a = (color.a as f64 * color_op).round() as u8;
        commands.push(SceneCommand::StrokePolyline {
            points: flat_points,
            color,
            stroke_width,
            closed: true,
            align: StrokeAlign::Center,
            fill_even_odd: false,
        });
    }
}