ferri-pdf 0.1.12

HTML and CSS to PDF renderer for print, in pure Rust
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
//! CSS Backgrounds 3 §7 box shadows: spread shapes, clip regions and blurred alpha masks.
use crate::paint::rounded::{Radii, inset, rounded_rect};
use crate::paint::{BoxPaint, Color, Contour, CornerShape, Rect};
use serde::Serialize;
use std::collections::HashMap;
use std::sync::Arc;

/// The blur is a Gaussian with a standard deviation of half the blur radius (§7.1.1).
const SIGMA_PER_BLUR: f32 = 0.5;
/// Gaussian tails beyond this many standard deviations hold under 0.2% alpha and are cut.
const BLUR_EXTENT_SIGMAS: f32 = 3.0;
/// Blurred alpha has no detail finer than its standard deviation, so the mask samples it
/// with this many pixels per deviation, at most this density and this many pixels in all.
const MASK_SIGMA_PIXELS: f32 = 6.0;
const MAX_MASK_PIXELS_PER_PT: f32 = 4.0;
const MAX_MASK_PIXELS: f32 = 4.0e6;
/// Vertical coverage samples per mask row; horizontal coverage is exact.
const COVERAGE_SUBROWS: usize = 4;
const ALPHA_MAX: f32 = 255.0;
/// Alpha bytes a render job keeps of the masks it has rasterized; past this it forgets them.
const MAX_CACHED_MASK_BYTES: usize = 4 << 20;
/// Tags of the shapes and segments in a mask's cache key.
const ROUNDED_KEY: u32 = 0;
const CONTOURS_KEY: u32 = 1;
const LINE_KEY: u32 = 0;
const CUBIC_KEY: u32 = 1;

#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub struct BoxShadow {
    pub offset: [f32; 2],
    /// Blur radius; the Gaussian's standard deviation is half of it.
    pub blur: f32,
    pub spread: f32,
    pub color: Color,
    pub inset: bool,
}
impl BoxShadow {
    pub(crate) fn sigma(&self) -> f32 {
        self.blur * SIGMA_PER_BLUR
    }
    /// Distance beyond the spread shape that the blurred shadow reaches.
    pub(crate) fn extent(&self) -> f32 {
        self.sigma() * BLUR_EXTENT_SIGMAS
    }
}

/// Alpha of a blurred shadow sampled on `size` pixels covering `rect`, rows top to bottom.
#[derive(Debug, PartialEq)]
pub struct ShadowMask {
    pub rect: Rect,
    pub size: [u32; 2],
    pub alpha: Vec<u8>,
}

/// The blurred masks a render job has rasterized, by every input of their pixels. The writer
/// paints shadowed boxes from their own origin, so equal boxes on any page share a mask.
#[derive(Default)]
pub(crate) struct ShadowMasks {
    masks: HashMap<Vec<u32>, Arc<ShadowMask>>,
    bytes: usize,
}

impl ShadowMasks {
    fn blurred(
        &mut self,
        region: Rect,
        shape: Option<ShadowArea>,
        sigma: f32,
        invert: bool,
    ) -> Arc<ShadowMask> {
        let mut key: Vec<u32> = [region.x, region.y, region.width, region.height, sigma]
            .map(f32::to_bits)
            .into();
        key.push(u32::from(invert));
        if let Some(shape) = &shape {
            shape.push_key(&mut key);
        }
        if let Some(mask) = self.masks.get(&key) {
            return mask.clone();
        }
        let mask = Arc::new(blurred_mask(region, shape, sigma, invert));
        if self.bytes + mask.alpha.len() > MAX_CACHED_MASK_BYTES {
            self.masks.clear();
            self.bytes = 0;
        }
        self.bytes += mask.alpha.len();
        self.masks.insert(key, mask.clone());
        mask
    }
}

#[derive(Clone, Debug)]
pub enum ShadowShape {
    /// Unblurred shadow, filled with the even-odd rule.
    Sharp(Vec<Contour>),
    Blurred(Arc<ShadowMask>),
}

/// One shadow ready to paint inside `clip`.
#[derive(Clone, Debug)]
pub struct ShadowPaint {
    pub bounds: Rect,
    /// Area the shadow may cover, filled even-odd: outside the border box for outer shadows,
    /// the padding box for inset ones.
    pub clip: Vec<Contour>,
    pub shape: ShadowShape,
    pub color: Color,
}

/// Radii of a shape grown by `growth` (negative shrinks); a radius smaller than a positive
/// growth grows by `growth × (1 + (r / growth − 1)³)` so that sharp corners stay sharp (§7.1.1).
pub(crate) fn spread_radii(radii: Radii, growth: f32) -> Radii {
    radii.map(|corner| corner.map(|radius| crate::paint::rounded::spread_radius(radius, growth)))
}

/// Radii of a box outset by `distance` (top/right/bottom/left), each radius growing by the
/// distance along its own axis as `spread_radii` grows it; CSS Shapes 1 §5 `margin-box`.
pub(crate) fn outset_radii(radii: Radii, [top, right, bottom, left]: [f32; 4]) -> Radii {
    let growth = [[left, top], [right, top], [right, bottom], [left, bottom]];
    std::array::from_fn(|corner| {
        std::array::from_fn(|axis| {
            crate::paint::rounded::spread_radius(radii[corner][axis], growth[corner][axis])
        })
    })
}

