zenith-scene 0.0.7

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
//! `frame` container compilation: clip-only (it does not translate children),
//! with optional rotation / blend / blur brackets and `flow` / `grid` layout.

use zenith_core::{Diagnostic, FrameNode, Node, dim_to_px};

use crate::ir::SceneCommand;

use super::super::paint::{
    NodeEffect, resolve_property_filter, resolve_property_mask, resolve_property_shadow,
};
use super::super::util::{
    blend_mode_ir, resolve_geometry_px, resolve_property_dimension_px, rotation_degrees,
    unsupported_unit_diag,
};
use super::super::{NodeCtx, RenderCtx, compile_node, style_prop};
use super::flow::{node_declared_h, node_declared_w, node_skipped_in_flow, with_flow_box};
use super::wrap::emit_wrapped_container;

/// The already-resolved frame box in page coordinates (pixels), passed to the
/// `flow`/`grid` layout helpers.
#[derive(Clone, Copy)]
struct FrameBox {
    x: f64,
    y: f64,
    w: f64,
    h: f64,
}

// NOTE: compile_frame → compile_node → compile_frame recursion has no depth
// guard, consistent with the compile_group limitation in v0.
pub(in crate::compile) fn compile_frame(
    frame: &FrameNode,
    cx: NodeCtx,
    commands: &mut Vec<SceneCommand>,
    diagnostics: &mut Vec<Diagnostic>,
    connector_strokes: &mut Vec<usize>,
    ctx: RenderCtx,
) {
    // Entire subtree excluded when visible=false (no PushClip emitted).
    if frame.visible == Some(false) {
        return;
    }

    // All four geometry dimensions are required for a frame clip rectangle.
    // Resolve them BEFORE pushing any PushClip to keep push/pop balanced.
    let (Some(x_dim), Some(y_dim), Some(w_dim), Some(h_dim)) =
        (&frame.x, &frame.y, &frame.w, &frame.h)
    else {
        diagnostics.push(Diagnostic::advisory(
            "scene.missing_geometry",
            format!(
                "frame '{}' is missing one or more geometry properties (x, y, w, h); \
                 skipped",
                frame.id
            ),
            frame.source_span,
            Some(frame.id.clone()),
        ));
        return;
    };

    let Some(frame_x) = resolve_geometry_px(Some(x_dim), cx.resolved) else {
        diagnostics.push(unsupported_unit_diag(
            "frame",
            &frame.id,
            "x",
            frame.source_span,
        ));
        return;
    };
    let Some(frame_y) = resolve_geometry_px(Some(y_dim), cx.resolved) else {
        diagnostics.push(unsupported_unit_diag(
            "frame",
            &frame.id,
            "y",
            frame.source_span,
        ));
        return;
    };
    let Some(frame_w) = resolve_geometry_px(Some(w_dim), cx.resolved) else {
        diagnostics.push(unsupported_unit_diag(
            "frame",
            &frame.id,
            "w",
            frame.source_span,
        ));
        return;
    };
    let Some(frame_h) = resolve_geometry_px(Some(h_dim), cx.resolved) else {
        diagnostics.push(unsupported_unit_diag(
            "frame",
            &frame.id,
            "h",
            frame.source_span,
        ));
        return;
    };

    // Rotation bracket — outermost, wrapping PushClip + children + PopClip.
    // v0 limitation: the clip rectangle below stays axis-aligned even when the
    // frame is rotated; rotated children may extend past the axis-aligned clip.
    let frame_rot = rotation_degrees(frame.rotate.as_ref());
    if let Some(angle) = frame_rot {
        let cx_pivot = ctx.dx + frame_x + frame_w / 2.0;
        let cy_pivot = ctx.dy + frame_y + frame_h / 2.0;
        commands.push(SceneCommand::PushTransform {
            angle_deg: angle,
            cx: cx_pivot,
            cy: cy_pivot,
        });
    }

    // Blend-mode layer (inside the rotation, around the clip + children). When a
    // non-normal blend is active the children render into an offscreen layer that
    // composites back with the frame's opacity cascade; the children therefore
    // inherit `ctx.opacity` UNMULTIPLIED (the layer carries the frame opacity so
    // it is not double-counted). With no blend the cascade is unchanged and the
    // command stream is byte-identical.
    let frame_opacity = frame.opacity.unwrap_or(1.0).clamp(0.0, 1.0);
    let blend = blend_mode_ir(frame.blend_mode.as_deref());
    let child_opacity = match blend {
        Some(blend_mode) => {
            commands.push(SceneCommand::PushLayer {
                opacity: ctx.opacity * frame_opacity,
                blend_mode: Some(blend_mode),
            });
            ctx.opacity
        }
        None => ctx.opacity * frame_opacity,
    };

    // Attached visual effect (inside blend, wrapping clip+children). The entire
    // frame ink (clipped composited children) is affected as one unit. Precedence
    // matches leaf nodes: blur > shadow > filter.
    let blur_sigma = frame
        .blur
        .as_ref()
        .and_then(|d| dim_to_px(d.value, &d.unit))
        .filter(|&s| s > 0.0);
    let effect: Option<NodeEffect> = if let Some(sigma) = blur_sigma {
        Some(NodeEffect::Blur(sigma))
    } else if let Some(shadows) = frame
        .shadow
        .as_ref()
        .and_then(|p| resolve_property_shadow(p, cx.resolved, &frame.id))
    {
        Some(NodeEffect::Shadow(shadows))
    } else {
        frame
            .filter
            .as_ref()
            .and_then(|p| resolve_property_filter(p, cx.resolved, &frame.id))
            .map(NodeEffect::Filter)
    };
    let mask = frame
        .mask
        .as_ref()
        .and_then(|p| resolve_property_mask(p, cx.resolved, (frame_x, frame_y, frame_w, frame_h)));

    let child_ctx = RenderCtx {
        opacity: child_opacity,
        dx: ctx.dx, // clip-only: no translation
        dy: ctx.dy, // clip-only: no translation
        // Page baseline grid cascades unchanged so all text shares one grid.
        baseline_grid: ctx.baseline_grid,
    };

    let fbox = FrameBox {
        x: frame_x,
        y: frame_y,
        w: frame_w,
        h: frame_h,
    };
    if effect.is_none() && mask.is_none() {
        compile_frame_clipped_children(
            frame,
            fbox,
            cx,
            commands,
            diagnostics,
            connector_strokes,
            child_ctx,
        );
    } else {
        let mut draws = Vec::new();
        let mut local_connector_strokes = Vec::new();
        compile_frame_clipped_children(
            frame,
            fbox,
            cx,
            &mut draws,
            diagnostics,
            &mut local_connector_strokes,
            child_ctx,
        );
        emit_wrapped_container(
            commands,
            draws,
            effect,
            mask,
            connector_strokes,
            local_connector_strokes,
        );
    }

    if blend.is_some() {
        commands.push(SceneCommand::PopLayer);
    }

    if frame_rot.is_some() {
        commands.push(SceneCommand::PopTransform);
    }
    // Frame emits no fill of its own in v0.
}

