tellur-core 0.3.0

Core component model for tellur: vector/raster/timeline components, layout, easing, and events
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
//! Placement: snap an anchor on a child to either an absolute point or an
//! anchor on the parent box, then apply an optional pixel offset.
//!
//! [`Positioned`] is the component-system replacement for the old `Placed`
//! `(position, child)` pair. Placement is now expressed as a real component
//! that a parent stores as a plain `Box<dyn _Component>`, so it composes with
//! [`Fragment`](crate::fragment::Fragment), the layout containers, and
//! everything else uniformly — there is no separate "placed" world.
//!
//! The fluent entry points mirror the geometry vocabulary:
//!
//! ```ignore
//! use tellur_core::placement::VectorPlacement;
//!
//! background.place_at(Vec2::ZERO);                       // top-left at a point
//! circle.anchored(Anchor::CENTER).snap_to(target_point); // child anchor → point
//! badge.anchored(Anchor::CENTER).snap_to(Anchor::TOP_RIGHT); // child → parent
//! ```
//!
//! The same `anchored().snap_to()` grammar works in both layout worlds:
//! [`SnapTarget::Point`] keeps the child out of flow and measures it
//! unbounded, while [`SnapTarget::Anchor`] fills the finite parent box and
//! resolves the target against that box at render time.

use crate::geometry::{Anchor, Constraints, Rect, Transform, Vec2};
use crate::layer::translate_rect;
use crate::vector::{Group, Node, VectorComponent, VectorGraphic};
use crate::Keyable;

/// The destination used by [`Positioned`].
#[derive(Debug, Clone, Copy, Keyable)]
pub enum SnapTarget {
    /// An absolute point in the parent's coordinate system (canvas world).
    Point(Vec2),
    /// A proportional point on the parent box, resolved from the size chosen
    /// during layout (flow world).
    Anchor(Anchor),
}

impl SnapTarget {
    fn point(self, size: Vec2) -> Vec2 {
        match self {
            Self::Point(point) => point,
            Self::Anchor(anchor) => anchor.point(size),
        }
    }
}

impl From<Vec2> for SnapTarget {
    fn from(point: Vec2) -> Self {
        Self::Point(point)
    }
}

impl From<Anchor> for SnapTarget {
    fn from(anchor: Anchor) -> Self {
        Self::Anchor(anchor)
    }
}

fn positioned_layout(
    target: SnapTarget,
    constraints: Constraints,
    child_layout: impl FnOnce(Constraints) -> Vec2,
) -> Vec2 {
    match target {
        SnapTarget::Point(_) => child_layout(Constraints::UNBOUNDED),
        SnapTarget::Anchor(_) => constraints.fill_size(),
    }
}

fn resolved_position(
    anchor: Anchor,
    target: SnapTarget,
    offset: Vec2,
    size: Vec2,
    child_size: Vec2,
) -> Vec2 {
    child_size.anchored(anchor).snap_to(target.point(size)) + offset
}

fn resolved_paint_bounds(child_bounds: Rect, position: Vec2) -> Rect {
    translate_rect(child_bounds, position)
}

/// Places a [`VectorComponent`] by snapping `anchor` on the child to `target`,
/// then translating it by `offset`.
///
/// A point target is out of flow and reports the child's unbounded intrinsic
/// size. An anchor target reports the finite parent maximum on each axis (and
/// collapses an unbounded axis to zero), so it can position the child relative
/// to the box chosen by a flow container.
#[derive(Clone, Keyable)]
pub struct Positioned {
    pub child: Box<dyn VectorComponent>,
    pub anchor: Anchor,
    pub target: SnapTarget,
    pub offset: Vec2,
}

impl Positioned {
    pub fn new(
        child: impl Into<Box<dyn VectorComponent>>,
        anchor: Anchor,
        target: impl Into<SnapTarget>,
    ) -> Self {
        Self {
            child: child.into(),
            anchor,
            target: target.into(),
            offset: Vec2::ZERO,
        }
    }

    /// Applies a constant pixel translation after snapping.
    pub fn offset(mut self, offset: Vec2) -> Self {
        self.offset = offset;
        self
    }

    fn child_size(&self, size: Vec2) -> Vec2 {
        match self.target {
            SnapTarget::Point(_) => size,
            SnapTarget::Anchor(_) => self.child.layout(Constraints::loose(size)),
        }
    }

    fn position(&self, size: Vec2, child_size: Vec2) -> Vec2 {
        resolved_position(self.anchor, self.target, self.offset, size, child_size)
    }
}

impl VectorComponent for Positioned {
    fn layout(&self, constraints: Constraints) -> Vec2 {
        positioned_layout(self.target, constraints, |c| self.child.layout(c))
    }

    fn paint_bounds(&self, size: Vec2) -> Rect {
        let child_size = self.child_size(size);
        let position = self.position(size, child_size);
        resolved_paint_bounds(self.child.paint_bounds(child_size), position)
    }