pub(crate) fn outset(rect: Rect, distance: [f32; 4]) -> Rect {
    let [top, right, bottom, left] = distance;
    Rect {
        x: rect.x - left,
        y: rect.y - top,
        width: (rect.width + left + right).max(0.0),
        height: (rect.height + top + bottom).max(0.0),
    }
}
fn is_empty(rect: Rect) -> bool {
    rect.width <= 0.0 || rect.height <= 0.0
}

#[derive(Clone)]
enum ShadowArea {
    Rounded(Rect, Radii),
    Contours(Vec<Contour>),
}

impl ShadowArea {
    fn is_empty(&self) -> bool {
        match self {
            Self::Rounded(rect, _) => is_empty(*rect),
            Self::Contours(contours) => contours.is_empty(),
        }
    }

    fn translated(self, offset: [f32; 2]) -> Self {
        match self {
            Self::Rounded(rect, radii) => Self::Rounded(rect.translated(offset), radii),
            Self::Contours(contours) => Self::Contours(
                contours
                    .into_iter()
                    .map(|contour| contour.map(|[x, y]| [x + offset[0], y + offset[1]]))
                    .collect(),
            ),
        }
    }

    /// Appends the shape's bits to a mask's cache key.
    fn push_key(&self, key: &mut Vec<u32>) {
        let length = |length: usize| u32::try_from(length).unwrap_or(u32::MAX);
        let points = |points: &[crate::paint::Point], key: &mut Vec<u32>| {
            key.extend(points.iter().flatten().map(|value| value.to_bits()));
        };
        match self {
            Self::Rounded(rect, radii) => {
                key.push(ROUNDED_KEY);
                key.extend(
                    [rect.x, rect.y, rect.width, rect.height]
                        .iter()
                        .chain(radii.as_flattened())
                        .map(|value| value.to_bits()),
                );
            }
            Self::Contours(contours) => {
                key.extend([CONTOURS_KEY, length(contours.len())]);
                for contour in contours {
                    key.extend([u32::from(contour.closed), length(contour.segments.len())]);
                    points(&[contour.start], key);
                    for segment in &contour.segments {
                        match segment {
                            crate::paint::Segment::Line(end) => {
                                key.push(LINE_KEY);
                                points(&[*end], key);
                            }
                            crate::paint::Segment::Cubic(controls) => {
                                key.push(CUBIC_KEY);
                                points(controls, key);
                            }
                        }
                    }
                }
            }
        }
    }

    fn contours(self) -> Vec<Contour> {
        match self {
            Self::Rounded(rect, radii) => vec![rounded_rect(rect, radii)],
            Self::Contours(contours) => contours,
        }
    }

    fn coverage(self, region: Rect, size: [usize; 2], pitch: [f32; 2]) -> Vec<f32> {
        let contours = match self {
            Self::Rounded(rect, radii) => return coverage(region, size, pitch, (rect, radii)),
            Self::Contours(contours) => contours,
        };
        let mut target = tiny_skia::Pixmap::new(size[0] as u32, size[1] as u32)
            .expect("shadow grid dimensions fit the pixel budget");
        let bounds = [
            [f64::from(region.x), f64::from(region.x + region.width)],
            [f64::from(region.y), f64::from(region.y + region.height)],
        ];
        let tolerance = f64::from(pitch[0].min(pitch[1])) / COVERAGE_SUBROWS as f64;
        let contours = contours
            .into_iter()
            .filter_map(|contour| {
                let polygon =
                    crate::paint::clip_polygon(&contour.polygon_points(tolerance), bounds);
                (polygon.len() >= 3).then(|| {
                    Contour::polygon(
                        &polygon
                            .into_iter()
                            .map(|point| {
                                std::array::from_fn(|axis| {
                                    ((point[axis] - bounds[axis][0]) / f64::from(pitch[axis]))
                                        as f32
                                })
                            })
                            .collect::<Vec<_>>(),
                    )
                })
            })
            .collect();
        let local = Rect {
            x: 0.0,
            y: 0.0,
            width: size[0] as f32,
            height: size[1] as f32,
        };
        crate::paint::raster::cover(
            &crate::paint::Shape::Path {
                contours,
                even_odd: false,
                clip: None,
            },
            local,
            tiny_skia::Transform::identity(),
            &mut target,
            None,
        );
        target
            .pixels()
            .iter()
            .map(|pixel| f32::from(pixel.alpha()) / ALPHA_MAX)
            .collect()
    }
}

impl<I> BoxPaint<I> {
    /// Outer or inset shadows in painting order, the last listed first (§7.1). A sliced
    /// fragment casts the shadow of its unbroken box, cut at its sliced edges.
    pub fn shadows(&self, inset_shadows: bool) -> Vec<ShadowPaint> {
        self.cached_shadows(inset_shadows, &mut ShadowMasks::default())
    }

    /// `shadows`, reusing the blurred masks in `masks` and adding those it rasterizes.
    pub(crate) fn cached_shadows(
        &self,
        inset_shadows: bool,
        masks: &mut ShadowMasks,
    ) -> Vec<ShadowPaint> {
        let rect = self.decoration_rect();
        if is_empty(rect) {
            return Vec::new();
        }
        let radii = self.radii();
        self.cast_shadows(inset_shadows)
            .filter_map(|shadow| {
                if shadow.inset {
                    self.inset_shadow(shadow, rect, radii, masks)
                } else {
                    self.outer_shadow(shadow, rect, radii, masks)
                }
            })
            .collect()
    }

