bevy_vello 0.14.0

Render assets and scenes in Bevy with Vello
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
use std::borrow::Cow;

use bevy::{prelude::*, reflect::TypePath, render::render_asset::RenderAsset};
use parley::{
    FontSettings, FontStyle, FontVariation, Layout, PositionedLayoutItem, RangedBuilder,
    StyleProperty,
};
use vello::{
    Scene,
    kurbo::Affine,
    peniko::{Brush, Fill},
};

use super::{VelloFontAxes, VelloTextAnchor, context::LOCAL_FONT_CONTEXT};
use crate::{
    integrations::text::context::{LOCAL_LAYOUT_CONTEXT, get_global_font_context},
    prelude::{VelloTextAlign, VelloTextStyle},
};

#[derive(Asset, TypePath, Debug, Clone)]
pub struct VelloFont {
    /// Defaults to Bevy's bevy_text default font family name.
    ///
    /// https://github.com/bevyengine/bevy/tree/v0.15.3/crates/bevy_text/src/FiraMono-subset.ttf
    pub(crate) family_name: String,
    pub bytes: Vec<u8>,
}

impl RenderAsset for VelloFont {
    type SourceAsset = VelloFont;

    type Param = ();

    fn prepare_asset(
        source_asset: Self::SourceAsset,
        _asset_id: AssetId<Self::SourceAsset>,
        _param: &mut bevy::ecs::system::SystemParamItem<Self::Param>,
        _previous_asset: Option<&Self>,
    ) -> Result<Self, bevy::render::render_asset::PrepareAssetError<Self::SourceAsset>> {
        Ok(source_asset)
    }
}

impl VelloFont {
    pub fn new(font_data: Vec<u8>) -> Self {
        Self {
            bytes: font_data,
            family_name: "Fira Mono".to_string(),
        }
    }

    pub fn layout(
        &self,
        value: &str,
        style: &VelloTextStyle,
        text_align: VelloTextAlign,
        max_advance: Option<f32>,
    ) -> Layout<Brush> {
        LOCAL_FONT_CONTEXT.with_borrow_mut(|font_context| {
            if font_context.is_none() {
                *font_context = Some(get_global_font_context().clone());
            }

            let font_context = font_context.as_mut().unwrap();

            LOCAL_LAYOUT_CONTEXT.with_borrow_mut(|layout_context| {
                let mut builder = layout_context.ranged_builder(font_context, value, 1.0, true);

                apply_font_styles(&mut builder, style);
                apply_variable_axes(&mut builder, &style.font_axes);

                builder.push_default(StyleProperty::FontStack(parley::FontStack::Single(
                    parley::FontFamily::Named(Cow::Owned(self.family_name.clone())),
                )));

                let mut layout = builder.build(value);
                layout.break_all_lines(max_advance);
                layout.align(
                    max_advance,
                    text_align.into(),
                    parley::AlignmentOptions::default(),
                );

                layout
            })
        })
    }

