cranpose-render-pixels 0.22.0

Software renderer for Cranpose scenes and RGBA framebuffers
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
use cranpose_render_common::{
    brush_sampling::{DitherAxis, DitheredLine, color_to_rgba, sample_brush_rgba},
    glyph_mask::GlyphMaskCache,
    graph_scene::RenderDiagnostics,
    layer_shadow::ShadowRRect,
    shape_sdf,
    text_fonts::TextFonts,
};
use cranpose_ui_graphics::{
    ArcGeometry, BlendMode, Brush, BrushSampler, CornerRadii, LineGeometry, Point, Rect,
    StrokeJoin, Trapezoid, expand_rect, whole,
};

use crate::{
    pipeline,
    pixel::{GradientColors, Pixel},
    scene::{Outline, RasterScene, Scene},
};

mod image;
mod reach;
mod row_runs;
mod shape_rows;
mod text;

use image::{blit_image_between_pixels, draw_image, image_rect};
use reach::text_bounds;
use row_runs::{INK_REACH, LineRows, NO_RUN, RoundedFill, RowRuns, arc_row_runs, stroke_row_runs};
use shape_rows::{Coverage, ShapeRows};
pub(crate) use text::rasterize_text;
use text::{draw_text, is_static_text, rasterize_text_part, text_raster_rect};

/// Glyph masks a renderer keeps between frames.
const GLYPH_MASKS: usize = 256;

/// What drawing text needs: the fonts the app's text lays out with, and the
/// masks of the glyphs drawn before, so a text that changes rasterizes only
/// glyphs it did not show yet.
pub(crate) struct TextRaster {
    pub(crate) fonts: TextFonts,
    glyphs: std::cell::RefCell<GlyphMaskCache>,
}

impl TextRaster {
    pub(crate) fn new(fonts: TextFonts) -> Self {
        Self {
            fonts,
            glyphs: std::cell::RefCell::new(GlyphMaskCache::with_capacity_at_least_one(
                GLYPH_MASKS,
            )),
        }
    }
}

fn is_blend_mode_supported(mode: BlendMode) -> bool {
    matches!(mode, BlendMode::SrcOver | BlendMode::DstOut)
}

fn snapped_anchor_origin(anchor: Point) -> Point {
    Point::new(whole::round(anchor.x), whole::round(anchor.y))
}

pub(crate) fn snap_delta_for_anchor(anchor: Point) -> Point {
    let snapped = snapped_anchor_origin(anchor);
    Point::new(snapped.x - anchor.x, snapped.y - anchor.y)
}

/// A window of the device frame: the pixels `left..left + width` by
/// `top..top + height`, row by row. The whole frame is the window at the
/// origin; a sprite's texture is the window around the one item it holds.
pub(crate) struct Target<'a, P = [u8; 4]> {
    pub pixels: &'a mut [P],
    pub left: i32,
    pub top: i32,
    pub width: u32,
    pub height: u32,
}

impl<P> Target<'_, P> {
    pub(crate) fn window(&self) -> ClipBounds {
        ClipBounds {
            min_x: self.left,
            min_y: self.top,
            max_x: self.left + self.width as i32,
            max_y: self.top + self.height as i32,
        }
    }

    /// The pixels `start..end` of row `y`, which lie inside the window
    /// unless the run is empty (`end <= start`).
    fn row(&mut self, y: i32, start: i32, end: i32) -> &mut [P] {
        if end <= start {
            return &mut [];
        }
        let row = (y - self.top) as usize * self.width as usize;
        let from = row + (start - self.left) as usize;
        let to = row + (end - self.left) as usize;
        &mut self.pixels[from..to]
    }
}

/// `UNIT[c]` is `c as f32 / 255.0`, worked out at build time, so a byte
/// channel becomes a float without a division per pixel.
static UNIT: [f32; 256] = {
    let mut table = [0.0; 256];
    let mut channel = 0;
    while channel < 256 {
        table[channel] = channel as f32 / 255.0;
        channel += 1;
    }
    table
};

#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct ClipBounds {
    pub min_x: i32,
    pub min_y: i32,
    pub max_x: i32,
    pub max_y: i32,
}

impl ClipBounds {
    fn within(self, limit: i32) -> bool {
        [self.min_x, self.min_y, self.max_x, self.max_y]
            .iter()
            .all(|edge| edge.abs() <= limit)
    }

    fn overlaps(self, other: Self) -> bool {
        self.min_x < other.max_x
            && other.min_x < self.max_x
            && self.min_y < other.max_y
            && other.min_y < self.max_y
    }