    /// The bounds of the outer shadows' paints, without rasterizing their blur.
    pub(crate) fn outer_shadow_bounds(&self) -> Vec<Rect> {
        let rect = self.decoration_rect();
        if is_empty(rect) {
            return Vec::new();
        }
        let radii = self.radii();
        self.cast_shadows(false)
            .filter_map(|shadow| self.outer_shadow_area(shadow, rect, radii))
            .map(|(_, _, bounds)| self.slice_bounds(bounds))
            .collect()
    }

    /// The visible shadows of one kind, the last listed first.
    fn cast_shadows(&self, inset_shadows: bool) -> impl Iterator<Item = &BoxShadow> {
        self.decoration
            .shadows
            .iter()
            .rev()
            .filter(move |shadow| shadow.inset == inset_shadows && shadow.color.alpha > 0.0)
    }

    fn inset_shadow(
        &self,
        shadow: &BoxShadow,
        unbroken: Rect,
        radii: Radii,
        masks: &mut ShadowMasks,
    ) -> Option<ShadowPaint> {
        let (unbroken_padding, padding_radii) = inset(unbroken, radii, self.inner_widths());
        let bounds = self.slice_bounds(unbroken_padding);
        if is_empty(bounds) {
            return None;
        }
        let clip = self.inner_clip([0.0; 4], [true; 2]).contours().into_owned();
        if clip.is_empty() {
            return None;
        }
        let hole = if *self.decoration.corner_shapes == [CornerShape::ROUND; 4] {
            ShadowArea::Rounded(
                outset(unbroken_padding, [-shadow.spread; 4]),
                spread_radii(padding_radii, -shadow.spread),
            )
        } else {
            ShadowArea::Contours(crate::paint::rounded::box_contours(
                unbroken,
                radii,
                self.decoration.corner_shapes.map(CornerShape::curvature),
                self.inner_widths().map(|width| width + shadow.spread),
            ))
        }
        .translated(shadow.offset);
        let hole = (!hole.is_empty()).then_some(hole);
        let shape = if shadow.blur > 0.0 {
            let region = outset(unbroken_padding, [shadow.extent(); 4]);
            ShadowShape::Blurred(masks.blurred(region, hole, shadow.sigma(), true))
        } else {
            let mut contours = vec![Contour::polygon(&corners(unbroken_padding))];
            contours.extend(hole.into_iter().flat_map(ShadowArea::contours));
            ShadowShape::Sharp(contours)
        };
        Some(ShadowPaint {
            bounds,
            clip,
            shape,
            color: shadow.color,
        })
    }
    fn outer_shadow(
        &self,
        shadow: &BoxShadow,
        rect: Rect,
        radii: Radii,
        masks: &mut ShadowMasks,
    ) -> Option<ShadowPaint> {
        let (ink, area, bounds) = self.outer_shadow_area(shadow, rect, radii)?;
        let shape = if shadow.blur > 0.0 {
            ShadowShape::Blurred(masks.blurred(ink, Some(area), shadow.sigma(), false))
        } else {
            ShadowShape::Sharp(area.contours())
        };
        let mut clip = vec![Contour::polygon(&corners(bounds))];
        clip.extend(
            self.box_area(crate::paint::BoxArea::BorderBox)
                .contours()
                .iter()
                .cloned(),
        );
        Some(ShadowPaint {
            bounds: self.slice_bounds(bounds),
            clip,
            shape,
            color: shadow.color,
        })
    }
    /// A visible outer shadow's blurred extent, its spread shape and, unsliced, the area it
    /// paints in.
    fn outer_shadow_area(
        &self,
        shadow: &BoxShadow,
        rect: Rect,
        radii: Radii,
    ) -> Option<(Rect, ShadowArea, Rect)> {
        let shape = outset(rect, [shadow.spread; 4]).translated(shadow.offset);
        if is_empty(shape) {
            return None;
        }
        let area = if *self.decoration.corner_shapes == [CornerShape::ROUND; 4] {
            ShadowArea::Rounded(
                outset(rect, [shadow.spread; 4]),
                spread_radii(radii, shadow.spread),
            )
        } else {
            ShadowArea::Contours(crate::paint::rounded::box_contours(
                rect,
                radii,
                self.decoration.corner_shapes.map(CornerShape::curvature),
                [-shadow.spread; 4],
            ))
        }
        .translated(shadow.offset);
        if area.is_empty() {
            return None;
        }
        let ink = outset(shape, [shadow.extent(); 4]);
        let visible = self.slice_bounds(ink);
        if is_empty(visible) {
            return None;
        }
        Some((ink, area, ink.union(rect)))
    }
}

fn corners(rect: Rect) -> [crate::paint::Point; 4] {
    [
        [rect.x, rect.y],
        [rect.x + rect.width, rect.y],
        [rect.x + rect.width, rect.y + rect.height],
        [rect.x, rect.y + rect.height],
    ]
}

