anyrender_vello_gpu 0.12.0

vello_gpu (formerly vello_hybrid) backend for anyrender
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use std::sync::Arc;

use crate::common::{DEFAULT_TOLERANCE, ImageCache, LayerKind, supports_glyph_caching};
use anyrender::{
    AntiAliasing, Filter, MaskMode, NormalizedCoord, Paint, PaintRef, PaintScene, RenderContext,
    ResourceId,
};
use glifo::FontEmbolden;
use kurbo::{Affine, Diagonal2, Rect, Shape, Stroke};
use peniko::{BlendMode, Color, Compose, Fill, FontData, ImageBrush, ImageData, Mix, StyleRef};
use rustc_hash::FxHashSet;
use vello_common::{
    TextureId,
    paint::{ImageSource, PaintType, TextureRegion},
};
use vello_gpu::{Resources, TextureBindings};
use wgpu::{
    Device, Extent3d, Origin3d, Queue, TexelCopyBufferLayout, TexelCopyTextureInfo, Texture,
    TextureAspect, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, TextureView,
    TextureViewDescriptor,
};
use wgpu_context::DeviceHandle;

/// Snaps glyph origins to the horizontal subpixel grid of the platform's native text renderer.
///
/// CoreGraphics (and so Safari and other native Apple text) floors each glyph origin's device x
/// to a grid that depends on the device font size: 1/4px at 9-11ppem, 1/3px at 12-16ppem, 1/2px
/// at 17-33ppem and whole pixels from 34ppem. Vello positions glyphs continuously, so without
/// this, stems land up to 1/2px away from native ones. Elsewhere, x is rounded to the nearest
/// 1/4px, as Skia does for horizontal text (and so Chrome off Apple). On Apple, CoreGraphics
/// also floors the device y of the baseline to a whole pixel (y up, so the baseline moves down
/// in Vello's y-down device space); off Apple, y is not snapped.
///
/// CoreGraphics font smoothing also dilates glyphs upwards only: stems grow by the same amount
/// on both sides in x, but the bottom edge stays on the baseline and the top grows by twice
/// that. Vello's embolden grows both sides in y, so emboldened glyphs are raised by the
/// vertical embolden amount, which keeps baselines and other bottom edges as crisp as native.
#[derive(Clone, Copy)]
pub(crate) struct GlyphOriginSnap {
    scale_x: f64,
    offset_x: f64,
    positions_per_px: f64,
    /// Skia rounds to the nearest grid position; CoreGraphics floors.
    round_x: f64,
    /// The transform's y scale and offset when device y is snapped, as on Apple.
    snap_y: Option<(f64, f64)>,
    lift_y: f64,
}

impl GlyphOriginSnap {
    pub(crate) fn new(transform: Affine, font_size: f32, embolden: kurbo::Vec2) -> Option<Self> {
        if cfg!(any(target_os = "macos", target_os = "ios")) {
            Self::core_graphics(transform, font_size, embolden)
        } else {
            Self::skia(transform)
        }
    }

    fn skia(transform: Affine) -> Option<Self> {
        let [a, b, c, _, e, _] = transform.as_coeffs();
        if b != 0.0 || c != 0.0 || a <= 0.0 {
            return None;
        }
        Some(Self {
            scale_x: a,
            offset_x: e,
            positions_per_px: 4.0,
            round_x: 0.5,
            snap_y: None,
            lift_y: 0.0,
        })
    }

    fn core_graphics(transform: Affine, font_size: f32, embolden: kurbo::Vec2) -> Option<Self> {
        let [a, b, c, d, e, f] = transform.as_coeffs();
        let ppem = font_size as f64 * d.abs();
        if b != 0.0 || c != 0.0 || a <= 0.0 || !ppem.is_finite() || ppem <= 0.0 {
            return None;
        }
        Some(Self {
            scale_x: a,
            offset_x: e,
            // CoreGraphics' grid gets coarser as glyphs grow: n = ceil(100 / (3 * ppem)), at most 5.
            positions_per_px: (100.0 / (3.0 * ppem)).ceil().min(5.0),
            round_x: 0.0,
            snap_y: Some((d, f)),
            // glifo scales the embolden amount with the transform, so it is in user units.
            lift_y: embolden.y * d.signum(),
        })
    }

