zenith-scene 0.0.0

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
//! Paint resolvers: turn `PropertyValue`s into concrete `Color`,
//! `GradientPaint`, and `ShadowSpec` values, plus the gradient opacity cascade.

use std::collections::BTreeMap;

use zenith_core::{Diagnostic, GradientKind, PropertyValue, ResolvedToken, ResolvedValue};

use crate::color::{parse_color, parse_srgb_hex};
use crate::ir::{
    Color, FilterSpec, GradientPaint, GradientStop, MaskShape, MaskSpec, SceneCommand, ShadowSpec,
};

/// Build an [`ir::Color`](Color) from a resolved color token, preserving its
/// CMYK origin when present. Returns `None` only when the resolved value is not
/// a color or its stored hex is somehow unparseable (which token resolution
/// already guarantees not to happen).
fn color_from_resolved(rv: &ResolvedValue) -> Option<Color> {
    let hex = rv.as_color_hex()?;
    let mut color = parse_srgb_hex(hex)?;
    if let Some((c, m, y, k)) = rv.cmyk() {
        color.cmyk = Some([c, m, y, k]);
    }
    Some(color)
}

/// Resolve a `PropertyValue` to a `Color`, or push a diagnostic and return
/// `None`.
///
/// Accepts:
/// - `TokenRef(id)` → looks up in `resolved`, must be a `ResolvedValue::Color`.
/// - `Literal(hex)` → parses as sRGB hex string directly.
pub(super) fn resolve_property_color(
    prop: &PropertyValue,
    resolved: &BTreeMap<String, ResolvedToken>,
    diagnostics: &mut Vec<Diagnostic>,
    subject_id: &str,
) -> Option<Color> {
    match prop {
        PropertyValue::TokenRef(token_id) => {
            match resolved.get(token_id.as_str()) {
                Some(rt) if rt.value.as_color_hex().is_some() => {
                    match color_from_resolved(&rt.value) {
                        Some(c) => Some(c),
                        None => {
                            // Should not happen — token resolution validates the
                            // hex / cmyk literal — but be robust.
                            diagnostics.push(Diagnostic::advisory(
                                "scene.invalid_color",
                                format!(
                                    "token '{}' resolved to an invalid color; skipped",
                                    token_id
                                ),
                                None,
                                Some(subject_id.to_owned()),
                            ));
                            None
                        }
                    }
                }
                Some(rt) => {
                    diagnostics.push(Diagnostic::advisory(
                        "scene.wrong_token_type",
                        format!(
                            "node '{}' references token '{}' which resolved to a \
                             non-color value ({:?}); skipped",
                            subject_id, token_id, &rt.value
                        ),
                        None,
                        Some(subject_id.to_owned()),
                    ));
                    None
                }
                None => {
                    diagnostics.push(Diagnostic::advisory(
                        "scene.unresolved_token",
                        format!(
                            "node '{}' references token '{}' which did not resolve \
                             (check token diagnostics); skipped",
                            subject_id, token_id
                        ),
                        None,
                        Some(subject_id.to_owned()),
                    ));
                    None
                }
            }
        }
        PropertyValue::Literal(literal) => match parse_color(literal) {
            Some(c) => Some(c),
            None => {
                diagnostics.push(Diagnostic::advisory(
                    "scene.invalid_color",
                    format!(
                        "node '{}' has a fill literal '{}' that is not a valid \
                         sRGB hex or cmyk(...) color; skipped",
                        subject_id, literal
                    ),
                    None,
                    Some(subject_id.to_owned()),
                ));
                None
            }
        },
        // A dimension is not a color; advise and skip (mirrors wrong-type tokens).
        PropertyValue::Dimension(_) => {
            diagnostics.push(Diagnostic::advisory(
                "scene.wrong_token_type",
                format!(
                    "node '{}' has a dimension value where a color is expected; skipped",
                    subject_id
                ),
                None,
                Some(subject_id.to_owned()),
            ));
            None
        }

        // A data reference is unresolved at this point; the caller (data_resolve)
        // should have substituted the resolved value before reaching here. Treat
        // as unresolvable and skip without emitting a redundant diagnostic.
        PropertyValue::DataRef(_) => None,
    }
}