/// Horizontal extent of a rounded rectangle at height `y`.
fn row_span(rect: Rect, radii: Radii, y: f32) -> Option<[f32; 2]> {
    let below_top = y - rect.y;
    let above_bottom = rect.y + rect.height - y;
    if below_top < 0.0 || above_bottom < 0.0 {
        return None;
    }
    // Horizontal inset of an elliptical corner `depth` from its straight horizontal edge.
    let corner = |[rx, ry]: [f32; 2], depth: f32| {
        if rx <= 0.0 || ry <= 0.0 || depth >= ry {
            return 0.0;
        }
        let along = (ry - depth) / ry;
        rx * (1.0 - (1.0 - along * along).max(0.0).sqrt())
    };
    let left = corner(radii[0], below_top).max(corner(radii[3], above_bottom));
    let right = corner(radii[1], below_top).max(corner(radii[2], above_bottom));
    let span = [rect.x + left, rect.x + rect.width - right];
    (span[0] < span[1]).then_some(span)
}

/// Area coverage of `shape` per pixel, exact along rows and sampled down columns.
fn coverage(region: Rect, size: [usize; 2], pitch: [f32; 2], shape: (Rect, Radii)) -> Vec<f32> {
    let [columns, rows] = size;
    let mut values = vec![0.0; columns * rows];
    let weight = 1.0 / COVERAGE_SUBROWS as f32;
    let mut previous = None;
    for row in 0..rows {
        let spans: [_; COVERAGE_SUBROWS] = std::array::from_fn(|sample| {
            let y = region.y + (row as f32 + (sample as f32 + 0.5) * weight) * pitch[1];
            row_span(shape.0, shape.1, y)
        });
        let (done, rest) = values.split_at_mut(row * columns);
        let line = &mut rest[..columns];
        // A row sampling the same spans as the row above has its coverage.
        let bits = spans.map(|span| span.map(|ends| ends.map(f32::to_bits)));
        if previous.replace(bits) == Some(bits) {
            line.copy_from_slice(&done[(row - 1) * columns..]);
            continue;
        }
        for [x0, x1] in spans.into_iter().flatten() {
            let from = ((x0 - region.x) / pitch[0]).max(0.0);
            let to = ((x1 - region.x) / pitch[0]).min(columns as f32);
            if from >= to {
                continue;
            }
            for (column, value) in line
                .iter_mut()
                .enumerate()
                .take(to.ceil() as usize)
                .skip(from.floor() as usize)
            {
                let column = column as f32;
                *value += (to.min(column + 1.0) - from.max(column)) * weight;
            }
        }
    }
    values
}

/// Normalised Gaussian weights for offsets `-radius..=radius` pixels.
fn gaussian_kernel(sigma: f32) -> Vec<f32> {
    let radius = (sigma * BLUR_EXTENT_SIGMAS).ceil() as i32;
    let weights: Vec<f32> = (-radius..=radius)
        .map(|offset| (-(offset * offset) as f32 / (2.0 * sigma * sigma)).exp())
        .collect();
    let total: f32 = weights.iter().sum();
    weights.into_iter().map(|weight| weight / total).collect()
}

/// Convolves `values` along one axis; samples beyond the grid are zero. Each tap is added
/// across a whole row at once, so every pixel sums its products in kernel order. A row whose
/// taps read the same input rows as the row before it has that row's sums, and copies them.
fn convolve(values: &[f32], size: [usize; 2], kernel: &[f32], vertical: bool) -> Vec<f32> {
    let [columns, rows] = size;
    let radius = kernel.len() / 2;
    // Input rows an output row reads on either side of its own.
    let reach = if vertical { radius } else { 0 };
    let line = |row: usize| &values[row * columns..][..columns];
    // How many consecutive input rows, ending at each row, equal the row before them.
    let mut repeats = vec![0; rows];
    for row in 1..rows {
        if line(row) == line(row - 1) {
            repeats[row] = repeats[row - 1] + 1;
        }
    }
    let mut output = vec![0.0; values.len()];
    let accumulate = |output: &mut [f32], input: &[f32], weight: f32| {
        for (sum, value) in output.iter_mut().zip(input) {
            *sum += weight * value;
        }
    };
    for row in 0..rows {
        let (done, rest) = output.split_at_mut(row * columns);
        let sums = &mut rest[..columns];
        if row > reach && row + reach < rows && repeats[row + reach] > 2 * reach {
            sums.copy_from_slice(&done[(row - 1) * columns..]);
            continue;
        }
        for (tap, &weight) in kernel.iter().enumerate() {
            if vertical {
                if let Some(source) = (row + tap).checked_sub(radius).filter(|&at| at < rows) {
                    accumulate(sums, line(source), weight);
                }
            } else {
                // Output `at` reads input `at + tap - radius`.
                let (start, source) = (radius.saturating_sub(tap), tap.saturating_sub(radius));
                if let (Some(target), Some(input)) =
                    (sums.get_mut(start..), line(row).get(source..))
                {
                    accumulate(target, input, weight);
                }
            }
        }
    }
    output
}

/// The shape's coverage over `region` blurred by a Gaussian of standard deviation `sigma`;
/// `invert` gives the blurred complement, the shadow around an inset shadow's hole.
fn blurred_mask(region: Rect, shape: Option<ShadowArea>, sigma: f32, invert: bool) -> ShadowMask {
    blurred(region, sigma, invert, |size, pitch| {
        shape.map_or_else(
            || vec![0.0; size[0] * size[1]],
            |shape| shape.coverage(region, size, pitch),
        )
    })
}