    pub(crate) fn x(self, x: f32) -> f32 {
        let device_x = self.scale_x * x as f64 + self.offset_x;
        let snapped =
            (device_x * self.positions_per_px + self.round_x).floor() / self.positions_per_px;
        ((snapped - self.offset_x) / self.scale_x) as f32
    }

    pub(crate) fn y(self, y: f32) -> f32 {
        let mut y = y as f64;
        if let Some((scale_y, offset_y)) = self.snap_y {
            // CoreGraphics tolerates about 1/1000 device px before moving to the next pixel.
            let snapped = (scale_y * y + offset_y - 1.0 / 1024.0).ceil();
            y = (snapped - offset_y) / scale_y;
        }
        (y - self.lift_y) as f32
    }
}

fn anyrender_paint_to_vello_gpu_paint<'a>(
    paint: PaintRef<'a>,
    image_manager: &mut ImageManager<'_>,
) -> PaintType {
    let texture_bindings = &*image_manager.texture_bindings;
    match paint {
        Paint::Solid(alpha_color) => PaintType::Solid(alpha_color),
        Paint::Gradient(gradient) => PaintType::Gradient(gradient.clone()),

        Paint::Image(image_brush) => {
            let Some((texture_id, may_have_transparency)) =
                image_manager.upload_image(image_brush.image)
            else {
                return PaintType::Solid(Color::TRANSPARENT);
            };
            PaintType::Image(ImageBrush {
                image: ImageSource::external_texture(
                    texture_id,
                    TextureRegion::Full,
                    may_have_transparency,
                ),
                sampler: image_brush.sampler,
            })
        }

        Paint::Resource(brush) if texture_bindings.contains(brush.image) => {
            PaintType::Image(ImageBrush {
                image: ImageSource::external_texture(
                    TextureId(brush.image.into_ffi()),
                    TextureRegion::Full,
                    true,
                ),
                sampler: brush.sampler,
            })
        }
        Paint::Resource(_) => PaintType::Solid(peniko::color::palette::css::TRANSPARENT),

        // TODO: custom paint
        Paint::Custom(_) => PaintType::Solid(peniko::color::palette::css::TRANSPARENT),
        // Patterns are drawn with `fill_pattern_tiles`
        Paint::Pattern(_) => PaintType::Solid(peniko::color::palette::css::TRANSPARENT),
    }
}

/// Textures bound for rendering (uploaded images and registered custom
/// resources), kept in a persistent [`TextureBindings`] keyed by
/// `TextureId(resource_id.into_ffi())`.
#[derive(Debug, Default)]
pub struct ExternalTextures {
    bindings: TextureBindings,
    ids: FxHashSet<ResourceId>,
}

impl ExternalTextures {
    pub fn insert(&mut self, id: ResourceId, view: TextureView) {
        self.ids.insert(id);
        self.bindings.insert(TextureId(id.into_ffi()), view);
    }

    pub fn remove(&mut self, id: ResourceId) {
        if self.ids.remove(&id) {
            self.bindings.remove(TextureId(id.into_ffi()));
        }
    }

    pub fn contains(&self, id: ResourceId) -> bool {
        self.ids.contains(&id)
    }

    pub fn bindings(&self) -> &TextureBindings {
        &self.bindings
    }
}

pub struct ImageManager<'a> {
    pub(crate) resources: &'a mut Resources,
    pub(crate) device: &'a Device,
    pub(crate) queue: &'a Queue,
    pub(crate) cache: &'a mut ImageCache,
    pub(crate) texture_bindings: &'a mut ExternalTextures,
}

impl<'a> ImageManager<'a> {
    pub fn new(
        resources: &'a mut Resources,
        device: &'a Device,
        queue: &'a Queue,
        cache: &'a mut ImageCache,
        texture_bindings: &'a mut ExternalTextures,
    ) -> Self {
        Self {
            resources,
            device,
            queue,
            cache,
            texture_bindings,
        }
    }