    fn render(&self, size: Vec2) -> VectorGraphic {
        let child_size = self.child_size(size);
        let position = self.position(size, child_size);
        let inner = self.child.render(child_size);
        VectorGraphic {
            view_box: resolved_paint_bounds(inner.view_box, position),
            root: Node::Group(Group {
                transform: Transform::translate(position),
                opacity: 1.0,
                children: vec![inner.root],
            }),
        }
    }
}

impl From<Positioned> for Box<dyn VectorComponent> {
    fn from(positioned: Positioned) -> Self {
        Box::new(positioned)
    }
}

/// Extension trait that adds placement methods to every [`VectorComponent`].
///
/// Brought into scope alongside `use tellur_core::vector::VectorComponent`, it
/// lets callers write `circle.place_at(pos)` or
/// `circle.anchored(Anchor::CENTER).snap_to(target)` to obtain a
/// [`Positioned`] component.
pub trait VectorPlacement: VectorComponent + Sized + 'static {
    /// Places the component so its local origin `(0, 0)` lands at `position`.
    fn place_at(self, position: Vec2) -> Positioned {
        Positioned::new(self.boxed(), Anchor::TOP_LEFT, position)
    }

    /// Begins an anchor-based placement: `anchor` picks a point on the
    /// component's box, which a follow-up `snap_to` aligns onto a point or a
    /// parent-box anchor.
    fn anchored(self, anchor: Anchor) -> AnchoredVectorComponent<Self> {
        AnchoredVectorComponent {
            component: self,
            anchor,
        }
    }
}

impl<T: VectorComponent + 'static> VectorPlacement for T {}

/// Intermediate produced by [`VectorPlacement::anchored`]. Holds the component
/// and the chosen anchor until a snap target is provided.
pub struct AnchoredVectorComponent<C: VectorComponent> {
    component: C,
    anchor: Anchor,
}

impl<C: VectorComponent + 'static> AnchoredVectorComponent<C> {
    /// Places the component so the chosen child anchor lands on `target`.
    pub fn snap_to(self, target: impl Into<SnapTarget>) -> Positioned {
        Positioned::new(self.component.boxed(), self.anchor, target)
    }
}

/// Raster counterparts of the vector placement types. Same shape and
/// semantics, operating on `Box<dyn RasterComponent>`.
///
/// A raster [`Positioned`](raster::Positioned) carries its offset purely in its
/// `paint_bounds` origin; `render` delegates to the child untouched, because
/// the parent's `composite_children` pass applies the offset at compositing
/// time (`position + paint_bounds.origin - paint_rect.origin`).
pub mod raster {
    use super::{positioned_layout, resolved_paint_bounds, resolved_position, SnapTarget};
    use crate::geometry::{Anchor, Constraints, Rect, Vec2};
    use crate::raster::{RasterComponent, RasterImage, RasterResidency, Resolution};
    use crate::render_context::{CachePolicy, RenderContext};
    use crate::Keyable;

    /// Raster mirror of [`super::Positioned`].
    #[derive(Clone, Keyable)]
    pub struct Positioned {
        pub child: Box<dyn RasterComponent>,
        pub anchor: Anchor,
        pub target: SnapTarget,
        pub offset: Vec2,
    }

    impl Positioned {
        pub fn new(
            child: impl Into<Box<dyn RasterComponent>>,
            anchor: Anchor,
            target: impl Into<SnapTarget>,
        ) -> Self {
            Self {
                child: child.into(),
                anchor,
                target: target.into(),
                offset: Vec2::ZERO,
            }
        }

        /// Applies a constant pixel translation after snapping.
        pub fn offset(mut self, offset: Vec2) -> Self {
            self.offset = offset;
            self
        }

        fn child_size(&self, size: Vec2) -> Vec2 {
            match self.target {
                SnapTarget::Point(_) => size,
                SnapTarget::Anchor(_) => self.child.layout(Constraints::loose(size)),
            }
        }

        fn position(&self, size: Vec2, child_size: Vec2) -> Vec2 {
            resolved_position(self.anchor, self.target, self.offset, size, child_size)
        }
    }

    impl RasterComponent for Positioned {
        fn layout(&self, constraints: Constraints) -> Vec2 {
            positioned_layout(self.target, constraints, |c| self.child.layout(c))
        }

        fn paint_bounds(&self, size: Vec2) -> Rect {
            let child_size = self.child_size(size);
            let position = self.position(size, child_size);
            resolved_paint_bounds(self.child.paint_bounds(child_size), position)
        }

        fn cache_policy(&self) -> CachePolicy {
            // Position lives entirely in `paint_bounds`; the wrapper's pixels
            // are exactly the child's, so let the child own the cache entry.
            CachePolicy::Transparent
        }