    #[expect(clippy::too_many_arguments, reason = "Common lint in bevy")]
    pub(crate) fn render(
        &self,
        scene: &mut Scene,
        mut transform: Affine,
        value: &str,
        style: &VelloTextStyle,
        text_align: VelloTextAlign,
        max_advance: Option<f32>,
        text_anchor: VelloTextAnchor,
        ui_content: Option<Vec2>,
        clip: Option<vello::kurbo::Rect>,
    ) {
        let layout = self.layout(value, style, text_align, max_advance);

        let text_w = layout.width() as f64;
        let text_h = layout.height() as f64;

        let (dx, dy) = if let Some(content_size) = ui_content {
            let offset = compute_ui_anchor_offset(
                text_anchor,
                text_w,
                text_h,
                content_size.x,
                content_size.y,
            );
            // Transform the logical offset through the linear part of the affine
            // (rotation + scale), so anchor positioning is correct under any transform.
            let c = transform.as_coeffs();
            (
                c[0] * offset.0 + c[2] * offset.1,
                c[1] * offset.0 + c[3] * offset.1,
            )
        } else {
            compute_world_anchor_offset(text_anchor, text_w, text_h)
        };

        transform = transform.then_translate(vello::kurbo::Vec2::new(dx, dy));

        // Precompute Y-axis clip range for vertical line culling (CPU-side).
        // Inverse-transform the clip rect corners to layout space and compute
        // their vertical AABB. Horizontal clipping is handled by Vello's
        // GPU-side clip path.
        let cull_y: Option<(f64, f64)> = clip.and_then(|r| {
            let inv = transform.inverse();
            // A zero-determinant affine has no inverse — skip culling.
            // Affine::inverse() returns an identity-ish result for singular
            // matrices, so check the determinant explicitly.
            let c = transform.as_coeffs();
            let det = c[0] * c[3] - c[1] * c[2];
            if det.abs() < f64::EPSILON {
                return None;
            }
            // Map clip-rect corners to layout space, extract Y-axis AABB.
            let corners = [
                inv * vello::kurbo::Point::new(r.x0, r.y0),
                inv * vello::kurbo::Point::new(r.x1, r.y0),
                inv * vello::kurbo::Point::new(r.x0, r.y1),
                inv * vello::kurbo::Point::new(r.x1, r.y1),
            ];
            let y_min = corners.iter().map(|p| p.y).fold(f64::INFINITY, f64::min);
            let y_max = corners
                .iter()
                .map(|p| p.y)
                .fold(f64::NEG_INFINITY, f64::max);
            Some((y_min, y_max))
        });

        'lines: for line in layout.lines() {
            // Vertical line culling using Parley's LineMetrics. min_coord is the
            // line top (including ascenders), max_coord is the bottom (including
            // descenders). Lines emit top-to-bottom, so exceeding y_max means
            // all remaining lines are outside (early exit).
            if let Some((y_min, y_max)) = cull_y {
                let lm = line.metrics();
                if (lm.max_coord as f64) < y_min {
                    continue;
                }
                if (lm.min_coord as f64) > y_max {
                    break 'lines;
                }
            }

            for item in line.items() {
                let PositionedLayoutItem::GlyphRun(glyph_run) = item else {
                    continue;
                };

                let mut x = glyph_run.offset();
                let y = glyph_run.baseline();
                let run = glyph_run.run();
                let font = run.font();
                let font_size = run.font_size();
                let synthesis = run.synthesis();
                let glyph_xform = synthesis
                    .skew()
                    .map(|angle| Affine::skew(angle.to_radians().tan() as f64, 0.0));

                scene
                    .draw_glyphs(font)
                    .brush(&style.brush)
                    .hint(true)
                    .transform(transform)
                    .glyph_transform(glyph_xform)
                    .font_size(font_size)
                    .normalized_coords(run.normalized_coords())
                    .draw(
                        Fill::NonZero,
                        glyph_run.glyphs().map(|glyph| {
                            let gx = x + glyph.x;
                            let gy = y - glyph.y;
                            x += glyph.advance;
                            vello::Glyph {
                                id: glyph.id as _,
                                x: gx,
                                y: gy,
                            }
                        }),
                    );
            }
        }
    }
}

/// Applies the font styles to the text
///
/// font_size - font size
/// line_height - line height
/// word_spacing - extra spacing between words
/// letter_spacing - extra spacing between letters
fn apply_font_styles(builder: &mut RangedBuilder<'_, Brush>, style: &VelloTextStyle) {
    builder.push_default(StyleProperty::FontSize(style.font_size));
    builder.push_default(StyleProperty::LineHeight(
        parley::LineHeight::MetricsRelative(style.line_height),
    ));
    builder.push_default(StyleProperty::WordSpacing(style.word_spacing));
    builder.push_default(StyleProperty::LetterSpacing(style.letter_spacing));
}