    /// Upload `image` to its own texture and bind it for rendering, returning
    /// its [`TextureId`] and transparency hint.
    pub(crate) fn upload_image(&mut self, image: &ImageData) -> Option<(TextureId, bool)> {
        if !self.cache.fits(image.width, image.height) {
            return None;
        }
        let peniko_id = image.data.id();

        // Try to get TextureId from cache first
        if let Some(cached) = self.cache.get(peniko_id) {
            return Some(cached);
        };

        // Convert ImageData to Pixmap
        let ImageSource::Pixmap(pixmap) = ImageSource::from_peniko_image_data(image) else {
            unreachable!(); // ImageSource::from_peniko_image_data always returns a Pixmap
        };
        let may_have_transparency = pixmap.may_have_transparency();

        // Upload the pixmap to its own texture.
        let texture = self.device.create_texture(&TextureDescriptor {
            label: Some("AnyRender uploaded image"),
            size: Extent3d {
                width: u32::from(pixmap.width()),
                height: u32::from(pixmap.height()),
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: TextureDimension::D2,
            format: TextureFormat::Rgba8Unorm,
            usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
            view_formats: &[],
        });
        self.queue.write_texture(
            TexelCopyTextureInfo {
                texture: &texture,
                mip_level: 0,
                origin: Origin3d::ZERO,
                aspect: TextureAspect::All,
            },
            pixmap.data_as_u8_slice(),
            TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(4 * u32::from(pixmap.width())),
                rows_per_image: Some(u32::from(pixmap.height())),
            },
            Extent3d {
                width: u32::from(pixmap.width()),
                height: u32::from(pixmap.height()),
                depth_or_array_layers: 1,
            },
        );

        // Bind the texture and remember the TextureId in the cache.
        let resource_id = ResourceId::new();
        self.texture_bindings.insert(
            resource_id,
            texture.create_view(&TextureViewDescriptor::default()),
        );
        let texture_id = TextureId(resource_id.into_ffi());
        self.cache.insert(
            &image.data,
            texture_id,
            may_have_transparency,
            image.width,
            image.height,
        );

        Some((texture_id, may_have_transparency))
    }

    /// Evict unused images after encoding the frame's rendering.
    pub fn finish_frame(&mut self) {
        for id in self.cache.finish_frame() {
            // Dropping the binding drops the last handle to the texture.
            self.texture_bindings.remove(ResourceId::from_ffi(id.0));
        }
    }
}

pub struct VelloGpuScenePainter<'s> {
    pub(crate) scene: &'s mut vello_gpu::Scene,
    pub(crate) layer_stack: Vec<LayerKind>,
    pub(crate) image_manager: ImageManager<'s>,
    pub(crate) device_handle: &'s DeviceHandle,
    pub(crate) glyph_caching: bool,
}

impl VelloGpuScenePainter<'_> {
    fn cull_rect(&self) -> Option<Rect> {
        Some(Rect::new(
            0.0,
            0.0,
            f64::from(self.scene.width()),
            f64::from(self.scene.height()),
        ))
    }

    pub fn new<'s>(
        scene: &'s mut vello_gpu::Scene,
        image_manager: ImageManager<'s>,
        device_handle: &'s DeviceHandle,
    ) -> VelloGpuScenePainter<'s> {
        VelloGpuScenePainter {
            scene,
            layer_stack: Vec::with_capacity(16),
            image_manager,
            device_handle,
            glyph_caching: false,
        }
    }

    /// Enable or disable caching of rasterized glyphs in Vello's glyph atlas.
    ///
    /// Defaults to `false`.
    ///
    /// Note: Vello considers atlas-backed glyph caching experimental.
    pub fn with_glyph_caching(mut self, enabled: bool) -> Self {
        self.glyph_caching = enabled;
        self
    }

    fn convert_paint(&mut self, paint: PaintRef<'_>) -> PaintType {
        anyrender_paint_to_vello_gpu_paint(paint, &mut self.image_manager)
    }
}