    pub(crate) fn holds(self, other: Self) -> bool {
        self.min_x <= other.min_x
            && self.min_y <= other.min_y
            && self.max_x >= other.max_x
            && self.max_y >= other.max_y
    }
}

/// The whole pixels of `rect` cut to `clip` and to the target's window.
fn clip_rect_to_bounds<P>(
    rect: Rect,
    clip: Option<Rect>,
    target: &Target<'_, P>,
) -> Option<ClipBounds> {
    clip_rect_to_window(rect, clip, target.window())
}

/// The pixel at or below `edge`, a finite value within the `i32` range:
/// the conversion truncates toward zero, so a negative edge with a fraction
/// steps one down. A float floor and a second conversion cost more where
/// no instruction rounds a float.
fn floor_pixel(edge: f32) -> i32 {
    let whole = edge as i32;
    whole - i32::from((whole as f32) > edge)
}

/// The pixel at or above `edge`, a finite value within the `i32` range.
fn ceil_pixel(edge: f32) -> i32 {
    let whole = edge as i32;
    whole + i32::from((whole as f32) < edge)
}

/// The whole pixels of `rect` cut to `clip` and to `window`.
pub(crate) fn clip_rect_to_window(
    rect: Rect,
    clip: Option<Rect>,
    window: ClipBounds,
) -> Option<ClipBounds> {
    let mut min_x = rect.x;
    let mut min_y = rect.y;
    let mut max_x = rect.x + rect.width;
    let mut max_y = rect.y + rect.height;

    if let Some(clip_rect) = clip {
        min_x = min_x.max(clip_rect.x);
        min_y = min_y.max(clip_rect.y);
        max_x = max_x.min(clip_rect.x + clip_rect.width);
        max_y = max_y.min(clip_rect.y + clip_rect.height);
    }

    min_x = min_x.max(window.min_x as f32);
    min_y = min_y.max(window.min_y as f32);
    max_x = max_x.min(window.max_x as f32);
    max_y = max_y.min(window.max_y as f32);

    if max_x <= min_x || max_y <= min_y {
        return None;
    }

    // Each edge now lies within the window, whose bounds are whole, so it
    // is finite (`max` and `min` pass over a NaN) and its whole pixel too.
    let min_x = floor_pixel(min_x);
    let min_y = floor_pixel(min_y);
    let max_x = ceil_pixel(max_x);
    let max_y = ceil_pixel(max_y);

    if min_x >= max_x || min_y >= max_y {
        return None;
    }

    Some(ClipBounds {
        min_x,
        min_y,
        max_x,
        max_y,
    })
}

/// Draws `scene` into `frame`, an RGBA8 buffer of `width` by `height` pixels, at one device pixel
/// per logical pixel. Its texts draw the glyphs of the typefaces they were laid out in.
pub fn draw_scene(frame: &mut [u8], width: u32, height: u32, scene: &Scene) {
    let text = TextRaster::new(TextFonts::default());
    draw_scene_scaled(frame, width, height, scene, &text, 1.0);
}

pub(crate) fn draw_scene_scaled(
    frame: &mut [u8],
    width: u32,
    height: u32,
    scene: &Scene,
    text: &TextRaster,
    scale: f32,
) {
    if let Some(graph) = scene.graph.as_ref() {
        let raster_scene = pipeline::build_raster_scene(graph, scene.diagnostics(), scale, text);
        draw_raster_scene(
            frame,
            width,
            height,
            &raster_scene,
            scene.diagnostics(),
            text,
        );
    } else {
        clear_frame(frame.as_chunks_mut::<4>().0);
    }
}

fn clear_frame<P: Pixel>(frame: &mut [P]) {
    P::fill(frame, P::opaque([18, 18, 24, 255]));
}

fn draw_raster_scene(
    frame: &mut [u8],
    width: u32,
    height: u32,
    scene: &RasterScene,
    diagnostics: &RenderDiagnostics,
    text: &TextRaster,
) {
    let mut target = Target {
        pixels: frame.as_chunks_mut::<4>().0,
        left: 0,
        top: 0,
        width,
        height,
    };
    let mut reach = Vec::new();
    scene.reach_into(target.window(), &mut reach);
    draw_raster_window(&mut target, scene, &reach, diagnostics, text);
}