/// Applies the variable axes to the text
///
/// wght - font weight
/// wdth - font width
/// opsz - optical size
/// ital - italic
/// slnt - slant
/// GRAD - grade
/// XOPQ - thick stroke
/// YOPQ - thin stroke
/// YTUC - uppercase height
/// YTLC - lowercase height
/// YTAS - ascender height
/// YTDE - descender depth
/// YTFI - figure height
fn apply_variable_axes(builder: &mut RangedBuilder<'_, Brush>, axes: &VelloFontAxes) {
    let mut variable_axes: Vec<FontVariation> = vec![];

    if let Some(weight) = axes.weight {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("wght"),
            value: weight,
        });
    }

    if let Some(width) = axes.width {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("wdth"),
            value: width,
        });
    }

    if let Some(optical_size) = axes.optical_size {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("opsz"),
            value: optical_size,
        });
    }

    if let Some(grade) = axes.grade {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("GRAD"),
            value: grade,
        });
    }

    if let Some(thick_stroke) = axes.thick_stroke {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("XOPQ"),
            value: thick_stroke,
        });
    }

    if let Some(thin_stroke) = axes.thin_stroke {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("YOPQ"),
            value: thin_stroke,
        });
    }

    if let Some(counter_width) = axes.counter_width {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("XTRA"),
            value: counter_width,
        });
    }

    if let Some(uppercase_height) = axes.uppercase_height {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("YTUC"),
            value: uppercase_height,
        });
    }

    if let Some(lowercase_height) = axes.lowercase_height {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("YTLC"),
            value: lowercase_height,
        });
    }

    if let Some(ascender_height) = axes.ascender_height {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("YTAS"),
            value: ascender_height,
        });
    }

    if let Some(descender_depth) = axes.descender_depth {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("YTDE"),
            value: descender_depth,
        });
    }

    if let Some(figure_height) = axes.figure_height {
        variable_axes.push(parley::swash::Setting {
            tag: parley::swash::tag_from_str_lossy("YTFI"),
            value: figure_height,
        });
    }

    if axes.italic {
        builder.push_default(StyleProperty::FontStyle(FontStyle::Italic));
    } else if axes.slant.is_some() {
        builder.push_default(StyleProperty::FontStyle(FontStyle::Oblique(axes.slant)));
    }

    builder.push_default(StyleProperty::FontVariations(FontSettings::List(
        variable_axes.into(),
    )));
}

/// Computes the (dx, dy) translation offset for world-space text anchoring.
///
/// Positions the text bounding box relative to the transform origin.
/// `TopLeft=(0,0)` means text grows down-right from origin.
pub(crate) fn compute_world_anchor_offset(
    text_anchor: VelloTextAnchor,
    text_w: f64,
    text_h: f64,
) -> (f64, f64) {
    match text_anchor {
        VelloTextAnchor::TopLeft => (0.0, 0.0),
        VelloTextAnchor::Left => (0.0, -text_h / 2.0),
        VelloTextAnchor::BottomLeft => (0.0, -text_h),
        VelloTextAnchor::Top => (-text_w / 2.0, 0.0),
        VelloTextAnchor::Center => (-text_w / 2.0, -text_h / 2.0),
        VelloTextAnchor::Bottom => (-text_w / 2.0, -text_h),
        VelloTextAnchor::TopRight => (-text_w, 0.0),
        VelloTextAnchor::Right => (-text_w, -text_h / 2.0),
        VelloTextAnchor::BottomRight => (-text_w, -text_h),
    }
}