        fn render(
            &self,
            size: Vec2,
            target: Resolution,
            residency: RasterResidency,
            ctx: &mut dyn RenderContext,
        ) -> RasterImage {
            let child_size = self.child_size(size);
            // The resolved translation rides in `paint_bounds`; the parent
            // applies it while compositing this child image.
            ctx.render(self.child.as_ref(), child_size, target, residency)
        }
    }

    impl From<Positioned> for Box<dyn RasterComponent> {
        fn from(positioned: Positioned) -> Self {
            Box::new(positioned)
        }
    }

    /// Raster mirror of [`VectorPlacement`](super::VectorPlacement).
    pub trait RasterPlacement: RasterComponent + Sized + 'static {
        fn place_at(self, position: Vec2) -> Positioned {
            Positioned::new(self.boxed(), Anchor::TOP_LEFT, position)
        }

        fn anchored(self, anchor: Anchor) -> AnchoredRasterComponent<Self> {
            AnchoredRasterComponent {
                component: self,
                anchor,
            }
        }
    }

    impl<T: RasterComponent + 'static> RasterPlacement for T {}

    /// Intermediate produced by [`RasterPlacement::anchored`].
    pub struct AnchoredRasterComponent<C: RasterComponent> {
        component: C,
        anchor: Anchor,
    }

    impl<C: RasterComponent + 'static> AnchoredRasterComponent<C> {
        pub fn snap_to(self, target: impl Into<SnapTarget>) -> Positioned {
            Positioned::new(self.component.boxed(), self.anchor, target)
        }
    }
}

// Re-export the raster placement trait at the module root so existing
// `use tellur_core::placement::RasterPlacement;` paths keep resolving.
pub use raster::RasterPlacement;

#[cfg(test)]
mod tests {
    use super::raster::RasterPlacement;
    use super::*;
    use crate::color::Color;
    use crate::raster::{PixelFormat, RasterComponent, RasterImage, RasterResidency, Resolution};
    use crate::render_context::{PassThrough, RenderContext};
    use crate::shapes::Circle;
    use crate::vector::{Paint, Stroke};

    fn big_circle() -> Circle {
        // Diameter 1440 — taller than a 1080-high canvas.
        Circle::builder()
            .radius(720.0)
            .fill(Paint::Solid(Color::rgb_u8(0, 0, 0)))
            .build()
    }

    fn root_translation(graphic: &VectorGraphic) -> Vec2 {
        let Node::Group(group) = &graphic.root else {
            panic!("positioned vector should render a translating group");
        };
        Vec2(group.transform.tx, group.transform.ty)
    }

    #[test]
    fn place_at_is_top_left_point_with_zero_offset() {
        let placed = big_circle().place_at(Vec2(12.0, 34.0));

        assert_eq!(placed.anchor, Anchor::TOP_LEFT);
        assert_eq!(placed.target, SnapTarget::Point(Vec2(12.0, 34.0)));
        assert_eq!(placed.offset, Vec2::ZERO);
    }

    #[test]
    fn placed_child_is_measured_unbounded() {
        // The canvas hands loose constraints smaller than the circle; the
        // placed child still reports its intrinsic size instead of getting
        // squashed into an ellipse.
        let placed = big_circle().place_at(Vec2::ZERO);
        let size = placed.layout(Constraints::loose(Vec2(1920.0, 1080.0)));
        assert_eq!(size, Vec2(1440.0, 1440.0));
    }

    #[test]
    fn point_target_resolves_from_unbounded_child_and_translates_paint_bounds() {
        let placed = big_circle()
            .anchored(Anchor::CENTER)
            .snap_to(Vec2(960.0, 540.0))
            .offset(Vec2(13.0, 9.0));
        let size = placed.layout(Constraints::loose(Vec2(1920.0, 1080.0)));
        assert_eq!(size, Vec2(1440.0, 1440.0));

        let expected = Rect {
            origin: Vec2(253.0, -171.0),
            size,
        };
        assert_eq!(placed.paint_bounds(size), expected);

        let graphic = placed.render(size);
        assert_eq!(graphic.view_box, expected);
        assert_eq!(root_translation(&graphic), expected.origin);
    }

    #[test]
    fn point_target_preserves_child_stroke_outset_while_translating_bounds() {
        let placed = Circle::builder()
            .radius(10.0)
            .stroke(Stroke::new(Paint::Solid(Color::rgb_u8(0, 0, 0)), 4.0))
            .build()
            .anchored(Anchor::CENTER)
            .snap_to(Vec2(50.0, 40.0))
            .offset(Vec2(3.0, -4.0));
        let size = placed.layout(Constraints::UNBOUNDED);
        let expected = Rect {
            // Child origin is (43,26); the 4 px centered stroke adds a
            // 2 px outset on every edge.
            origin: Vec2(41.0, 24.0),
            size: Vec2(24.0, 24.0),
        };

        assert_eq!(placed.paint_bounds(size), expected);
        let graphic = placed.render(size);
        assert_eq!(graphic.view_box, expected);
        assert_eq!(root_translation(&graphic), Vec2(43.0, 26.0));
    }