/// Pixels sampling `region` for a blur of standard deviation `sigma`, and their pitch.
fn mask_grid(region: Rect, sigma: f32) -> ([usize; 2], [f32; 2]) {
    let mut density = (MASK_SIGMA_PIXELS / sigma).min(MAX_MASK_PIXELS_PER_PT);
    // The area of a large region overflows `f32` even where the pixels it holds do not.
    let area = f64::from(region.width) * f64::from(region.height);
    if area * f64::from(density).powi(2) > f64::from(MAX_MASK_PIXELS) {
        density = (f64::from(MAX_MASK_PIXELS) / area).sqrt() as f32;
    }
    let limit = MAX_MASK_PIXELS as usize;
    let mut size = [region.width, region.height]
        .map(|extent| ((extent * density).ceil() as usize).clamp(1, limit));
    // Rounding a thin axis up to one pixel must not undo the area budget.
    if size[0] > limit / size[1] {
        let larger = usize::from(size[1] > size[0]);
        size[larger] = limit / size[1 - larger];
    }
    let pitch = [
        region.width / size[0] as f32,
        region.height / size[1] as f32,
    ];
    (size, pitch)
}

/// A blurred text shadow over `region`: `coverage` gives the alpha, `0..=1`, of the
/// unblurred shadow on a grid of `size` pixels whose pitch is in points, rows top to bottom.
pub(crate) fn blurred_shadow(
    region: Rect,
    sigma: f32,
    coverage: impl FnOnce([usize; 2], [f32; 2]) -> Vec<f32>,
) -> ShadowMask {
    blurred(region, sigma, false, coverage)
}