/// Draws the items of `scene` into `target`'s window. `reach` holds where
/// each item of the scene's order can draw, as [`RasterScene::reach_into`]
/// puts it: an item that cannot draw in the window is passed over.
pub(crate) fn draw_raster_window<P: Pixel>(
    target: &mut Target<'_, P>,
    scene: &RasterScene,
    reach: &[Option<ClipBounds>],
    diagnostics: &RenderDiagnostics,
    text: &TextRaster,
) {
    let window = target.window();
    // The topmost item that paints the whole window opaquely hides what
    // lies under it there: drawing starts with it.
    let first = scene.order.iter().zip(reach).rposition(|(&item, bounds)| {
        bounds.is_some_and(|bounds| bounds.holds(window)) && scene.covers_window(item, window)
    });
    if first.is_none() {
        clear_frame(target.pixels);
    }
    let first = first.unwrap_or(0);
    for (&item, bounds) in scene.order[first..].iter().zip(&reach[first..]) {
        if bounds.is_some_and(|bounds| bounds.overlaps(window)) {
            draw_item(target, scene, item, diagnostics, text);
        }
    }
}

/// Rasterizes into the glyph cache the glyphs of the text of `scene` that
/// reaches `window`, so that drawing the window takes them from the cache.
pub(crate) fn prepare_raster_window(scene: &RasterScene, window: ClipBounds, text: &TextRaster) {
    for draw in &scene.texts {
        if is_static_text(draw)
            && let Some(raster_rect) = text_raster_rect(draw)
            && text_bounds(draw, window).is_some()
        {
            // A part of one pixel visits every glyph, and the cache keeps
            // each glyph it misses, whether the part shows it or not.
            let part = Rect {
                width: 1.0,
                height: 1.0,
                ..raster_rect
            };
            rasterize_text_part(draw, raster_rect, part, text);
        }
    }
}