/// Resolve a fill `PropertyValue` into a [`GradientPaint`], or `None`.
///
/// Returns `Some` only when `prop` is a `TokenRef` whose token resolved to a
/// `ResolvedValue::Gradient`. Each stop's color is resolved from its
/// `color_token_id` via the resolved token map (must be `ResolvedValue::Color`);
/// stops whose color cannot resolve are skipped. Returns `None` (so the caller
/// falls back to the solid path) for non-gradient props, or when fewer than two
/// valid stops survive.
pub(super) fn resolve_property_gradient(
    prop: &PropertyValue,
    resolved: &BTreeMap<String, ResolvedToken>,
    _subject_id: &str,
) -> Option<GradientPaint> {
    let PropertyValue::TokenRef(token_id) = prop else {
        return None;
    };
    let ResolvedValue::Gradient(g) = &resolved.get(token_id.as_str())?.value else {
        return None;
    };

    let mut stops: Vec<GradientStop> = Vec::with_capacity(g.stops.len());
    for (offset, color_token_id) in &g.stops {
        let Some(rt) = resolved.get(color_token_id.as_str()) else {
            continue;
        };
        let Some(color) = color_from_resolved(&rt.value) else {
            continue;
        };
        stops.push(GradientStop {
            offset: *offset,
            color,
        });
    }

    if stops.len() < 2 {
        return None;
    }
    Some(GradientPaint {
        angle_deg: g.angle_deg,
        stops,
        radial: matches!(g.kind, GradientKind::Radial),
        center_x: g.center_x,
        center_y: g.center_y,
        radius_frac: g.radius,
    })
}

/// Resolve a `shadow` `PropertyValue` into a list of [`ShadowSpec`] layers, or
/// `None`.
///
/// Mirrors [`resolve_property_gradient`]: returns `Some` only when `prop` is a
/// `TokenRef` whose token resolved to a `ResolvedValue::Shadow`. Each layer's
/// color is resolved from its `color_token` via the resolved token map (must be
/// `ResolvedValue::Color`); layers whose color cannot resolve are skipped.
/// Returns `None` for non-shadow props, or when zero valid layers survive.
pub(super) fn resolve_property_shadow(
    prop: &PropertyValue,
    resolved: &BTreeMap<String, ResolvedToken>,
    _subject_id: &str,
) -> Option<Vec<ShadowSpec>> {
    let PropertyValue::TokenRef(token_id) = prop else {
        return None;
    };
    let ResolvedValue::Shadow(s) = &resolved.get(token_id.as_str())?.value else {
        return None;
    };

    let mut layers: Vec<ShadowSpec> = Vec::with_capacity(s.layers.len());
    for layer in &s.layers {
        let Some(rt) = resolved.get(layer.color_token.as_str()) else {
            continue;
        };
        let Some(color) = color_from_resolved(&rt.value) else {
            continue;
        };
        layers.push(ShadowSpec {
            dx: layer.dx,
            dy: layer.dy,
            blur: layer.blur,
            color,
        });
    }

    if layers.is_empty() {
        return None;
    }
    Some(layers)
}