fn compile_frame_clipped_children(
    frame: &FrameNode,
    fbox: FrameBox,
    cx: NodeCtx,
    commands: &mut Vec<SceneCommand>,
    diagnostics: &mut Vec<Diagnostic>,
    connector_strokes: &mut Vec<usize>,
    child_ctx: RenderCtx,
) {
    commands.push(SceneCommand::PushClip {
        x: fbox.x,
        y: fbox.y,
        w: fbox.w,
        h: fbox.h,
    });

    match frame.layout.as_deref() {
        Some("flow") => {
            compile_frame_flow(
                frame,
                fbox,
                cx,
                commands,
                diagnostics,
                connector_strokes,
                child_ctx,
            );
        }
        Some("grid") => {
            compile_frame_grid(
                frame,
                fbox,
                cx,
                commands,
                diagnostics,
                connector_strokes,
                child_ctx,
            );
        }
        _ => {
            for child in &frame.children {
                compile_node(
                    child,
                    cx,
                    commands,
                    diagnostics,
                    connector_strokes,
                    child_ctx,
                );
            }
        }
    }

    commands.push(SceneCommand::PopClip);
}

/// Resolve `padding` and `gap` from a frame's style; both default to `0.0`.
fn frame_pad_gap(frame: &FrameNode, cx: NodeCtx) -> (f64, f64) {
    let pad = resolve_property_dimension_px(
        style_prop(&frame.style, cx.style_map, "padding"),
        cx.resolved,
        0.0,
    );
    let gap = resolve_property_dimension_px(
        style_prop(&frame.style, cx.style_map, "gap"),
        cx.resolved,
        0.0,
    );
    (pad, gap)
}