/// Draws one item of `scene` into `target`, where it reaches the target's
/// window. A shape, an image or a shadow cuts itself to the window as it
/// places itself; a text is checked first, as rasterizing it costs more.
pub(crate) fn draw_item<P: Pixel>(
    target: &mut Target<'_, P>,
    scene: &RasterScene,
    item: RenderItem,
    diagnostics: &RenderDiagnostics,
    text: &TextRaster,
) {
    match item {
        RenderItem::Shape(index) => draw_shape(target, &scene.shapes[index], diagnostics),
        RenderItem::Image(index) => draw_image(target, &scene.images[index], diagnostics),
        RenderItem::Shadow(index) => draw_shadow(target, &scene.shadows[index], diagnostics),
        RenderItem::Text(index) => {
            let draw = &scene.texts[index];
            if text_bounds(draw, target.window()).is_some() {
                draw_text(target, draw, diagnostics, text);
            }
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum RenderItem {
    Shape(usize),
    Image(usize),
    Text(usize),
    Shadow(usize),
}

/// Draws one pass of an elevation shadow: its color at the shadow's
/// coverage of each pixel's centre. Out of line, as are images and the
/// shapes a fill does not paint: the item loop keeps the solid fills and
/// text that most frames draw, and the rest takes code only where it draws.
#[inline(never)]
fn draw_shadow<P: Pixel>(
    target: &mut Target<'_, P>,
    draw: &crate::scene::ShadowDraw,
    diagnostics: &RenderDiagnostics,
) {
    let (shadow, clip) = placed_shadow(draw);
    let Some(bounds) = clip_rect_to_bounds(shadow.bounds, clip, target) else {
        return;
    };
    let paint = P::paint(
        [
            draw.color.r(),
            draw.color.g(),
            draw.color.b(),
            draw.color.a(),
        ],
        BlendMode::SrcOver,
    );
    // Rows alike in what their y gives the coverage take the same coverage
    // at every x: a band of columns computes it for the first of them only.
    let mut covered = [0.0_f32; SHADOW_COLUMNS];
    for start in (bounds.min_x..bounds.max_x).step_by(SHADOW_COLUMNS) {
        let end = bounds.max_x.min(start + SHADOW_COLUMNS as i32);
        let mut computed = None;
        for py in bounds.min_y..bounds.max_y {
            let center_y = py as f32 + 0.5;
            let key = shadow.row_key(center_y);
            if computed != Some(key) {
                shadow.row_coverage(start..end, center_y, &mut covered);
                computed = Some(key);
            }
            for (dst, &coverage) in target.row(py, start, end).iter_mut().zip(&covered) {
                if coverage <= 0.0 {
                    continue;
                }
                dst.blend_paint(paint, coverage, diagnostics);
            }
        }
    }
}

// The columns of a shadow whose coverage one band of rows shares.
const SHADOW_COLUMNS: usize = 128;

// Paints the runs of row `py` with one color: a fill where it covers them
// opaquely, else a blend over each pixel.
fn paint_row<P: Pixel>(
    target: &mut Target<'_, P>,
    py: i32,
    runs: RowRuns,
    color: [f32; 4],
    blend_mode: BlendMode,
    diagnostics: &RenderDiagnostics,
) {
    let runs = runs.into_iter().filter(|(start, end)| start < end);
    if blend_mode == BlendMode::SrcOver && color[3] >= 1.0 {
        let pixel = P::opaque(opaque_bytes(color));
        for (start, end) in runs {
            P::fill(target.row(py, start, end), pixel);
        }
    } else if color[3] > 0.0 {
        let paint = P::paint(color, blend_mode);
        for (start, end) in runs {
            for dst in target.row(py, start, end) {
                dst.blend_paint(paint, 1.0, diagnostics);
            }
        }
    }
}

/// A shape where its snap puts it: its rect, the coverage of each pixel and
/// the origin its gradient's dither is measured from.
pub(crate) struct PlacedShape {
    pub rect: Rect,
    pub coverage: ShapeCoverage,
    pub dither_origin: Point,
}

/// `rect` moved to whole pixels and grown to cover at least one.
// Twice the x of the vertical line through the middle of `rect`, when the
// line lies on a pixel's edge or centre and the window's pixels lie close
// enough to it for a float to hold their offsets from it exactly: a pixel
// and its twin across the line then lie as far from the line, to the bit.
fn mirror_line(rect: Rect) -> Option<i32> {
    // Pixels within this many of the line, and their centres' offsets, are
    // whole or half numbers a float holds exactly.
    const LIMIT: f32 = 4_194_304.0;
    let twice = (rect.x + rect.width * 0.5) * 2.0;
    (twice.fract() == 0.0 && twice.abs() < LIMIT).then(|| floor_pixel(twice))
}

fn on_pixel_grid(rect: Rect) -> Rect {
    Rect {
        x: whole::round(rect.x),
        y: whole::round(rect.y),
        width: if rect.width > 0.0 {
            whole::ceil(rect.width).max(1.0)
        } else {
            rect.width
        },
        height: if rect.height > 0.0 {
            whole::ceil(rect.height).max(1.0)
        } else {
            rect.height
        },
    }
}

/// `draw` where its snap puts it; `None` when it covers nothing.
pub(crate) fn place_shape(draw: &crate::scene::DrawShape) -> Option<PlacedShape> {
    let snap_delta = draw
        .snap_anchor
        .map(snap_delta_for_anchor)
        .unwrap_or_default();
    let rect = draw.rect.translate(snap_delta.x, snap_delta.y);
    let dither_origin = draw
        .snap_anchor
        .map(snapped_anchor_origin)
        .unwrap_or_default();
    let rect = if draw.snap_to_pixel_grid {
        on_pixel_grid(rect)
    } else {
        rect
    };
    let coverage = ShapeCoverage::of(draw, rect, snap_delta)?;
    Some(PlacedShape {
        rect,
        coverage,
        dither_origin,
    })
}

fn draw_shape<P: Pixel>(
    target: &mut Target<'_, P>,
    draw: &crate::scene::DrawShape,
    diagnostics: &RenderDiagnostics,
) {
    let Some(PlacedShape {
        rect,
        coverage,
        dither_origin,
    }) = place_shape(draw)
    else {
        return;
    };
    let Some(clip_bounds) = clip_rect_to_bounds(coverage.reach(rect), draw.clip, target) else {
        return;
    };

    let paint = ShapeRows {
        target,
        draw,
        rect,
        clip_bounds,
        dither_origin,
        opaque: opaque_solid_bytes(&draw.brush, draw.blend_mode).map(P::opaque),
        diagnostics,
    };
    let whole_rows = |_: f32, min_x: i32, max_x: i32| [(min_x, max_x), NO_RUN];
    match &coverage {
        ShapeCoverage::Line(line) => {
            let frame = line.frame();
            let rows = LineRows::new(line, &frame);
            paint.paint(
                |y, min_x, max_x| rows.runs(y, min_x, max_x),
                |point| Coverage::of(line.coverage_in(&frame, point)),
            );
        }
        ShapeCoverage::Trapezoid(trapezoid) => {
            paint.paint(whole_rows, |point| Coverage::of(trapezoid.coverage(point)));
        }
        ShapeCoverage::Arc(arc) => {
            let band = shape_sdf::ArcBand::new(arc);
            paint.paint(
                |y, min_x, max_x| arc_row_runs(arc, y, min_x, max_x),
                |point| Coverage::of_distance(band.distance(point)),
            );
        }
        &ShapeCoverage::Stroke {
            rect,
            radii,
            half_width,
            join,
        } => {
            let runs =
                |y, min_x, max_x| stroke_row_runs(rect, radii, half_width, join, y, min_x, max_x);
            let band = shape_sdf::StrokedBox::of_rect(rect, radii, half_width, join);
            // With one radius at every corner the distance is the exact one
            // to a rounded box's band; corners of different radii meet
            // where the distance can step.
            if radii.is_none_or(|radii| radii == CornerRadii::uniform(radii.top_left)) {
                paint.paint_stroke(
                    runs,
                    |point| Coverage::of_distance(band.distance(point)),
                    mirror_line(rect),
                );
            } else {
                paint.paint(runs, |point| {
                    Coverage::of(shape_sdf::coverage_for_distance(band.distance(point)))
                });
            }
        }
        ShapeCoverage::Fill {
            rect,
            radii: Some(radii),
        } => {
            let fill = RoundedFill::new(*rect, radii);
            paint.fill(|y, min_x, max_x| [fill.run(y, min_x, max_x), NO_RUN]);
        }
        ShapeCoverage::Fill { radii: None, .. } => paint.fill(whole_rows),
    }
}

/// The bytes an opaque solid colour leaves on a pixel it covers fully,
/// whatever was there: what `blend_pixel` works out for it.
fn opaque_solid_bytes(brush: &Brush, blend_mode: BlendMode) -> Option<[u8; 4]> {
    let Brush::Solid(color) = brush else {
        return None;
    };
    paints_opaque(brush, blend_mode).then(|| opaque_bytes(color_to_rgba(*color)))
}

/// Whether [`opaque_solid_bytes`] has bytes for `brush` under `blend_mode`,
/// without working them out.
fn paints_opaque(brush: &Brush, blend_mode: BlendMode) -> bool {
    blend_mode == BlendMode::SrcOver && matches!(brush, Brush::Solid(color) if color.3 >= 1.0)
}

/// How much of a pixel a shape covers, decided once per shape: a line, an
/// arc band, a stroked outline or a fill, placed where its snap moved it.
pub(crate) enum ShapeCoverage {
    Line(LineGeometry),
    Trapezoid(Trapezoid),
    Arc(ArcGeometry),
    Stroke {
        rect: Rect,
        radii: Option<CornerRadii>,
        half_width: f32,
        join: StrokeJoin,
    },
    Fill {
        rect: Rect,
        radii: Option<CornerRadii>,
    },
}

impl ShapeCoverage {
    /// The coverage of `draw` laid at `rect`, its line or arc moved by
    /// `snap_delta` as the rect was; `None` when it covers nothing.
    fn of(draw: &crate::scene::DrawShape, rect: Rect, snap_delta: Point) -> Option<Self> {
        let shift = |point: Point| Point::new(point.x + snap_delta.x, point.y + snap_delta.y);
        let (shape, stroke) = match draw.outline {
            Outline::Line(line) => {
                return (!line.is_degenerate())
                    .then(|| Self::Line(line.placed(shift(line.start), shift(line.end), 1.0)));
            }
            Outline::Trapezoid(trapezoid) => {
                return Some(Self::Trapezoid(
                    trapezoid.translate(snap_delta.x, snap_delta.y),
                ));
            }
            Outline::Arc(arc) => {
                return (!arc.is_degenerate()).then(|| {
                    Self::Arc(ArcGeometry {
                        center: shift(arc.center),
                        ..arc
                    })
                });
            }
            Outline::Stroke(shape, stroke) => (shape, Some(stroke)),
            Outline::Fill(shape) => (shape, None),
        };
        let outset = stroke.map_or(0.0, |stroke| stroke.half_width());
        let radii = shape.map(|shape| {
            shape.resolve(
                (rect.width - outset * 2.0).max(0.0),
                (rect.height - outset * 2.0).max(0.0),
            )
        });
        match stroke {
            Some(stroke) if !stroke.is_visible() => None,
            Some(stroke) => Some(Self::Stroke {
                rect,
                radii,
                half_width: stroke.half_width(),
                join: stroke.join,
            }),
            None => Some(Self::Fill { rect, radii }),
        }
    }

    /// The pixels it can cover: a slice's own box, which its edges reach
    /// half a pixel past, an arc's band, otherwise the shape's `rect`. An
    /// arc's `rect` is the disc around its whole circle.
    pub(crate) fn reach(&self, rect: Rect) -> Rect {
        match self {
            Self::Trapezoid(trapezoid) => expand_rect(trapezoid.bounds(), 0.5),
            Self::Arc(arc) => expand_rect(arc.bounds(), INK_REACH),
            _ => rect,
        }
    }
}

fn placed_shadow(draw: &crate::scene::ShadowDraw) -> (ShadowRRect, Option<Rect>) {
    let snap_delta = draw
        .snap_anchor
        .map(snap_delta_for_anchor)
        .unwrap_or_default();
    (
        draw.shadow.translated(snap_delta.x, snap_delta.y),
        draw.clip
            .map(|clip| clip.translate(snap_delta.x, snap_delta.y)),
    )
}

// The columns [`ShapeRows::fill_uniform_columns`] works out at a time: their
// bytes, a kilobyte, sit on the stack.
const COLUMN_BAND: usize = 64;

// `unit` clamped to 0..=1 as the nearest of 256 levels, halves rounded up:
// `whole::round(unit.clamp(0.0, 1.0) * 255.0) as u8`. Below 2^23 the
// truncation and the fraction left are exact, and the level is never
// negative, so a fraction from one half up is the whole step `round` takes.
pub(crate) fn unit_byte(unit: f32) -> u8 {
    let level = unit.clamp(0.0, 1.0) * 255.0;
    let truncated = level as u32;
    (truncated + u32::from(level - truncated as f32 >= 0.5)) as u8
}

// The bytes an opaque `src` leaves on any pixel under `SrcOver`.
fn opaque_bytes(src: [f32; 4]) -> [u8; 4] {
    [unit_byte(src[0]), unit_byte(src[1]), unit_byte(src[2]), 255]
}

pub(crate) fn blend_pixel(
    dst: &mut [u8; 4],
    src: [f32; 4],
    blend_mode: BlendMode,
    diagnostics: &RenderDiagnostics,
) {
    let resolved_blend_mode = if is_blend_mode_supported(blend_mode) {
        blend_mode
    } else {
        if diagnostics.claim_warning_once("pixels.unsupported-blend-mode") {
            log::warn!(
                "Pixels renderer currently supports BlendMode::SrcOver and BlendMode::DstOut; falling back to SrcOver for unsupported modes"
            );
        }
        BlendMode::SrcOver
    };

    let src_alpha = src[3].clamp(0.0, 1.0);
    if src_alpha <= 0.0 {
        return;
    }
    // An opaque source over anything is the source: the same bytes the
    // blend below works out, without reading the destination.
    if src_alpha >= 1.0 && resolved_blend_mode != BlendMode::DstOut {
        *dst = opaque_bytes(src);
        return;
    }
    let [dst_r, dst_g, dst_b, dst_a] = dst.map(|channel| UNIT[usize::from(channel)]);

    let (out_r, out_g, out_b, out_a) = match resolved_blend_mode {
        BlendMode::DstOut => {
            let keep = 1.0 - src_alpha;
            (dst_r * keep, dst_g * keep, dst_b * keep, dst_a * keep)
        }
        BlendMode::SrcOver => (
            src[0].clamp(0.0, 1.0) * src_alpha + dst_r * (1.0 - src_alpha),
            src[1].clamp(0.0, 1.0) * src_alpha + dst_g * (1.0 - src_alpha),
            src[2].clamp(0.0, 1.0) * src_alpha + dst_b * (1.0 - src_alpha),
            src_alpha + dst_a * (1.0 - src_alpha),
        ),
        _ => (
            src[0].clamp(0.0, 1.0) * src_alpha + dst_r * (1.0 - src_alpha),
            src[1].clamp(0.0, 1.0) * src_alpha + dst_g * (1.0 - src_alpha),
            src[2].clamp(0.0, 1.0) * src_alpha + dst_b * (1.0 - src_alpha),
            src_alpha + dst_a * (1.0 - src_alpha),
        ),
    };

    *dst = [
        unit_byte(out_r),
        unit_byte(out_g),
        unit_byte(out_b),
        unit_byte(out_a),
    ];
}

#[cfg(test)]
#[path = "tests/draw_tests.rs"]
mod tests;