    #[test]
    fn anchor_target_fills_finite_constraints_and_collapses_unbounded_axes() {
        let placed = Circle::builder()
            .radius(10.0)
            .fill(Paint::Solid(Color::rgb_u8(0, 0, 0)))
            .build()
            .anchored(Anchor::CENTER)
            .snap_to(Anchor::BOTTOM_RIGHT);

        assert_eq!(
            placed.layout(Constraints::tight(Vec2(100.0, 60.0))),
            Vec2(100.0, 60.0)
        );
        assert_eq!(
            placed.layout(Constraints::loose(Vec2(100.0, 60.0))),
            Vec2(100.0, 60.0)
        );
        assert_eq!(placed.layout(Constraints::UNBOUNDED), Vec2::ZERO);
        assert_eq!(
            placed.layout(Constraints::loose(Vec2(f32::INFINITY, 60.0))),
            Vec2(0.0, 60.0)
        );
    }

    #[test]
    fn anchor_target_resolves_against_own_size_then_applies_offset() {
        let placed = Circle::builder()
            .radius(10.0)
            .fill(Paint::Solid(Color::rgb_u8(0, 0, 0)))
            .build()
            .anchored(Anchor::CENTER)
            .snap_to(Anchor::BOTTOM_RIGHT)
            .offset(Vec2(-5.0, 3.0));
        let size = Vec2(100.0, 60.0);
        let expected = Rect {
            // (100,60) - child center (10,10) + (-5,3)
            origin: Vec2(85.0, 53.0),
            size: Vec2(20.0, 20.0),
        };

        assert_eq!(placed.paint_bounds(size), expected);
        let graphic = placed.render(size);
        assert_eq!(graphic.view_box, expected);
        assert_eq!(root_translation(&graphic), expected.origin);
    }

    #[derive(Clone, PartialEq, Hash)]
    struct FixedRaster;

    impl RasterComponent for FixedRaster {
        fn layout(&self, constraints: Constraints) -> Vec2 {
            constraints.constrain(Vec2(2000.0, 2000.0))
        }

        fn render(
            &self,
            _s: Vec2,
            t: Resolution,
            _residency: RasterResidency,
            _ctx: &mut dyn RenderContext,
        ) -> RasterImage {
            let bytes = (t.width as usize) * (t.height as usize) * 4;
            RasterImage::cpu(t.width, t.height, PixelFormat::Rgba8, vec![0; bytes])
        }
    }

    #[test]
    fn raster_placed_child_is_measured_unbounded() {
        let placed = FixedRaster
            .place_at(Vec2(7.0, 11.0))
            .offset(Vec2(3.0, -2.0));
        let size = placed.layout(Constraints::loose(Vec2(100.0, 100.0)));
        assert_eq!(size, Vec2(2000.0, 2000.0));
        assert_eq!(
            placed.paint_bounds(size),
            Rect {
                origin: Vec2(10.0, 9.0),
                size,
            }
        );
    }

    #[derive(Clone, PartialEq, Hash)]
    struct SmallRaster;

    impl RasterComponent for SmallRaster {
        fn layout(&self, constraints: Constraints) -> Vec2 {
            constraints.constrain(Vec2(20.0, 10.0))
        }

        fn render(
            &self,
            size: Vec2,
            target: Resolution,
            _residency: RasterResidency,
            _ctx: &mut dyn RenderContext,
        ) -> RasterImage {
            let pixel = [size.0 as u8, size.1 as u8, 0, 255];
            RasterImage::cpu(
                target.width,
                target.height,
                PixelFormat::Rgba8,
                pixel.repeat((target.width * target.height) as usize),
            )
        }
    }

    #[test]
    fn raster_anchor_target_uses_box_for_position_but_child_size_for_pixels() {
        let placed = SmallRaster
            .anchored(Anchor::CENTER)
            .snap_to(Anchor::BOTTOM_RIGHT)
            .offset(Vec2(-5.0, 3.0));
        let size = placed.layout(Constraints::tight(Vec2(100.0, 60.0)));

        assert_eq!(
            placed.paint_bounds(size),
            Rect {
                origin: Vec2(85.0, 58.0),
                size: Vec2(20.0, 10.0),
            }
        );
        let image = placed
            .render(
                size,
                Resolution::new(1, 1),
                RasterResidency::Cpu,
                &mut PassThrough,
            )
            .into_cpu()
            .expect("small raster stays on CPU");
        assert_eq!(image.pixels.as_ref(), &[20, 10, 0, 255]);
    }
}