impl RenderContext for VelloGpuScenePainter<'_> {
    fn try_register_custom_resource(
        &mut self,
        resource: Box<dyn std::any::Any>,
    ) -> Result<ResourceId, anyrender::RegisterResourceError> {
        // Try to downcast as Texture
        match resource.downcast::<Texture>() {
            Ok(texture) => {
                let id = ResourceId::new();
                let texture_view = texture.create_view(&TextureViewDescriptor::default());
                self.image_manager.texture_bindings.insert(id, texture_view);
                Ok(id)
            }
            Err(resource) => {
                // Else try to downcast as TextureView
                if let Ok(texture_view) = resource.downcast::<TextureView>() {
                    let id = ResourceId::new();
                    self.image_manager
                        .texture_bindings
                        .insert(id, *texture_view);
                    Ok(id)
                }
                // Else return error
                else {
                    Err(anyrender::RegisterResourceErrorKind::UnsupportedResourceKind.into())
                }
            }
        }
    }

    fn unregister_resource(&mut self, resource_id: ResourceId) {
        self.image_manager.texture_bindings.remove(resource_id);
    }

    fn renderer_specific_context(&self) -> Option<Box<dyn std::any::Any>> {
        Some(Box::new(self.device_handle.clone()) as _)
    }
}

impl PaintScene for VelloGpuScenePainter<'_> {
    fn reset(&mut self) {
        self.scene.reset();
    }

    fn push_layer(
        &mut self,
        fill: Fill,
        blend: impl Into<BlendMode>,
        alpha: f32,
        transform: Affine,
        clip: &impl Shape,
        filter: Option<Arc<Filter>>,
        _backdrop_filter: Option<Arc<Filter>>,
    ) {
        let (filter, region) = match filter.as_deref().and_then(crate::common::convert_filter) {
            Some(converted) => (
                Some(converted),
                filter.and_then(|f| Some(f.nodes().first()?.region)),
            ),
            None => (None, None),
        };
        self.scene.set_transform(transform);
        if let Some(region) = &region {
            self.scene.push_clip_rect(region);
        }
        self.scene.set_fill_rule(fill);
        if let Some(rect) = clip.as_rect() {
            self.scene.push_clip_rect(&rect);
        } else {
            self.scene
                .push_clip_path(&clip.into_path(DEFAULT_TOLERANCE));
        }
        self.scene
            .push_layer(None, Some(blend.into()), Some(alpha), None, filter);
        match &region {
            Some(region) => {
                self.scene.push_clip_rect(region);
                self.layer_stack.push(LayerKind::FilterRegionLayer);
            }
            None => self.layer_stack.push(LayerKind::Layer),
        }
    }

    fn push_mask_layer(
        &mut self,
        fill: Fill,
        _mode: MaskMode,
        transform: Affine,
        clip: &impl Shape,
    ) {
        let blend = BlendMode::new(Mix::Normal, Compose::DestIn);

        // Everything outside of the clip is masked out
        let everything = Rect::new(-1e6, -1e6, 1e6, 1e6);
        self.push_layer(
            Fill::NonZero,
            blend,
            1.0,
            Affine::IDENTITY,
            &everything,
            None,
            None,
        );
        self.fill(
            fill,
            transform,
            Color::BLACK,
            None,
            clip,
            AntiAliasing::Enabled,
        );
        self.pop_layer();

        // Luminance masks aren't supported yet, so they are treated as alpha masks
        self.push_layer(fill, blend, 1.0, transform, clip, None, None);
    }

    fn push_clip_layer(&mut self, fill: Fill, transform: Affine, clip: &impl Shape) {
        self.scene.set_transform(transform);
        self.scene.set_fill_rule(fill);
        self.layer_stack.push(LayerKind::Clip);
        if let Some(rect) = clip.as_rect() {
            self.scene.push_clip_rect(&rect);
        } else {
            self.scene
                .push_clip_path(&clip.into_path(DEFAULT_TOLERANCE));
        }
    }

    fn pop_layer(&mut self) {
        if let Some(kind) = self.layer_stack.pop() {
            match kind {
                LayerKind::Layer => {
                    self.scene.pop_layer();
                    self.scene.pop_clip();
                }
                LayerKind::Clip => self.scene.pop_clip(),
                LayerKind::FilterRegionLayer => {
                    self.scene.pop_clip();
                    self.scene.pop_layer();
                    self.scene.pop_clip();
                    self.scene.pop_clip();
                }
            }
        }
    }

    fn stroke<'a>(
        &mut self,
        style: &Stroke,
        transform: Affine,
        paint: impl Into<PaintRef<'a>>,
        brush_transform: Option<Affine>,
        shape: &impl Shape,
        anti_aliasing: AntiAliasing,
    ) {
        let paint: anyrender::PaintRef<'a> = paint.into();
        if let anyrender::Paint::Pattern(pattern) = paint {
            let cull_rect = self.cull_rect();
            anyrender::stroke_pattern_tiles(
                self,
                pattern,
                style,
                transform,
                brush_transform,
                shape,
                cull_rect,
            );
            return;
        }
        self.scene.set_transform(transform);
        self.scene.set_stroke(style.clone());
        self.scene
            .set_aliasing_threshold((!anti_aliasing.is_enabled()).then_some(127));
        let paint = self.convert_paint(paint);
        self.scene.set_paint(paint);
        self.scene
            .set_paint_transform(brush_transform.unwrap_or(Affine::IDENTITY));
        self.scene.stroke_path(&shape.into_path(DEFAULT_TOLERANCE));
        self.scene.set_aliasing_threshold(None);
    }

    fn fill<'a>(
        &mut self,
        style: Fill,
        transform: Affine,
        paint: impl Into<PaintRef<'a>>,
        brush_transform: Option<Affine>,
        shape: &impl Shape,
        anti_aliasing: AntiAliasing,
    ) {
        let paint: anyrender::PaintRef<'a> = paint.into();
        if let anyrender::Paint::Pattern(pattern) = paint {
            let cull_rect = self.cull_rect();
            anyrender::fill_pattern_tiles(
                self,
                pattern,
                style,
                transform,
                brush_transform,
                shape,
                cull_rect,
            );
            return;
        }
        self.scene.set_transform(transform);
        self.scene.set_fill_rule(style);
        self.scene
            .set_aliasing_threshold((!anti_aliasing.is_enabled()).then_some(127));
        let paint = self.convert_paint(paint);
        self.scene.set_paint(paint);
        self.scene
            .set_paint_transform(brush_transform.unwrap_or(Affine::IDENTITY));
        if let Some(rect) = shape.as_rect() {
            self.scene.fill_rect(&rect);
        } else {
            self.scene.fill_path(&shape.into_path(DEFAULT_TOLERANCE));
        }
        self.scene.set_aliasing_threshold(None);
    }

    fn draw_glyphs<'a, 's: 'a>(
        &'a mut self,
        font: &'a FontData,
        font_size: f32,
        hint: bool,
        normalized_coords: &'a [NormalizedCoord],
        embolden: kurbo::Vec2,
        style: impl Into<StyleRef<'a>>,
        paint: impl Into<PaintRef<'a>>,
        brush_alpha: f32,
        transform: Affine,
        glyph_transform: Option<Affine>,
        glyphs: impl Iterator<Item = anyrender::Glyph> + Clone,
        anti_aliasing: AntiAliasing,
    ) {
        let paint: PaintRef<'a> = paint.into();
        if let anyrender::Paint::Pattern(pattern) = paint {
            let cull_rect = self.cull_rect();
            anyrender::draw_glyphs_pattern_tiles(
                self,
                pattern,
                font,
                font_size,
                hint,
                normalized_coords,
                embolden,
                style,
                brush_alpha,
                transform,
                glyph_transform,
                glyphs,
                cull_rect,
            );
            return;
        }
        let paint = self.convert_paint(paint);
        self.scene.set_paint(paint);
        self.scene.reset_paint_transform();
        self.scene.set_transform(transform);

        self.scene
            .set_aliasing_threshold((!anti_aliasing.is_enabled()).then_some(127));
        let glyph_caching = anti_aliasing.is_enabled()
            && self.glyph_caching
            && supports_glyph_caching(transform, glyph_transform);
        let snap = GlyphOriginSnap::new(transform, font_size, embolden);
        let style: StyleRef<'a> = style.into();
        match style {
            StyleRef::Fill(fill) => {
                self.scene.set_fill_rule(fill);
                let _ = self
                    .scene
                    .glyph_run(self.image_manager.resources, font)
                    .atlas_cache(glyph_caching)
                    .font_size(font_size)
                    .hint(hint)
                    .normalized_coords(normalized_coords)
                    .font_embolden(FontEmbolden::new(Diagonal2::new(embolden.x, embolden.y)))
                    .glyph_transform(glyph_transform.unwrap_or_default())
                    .fill_glyphs(glyphs.map(|g| glifo::Glyph {
                        id: g.id,
                        x: snap.map_or(g.x, |snap| snap.x(g.x)),
                        y: snap.map_or(g.y, |snap| snap.y(g.y)),
                    }));
            }
            StyleRef::Stroke(stroke) => {
                self.scene.set_stroke(stroke.clone());
                let _ = self
                    .scene
                    .glyph_run(self.image_manager.resources, font)
                    .atlas_cache(glyph_caching)
                    .font_size(font_size)
                    .hint(hint)
                    .normalized_coords(normalized_coords)
                    .glyph_transform(glyph_transform.unwrap_or_default())
                    .stroke_glyphs(glyphs.map(|g| glifo::Glyph {
                        id: g.id,
                        x: snap.map_or(g.x, |snap| snap.x(g.x)),
                        y: snap.map_or(g.y, |snap| snap.y(g.y)),
                    }));
            }
        }
        self.scene.set_aliasing_threshold(None);
    }
    fn draw_box_shadow(
        &mut self,
        transform: Affine,
        box_shape: &anyrender::NonUniformRoundedRect,
        offset: kurbo::Vec2,
        spread: f64,
        std_dev: f64,
        color: Color,
        kind: anyrender::BoxShadowKind,
    ) {
        let geometry = anyrender::BoxShadowGeometry::new(box_shape, offset, spread, std_dev, kind);
        self.scene.set_transform(transform);
        self.scene.set_paint(PaintType::Solid(color));
        self.scene.reset_paint_transform();
        self.scene.set_fill_rule(Fill::NonZero);
        if geometry.std_dev == 0.0 {
            // A single fill, so a non-isolated clip anti-aliases the clip edge only once.
            let clip = geometry.needs_clip();
            if clip {
                self.scene.push_clip_path(&geometry.area);
            }
            self.scene.fill_path(&geometry.unblurred_path());
            if clip {
                self.scene.pop_clip();
            }
            return;
        }
        // TODO: draw shadows with matching individual radii instead of averaging them
        self.scene.fill_blurred_rounded_rect_in(
            &geometry.area,
            &geometry.shadow.rect,
            geometry.shadow.average_radius() as f32,
            geometry.std_dev as f32,
            geometry.is_inset(),
        );
    }
}