/// Resolve a `filter` `PropertyValue` into a list of [`FilterSpec`] operations,
/// or `None`.
///
/// Mirrors [`resolve_property_shadow`]: returns `Some` only when `prop` is a
/// `TokenRef` whose token resolved to a `ResolvedValue::Filter`. Each core op is
/// mapped to a [`FilterSpec`] variant carrying its resolved scalar payload; the
/// per-kind default `amount` is substituted when the op leaves it unspecified.
/// For `Duotone`, the op's shadow/highlight color-token ids are resolved to
/// concrete [`Color`]s (the same lookup [`resolve_property_shadow`] uses); if
/// either color is missing or unresolvable, that op is SKIPPED. Returns `None`
/// for non-filter props, or when no op survives.
pub(super) fn resolve_property_filter(
    prop: &PropertyValue,
    resolved: &BTreeMap<String, ResolvedToken>,
    _subject_id: &str,
) -> Option<Vec<FilterSpec>> {
    let PropertyValue::TokenRef(token_id) = prop else {
        return None;
    };
    let ResolvedValue::Filter(f) = &resolved.get(token_id.as_str())?.value else {
        return None;
    };

    let mut ops: Vec<FilterSpec> = Vec::with_capacity(f.ops.len());
    for op in &f.ops {
        // Default amounts match the historical scalar defaults: 1.0 for every
        // kind except hue-rotate (0.0 degrees) and duotone (1.0 mix factor).
        let spec = match op.kind {
            zenith_core::FilterKind::Grayscale => FilterSpec::Grayscale(op.amount.unwrap_or(1.0)),
            zenith_core::FilterKind::Invert => FilterSpec::Invert(op.amount.unwrap_or(1.0)),
            zenith_core::FilterKind::Sepia => FilterSpec::Sepia(op.amount.unwrap_or(1.0)),
            zenith_core::FilterKind::Saturate => FilterSpec::Saturate(op.amount.unwrap_or(1.0)),
            zenith_core::FilterKind::Brightness => FilterSpec::Brightness(op.amount.unwrap_or(1.0)),
            zenith_core::FilterKind::Contrast => FilterSpec::Contrast(op.amount.unwrap_or(1.0)),
            zenith_core::FilterKind::HueRotate => FilterSpec::HueRotate(op.amount.unwrap_or(0.0)),
            zenith_core::FilterKind::Duotone => {
                // Resolve both color tokens; skip the op if either is missing
                // or not a color, mirroring how unresolvable shadow layers skip.
                let (Some(shadow_id), Some(highlight_id)) =
                    (op.shadow.as_deref(), op.highlight.as_deref())
                else {
                    continue;
                };
                let Some(shadow) = resolved
                    .get(shadow_id)
                    .and_then(|rt| color_from_resolved(&rt.value))
                else {
                    continue;
                };
                let Some(highlight) = resolved
                    .get(highlight_id)
                    .and_then(|rt| color_from_resolved(&rt.value))
                else {
                    continue;
                };
                FilterSpec::Duotone {
                    amount: op.amount.unwrap_or(1.0),
                    shadow,
                    highlight,
                }
            }
            zenith_core::FilterKind::Noise => FilterSpec::Noise {
                amount: op.amount.unwrap_or(1.0),
                seed: op.seed.unwrap_or(0),
                scale: op.scale.unwrap_or(1.0),
            },
        };
        ops.push(spec);
    }

    if ops.is_empty() {
        return None;
    }
    Some(ops)
}

/// Map a `zenith_core::MaskShape` to the scene-IR [`MaskShape`].
///
/// Exhaustive match keeps the scene IR decoupled from the core enum and surfaces
/// a compile error if a new core variant is ever added.
fn map_shape(shape: zenith_core::MaskShape) -> MaskShape {
    match shape {
        zenith_core::MaskShape::Rect => MaskShape::Rect,
        zenith_core::MaskShape::RoundedRect => MaskShape::RoundedRect,
        zenith_core::MaskShape::Ellipse => MaskShape::Ellipse,
    }
}