/// Lay a flow-frame's children out as a vertical stack inside its padded
/// content box, compiling each at the injected absolute coordinates.
///
/// Triggered only when `frame.layout == Some("flow")`. `frame_x`/`frame_y`/
/// `frame_w` are the already-resolved frame box in page coordinates (the same
/// values used for the surrounding `PushClip`). Children stack in source order
/// with `gap` between them; `padding` insets the content box uniformly. Both
/// `padding` and `gap` are token-only dimension style props on the frame's
/// style, defaulting to `0.0` when absent.
fn compile_frame_flow(
    frame: &FrameNode,
    fbox: FrameBox,
    cx: NodeCtx,
    commands: &mut Vec<SceneCommand>,
    diagnostics: &mut Vec<Diagnostic>,
    connector_strokes: &mut Vec<usize>,
    child_ctx: RenderCtx,
) {
    let FrameBox {
        x: frame_x,
        y: frame_y,
        w: frame_w,
        ..
    } = fbox;
    let (pad, gap) = frame_pad_gap(frame, cx);

    // Content box: uniform padding on all four sides.
    let content_left = frame_x + pad;
    let content_top = frame_y + pad;
    let content_w = (frame_w - 2.0 * pad).max(0.0);

    // Lay out children that participate (skip invisible and guide nodes) so a
    // trailing gap is only suppressed relative to the LAST laid-out child.
    let laid_out: Vec<&Node> = frame
        .children
        .iter()
        .filter(|c| !node_skipped_in_flow(c))
        .collect();
    let last_idx = laid_out.len().saturating_sub(1);

    let mut cursor_y = content_top;
    for (i, child) in laid_out.iter().enumerate() {
        // Cross-axis = start; child width = own declared `w` or the content
        // width. (A text child's own `align` still centers WITHIN its width.)
        let child_w = node_declared_w(child, cx.resolved).unwrap_or(content_w);

        // Vertical extent: own declared `h` when present, else the MEASURED
        // intrinsic height returned by compiling the child (text/code wrapped
        // height; 0.0 for leaves with no declared height).
        let declared_h = node_declared_h(child, cx.resolved);

        // Inject the absolute box onto a clone; compile with the SAME ctx
        // (dx/dy unchanged — injected coords are absolute page coords).
        let positioned = with_flow_box(child, content_left, cursor_y, child_w, declared_h);
        let measured_h = compile_node(
            &positioned,
            cx,
            commands,
            diagnostics,
            connector_strokes,
            child_ctx,
        );

        // Advance by the declared height when present, otherwise the measured
        // intrinsic height read back from the compile.
        let advance = declared_h.unwrap_or(measured_h);
        cursor_y += advance;
        if i != last_idx {
            cursor_y += gap;
        }
    }
}

/// Lay a grid-frame's children out into a `columns × rows` grid inside its
/// padded content box, compiling each at the injected absolute coordinates.
///
/// Triggered only when `frame.layout == Some("grid")`. `frame_x`/`frame_y`/
/// `frame_w`/`frame_h` are the already-resolved frame box in page coordinates
/// (the same values used for the surrounding `PushClip`). Participating children
/// (the same set the flow layout lays out: visible, non-guide) auto-place
/// row-major into the grid. Both `padding` and `gap` are token-only dimension
/// style props on the frame's style, defaulting to `0.0` when absent.
///
/// Cell sizing (uniform gutters of `gap`):
/// - `cols = frame.columns.unwrap_or(1).max(1)`
/// - `effective_rows = frame.rows.max(1)` or, when absent,
///   `ceil(n / cols).max(1)` so the grid grows to fit its children.
/// - `col_w = ((content_w - (cols-1)*gap) / cols).max(0.0)`
/// - `row_h = ((content_h - (effective_rows-1)*gap) / effective_rows).max(0.0)`
///
/// Unlike flow, every cell's height is FIXED (`Some(row_h)`) so an image child
/// with `fit="cover"` fills its cell.
fn compile_frame_grid(
    frame: &FrameNode,
    fbox: FrameBox,
    cx: NodeCtx,
    commands: &mut Vec<SceneCommand>,
    diagnostics: &mut Vec<Diagnostic>,
    connector_strokes: &mut Vec<usize>,
    child_ctx: RenderCtx,
) {
    let FrameBox {
        x: frame_x,
        y: frame_y,
        w: frame_w,
        h: frame_h,
    } = fbox;
    let (pad, gap) = frame_pad_gap(frame, cx);

    // Content box: uniform padding on all four sides.
    let content_left = frame_x + pad;
    let content_top = frame_y + pad;
    let content_w = (frame_w - 2.0 * pad).max(0.0);
    let content_h = (frame_h - 2.0 * pad).max(0.0);

    // Participating children: skip invisible and guide nodes (reuse flow helper).
    let participating: Vec<&Node> = frame
        .children
        .iter()
        .filter(|c| !node_skipped_in_flow(c))
        .collect();
    let n = participating.len();

    // Column / row counts (both guaranteed >= 1 so divisions are safe).
    let cols = frame.columns.unwrap_or(1).max(1) as usize;
    let effective_rows = frame
        .rows
        .map(|r| r.max(1) as usize)
        .unwrap_or_else(|| n.div_ceil(cols).max(1));

    // Uniform cell sizing with `gap` gutters between cells.
    let col_w = ((content_w - (cols - 1) as f64 * gap) / cols as f64).max(0.0);
    let row_h = ((content_h - (effective_rows - 1) as f64 * gap) / effective_rows as f64).max(0.0);

    for (i, child) in participating.iter().enumerate() {
        let col = i % cols;
        let row = i / cols;
        let cell_x = content_left + col as f64 * (col_w + gap);
        let cell_y = content_top + row as f64 * (row_h + gap);

        // Inject the FULL fixed cell box (always Some(row_h)) so e.g. an image
        // with fit="cover" fills its cell. Compile at absolute page coords
        // (dx/dy unchanged). The measured height is ignored — cells are fixed.
        let positioned = with_flow_box(child, cell_x, cell_y, col_w, Some(row_h));
        let _ = compile_node(
            &positioned,
            cx,
            commands,
            diagnostics,
            connector_strokes,
            child_ctx,
        );
    }
}