#[cfg(test)]
mod glyph_origin_snap_tests {
    use super::GlyphOriginSnap;
    use kurbo::Affine;

    fn snapped_device_x(transform: Affine, font_size: f32, x: f32) -> f64 {
        let snap = GlyphOriginSnap::core_graphics(transform, font_size, kurbo::Vec2::ZERO).unwrap();
        transform.as_coeffs()[0] * snap.x(x) as f64 + transform.as_coeffs()[4]
    }

    #[test]
    fn grid_depends_on_device_ppem() {
        for (ppem, step) in [
            (8.0, 0.2),
            (11.0, 0.25),
            (12.0, 1.0 / 3.0),
            (16.0, 1.0 / 3.0),
            (17.0, 0.5),
            (33.0, 0.5),
            (34.0, 1.0),
            (72.0, 1.0),
        ] {
            let snap =
                GlyphOriginSnap::core_graphics(Affine::IDENTITY, ppem, kurbo::Vec2::ZERO).unwrap();
            assert_eq!(snap.positions_per_px, (1.0f64 / step).round(), "{ppem}ppem");
        }
    }

    #[test]
    fn floors_device_x() {
        assert_eq!(snapped_device_x(Affine::IDENTITY, 11.0, 10.7), 10.5);
        assert_eq!(snapped_device_x(Affine::IDENTITY, 16.0, 10.3), 10.0);
        assert_eq!(snapped_device_x(Affine::IDENTITY, 40.0, 10.9), 10.0);
        // 8px at 2x is 16 device ppem, so a 1/3 device px grid.
        let transform = Affine::translate((3.0, 0.0)) * Affine::scale(2.0);
        assert!((snapped_device_x(transform, 8.0, 5.2) - 13.333333).abs() < 1e-4);
    }