/// Resolve a `mask` `PropertyValue` into a [`MaskSpec`] carrying the node box,
/// or `None`.
///
/// Mirrors [`resolve_property_filter`] but additionally needs the node's
/// page-absolute box `(x, y, w, h)` so the resolved spec can describe the mask
/// coverage geometry. Returns `Some` only when `prop` is a `TokenRef` whose
/// token resolved to a `ResolvedValue::Mask`; the resolved corner radius defaults
/// to `0.0` when the mask token leaves it unspecified. Returns `None` for any
/// non-mask prop.
pub(super) fn resolve_property_mask(
    prop: &PropertyValue,
    resolved: &BTreeMap<String, ResolvedToken>,
    node_box: (f64, f64, f64, f64),
) -> Option<MaskSpec> {
    let PropertyValue::TokenRef(token_id) = prop else {
        return None;
    };
    let ResolvedValue::Mask(m) = &resolved.get(token_id.as_str())?.value else {
        return None;
    };
    let (x, y, w, h) = node_box;
    Some(MaskSpec {
        shape: map_shape(m.shape),
        radius: m.radius.unwrap_or(0.0),
        feather: m.feather,
        invert: m.invert,
        x,
        y,
        w,
        h,
    })
}

/// The single winning post-draw effect a leaf node carries, in precedence order
/// blur > shadow > filter (exactly the precedence the leaf resolution applies).
///
/// Used by [`emit_node_with_effects`] to emit the correct Begin*/End* bracket
/// around a node's draws.
pub(super) enum NodeEffect {
    Blur(f64),
    Shadow(Vec<ShadowSpec>),
    Filter(Vec<FilterSpec>),
}

/// Emit `draws` into `out`, wrapped by the node's effect and/or mask.
///
/// - no effect, no mask  → draws verbatim (BYTE-IDENTICAL to the pre-mask path).
/// - effect, no mask     → BeginEffect, draws, EndEffect (BYTE-IDENTICAL).
/// - mask, no effect     → BeginMask, draws, EndMask (soft reveal).
/// - effect AND mask     → draws (sharp base), then
///   BeginMask, BeginEffect, draws, EndEffect, EndMask (sharp center where
///   coverage = 0, effected feathered edges where coverage = 1).
pub(super) fn emit_node_with_effects(
    out: &mut Vec<SceneCommand>,
    draws: Vec<SceneCommand>,
    effect: Option<NodeEffect>,
    mask: Option<MaskSpec>,
) {
    let (begin, end): (Option<SceneCommand>, Option<SceneCommand>) = match &effect {
        Some(NodeEffect::Blur(r)) => (
            Some(SceneCommand::BeginBlur { radius: *r }),
            Some(SceneCommand::EndBlur),
        ),
        Some(NodeEffect::Shadow(s)) => (
            Some(SceneCommand::BeginShadow { shadows: s.clone() }),
            Some(SceneCommand::EndShadow),
        ),
        Some(NodeEffect::Filter(f)) => (
            Some(SceneCommand::BeginFilter { filters: f.clone() }),
            Some(SceneCommand::EndFilter),
        ),
        None => (None, None),
    };
    match (mask, begin) {
        (None, None) => out.extend(draws),
        (None, Some(b)) => {
            out.push(b);
            out.extend(draws);
            if let Some(e) = end {
                out.push(e);
            }
        }
        (Some(m), None) => {
            out.push(SceneCommand::BeginMask { mask: m });
            out.extend(draws);
            out.push(SceneCommand::EndMask);
        }
        (Some(m), Some(b)) => {
            out.extend(draws.clone()); // sharp base
            out.push(SceneCommand::BeginMask { mask: m });
            out.push(b);
            out.extend(draws);
            if let Some(e) = end {
                out.push(e);
            }
            out.push(SceneCommand::EndMask);
        }
    }
}

/// Apply the cascaded opacity multiplier to every stop's alpha, matching the
/// solid path's `color.a = (color.a * node_opacity * ctx.opacity).round()`.
pub(super) fn apply_gradient_opacity(
    gradient: &mut GradientPaint,
    node_opacity: f64,
    ctx_opacity: f64,
) {
    for stop in &mut gradient.stops {
        stop.color.a = (stop.color.a as f64 * node_opacity * ctx_opacity).round() as u8;
    }
}