/// Computes the (dx, dy) translation offset for UI text anchoring.
///
/// Aligns text within the node's content box. The UiGlobalTransform places the
/// origin at the node's center, so we compute offsets relative to that center
/// to position text according to the anchor.
pub(crate) fn compute_ui_anchor_offset(
    text_anchor: VelloTextAnchor,
    text_w: f64,
    text_h: f64,
    node_w: f32,
    node_h: f32,
) -> (f64, f64) {
    let node_w = node_w as f64;
    let node_h = node_h as f64;
    let top_left_x = -node_w / 2.0;
    let top_left_y = -node_h / 2.0;

    let (anchor_x, anchor_y) = match text_anchor {
        VelloTextAnchor::TopLeft => (0.0, 0.0),
        VelloTextAnchor::Top => ((node_w - text_w) / 2.0, 0.0),
        VelloTextAnchor::TopRight => (node_w - text_w, 0.0),
        VelloTextAnchor::Left => (0.0, (node_h - text_h) / 2.0),
        VelloTextAnchor::Center => ((node_w - text_w) / 2.0, (node_h - text_h) / 2.0),
        VelloTextAnchor::Right => (node_w - text_w, (node_h - text_h) / 2.0),
        VelloTextAnchor::BottomLeft => (0.0, node_h - text_h),
        VelloTextAnchor::Bottom => ((node_w - text_w) / 2.0, node_h - text_h),
        VelloTextAnchor::BottomRight => (node_w - text_w, node_h - text_h),
    };

    (top_left_x + anchor_x, top_left_y + anchor_y)
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- World-space text ---

    #[test]
    fn world_center_offsets_by_half_text_dims() {
        let (dx, dy) = compute_world_anchor_offset(VelloTextAnchor::Center, 200.0, 40.0);
        assert_eq!((dx, dy), (-100.0, -20.0));
    }

    #[test]
    fn world_top_left_is_zero() {
        let (dx, dy) = compute_world_anchor_offset(VelloTextAnchor::TopLeft, 200.0, 40.0);
        assert_eq!((dx, dy), (0.0, 0.0));
    }

    #[test]
    fn world_bottom_right_offsets_by_full_text_dims() {
        let (dx, dy) = compute_world_anchor_offset(VelloTextAnchor::BottomRight, 200.0, 40.0);
        assert_eq!((dx, dy), (-200.0, -40.0));
    }

    // --- UI text ---
    // Anchor positions text within the node's content box.
    // Transform origin is at the node's CENTER (UiGlobalTransform origin).
    //
    // Given: node 400x200, text 200x40
    // Top-left corner is at (-200, -100) from center
    //
    // TopLeft:     (-200, -100)
    // Center:      (-100, -20)
    // BottomRight: (0, 60)

    const NODE_W: f32 = 400.0;
    const NODE_H: f32 = 200.0;
    const TEXT_W: f64 = 200.0;
    const TEXT_H: f64 = 40.0;

    #[test]
    fn ui_top_left_positions_at_node_top_left() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::TopLeft, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (-200.0, -100.0));
    }

    #[test]
    fn ui_center_centers_text_in_node() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::Center, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (-100.0, -20.0));
    }

    #[test]
    fn ui_bottom_right_positions_at_node_bottom_right() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::BottomRight, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (0.0, 60.0));
    }

    #[test]
    fn ui_left_vertically_centers_at_left_edge() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::Left, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (-200.0, -20.0));
    }

    #[test]
    fn ui_top_right_positions_at_node_top_right() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::TopRight, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (0.0, -100.0));
    }

    #[test]
    fn ui_bottom_centers_at_bottom_edge() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::Bottom, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (-100.0, 60.0));
    }

    #[test]
    fn ui_top_centers_at_top_edge() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::Top, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (-100.0, -100.0));
    }

    #[test]
    fn ui_right_vertically_centers_at_right_edge() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::Right, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (0.0, -20.0));
    }

    #[test]
    fn ui_bottom_left_positions_at_node_bottom_left() {
        let (dx, dy) =
            compute_ui_anchor_offset(VelloTextAnchor::BottomLeft, TEXT_W, TEXT_H, NODE_W, NODE_H);
        assert_eq!((dx, dy), (-200.0, 60.0));
    }

    /// The full UI anchor pipeline — compute logical offset, transform through
    /// the affine's linear part, apply to affine — must produce correct
    /// translations at non-1x scale factors.
    #[test]
    fn ui_anchor_pipeline_correct_at_2x_dpi() {
        let scale = 2.0_f64;
        // Affine for a UI node at (250, 150) logical on a 2x display.
        let affine = Affine::new([scale, 0.0, 0.0, scale, 500.0, 300.0]);

        // Node is 200x100 logical, text is 100x20.
        // Center anchor: offset = (-50, -10) logical.
        let offset = compute_ui_anchor_offset(VelloTextAnchor::Center, 100.0, 20.0, 200.0, 100.0);

        // Apply linear part of affine to offset — same path as render().
        let c = affine.as_coeffs();
        let dx = c[0] * offset.0 + c[2] * offset.1;
        let dy = c[1] * offset.0 + c[3] * offset.1;
        let result = affine.then_translate(vello::kurbo::Vec2::new(dx, dy));
        let c = result.as_coeffs();

        // Text origin should land at (250-50, 150-10) = (200, 140) logical,
        // which is (400, 280) physical.
        assert_eq!((c[4], c[5]), (400.0, 280.0));
    }

    /// Anchor offset must be rotated correctly when the node's affine includes
    /// a rotation. A 90-degree CCW rotation swaps axes, so the logical x-offset
    /// becomes a physical y-offset and vice versa.
    #[test]
    fn ui_anchor_pipeline_correct_under_rotation() {
        let scale = 2.0_f64;
        let cos90 = 0.0_f64;
        let sin90 = 1.0_f64;
        // 90-degree CCW rotation with 2x scale, node center at (500, 300) physical.
        // Affine columns: [cos*s, sin*s, -sin*s, cos*s, tx, ty]
        let affine = Affine::new([
            cos90 * scale,
            sin90 * scale,
            -sin90 * scale,
            cos90 * scale,
            500.0,
            300.0,
        ]);

        // Node is 200x100 logical, text is 100x20.
        // Center anchor: offset = (-50, -10) logical.
        let offset = compute_ui_anchor_offset(VelloTextAnchor::Center, 100.0, 20.0, 200.0, 100.0);

        // Apply linear part of affine to offset — same path as render().
        let c = affine.as_coeffs();
        let dx = c[0] * offset.0 + c[2] * offset.1;
        let dy = c[1] * offset.0 + c[3] * offset.1;
        let result = affine.then_translate(vello::kurbo::Vec2::new(dx, dy));
        let c = result.as_coeffs();

        // Under 90-degree CCW rotation, logical (-50, -10) maps to physical:
        //   screen_x += cos*s*(-50) + (-sin*s)*(-10) = 0*(-50) + (-2)*(-10) = +20
        //   screen_y += sin*s*(-50) + cos*s*(-10)    = 2*(-50) + 0*(-10)    = -100
        // So text origin lands at (500+20, 300-100) = (520, 200) physical.
        assert_eq!((c[4], c[5]), (520.0, 200.0));
    }

    #[test]
    fn ui_center_is_origin_when_text_fills_node() {
        let (ui_dx, ui_dy) =
            compute_ui_anchor_offset(VelloTextAnchor::Center, 400.0, 200.0, 400.0, 200.0);
        assert_eq!((ui_dx, ui_dy), (-200.0, -100.0));

        let (ui_tl_dx, ui_tl_dy) =
            compute_ui_anchor_offset(VelloTextAnchor::TopLeft, 400.0, 200.0, 400.0, 200.0);
        assert_eq!((ui_tl_dx, ui_tl_dy), (-200.0, -100.0));

        let (w_dx, w_dy) = compute_world_anchor_offset(VelloTextAnchor::TopLeft, 400.0, 200.0);
        assert_eq!((w_dx, w_dy), (0.0, 0.0));
    }
}