    #[test]
    fn rounds_device_x_to_quarter_px_for_skia() {
        let device_x = |transform: Affine, x: f32| {
            let snap = GlyphOriginSnap::skia(transform).unwrap();
            transform.as_coeffs()[0] * snap.x(x) as f64 + transform.as_coeffs()[4]
        };
        assert_eq!(device_x(Affine::IDENTITY, 10.1), 10.0);
        assert_eq!(device_x(Affine::IDENTITY, 10.2), 10.25);
        assert_eq!(device_x(Affine::IDENTITY, 10.9), 11.0);
        let transform = Affine::translate((3.0, 0.0)) * Affine::scale(2.0);
        assert_eq!(device_x(transform, 5.2), 13.5);
        assert!(GlyphOriginSnap::skia(Affine::rotate(0.5)).is_none());
    }

    #[test]
    fn snaps_device_y_down_to_whole_px() {
        let device_y = |transform: Affine, y: f32| {
            let snap = GlyphOriginSnap::core_graphics(transform, 16.0, kurbo::Vec2::ZERO).unwrap();
            transform.as_coeffs()[3] * snap.y(y) as f64 + transform.as_coeffs()[5]
        };
        assert_eq!(device_y(Affine::IDENTITY, 10.0), 10.0);
        assert_eq!(device_y(Affine::IDENTITY, 10.0005), 10.0);
        assert_eq!(device_y(Affine::IDENTITY, 10.1), 11.0);
        assert_eq!(device_y(Affine::IDENTITY, 10.9), 11.0);
        // At scale(1.5), a baseline at y = 7 is 10.5 device px down.
        assert!((device_y(Affine::scale(1.5), 7.0) - 11.0).abs() < 1e-6);
        let transform = Affine::translate((0.0, 0.25)) * Affine::scale(2.0);
        assert!((device_y(transform, 5.0) - 11.0).abs() < 1e-6);
        let snap = GlyphOriginSnap::skia(Affine::IDENTITY).unwrap();
        assert_eq!(snap.y(10.3), 10.3);
    }

    #[test]
    fn raises_emboldened_glyphs_by_vertical_embolden() {
        let embolden = kurbo::Vec2::new(0.1, 0.3);
        let snap = GlyphOriginSnap::core_graphics(Affine::IDENTITY, 16.0, embolden).unwrap();
        assert!((snap.y(10.0) - 9.7).abs() < 1e-6);
        // The lift is in user units, so at 2x it is 0.6 device px.
        let snap = GlyphOriginSnap::core_graphics(Affine::scale(2.0), 8.0, embolden).unwrap();
        assert!((snap.y(10.0) - 9.7).abs() < 1e-6);
        let snap = GlyphOriginSnap::core_graphics(Affine::IDENTITY, 16.0, kurbo::Vec2::ZERO);
        assert_eq!(snap.unwrap().y(10.0), 10.0);
    }

    #[test]
    fn skips_rotated_runs() {
        assert!(
            GlyphOriginSnap::core_graphics(Affine::rotate(0.3), 16.0, kurbo::Vec2::ZERO).is_none()
        );
    }
}