fn blurred(
    region: Rect,
    sigma: f32,
    invert: bool,
    coverage: impl FnOnce([usize; 2], [f32; 2]) -> Vec<f32>,
) -> ShadowMask {
    crate::work::count(crate::work::Work::ShadowMask, 1);
    let (size, pitch) = mask_grid(region, sigma);
    let mut values = coverage(size, pitch);
    for (axis, vertical) in [(0, false), (1, true)] {
        values = convolve(
            &values,
            size,
            &gaussian_kernel(sigma / pitch[axis]),
            vertical,
        );
    }
    ShadowMask {
        rect: region,
        size: size.map(|extent| extent as u32),
        alpha: values
            .into_iter()
            .map(|value| {
                let value = value.clamp(0.0, 1.0);
                ((if invert { 1.0 - value } else { value }) * ALPHA_MAX).round() as u8
            })
            .collect(),
    }
}

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

    /// Standard normal cumulative distribution by Abramowitz–Stegun 7.1.26 (error < 1.5e-7).
    fn phi(x: f32) -> f32 {
        let z = x.abs() / std::f32::consts::SQRT_2;
        let t = 1.0 / (1.0 + 0.327_591_1 * z);
        let poly = t
            * (0.254_829_6
                + t * (-0.284_496_7 + t * (1.421_413_8 + t * (-1.453_152 + t * 1.061_405_4))));
        let erf = 1.0 - poly * (-(z * z)).exp();
        f32::midpoint(1.0, erf.copysign(x))
    }

    /// Alpha of the pixel containing `point`, and that pixel's centre.
    fn pixel_at(mask: &ShadowMask, point: [f32; 2]) -> (f32, [f32; 2]) {
        let pitch = [
            mask.rect.width / mask.size[0] as f32,
            mask.rect.height / mask.size[1] as f32,
        ];
        let column = ((point[0] - mask.rect.x) / pitch[0]) as usize;
        let row = ((point[1] - mask.rect.y) / pitch[1]) as usize;
        (
            f32::from(mask.alpha[row * mask.size[0] as usize + column]) / ALPHA_MAX,
            [
                mask.rect.x + (column as f32 + 0.5) * pitch[0],
                mask.rect.y + (row as f32 + 0.5) * pitch[1],
            ],
        )
    }

    fn notched_shadow(inset: bool, blur: f32, spread: f32) -> BoxPaint {
        use crate::paint::{Border, BorderStyle, BoxDecoration, CornerShape, LengthPercentage};
        BoxPaint::whole(
            Rect {
                x: 0.0,
                y: 0.0,
                width: 100.0,
                height: 80.0,
            },
            BoxDecoration {
                radii: Arc::new(std::array::from_fn(|corner| {
                    std::array::from_fn(|_| {
                        LengthPercentage::Length(if corner == 0 { 30.0 } else { 0.0 })
                    })
                })),
                corner_shapes: Arc::new([
                    CornerShape::NOTCH,
                    CornerShape::ROUND,
                    CornerShape::ROUND,
                    CornerShape::ROUND,
                ]),
                borders: [4.0, 8.0, 12.0, 16.0].map(|width| Border {
                    width,
                    style: BorderStyle::Solid,
                    color: Color::BLACK,
                }),
                shadows: Arc::new(vec![BoxShadow {
                    offset: [0.0; 2],
                    blur,
                    spread,
                    color: Color::BLACK,
                    inset,
                }]),
                ..Default::default()
            },
        )
    }

    fn inside(contours: &[Contour], point: [f64; 2]) -> bool {
        use kurbo::Shape as _;
        contours
            .iter()
            .map(|c| {
                kurbo::BezPath::from_iter(c.path_elements())
                    .winding(kurbo::Point::new(point[0], point[1]))
            })
            .sum::<i32>()
            .abs()
            % 2
            == 1
    }

    #[test]
    fn sharp_notch_shadows_keep_the_cutout_and_total_inset_distances() {
        let paint = notched_shadow(false, 0.0, 0.0);
        let outer = paint.shadows(false).pop().unwrap();
        let ShadowShape::Sharp(contours) = &outer.shape else {
            panic!("sharp shadow")
        };
        assert!(
            !inside(contours, [15.0, 15.0]),
            "outer shadow retains the notch cutout"
        );
        assert!(inside(contours, [35.0, 15.0]));
        assert!(
            inside(&outer.clip, [15.0, 15.0]),
            "shadow is allowed in the box's missing corner"
        );
        assert!(
            !inside(&outer.clip, [35.0, 15.0]),
            "shadow must not cover the box itself"
        );
        let mut paint = notched_shadow(true, 0.0, 3.0);
        Arc::make_mut(&mut paint.decoration.shadows)[0].offset = [5.0, -2.0];
        let inner = paint.shadows(true).pop().unwrap();
        assert!(
            !inside(&inner.clip, [32.0, 20.0]),
            "padding notch ends at x34: the top border offsets its vertical segment"
        );
        assert!(inside(&inner.clip, [36.0, 20.0]));
        let ShadowShape::Sharp(contours) = &inner.shape else {
            panic!("sharp shadow")
        };
        assert!(
            inside(contours, [40.0, 20.0]),
            "hole notch ends at 30+4+3+5=42"
        );
        assert!(
            !inside(contours, [44.0, 20.0]),
            "the translated inset hole is transparent"
        );
    }

    #[test]
    fn blurred_notch_alpha_matches_the_difference_of_two_rectangle_gaussians() {
        let paint = notched_shadow(false, 4.0, 0.0);
        let shadow = paint.shadows(false).pop().unwrap();
        let ShadowShape::Blurred(mask) = &shadow.shape else {
            panic!("blurred shadow")
        };
        let sigma = 2.0;
        for point in [
            [15.0, 15.0],
            [28.0, 28.0],
            [30.0, 30.0],
            [32.0, 30.0],
            [35.0, 15.0],
            [0.0, 40.0],
        ] {
            let (actual, [x, y]) = pixel_at(mask, point);
            let outer = (phi(x / sigma) - phi((x - 100.0) / sigma))
                * (phi(y / sigma) - phi((y - 80.0) / sigma));
            let cutout = (phi(x / sigma) - phi((x - 30.0) / sigma))
                * (phi(y / sigma) - phi((y - 30.0) / sigma));
            assert!(
                (actual - (outer - cutout)).abs() < 0.015,
                "notch blur at {point:?}: {actual}, expected {}",
                outer - cutout
            );
        }
    }

    #[test]
    fn outer_notch_spread_and_offset_move_the_shadow_before_box_knockout() {
        for spread in [-4.0, 4.0] {
            let mut paint = notched_shadow(false, 0.0, spread);
            Arc::make_mut(&mut paint.decoration.shadows)[0].offset = [7.0, -3.0];
            let shadow = paint.shadows(false).pop().unwrap();
            let ShadowShape::Sharp(contours) = &shadow.shape else {
                panic!("sharp shadow")
            };
            let vertex = [30.0 - spread + 7.0, 30.0 - spread - 3.0].map(f64::from);
            assert!(
                !inside(contours, [vertex[0] - 1.0, 15.0]),
                "spread {spread}: translated vertical notch edge excludes its cutout"
            );
            assert!(inside(contours, [vertex[0] + 1.0, 15.0]));
            assert!(!inside(contours, [20.0, vertex[1] - 1.0]));
            assert!(inside(contours, [20.0, vertex[1] + 1.0]));
            assert!(
                inside(&shadow.clip, [15.0, 15.0]),
                "knockout remains at the original notch, independent of spread and offset"
            );
            assert!(!inside(&shadow.clip, [50.0, 50.0]));
        }
    }

    #[test]
    fn blurred_inset_notch_uses_the_complement_of_the_translated_hole() {
        let mut paint = notched_shadow(true, 4.0, 3.0);
        Arc::make_mut(&mut paint.decoration.shadows)[0].offset = [5.0, -2.0];
        let shadow = paint.shadows(true).pop().unwrap();
        let ShadowShape::Blurred(mask) = &shadow.shape else {
            panic!("blurred shadow")
        };
        let sigma = 2.0;
        for point in [
            [40.0, 20.0],
            [42.0, 20.0],
            [44.0, 20.0],
            [42.0, 47.0],
            [50.0, 47.0],
        ] {
            let (actual, [x, y]) = pixel_at(mask, point);
            // Hole bounds [19,89]x[7,65] translated by [5,-2], notch vertex [42,47].
            let outer = (phi((x - 24.0) / sigma) - phi((x - 94.0) / sigma))
                * (phi((y - 5.0) / sigma) - phi((y - 63.0) / sigma));
            let cutout = (phi((x - 24.0) / sigma) - phi((x - 42.0) / sigma))
                * (phi((y - 5.0) / sigma) - phi((y - 47.0) / sigma));
            assert!(
                (actual - (1.0 - outer + cutout)).abs() < 0.015,
                "inset notch at {point:?}: {actual}, expected {}",
                1.0 - outer + cutout
            );
        }
    }

    #[test]
    fn disconnected_shadow_islands_blur_independently_and_slices_keep_the_unbroken_mask() {
        use crate::paint::LengthPercentage;
        let mut paint = notched_shadow(false, 4.0, -4.0);
        paint.decoration.corner_shapes = Arc::new([
            CornerShape::NOTCH,
            CornerShape::ROUND,
            CornerShape::NOTCH,
            CornerShape::ROUND,
        ]);
        paint.decoration.radii = Arc::new(std::array::from_fn(|corner| {
            std::array::from_fn(|_| {
                if corner % 2 == 0 {
                    LengthPercentage::Percentage(0.5)
                } else {
                    LengthPercentage::ZERO
                }
            })
        }));
        let whole = paint.shadows(false).pop().unwrap();
        let ShadowShape::Blurred(mask) = &whole.shape else {
            panic!("blurred shadow")
        };
        let sigma = 2.0;
        for point in [
            [50.0, 40.0],
            [54.0, 30.0],
            [40.0, 44.0],
            [60.0, 20.0],
            [30.0, 50.0],
        ] {
            let (actual, [x, y]) = pixel_at(mask, point);
            let top_right = (phi((x - 54.0) / sigma) - phi((x - 96.0) / sigma))
                * (phi((y - 4.0) / sigma) - phi((y - 36.0) / sigma));
            let bottom_left = (phi((x - 4.0) / sigma) - phi((x - 46.0) / sigma))
                * (phi((y - 44.0) / sigma) - phi((y - 76.0) / sigma));
            assert!(
                (actual - top_right - bottom_left).abs() < 0.015,
                "disconnected shadow at {point:?}: {actual}, expected {}",
                top_right + bottom_left
            );
        }
        paint.background_box = Some(paint.rect);
        paint.rect.y = 10.0;
        paint.rect.height = 60.0;
        paint.first_fragment = false;
        paint.last_fragment = false;
        let sliced = paint.shadows(false).pop().unwrap();
        let ShadowShape::Blurred(sliced_mask) = &sliced.shape else {
            panic!("blurred shadow")
        };
        assert_eq!(
            sliced_mask, mask,
            "slicing preserves the complete unbroken Gaussian mask"
        );
        assert_eq!(
            [sliced.bounds.y, sliced.bounds.height],
            [10.0, 60.0],
            "only the visible fragment bounds crop the shadow"
        );
    }

    #[test]
    fn empty_concave_padding_produces_no_inset_shadow() {
        let mut paint = notched_shadow(true, 4.0, 0.0);
        paint.decoration.corner_shapes = Arc::new([CornerShape::NOTCH; 4]);
        paint.decoration.radii = Arc::new(std::array::from_fn(|_| {
            std::array::from_fn(|_| crate::paint::LengthPercentage::Percentage(0.5))
        }));
        assert!(
            paint.shadows(true).is_empty(),
            "four half-box notches consume the whole padding region"
        );
    }

    #[test]
    fn spread_sharpens_radii_smaller_than_the_spread() {
        let grown = spread_radii([[10.0, 30.0], [0.0, 0.0], [20.0, 20.0], [5.0, 40.0]], 20.0);
        assert_eq!(
            grown,
            [[27.5, 50.0], [0.0, 0.0], [40.0, 40.0], [16.5625, 60.0]],
            "specification example r = 10, spread = 20 gives 27.5; r = 5 grows by \
             20 × (1 + (0.25 − 1)³); zero stays sharp; r ≥ spread adds the spread"
        );
        assert_eq!(
            spread_radii([[10.0, 3.0]; 4], -4.0),
            [[6.0, 0.0]; 4],
            "a negative spread subtracts and floors at zero"
        );
    }

    #[test]
    fn blurred_edges_follow_the_gaussian_of_half_the_blur_radius() {
        let sigma = 8.0 * SIGMA_PER_BLUR;
        let shape = Rect {
            x: 0.0,
            y: 0.0,
            width: 100.0,
            height: 60.0,
        };
        let region = outset(shape, [sigma * BLUR_EXTENT_SIGMAS; 4]);
        let square = Some(ShadowArea::Rounded(shape, [[0.0; 2]; 4]));
        let mask = blurred_mask(region, square.clone(), sigma, false);
        let inverted = blurred_mask(region, square, sigma, true);
        for distance in [-2.0 * sigma, -sigma, 0.0, sigma, 2.0 * sigma] {
            // Along the middle of the left edge, far from the corners; inside is positive.
            let point = [shape.x + distance, shape.y + shape.height / 2.0];
            let (actual, centre) = pixel_at(&mask, point);
            let expected = phi((centre[0] - shape.x) / sigma);
            assert!(
                (actual - expected).abs() < 0.01,
                "pixel centred {}pt inside the edge: alpha {actual}, Gaussian {expected}",
                centre[0] - shape.x
            );
            let (complement, _) = pixel_at(&inverted, point);
            assert!(
                (complement - (1.0 - expected)).abs() < 0.01,
                "the same pixel around an inset hole: alpha {complement}, expected {}",
                1.0 - expected
            );
        }
        let (corner, centre) = pixel_at(&mask, [shape.x, shape.y]);
        let expected = phi((centre[0] - shape.x) / sigma) * phi((centre[1] - shape.y) / sigma);
        assert!(
            (corner - expected).abs() < 0.01,
            "a square corner is the product of the two edge profiles: {corner}, expected {expected}"
        );
    }

    #[test]
    fn mask_grid_keeps_extreme_aspect_ratios_within_the_allocation_budget() {
        for [width, height] in [[1.0e12, 1.0], [1.0, 1.0e12], [1.0e30, 1.0e30]] {
            let (size, pitch) = mask_grid(
                Rect {
                    x: 0.0,
                    y: 0.0,
                    width,
                    height,
                },
                0.5,
            );
            assert!(
                size[0] as f64 * size[1] as f64 <= f64::from(MAX_MASK_PIXELS),
                "{width}x{height}: rounded grid {size:?} exceeds the shadow pixel budget"
            );
            assert!(
                pitch.iter().all(|value| value.is_finite() && *value > 0.0),
                "finite positive region needs finite positive pitch: {pitch:?}"
            );
            if width == height {
                let expected = MAX_MASK_PIXELS.sqrt() as usize;
                assert!(
                    size.iter().all(|&extent| extent >= expected - 1),
                    "overflow in physical area must not collapse a large square to a single pixel: {size:?}"
                );
            }
        }
    }

    #[test]
    fn huge_blurs_of_huge_regions_sample_each_deviation_with_a_bounded_kernel() {
        const SIGMA: f32 = 5.0e29;
        let (size, pitch) = mask_grid(
            Rect {
                x: 0.0,
                y: 0.0,
                width: 5.0e30,
                height: 5.0e30,
            },
            SIGMA,
        );
        assert!(
            pitch
                .iter()
                .all(|pitch| SIGMA / pitch <= MASK_SIGMA_PIXELS * (1.0 + f32::EPSILON)),
            "a region whose area overflows f32 but holds few deviations must not be sampled \
             finer than {MASK_SIGMA_PIXELS} px per deviation, which widens the kernel: \
             {size:?} pixels of pitch {pitch:?}"
        );
    }

    #[test]
    fn mask_density_tracks_the_deviation_within_limits() {
        let region = Rect {
            x: 0.0,
            y: 0.0,
            width: 50.0,
            height: 20.0,
        };
        assert_eq!(
            blurred_mask(region, None, 0.5, false).size,
            [200, 80],
            "small blurs sample at the {MAX_MASK_PIXELS_PER_PT} px/pt cap"
        );
        assert_eq!(
            blurred_mask(region, None, 12.0, false).size,
            [25, 10],
            "a 12pt deviation needs only {MASK_SIGMA_PIXELS} px per deviation"
        );
        let huge = Rect {
            width: 10_000.0,
            height: 10_000.0,
            ..region
        };
        let capped = blurred_mask(huge, None, 0.5, false);
        let pixels = capped.size[0] as f32 * capped.size[1] as f32;
        assert!(
            pixels <= MAX_MASK_PIXELS * 1.01,
            "pixel budget exceeded: {:?} has {pixels} pixels",
            capped.size
        );
    }

    #[test]
    fn convolving_repeated_rows_matches_every_pixel_summed_in_kernel_order() {
        // Runs of equal rows longer and shorter than the kernel, at the edges and inside.
        let [columns, rows] = [7, 40];
        let pattern = |row: usize| match row {
            0..4 | 36.. => 0.0,
            4..6 => 0.25,
            6..25 => 1.0,
            25..27 => 0.5,
            _ => 0.75,
        };
        let values: Vec<f32> = (0..rows)
            .flat_map(|row| (0..columns).map(move |column| pattern(row) * (column + 1) as f32))
            .collect();
        let kernel = gaussian_kernel(1.7);
        let radius = kernel.len() / 2;
        for vertical in [false, true] {
            let actual = convolve(&values, [columns, rows], &kernel, vertical);
            for row in 0..rows {
                for column in 0..columns {
                    let mut expected = 0.0;
                    for (tap, weight) in kernel.iter().enumerate() {
                        let [x, y] = if vertical {
                            [Some(column), (row + tap).checked_sub(radius)]
                        } else {
                            [(column + tap).checked_sub(radius), Some(row)]
                        };
                        if let (Some(x), Some(y)) = (x.filter(|&x| x < columns), y)
                            && y < rows
                        {
                            expected += weight * values[y * columns + x];
                        }
                    }
                    let index = row * columns + column;
                    assert_eq!(
                        actual[index].to_bits(),
                        expected.to_bits(),
                        "vertical {vertical}, row {row}, column {column}: {} but summing the \
                         taps in order gives {expected}",
                        actual[index]
                    );
                }
            }
        }
    }

    #[test]
    fn rounded_rows_span_the_elliptical_corners() {
        let rect = Rect {
            x: 10.0,
            y: 0.0,
            width: 100.0,
            height: 40.0,
        };
        let radii = [[20.0, 10.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]];
        let top = row_span(rect, radii, 0.0).unwrap();
        assert!(
            (top[0] - 30.0).abs() < 1e-4 && top[1] == 110.0,
            "the top row starts where the corner arc meets the top edge: {top:?}"
        );
        let middle = row_span(rect, radii, 5.0).unwrap();
        let expected = 10.0 + 20.0 * (1.0 - 0.75_f32.sqrt());
        assert!(
            (middle[0] - expected).abs() < 1e-4,
            "half-way down the corner: {middle:?}, expected {expected}"
        );
        assert_eq!(row_span(rect, radii, 41.0), None, "below the box");
    }
}