mirui 0.45.0

A lightweight, no_std ECS-driven UI framework for embedded, desktop, and WebAssembly
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
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
use alloc::vec::Vec;

use crate::types::{Fixed, Point, Transform, Transform3D};

use super::path::{Path, PathCmd};

pub use mirx::scene::{LineCap, LineJoin};

/// Cap so a pathological path can't pin tens of KB on a 200 KB MCU heap.
pub const MAX_SEG_CAP: usize = 2048;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LineSeg {
    pub p1: Point,
    pub p2: Point,
}

/// Polygon fill rule.
#[allow(dead_code)] // NonZero is used by the ttf-parser path; tests verify both rules.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FillRule {
    /// Pixel is inside if a ray from it crosses an odd number of edges.
    /// Default for SVG / rectangles / shapes that don't self-overlap.
    EvenOdd,
    /// Pixel is inside if the signed winding count (downward-going edges
    /// add 1, upward subtract 1) is non-zero. Required for TrueType /
    /// CFF outlines where the same contour can wrap around itself.
    NonZero,
}

/// A range in the caller-owned flattened segment buffer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SubPath {
    pub start: usize,
    pub end: usize,
    pub closed: bool,
}

#[cfg(any(feature = "sdl-gpu", feature = "wgpu", test))]
#[derive(Clone, Copy)]
pub(crate) struct StrokeSpec<'a> {
    pub width: Fixed,
    pub cap: LineCap,
    pub join: LineJoin,
    pub miter_limit: Fixed,
    pub dash: &'a [Fixed],
    pub dash_scale: Fixed,
}

#[cfg(any(feature = "sdl-gpu", feature = "wgpu", test))]
pub(crate) struct StrokeScratch {
    outline: Path,
    flattened: Vec<LineSeg>,
    subpaths: Vec<SubPath>,
    semantic_joins: Vec<usize>,
    normals: Vec<Point>,
    rail: Vec<Point>,
    left_rail: Vec<Point>,
    arc: Vec<Point>,
    dashed: Vec<LineSeg>,
    dash_subpaths: Vec<SubPath>,
    dashed_semantic_joins: Vec<usize>,
    dash_lengths: Vec<Fixed>,
}

#[cfg(any(feature = "sdl-gpu", feature = "wgpu", test))]
impl StrokeScratch {
    pub fn new() -> Self {
        Self {
            outline: Path::new(),
            flattened: Vec::new(),
            subpaths: Vec::new(),
            semantic_joins: Vec::new(),
            normals: Vec::new(),
            rail: Vec::new(),
            left_rail: Vec::new(),
            arc: Vec::new(),
            dashed: Vec::new(),
            dash_subpaths: Vec::new(),
            dashed_semantic_joins: Vec::new(),
            dash_lengths: Vec::new(),
        }
    }

    pub fn outline(
        &mut self,
        path: &Path,
        transform: Option<&Transform>,
        spec: StrokeSpec<'_>,
    ) -> &Path {
        self.dash_lengths.clear();
        let dash = if spec.dash_scale == Fixed::ONE {
            spec.dash
        } else {
            self.dash_lengths
                .extend(spec.dash.iter().map(|length| *length * spec.dash_scale));
            &self.dash_lengths
        };
        offset_polygon_into(
            &path.cmds,
            transform,
            spec.width,
            spec.cap,
            spec.join,
            spec.miter_limit,
            (!dash.is_empty()).then_some(dash),
            &mut self.outline,
            &mut self.flattened,
            &mut self.subpaths,
            &mut self.semantic_joins,
            &mut self.normals,
            &mut self.rail,
            &mut self.left_rail,
            &mut self.arc,
            &mut self.dashed,
            &mut self.dash_subpaths,
            &mut self.dashed_semantic_joins,
        );
        &self.outline
    }
}

/// Subdivision step counts. Chosen to keep a single control-point radius
/// visually smooth on 128×128 screens; larger paths may show facets but UI
/// radii are small.
const QUAD_STEPS: i32 = 8;
const CUBIC_STEPS: i32 = 16;

/// `transform` folds in at emit time so backends can compose
/// `viewport × cmd` once and avoid pre-rebuilding the PathCmd stream.
pub fn flatten_into(cmds: &[PathCmd], transform: Option<&Transform>, out: &mut Vec<LineSeg>) {
    out.clear();
    let mut subpath_start = Point::ZERO;
    let mut current = Point::ZERO;

    let apply = |p: Point| -> Point {
        match transform {
            Some(tf) => tf.apply_point(p),
            None => p,
        }
    };

    for cmd in cmds {
        match cmd {
            PathCmd::MoveTo(p) => {
                let p = apply(*p);
                subpath_start = p;
                current = p;
            }
            PathCmd::LineTo(p) => {
                let p = apply(*p);
                out.push(LineSeg { p1: current, p2: p });
                current = p;
            }
            PathCmd::QuadTo { ctrl, end } => {
                let ctrl = apply(*ctrl);
                let end = apply(*end);
                let p0 = current;
                for i in 1..=QUAD_STEPS {
                    let t = Fixed::from_int(i) / Fixed::from_int(QUAD_STEPS);
                    let next = quad_at(p0, ctrl, end, t);
                    out.push(LineSeg {
                        p1: current,
                        p2: next,
                    });
                    current = next;
                }
            }
            PathCmd::CubicTo { ctrl1, ctrl2, end } => {
                let ctrl1 = apply(*ctrl1);
                let ctrl2 = apply(*ctrl2);
                let end = apply(*end);
                let p0 = current;
                for i in 1..=CUBIC_STEPS {
                    let t = Fixed::from_int(i) / Fixed::from_int(CUBIC_STEPS);
                    let next = cubic_at(p0, ctrl1, ctrl2, end, t);
                    out.push(LineSeg {
                        p1: current,
                        p2: next,
                    });
                    current = next;
                }
            }
            PathCmd::Close => {
                if current != subpath_start {
                    out.push(LineSeg {
                        p1: current,
                        p2: subpath_start,
                    });
                }
                current = subpath_start;
            }
        }
    }

    if out.capacity() > MAX_SEG_CAP {
        out.shrink_to(MAX_SEG_CAP);
    }
}

/// Flatten a path after homographic projection into caller-owned segments.
/// Curve samples are projected individually so perspective does not get
/// approximated by an affine transform of the control polygon.
pub(crate) fn flatten_projective_into(
    cmds: &[PathCmd],
    transform: &Transform3D,
    out: &mut Vec<LineSeg>,
) -> Result<(), ()> {
    out.clear();
    let mut subpath_start = Point::ZERO;
    let mut current = Point::ZERO;
    let mut projected_start = Point::ZERO;
    let mut projected_current = Point::ZERO;
    let mut has_current = false;

    for cmd in cmds {
        match cmd {
            PathCmd::MoveTo(point) => {
                let projected = transform.apply_point(*point).ok_or(())?;
                subpath_start = *point;
                current = *point;
                projected_start = projected;
                projected_current = projected;
                has_current = true;
            }
            PathCmd::LineTo(point) if has_current => {
                let projected = transform.apply_point(*point).ok_or(())?;
                out.push(LineSeg {
                    p1: projected_current,
                    p2: projected,
                });
                current = *point;
                projected_current = projected;
            }
            PathCmd::QuadTo { ctrl, end } if has_current => {
                let p0 = current;
                for i in 1..=QUAD_STEPS {
                    let t = Fixed::from_int(i) / Fixed::from_int(QUAD_STEPS);
                    let point = quad_at(p0, *ctrl, *end, t);
                    let projected = transform.apply_point(point).ok_or(())?;
                    out.push(LineSeg {
                        p1: projected_current,
                        p2: projected,
                    });
                    projected_current = projected;
                }
                current = *end;
            }
            PathCmd::CubicTo { ctrl1, ctrl2, end } if has_current => {
                let p0 = current;
                for i in 1..=CUBIC_STEPS {
                    let t = Fixed::from_int(i) / Fixed::from_int(CUBIC_STEPS);
                    let point = cubic_at(p0, *ctrl1, *ctrl2, *end, t);
                    let projected = transform.apply_point(point).ok_or(())?;
                    out.push(LineSeg {
                        p1: projected_current,
                        p2: projected,
                    });
                    projected_current = projected;
                }
                current = *end;
            }
            PathCmd::Close if has_current => {
                if projected_current != projected_start {
                    out.push(LineSeg {
                        p1: projected_current,
                        p2: projected_start,
                    });
                }
                current = subpath_start;
                projected_current = projected_start;
            }
            _ => {}
        }
    }

    if out.capacity() > MAX_SEG_CAP {
        out.shrink_to(MAX_SEG_CAP);
    }
    Ok(())
}

/// Flatten path into per-subpath groups, tracking whether each ended with
/// Close. stroke_path needs this to decide between offset-ring (closed) and
/// butt-capped strip (open) handling.
pub fn flatten_subpaths_into(
    cmds: &[PathCmd],
    transform: Option<&Transform>,
    segments: &mut Vec<LineSeg>,
    out: &mut Vec<SubPath>,
) {
    flatten_subpaths_impl(cmds, transform, segments, out, None);
}

fn flatten_stroke_subpaths_into(
    cmds: &[PathCmd],
    transform: Option<&Transform>,
    segments: &mut Vec<LineSeg>,
    out: &mut Vec<SubPath>,
    semantic_joins: &mut Vec<usize>,
) {
    flatten_subpaths_impl(cmds, transform, segments, out, Some(semantic_joins));
}

fn flatten_subpaths_impl(
    cmds: &[PathCmd],
    transform: Option<&Transform>,
    segments: &mut Vec<LineSeg>,
    out: &mut Vec<SubPath>,
    mut semantic_joins: Option<&mut Vec<usize>>,
) {
    segments.clear();
    out.clear();
    if let Some(joins) = semantic_joins.as_deref_mut() {
        joins.clear();
    }
    let mut subpath_start = Point::ZERO;
    let mut current = Point::ZERO;
    let mut start = 0;
    let mut has_moveto = false;

    let apply = |p: Point| -> Point {
        match transform {
            Some(tf) => tf.apply_point(p),
            None => p,
        }
    };

    let flush = |segments: &[LineSeg], out: &mut Vec<SubPath>, start: usize, closed: bool| {
        if segments.len() > start {
            out.push(SubPath {
                start,
                end: segments.len(),
                closed,
            });
        }
    };

    for cmd in cmds {
        match cmd {
            PathCmd::MoveTo(p) => {
                flush(segments, out, start, false);
                start = segments.len();
                let p = apply(*p);
                subpath_start = p;
                current = p;
                has_moveto = true;
            }
            PathCmd::LineTo(p) => {
                if !has_moveto {
                    continue;
                }
                if let Some(joins) = semantic_joins.as_deref_mut() {
                    joins.push(segments.len());
                }
                let p = apply(*p);
                segments.push(LineSeg { p1: current, p2: p });
                current = p;
            }
            PathCmd::QuadTo { ctrl, end } => {
                if !has_moveto {
                    continue;
                }
                if let Some(joins) = semantic_joins.as_deref_mut() {
                    joins.push(segments.len());
                }
                let ctrl = apply(*ctrl);
                let end = apply(*end);
                let p0 = current;
                for i in 1..=QUAD_STEPS {
                    let t = Fixed::from_int(i) / Fixed::from_int(QUAD_STEPS);
                    let next = quad_at(p0, ctrl, end, t);
                    segments.push(LineSeg {
                        p1: current,
                        p2: next,
                    });
                    current = next;
                }
            }
            PathCmd::CubicTo { ctrl1, ctrl2, end } => {
                if !has_moveto {
                    continue;
                }
                if let Some(joins) = semantic_joins.as_deref_mut() {
                    joins.push(segments.len());
                }
                let ctrl1 = apply(*ctrl1);
                let ctrl2 = apply(*ctrl2);
                let end = apply(*end);
                let p0 = current;
                for i in 1..=CUBIC_STEPS {
                    let t = Fixed::from_int(i) / Fixed::from_int(CUBIC_STEPS);
                    let next = cubic_at(p0, ctrl1, ctrl2, end, t);
                    segments.push(LineSeg {
                        p1: current,
                        p2: next,
                    });
                    current = next;
                }
            }
            PathCmd::Close => {
                if current != subpath_start {
                    if let Some(joins) = semantic_joins.as_deref_mut() {
                        joins.push(segments.len());
                    }
                    segments.push(LineSeg {
                        p1: current,
                        p2: subpath_start,
                    });
                }
                current = subpath_start;
                flush(segments, out, start, true);
                start = segments.len();
                has_moveto = false;
            }
        }
    }
    flush(segments, out, start, false);
}

/// Vertical supersampling count per pixel row. 4 sub-scanlines gives 5-level
/// (0/4, 1/4, 2/4, 3/4, 4/4) coverage — enough to hide the worst jaggies
/// without making the rasterizer too heavy on ESP32.
const SUB_SCANLINES: i32 = 4;

/// Coverage-based fill rasterizer with 4 sub-scanlines per pixel row.
/// Emits `cov ∈ [0, 1]` per pixel under the chosen [`FillRule`].
/// `acc` and `crossings` are caller-owned scratch buffers, reused
/// across calls so a tight fill loop doesn't allocate per draw.
#[allow(clippy::too_many_arguments)]
pub fn scanline_fill(
    segs: &[LineSeg],
    px_x0: i32,
    py_y0: i32,
    px_x1: i32,
    py_y1: i32,
    rule: FillRule,
    acc: &mut Vec<Fixed>,
    crossings: &mut Vec<(Fixed, i8)>,
    mut emit: impl FnMut(i32, i32, Fixed),
) {
    if segs.is_empty() || px_x1 <= px_x0 || py_y1 <= py_y0 {
        return;
    }
    let row_w = (px_x1 - px_x0) as usize;
    acc.clear();
    acc.resize(row_w, Fixed::ZERO);
    let sub_weight = Fixed::ONE / SUB_SCANLINES;

    for py in py_y0..py_y1 {
        for a in acc.iter_mut() {
            *a = Fixed::ZERO;
        }

        for sub in 0..SUB_SCANLINES {
            let y_sample =
                Fixed::from_int(py) + (Fixed::from_int(sub) + Fixed::ONE / 2) / SUB_SCANLINES;

            crossings.clear();
            for s in segs {
                let (y_lo, y_hi, winding) = if s.p1.y <= s.p2.y {
                    (s.p1.y, s.p2.y, 1i8)
                } else {
                    (s.p2.y, s.p1.y, -1i8)
                };
                if y_sample < y_lo || y_sample >= y_hi {
                    continue;
                }
                let dy = s.p2.y - s.p1.y;
                if dy == Fixed::ZERO {
                    continue;
                }
                let t = (y_sample - s.p1.y) / dy;
                let x_cross = s.p1.x + (s.p2.x - s.p1.x) * t;
                crossings.push((x_cross, winding));
            }

            crossings.sort_by_key(|x| x.0);

            match rule {
                FillRule::EvenOdd => {
                    let mut i = 0;
                    while i + 1 < crossings.len() {
                        let xa = crossings[i].0;
                        let xb = crossings[i + 1].0;
                        accumulate_interval(acc, px_x0, px_x1, xa, xb, sub_weight);
                        i += 2;
                    }
                }
                FillRule::NonZero => {
                    let mut count: i32 = 0;
                    let mut span_start: Option<Fixed> = None;
                    for (x, winding) in crossings.iter() {
                        let prev = count;
                        count += *winding as i32;
                        let was_inside = prev != 0;
                        let now_inside = count != 0;
                        match (was_inside, now_inside) {
                            (false, true) => span_start = Some(*x),
                            (true, false) => {
                                if let Some(start) = span_start.take() {
                                    accumulate_interval(acc, px_x0, px_x1, start, *x, sub_weight);
                                }
                            }
                            _ => {}
                        }
                    }
                }
            }
        }

        for (i, cov) in acc.iter().enumerate() {
            if *cov > Fixed::ZERO {
                emit(px_x0 + i as i32, py, *cov);
            }
        }
    }
}

/// Add `weight` × (fraction of each pixel covered by [xa, xb]) into `acc`.
fn accumulate_interval(
    acc: &mut [Fixed],
    px_x0: i32,
    px_x1: i32,
    xa: Fixed,
    xb: Fixed,
    weight: Fixed,
) {
    let (xlo, xhi) = if xa <= xb { (xa, xb) } else { (xb, xa) };
    if xhi <= Fixed::from_int(px_x0) || xlo >= Fixed::from_int(px_x1) {
        return;
    }

    let lo_int = xlo.to_int().max(px_x0);
    let hi_int = xhi.ceil().to_int().min(px_x1);

    for px in lo_int..hi_int {
        let pixel_left = Fixed::from_int(px);
        let pixel_right = Fixed::from_int(px + 1);
        let left = xlo.max(pixel_left);
        let right = xhi.min(pixel_right);
        let frac = right - left;
        if frac > Fixed::ZERO {
            acc[(px - px_x0) as usize] += frac * weight;
        }
    }
}

/// Point-in-polygon test via +X ray casting with even-odd rule.
/// Uses the half-open convention [y_min, y_max) to avoid double-counting at
/// shared vertices.
#[allow(dead_code)] // Retained for hit-test use-cases and future rasterizers
pub(crate) fn point_in_segments(pt: Point, segs: &[LineSeg]) -> bool {
    let mut crossings = 0u32;
    for s in segs {
        let (y_lo, y_hi) = if s.p1.y <= s.p2.y {
            (s.p1.y, s.p2.y)
        } else {
            (s.p2.y, s.p1.y)
        };
        if pt.y < y_lo || pt.y >= y_hi {
            continue;
        }
        // x of the edge at this y-row. Skip horizontal edges (can't intersect).
        let dy = s.p2.y - s.p1.y;
        if dy == Fixed::ZERO {
            continue;
        }
        let t = (pt.y - s.p1.y) / dy;
        let x_cross = s.p1.x + (s.p2.x - s.p1.x) * t;
        if x_cross > pt.x {
            crossings += 1;
        }
    }
    crossings & 1 == 1
}

/// Minimum unsigned distance from `pt` to any segment in `segs`, capped at
/// `cap`. Segments whose AABB is farther than `cap` from `pt` are skipped
/// without any sqrt. Returns `cap` when nothing is within range.
///
/// `cap` is compared in **Fixed space** (not squared) to avoid `Fixed * Fixed`
/// overflow when callers pass `Fixed::MAX` or similar large bounds.
#[allow(dead_code)] // Retained for stroke width hit-testing and future use
pub(crate) fn min_dist_to_segments_capped(pt: Point, segs: &[LineSeg], cap: Fixed) -> Fixed {
    let mut best = cap;
    let mut found = false;
    for s in segs {
        let (xlo, xhi) = if s.p1.x <= s.p2.x {
            (s.p1.x, s.p2.x)
        } else {
            (s.p2.x, s.p1.x)
        };
        let (ylo, yhi) = if s.p1.y <= s.p2.y {
            (s.p1.y, s.p2.y)
        } else {
            (s.p2.y, s.p1.y)
        };
        // Quick Chebyshev reject: if the segment's bbox is strictly farther
        // than `best` on either axis, the true distance is ≥ best too.
        let dx_box = if pt.x < xlo {
            xlo - pt.x
        } else if pt.x > xhi {
            pt.x - xhi
        } else {
            Fixed::ZERO
        };
        let dy_box = if pt.y < ylo {
            ylo - pt.y
        } else if pt.y > yhi {
            pt.y - yhi
        } else {
            Fixed::ZERO
        };
        if dx_box >= best || dy_box >= best {
            continue;
        }
        let d = dist_sq_point_to_segment(pt, s.p1, s.p2).sqrt();
        if d < best {
            best = d;
            found = true;
        }
    }
    if found { best } else { cap }
}

/// Squared distance from point `p` to segment `ab`. Caller decides when to sqrt.
fn dist_sq_point_to_segment(p: Point, a: Point, b: Point) -> Fixed {
    let abx = b.x - a.x;
    let aby = b.y - a.y;
    let len_sq = abx * abx + aby * aby;
    if len_sq == Fixed::ZERO {
        let dx = p.x - a.x;
        let dy = p.y - a.y;
        return dx * dx + dy * dy;
    }
    let apx = p.x - a.x;
    let apy = p.y - a.y;
    let t_raw = (apx * abx + apy * aby) / len_sq;
    let t = t_raw.max(Fixed::ZERO).min(Fixed::ONE);
    let cx = a.x + abx * t;
    let cy = a.y + aby * t;
    let dx = p.x - cx;
    let dy = p.y - cy;
    dx * dx + dy * dy
}

struct DashJoinMap<'a> {
    source_start: usize,
    source_semantic_joins: &'a [usize],
    output_semantic_joins: &'a mut Vec<usize>,
}

fn apply_dash_pattern(
    segs: &[LineSeg],
    closed: bool,
    pattern: &[Fixed],
    segments: &mut Vec<LineSeg>,
    out: &mut Vec<SubPath>,
    joins: DashJoinMap<'_>,
) {
    let first_output = out.len();
    let first_segment = segments.len();
    if pattern.is_empty() || pattern.iter().any(|length| *length <= Fixed::ZERO) {
        segments.extend_from_slice(segs);
        joins.output_semantic_joins.extend(
            joins
                .source_semantic_joins
                .iter()
                .copied()
                .filter(|index| {
                    *index >= joins.source_start && *index < joins.source_start + segs.len()
                })
                .map(|index| first_segment + index - joins.source_start),
        );
        if !segs.is_empty() {
            out.push(SubPath {
                start: first_segment,
                end: segments.len(),
                closed,
            });
        }
        return;
    }

    let mut remaining = pattern[0];
    let mut pattern_idx = 0;
    let mut on = true;
    let mut start = segments.len();

    for (source_index, &seg) in segs.iter().enumerate() {
        let dx = seg.p2.x - seg.p1.x;
        let dy = seg.p2.y - seg.p1.y;
        let seg_len = (dx * dx + dy * dy).sqrt();

        if seg_len == Fixed::ZERO {
            continue;
        }

        let ux = dx / seg_len;
        let uy = dy / seg_len;
        let mut walked = Fixed::ZERO;
        let p_start = seg.p1;

        while walked < seg_len {
            let step = (seg_len - walked).min(remaining);
            let p_end = Point {
                x: p_start.x + ux * (walked + step),
                y: p_start.y + uy * (walked + step),
            };
            if on {
                if walked == Fixed::ZERO
                    && joins
                        .source_semantic_joins
                        .binary_search(&(joins.source_start + source_index))
                        .is_ok()
                {
                    joins.output_semantic_joins.push(segments.len());
                }
                segments.push(LineSeg {
                    p1: Point {
                        x: p_start.x + ux * walked,
                        y: p_start.y + uy * walked,
                    },
                    p2: p_end,
                });
            }
            walked += step;
            remaining -= step;
            if remaining <= Fixed::ZERO {
                if on && segments.len() > start {
                    out.push(SubPath {
                        start,
                        end: segments.len(),
                        closed: false,
                    });
                }
                start = segments.len();
                pattern_idx = (pattern_idx + 1) % pattern.len();
                on = !on;
                remaining = pattern[pattern_idx];
            }
        }
    }
    if segments.len() > start {
        out.push(SubPath {
            start,
            end: segments.len(),
            closed: false,
        });
    }
    if closed && out.len() == first_output + 1 && segments[first_segment..] == *segs {
        out[first_output].closed = true;
    }
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn offset_polygon_into(
    cmds: &[PathCmd],
    transform: Option<&Transform>,
    width: Fixed,
    cap: LineCap,
    join: LineJoin,
    miter_limit: Fixed,
    dash: Option<&[Fixed]>,
    out: &mut Path,
    flattened: &mut Vec<LineSeg>,
    subpath_scratch: &mut Vec<SubPath>,
    semantic_joins_scratch: &mut Vec<usize>,
    normals_scratch: &mut Vec<Point>,
    rail_scratch: &mut Vec<Point>,
    left_rail_scratch: &mut Vec<Point>,
    arc_scratch: &mut Vec<Point>,
    dashed: &mut Vec<LineSeg>,
    dash_scratch: &mut Vec<SubPath>,
    dashed_semantic_joins_scratch: &mut Vec<usize>,
) {
    out.cmds.to_mut().clear();
    if width <= Fixed::ZERO {
        return;
    }
    let half = width / 2;

    flatten_stroke_subpaths_into(
        cmds,
        transform,
        flattened,
        subpath_scratch,
        semantic_joins_scratch,
    );

    let (segments, subpaths, semantic_joins): (&[LineSeg], &[SubPath], &[usize]) =
        if let Some(pattern) = dash {
            if pattern.is_empty() {
                (flattened, subpath_scratch, semantic_joins_scratch)
            } else {
                dashed.clear();
                dash_scratch.clear();
                dashed_semantic_joins_scratch.clear();
                for sub in subpath_scratch.iter() {
                    apply_dash_pattern(
                        &flattened[sub.start..sub.end],
                        sub.closed,
                        pattern,
                        dashed,
                        dash_scratch,
                        DashJoinMap {
                            source_start: sub.start,
                            source_semantic_joins: semantic_joins_scratch,
                            output_semantic_joins: dashed_semantic_joins_scratch,
                        },
                    );
                }
                (dashed, dash_scratch, dashed_semantic_joins_scratch)
            }
        } else {
            (flattened, subpath_scratch, semantic_joins_scratch)
        };

    for sub in subpaths {
        let segs = &segments[sub.start..sub.end];
        compute_normals_into(segs, half, normals_scratch);
        if sub.closed {
            build_ring_into(
                segs,
                normals_scratch,
                half,
                join,
                miter_limit,
                /*left=*/ true,
                semantic_joins,
                sub.start,
                rail_scratch,
                arc_scratch,
            );
            append_closed_polyline(out, rail_scratch);
            build_ring_into(
                segs,
                normals_scratch,
                half,
                join,
                miter_limit,
                /*left=*/ false,
                semantic_joins,
                sub.start,
                rail_scratch,
                arc_scratch,
            );
            rail_scratch.reverse();
            append_closed_polyline(out, rail_scratch);
        } else {
            build_open_rail_into(
                segs,
                normals_scratch,
                half,
                join,
                miter_limit,
                /*left=*/ true,
                semantic_joins,
                sub.start,
                left_rail_scratch,
                arc_scratch,
            );
            build_open_rail_into(
                segs,
                normals_scratch,
                half,
                join,
                miter_limit,
                /*left=*/ false,
                semantic_joins,
                sub.start,
                rail_scratch,
                arc_scratch,
            );
            append_open_ribbon(out, segs, left_rail_scratch, rail_scratch, cap, half);
        }
    }
}

fn compute_normals_into(segs: &[LineSeg], half: Fixed, out: &mut Vec<Point>) {
    out.clear();
    out.reserve(segs.len());
    for s in segs {
        let dx = s.p2.x - s.p1.x;
        let dy = s.p2.y - s.p1.y;
        let len_sq = dx * dx + dy * dy;
        if len_sq == Fixed::ZERO {
            out.push(Point::ZERO);
            continue;
        }
        let len = len_sq.sqrt();
        let nx = -dy / len * half;
        let ny = dx / len * half;
        out.push(Point { x: nx, y: ny });
    }
}

#[allow(clippy::too_many_arguments)]
fn build_ring_into(
    segs: &[LineSeg],
    n: &[Point],
    half: Fixed,
    join: LineJoin,
    miter_limit: Fixed,
    left: bool,
    semantic_joins: &[usize],
    segment_offset: usize,
    out: &mut Vec<Point>,
    arc_scratch: &mut Vec<Point>,
) {
    out.clear();
    let sign = if left { Fixed::ONE } else { -Fixed::ONE };
    let count = segs.len();
    out.reserve(count + 1);

    for i in 0..count {
        let prev = if i == 0 { count - 1 } else { i - 1 };
        let p_prev = segs[prev];
        let p_curr = segs[i];
        let n_prev = scaled(n[prev], sign);
        let n_curr = scaled(n[i], sign);
        let point_join = if semantic_joins.binary_search(&(segment_offset + i)).is_ok() {
            join
        } else {
            LineJoin::Miter
        };

        compute_join_points(
            p_prev.p2,
            p_prev.p1,
            n_prev,
            p_curr.p1,
            p_curr.p2,
            n_curr,
            half,
            point_join,
            miter_limit,
            arc_scratch,
        );
        out.extend_from_slice(arc_scratch);
    }
    if let Some(&first) = out.first() {
        out.push(first);
    }
}

#[allow(clippy::too_many_arguments)]
fn build_open_rail_into(
    segs: &[LineSeg],
    n: &[Point],
    half: Fixed,
    join: LineJoin,
    miter_limit: Fixed,
    left: bool,
    semantic_joins: &[usize],
    segment_offset: usize,
    out: &mut Vec<Point>,
    arc_scratch: &mut Vec<Point>,
) {
    out.clear();
    let sign = if left { Fixed::ONE } else { -Fixed::ONE };
    let count = segs.len();
    out.reserve(count + 1);

    let n0 = scaled(n[0], sign);
    out.push(offset(segs[0].p1, n0));

    for i in 1..count {
        let p_prev = segs[i - 1];
        let p_curr = segs[i];
        let n_prev = scaled(n[i - 1], sign);
        let n_curr = scaled(n[i], sign);
        let point_join = if semantic_joins.binary_search(&(segment_offset + i)).is_ok() {
            join
        } else {
            LineJoin::Miter
        };

        compute_join_points(
            p_prev.p2,
            p_prev.p1,
            n_prev,
            p_curr.p1,
            p_curr.p2,
            n_curr,
            half,
            point_join,
            miter_limit,
            arc_scratch,
        );
        out.extend_from_slice(arc_scratch);
    }

    let n_last = scaled(n[count - 1], sign);
    out.push(offset(segs[count - 1].p2, n_last));
}

#[allow(clippy::too_many_arguments)]
fn compute_join_points(
    a: Point,
    b: Point,
    n_prev: Point,
    c: Point,
    d: Point,
    n_curr: Point,
    half: Fixed,
    join: LineJoin,
    miter_limit: Fixed,
    out: &mut Vec<Point>,
) {
    out.clear();
    let p_in = offset(b, n_prev);
    let p_out = offset(c, n_curr);

    if approx_eq(p_in, p_out) {
        out.push(p_in);
        return;
    }

    match join {
        LineJoin::Round => {
            let center = Point {
                x: (b.x + c.x) / 2,
                y: (b.y + c.y) / 2,
            };
            let dir_prev = Point {
                x: b.x - a.x,
                y: b.y - a.y,
            };
            let dir_curr = Point {
                x: d.x - c.x,
                y: d.y - c.y,
            };
            let turn = dir_prev.x * dir_curr.y - dir_prev.y * dir_curr.x;
            let side = dir_prev.x * n_prev.y - dir_prev.y * n_prev.x;
            let outer = (turn > Fixed::ZERO && side < Fixed::ZERO)
                || (turn < Fixed::ZERO && side > Fixed::ZERO);
            if !outer {
                if let Some(inner) = line_intersect(p_in, dir_prev, p_out, dir_curr) {
                    out.push(inner);
                } else {
                    out.push(Point {
                        x: (p_in.x + p_out.x) / 2,
                        y: (p_in.y + p_out.y) / 2,
                    });
                }
                return;
            }

            out.push(p_in);
            let chord_x = p_out.x - p_in.x;
            let chord_y = p_out.y - p_in.y;
            let chord = (chord_x * chord_x + chord_y * chord_y).sqrt();
            let target_step = (half / 4).max(Fixed::from_ratio(1, 4));
            let steps = (chord / target_step)
                .ceil()
                .to_int()
                .clamp(1, arc_steps(half) as i32) as usize;
            for i in 1..steps {
                let t = Fixed::from_int(i as i32) / Fixed::from_int(steps as i32);
                let p = Point {
                    x: p_in.x + (p_out.x - p_in.x) * t,
                    y: p_in.y + (p_out.y - p_in.y) * t,
                };
                let dx = p.x - center.x;
                let dy = p.y - center.y;
                let len_sq = dx * dx + dy * dy;
                if len_sq > Fixed::ZERO {
                    let len = len_sq.sqrt();
                    let scale = half / len;
                    out.push(Point {
                        x: center.x + dx * scale,
                        y: center.y + dy * scale,
                    });
                } else {
                    out.push(p);
                }
            }
            out.push(p_out);
        }
        LineJoin::Miter | LineJoin::Bevel => {
            if join == LineJoin::Miter {
                let dir_prev = Point {
                    x: b.x - a.x,
                    y: b.y - a.y,
                };
                let dir_curr = Point {
                    x: d.x - c.x,
                    y: d.y - c.y,
                };
                if let Some(miter) = line_intersect(p_in, dir_prev, p_out, dir_curr) {
                    let corner = Point {
                        x: (b.x + c.x) / 2,
                        y: (b.y + c.y) / 2,
                    };
                    let dx = miter.x - corner.x;
                    let dy = miter.y - corner.y;
                    let dist_sq = dx * dx + dy * dy;
                    let limit = half * miter_limit;
                    if dist_sq <= limit * limit {
                        out.push(miter);
                        return;
                    }
                }
            }
            out.push(p_in);
            out.push(p_out);
        }
    }
}

fn arc_steps(half: Fixed) -> usize {
    let r = half.to_int().max(2) as usize;
    (r * 3).clamp(4, 32)
}

/// Solve p1 + t*d1 = p2 + s*d2 for t. Returns the intersection point, or None
/// when the directions are parallel.
fn line_intersect(p1: Point, d1: Point, p2: Point, d2: Point) -> Option<Point> {
    let denom = d1.x * d2.y - d1.y * d2.x;
    if denom == Fixed::ZERO {
        return None;
    }
    let dx = p2.x - p1.x;
    let dy = p2.y - p1.y;
    let t = (dx * d2.y - dy * d2.x) / denom;
    Some(Point {
        x: p1.x + d1.x * t,
        y: p1.y + d1.y * t,
    })
}

fn append_closed_polyline(out: &mut Path, pts: &[Point]) {
    if pts.len() < 2 {
        return;
    }
    out.move_to(pts[0]);
    for p in &pts[1..] {
        out.line_to(*p);
    }
    out.close();
}

fn append_open_ribbon(
    out: &mut Path,
    segs: &[LineSeg],
    left: &[Point],
    right: &[Point],
    cap: LineCap,
    half: Fixed,
) {
    if left.is_empty() || right.is_empty() {
        return;
    }

    let (start_left, start_right) = (left[0], right[0]);
    let (end_left, end_right) = (
        left.last().copied().unwrap_or(left[0]),
        right.last().copied().unwrap_or(right[0]),
    );

    match cap {
        LineCap::Butt => {
            out.move_to(start_left);
            for p in &left[1..] {
                out.line_to(*p);
            }
            for p in right.iter().rev() {
                out.line_to(*p);
            }
            out.close();
        }
        LineCap::Square => {
            let start_dir = scaled(direction(segs[0].p2, segs[0].p1), half);
            let last = segs[segs.len() - 1];
            let end_dir = scaled(direction(last.p1, last.p2), half);

            let sl_ext = offset(start_left, start_dir);
            let sr_ext = offset(start_right, start_dir);
            let el_ext = offset(end_left, end_dir);
            let er_ext = offset(end_right, end_dir);

            out.move_to(sl_ext);
            out.line_to(start_left);
            for p in &left[1..] {
                out.line_to(*p);
            }
            out.line_to(el_ext);
            out.line_to(er_ext);
            for p in right.iter().rev() {
                out.line_to(*p);
            }
            out.line_to(sr_ext);
            out.close();
        }
        LineCap::Round => {
            out.move_to(start_left);
            for p in &left[1..] {
                out.line_to(*p);
            }
            append_arc_cap(out, end_left, end_right, half);
            for p in right.iter().rev().skip(1) {
                out.line_to(*p);
            }
            append_arc_cap(out, start_right, start_left, half);
            out.close();
        }
    }
}

fn direction(from: Point, to: Point) -> Point {
    let dx = to.x - from.x;
    let dy = to.y - from.y;
    let len_sq = dx * dx + dy * dy;
    if len_sq == Fixed::ZERO {
        return Point::ZERO;
    }
    let len = len_sq.sqrt();
    Point {
        x: dx / len * (Fixed::ONE / Fixed::from_int(1)),
        y: dy / len * (Fixed::ONE / Fixed::from_int(1)),
    }
}

fn append_arc_cap(out: &mut Path, p1: Point, p2: Point, half: Fixed) {
    let center = Point {
        x: (p1.x + p2.x) / 2,
        y: (p1.y + p2.y) / 2,
    };
    let steps = arc_steps(half);
    for i in 1..steps {
        let angle =
            -Fixed::from_int(i as i32) * Fixed::from_int(314) / Fixed::from_int(steps as i32 * 100);
        let (sin_v, cos_v) = sin_cos_approx(angle);
        let dx = p1.x - center.x;
        let dy = p1.y - center.y;
        let nx = dx * cos_v - dy * sin_v;
        let ny = dx * sin_v + dy * cos_v;
        out.line_to(Point {
            x: center.x + nx,
            y: center.y + ny,
        });
    }
    out.line_to(p2);
}

fn sin_cos_approx(rad: Fixed) -> (Fixed, Fixed) {
    #[cfg(feature = "std")]
    {
        let f = rad.to_f32();
        (Fixed::from_f32(f.sin()), Fixed::from_f32(f.cos()))
    }
    #[cfg(not(feature = "std"))]
    {
        let x = rad.to_f32();
        let x2 = x * x;
        let s = x - x * x2 / 6.0 + x * x2 * x2 / 120.0;
        let c = 1.0 - x2 / 2.0 + x2 * x2 / 24.0;
        (Fixed::from_f32(s), Fixed::from_f32(c))
    }
}

fn scaled(p: Point, s: Fixed) -> Point {
    Point {
        x: p.x * s,
        y: p.y * s,
    }
}

fn offset(p: Point, n: Point) -> Point {
    Point {
        x: p.x + n.x,
        y: p.y + n.y,
    }
}

fn approx_eq(a: Point, b: Point) -> bool {
    let dx = a.x - b.x;
    let dy = a.y - b.y;
    // Within ~1/256 pixel in Manhattan distance.
    dx.abs() + dy.abs() < Fixed::ONE / 128
}

fn lerp(a: Point, b: Point, t: Fixed) -> Point {
    Point {
        x: a.x + (b.x - a.x) * t,
        y: a.y + (b.y - a.y) * t,
    }
}

fn quad_at(p0: Point, p1: Point, p2: Point, t: Fixed) -> Point {
    lerp(lerp(p0, p1, t), lerp(p1, p2, t), t)
}

fn cubic_at(p0: Point, p1: Point, p2: Point, p3: Point, t: Fixed) -> Point {
    let q0 = lerp(p0, p1, t);
    let q1 = lerp(p1, p2, t);
    let q2 = lerp(p2, p3, t);
    lerp(lerp(q0, q1, t), lerp(q1, q2, t), t)
}

#[cfg(test)]
mod tests {
    use alloc::vec;

    use super::*;

    fn pt(x: i32, y: i32) -> Point {
        Point {
            x: Fixed::from_int(x),
            y: Fixed::from_int(y),
        }
    }

    fn flatten_path(p: &Path) -> Vec<LineSeg> {
        let mut out = Vec::new();
        flatten_into(&p.cmds, None, &mut out);
        out
    }

    fn flatten_subpaths_path(p: &Path) -> (Vec<LineSeg>, Vec<SubPath>) {
        let mut segments = Vec::new();
        let mut out = Vec::new();
        flatten_subpaths_into(&p.cmds, None, &mut segments, &mut out);
        (segments, out)
    }

    fn offset_polygon_path(p: &Path, width: Fixed) -> Path {
        offset_polygon_path_with_cap(p, width, LineCap::Butt)
    }

    fn offset_polygon_path_with_cap(p: &Path, width: Fixed, cap: LineCap) -> Path {
        offset_polygon_path_with_style(p, width, cap, LineJoin::Miter)
    }

    fn offset_polygon_path_with_style(
        p: &Path,
        width: Fixed,
        cap: LineCap,
        join: LineJoin,
    ) -> Path {
        let mut out = Path::new();
        let mut flattened = Vec::new();
        let mut scratch = Vec::new();
        let mut semantic_joins = Vec::new();
        let mut normals = Vec::new();
        let mut rail = Vec::new();
        let mut left_rail = Vec::new();
        let mut arc = Vec::new();
        let mut dashed = Vec::new();
        let mut dash_scratch = Vec::new();
        let mut dashed_semantic_joins = Vec::new();
        offset_polygon_into(
            &p.cmds,
            None,
            width,
            cap,
            join,
            Fixed::from_int(4),
            None,
            &mut out,
            &mut flattened,
            &mut scratch,
            &mut semantic_joins,
            &mut normals,
            &mut rail,
            &mut left_rail,
            &mut arc,
            &mut dashed,
            &mut dash_scratch,
            &mut dashed_semantic_joins,
        );
        out
    }

    #[test]
    fn flatten_empty_path() {
        assert!(flatten_path(&Path::new()).is_empty());
    }

    #[test]
    fn flatten_single_line() {
        let mut p = Path::new();
        p.move_to(pt(0, 0)).line_to(pt(10, 0));
        let segs = flatten_path(&p);
        assert_eq!(segs.len(), 1);
        assert_eq!(segs[0].p1, pt(0, 0));
        assert_eq!(segs[0].p2, pt(10, 0));
    }

    #[test]
    fn flatten_closed_triangle_emits_closing_edge() {
        let mut p = Path::new();
        p.move_to(pt(0, 0))
            .line_to(pt(10, 0))
            .line_to(pt(5, 10))
            .close();
        let segs = flatten_path(&p);
        // 3 explicit lines + 1 implicit close back to (0,0)
        assert_eq!(segs.len(), 3);
        assert_eq!(segs[2].p2, pt(0, 0));
    }

    #[test]
    fn flatten_close_is_noop_when_current_equals_start() {
        let mut p = Path::new();
        p.move_to(pt(0, 0))
            .line_to(pt(10, 0))
            .line_to(pt(0, 0))
            .close();
        let segs = flatten_path(&p);
        // close() sees current == subpath_start, so it must not add a redundant edge
        assert_eq!(segs.len(), 2);
    }

    #[test]
    fn flatten_quad_emits_n_segments_connected() {
        let mut p = Path::new();
        p.move_to(pt(0, 0)).quad_to(pt(10, 10), pt(20, 0));
        let segs = flatten_path(&p);
        assert_eq!(segs.len(), QUAD_STEPS as usize);
        // Chain: seg[i].p2 == seg[i+1].p1
        for i in 0..segs.len() - 1 {
            assert_eq!(segs[i].p2, segs[i + 1].p1);
        }
        assert_eq!(segs.last().unwrap().p2, pt(20, 0));
    }

    #[test]
    fn flatten_cubic_emits_n_segments_connected() {
        let mut p = Path::new();
        p.move_to(pt(0, 0))
            .cubic_to(pt(0, 10), pt(10, 10), pt(10, 0));
        let segs = flatten_path(&p);
        assert_eq!(segs.len(), CUBIC_STEPS as usize);
        for i in 0..segs.len() - 1 {
            assert_eq!(segs[i].p2, segs[i + 1].p1);
        }
        assert_eq!(segs.last().unwrap().p2, pt(10, 0));
    }

    #[test]
    fn flatten_multi_subpath() {
        // Two independent subpaths: a line then a separate triangle.
        let mut p = Path::new();
        p.move_to(pt(0, 0)).line_to(pt(10, 0));
        p.move_to(pt(50, 50)).line_to(pt(60, 50)).close();
        let segs = flatten_path(&p);
        // Subpath 1: 1 line. Subpath 2: 1 explicit + 1 close = 2.
        assert_eq!(segs.len(), 3);
        // Second subpath closes back to (50,50), not (0,0)
        assert_eq!(segs.last().unwrap().p2, pt(50, 50));
    }

    fn square_segs() -> Vec<LineSeg> {
        // Unit square [0, 10] × [0, 10] — 4 edges CCW.
        vec![
            LineSeg {
                p1: pt(0, 0),
                p2: pt(10, 0),
            },
            LineSeg {
                p1: pt(10, 0),
                p2: pt(10, 10),
            },
            LineSeg {
                p1: pt(10, 10),
                p2: pt(0, 10),
            },
            LineSeg {
                p1: pt(0, 10),
                p2: pt(0, 0),
            },
        ]
    }

    #[test]
    fn point_in_square_interior_detected() {
        assert!(point_in_segments(pt(5, 5), &square_segs()));
    }

    #[test]
    fn point_outside_square_rejected() {
        assert!(!point_in_segments(pt(-1, 5), &square_segs()));
        assert!(!point_in_segments(pt(11, 5), &square_segs()));
        assert!(!point_in_segments(pt(5, -1), &square_segs()));
        assert!(!point_in_segments(pt(5, 11), &square_segs()));
    }

    #[test]
    fn point_on_horizontal_edge_half_open() {
        // Half-open [y_lo, y_hi) — bottom edge is inclusive, top edge is exclusive.
        assert!(point_in_segments(pt(5, 0), &square_segs()));
        assert!(!point_in_segments(pt(5, 10), &square_segs()));
    }

    fn big_cap() -> Fixed {
        // Large enough to not affect any test case, small enough that
        // dx_box/dy_box comparisons stay well within i32 Fixed range.
        Fixed::from_int(1000)
    }

    #[test]
    fn dist_on_segment_is_zero() {
        let segs = square_segs();
        let d = min_dist_to_segments_capped(pt(5, 0), &segs, big_cap());
        assert_eq!(d, Fixed::ZERO);
    }

    #[test]
    fn dist_center_to_square_is_half_width() {
        let segs = square_segs();
        let d = min_dist_to_segments_capped(pt(5, 5), &segs, big_cap()).to_f32();
        assert!((d - 5.0).abs() < 0.01, "d = {d}");
    }

    #[test]
    fn dist_beyond_endpoint_uses_endpoint() {
        let segs = vec![LineSeg {
            p1: pt(0, 0),
            p2: pt(10, 0),
        }];
        let d = min_dist_to_segments_capped(pt(15, 0), &segs, big_cap()).to_f32();
        assert!((d - 5.0).abs() < 0.01);
    }

    #[test]
    fn dist_to_degenerate_segment() {
        let segs = vec![LineSeg {
            p1: pt(3, 4),
            p2: pt(3, 4),
        }];
        let d = min_dist_to_segments_capped(pt(0, 0), &segs, big_cap()).to_f32();
        assert!((d - 5.0).abs() < 0.01);
    }

    #[test]
    fn dist_capped_returns_cap_when_far() {
        let segs = vec![LineSeg {
            p1: pt(100, 100),
            p2: pt(110, 100),
        }];
        let cap = Fixed::from_int(5);
        let d = min_dist_to_segments_capped(pt(0, 0), &segs, cap);
        assert_eq!(d, cap);
    }

    #[test]
    fn flatten_subpaths_marks_closed_and_open() {
        // subpath A ends with Close (closed), subpath B is a dangling LineTo (open).
        let mut p = Path::new();
        p.move_to(pt(0, 0))
            .line_to(pt(10, 0))
            .line_to(pt(0, 10))
            .close();
        p.move_to(pt(50, 50)).line_to(pt(60, 50));
        let (_, subs) = flatten_subpaths_path(&p);
        assert_eq!(subs.len(), 2);
        assert!(subs[0].closed);
        assert!(!subs[1].closed);
    }

    #[test]
    fn shared_stroke_scratch_scales_dash_lengths_with_output_pixels() {
        let mut path = Path::new();
        path.move_to(pt(0, 0)).line_to(pt(40, 0));
        let mut scratch = StrokeScratch::new();
        let dash = [Fixed::from_int(5), Fixed::from_int(5)];
        let spec = |dash_scale| StrokeSpec {
            width: Fixed::from_int(2),
            cap: LineCap::Butt,
            join: LineJoin::Miter,
            miter_limit: Fixed::from_int(4),
            dash: &dash,
            dash_scale,
        };
        let contours = |path: &Path| {
            path.cmds
                .iter()
                .filter(|cmd| matches!(cmd, PathCmd::MoveTo(_)))
                .count()
        };
        assert_eq!(contours(scratch.outline(&path, None, spec(Fixed::ONE))), 4);
        assert_eq!(
            contours(scratch.outline(&path, None, spec(Fixed::from_int(2)))),
            2
        );
    }

    #[test]
    fn offset_polygon_rectangle_closed_produces_two_rings() {
        // 10×10 rectangle stroked with width=2. The outline path should have
        // exactly 2 subpaths: outer ring + inner ring.
        let path = Path::rect(
            Fixed::from_int(0),
            Fixed::from_int(0),
            Fixed::from_int(10),
            Fixed::from_int(10),
        );
        let outline = offset_polygon_path(&path, Fixed::from_int(2));
        let closes = outline
            .cmds
            .iter()
            .filter(|c| matches!(c, PathCmd::Close))
            .count();
        assert_eq!(closes, 2);
    }

    #[test]
    fn offset_polygon_open_line_makes_one_ribbon() {
        // A single open LineTo should yield a single closed ribbon (butt caps).
        let mut path = Path::new();
        path.move_to(pt(0, 0)).line_to(pt(10, 0));
        let outline = offset_polygon_path(&path, Fixed::from_int(2));
        let closes = outline
            .cmds
            .iter()
            .filter(|c| matches!(c, PathCmd::Close))
            .count();
        assert_eq!(closes, 1);
    }

    #[test]
    fn open_butt_stroke_connects_matching_rail_ends() {
        let mut path = Path::new();
        path.move_to(pt(0, 0)).line_to(pt(10, 0));
        let outline = offset_polygon_path(&path, Fixed::from_int(2));
        assert_eq!(
            &*outline.cmds,
            &[
                PathCmd::MoveTo(pt(0, 1)),
                PathCmd::LineTo(pt(10, 1)),
                PathCmd::LineTo(pt(10, -1)),
                PathCmd::LineTo(pt(0, -1)),
                PathCmd::Close,
            ]
        );
        let edges = flatten_path(&outline);
        for x in [1, 5, 9] {
            assert!(point_in_segments(pt(x, 0), &edges));
        }
        assert!(!point_in_segments(pt(5, 2), &edges));
    }

    #[test]
    fn open_curve_stroke_has_no_long_cross_rail_edge() {
        let mut path = Path::new();
        path.move_to(pt(0, 0)).quad_to(pt(12, 16), pt(24, 0));
        let outline = offset_polygon_path(&path, Fixed::from_int(4));
        let edges = flatten_path(&outline);
        assert!(edges.iter().all(|edge| {
            let dx = (edge.p2.x - edge.p1.x).to_f32();
            let dy = (edge.p2.y - edge.p1.y).to_f32();
            dx * dx + dy * dy <= 64.0
        }));
    }

    #[test]
    fn curve_flattening_does_not_apply_round_join_to_synthetic_vertices() {
        let mut path = Path::new();
        path.move_to(pt(0, 0))
            .cubic_to(pt(8, 12), pt(16, 12), pt(24, 0));
        let round = offset_polygon_path_with_style(
            &path,
            Fixed::from_int(4),
            LineCap::Butt,
            LineJoin::Round,
        );
        let miter = offset_polygon_path_with_style(
            &path,
            Fixed::from_int(4),
            LineCap::Butt,
            LineJoin::Miter,
        );
        assert_eq!(round.cmds, miter.cmds);
    }

    #[test]
    fn round_join_is_retained_at_semantic_line_corner() {
        let mut path = Path::new();
        path.move_to(pt(0, 0))
            .line_to(pt(10, 0))
            .line_to(pt(10, 10));
        let round = offset_polygon_path_with_style(
            &path,
            Fixed::from_int(4),
            LineCap::Butt,
            LineJoin::Round,
        );
        let miter = offset_polygon_path_with_style(
            &path,
            Fixed::from_int(4),
            LineCap::Butt,
            LineJoin::Miter,
        );
        assert_ne!(round.cmds, miter.cmds);
    }

    #[test]
    fn open_square_and_round_caps_extend_outward() {
        let mut path = Path::new();
        path.move_to(pt(0, 0)).line_to(pt(10, 0));
        for cap in [LineCap::Square, LineCap::Round] {
            let outline = offset_polygon_path_with_cap(&path, Fixed::from_int(4), cap);
            let xs = outline.cmds.iter().filter_map(|command| match command {
                PathCmd::MoveTo(point) | PathCmd::LineTo(point) => Some(point.x),
                _ => None,
            });
            assert!(xs.clone().min().unwrap() <= Fixed::from_int(-1), "{cap:?}");
            assert!(xs.max().unwrap() >= Fixed::from_int(11), "{cap:?}");
            assert!(flatten_path(&outline).iter().all(|edge| {
                let dx = (edge.p2.x - edge.p1.x).to_f32();
                let dy = (edge.p2.y - edge.p1.y).to_f32();
                dx * dx + dy * dy <= 100.0
            }));
        }
    }

    #[test]
    fn closed_dash_does_not_repeat_the_closing_edge() {
        let mut path = Path::new();
        path.move_to(pt(0, 0))
            .line_to(pt(10, 0))
            .line_to(pt(10, 10))
            .line_to(pt(0, 10))
            .close();
        let (segments, subpaths) = flatten_subpaths_path(&path);
        let mut dashed_segments = Vec::new();
        let mut dashed = Vec::new();
        let mut dashed_semantic_joins = Vec::new();
        apply_dash_pattern(
            &segments[subpaths[0].start..subpaths[0].end],
            true,
            &[Fixed::from_int(100), Fixed::from_int(100)],
            &mut dashed_segments,
            &mut dashed,
            DashJoinMap {
                source_start: subpaths[0].start,
                source_semantic_joins: &[],
                output_semantic_joins: &mut dashed_semantic_joins,
            },
        );
        assert_eq!(dashed.len(), 1);
        assert_eq!(dashed_segments, segments);
        assert!(dashed[0].closed);
    }

    #[test]
    fn open_stroke_reuses_both_rail_buffers() {
        let mut path = Path::new();
        path.move_to(pt(0, 0)).line_to(pt(10, 0));
        let mut outline = Path::new();
        let mut flattened = Vec::new();
        let mut subpaths = Vec::new();
        let mut semantic_joins = Vec::new();
        let mut normals = Vec::new();
        let mut right = Vec::new();
        let mut left = Vec::new();
        let mut arc = Vec::new();
        let mut dashed_segments = Vec::new();
        let mut dashed = Vec::new();
        let mut dashed_semantic_joins = Vec::new();
        let mut first = None;

        for _ in 0..2 {
            offset_polygon_into(
                &path.cmds,
                None,
                Fixed::from_int(2),
                LineCap::Butt,
                LineJoin::Miter,
                Fixed::from_int(4),
                None,
                &mut outline,
                &mut flattened,
                &mut subpaths,
                &mut semantic_joins,
                &mut normals,
                &mut right,
                &mut left,
                &mut arc,
                &mut dashed_segments,
                &mut dashed,
                &mut dashed_semantic_joins,
            );
            let buffers = [
                (left.as_ptr(), left.capacity()),
                (right.as_ptr(), right.capacity()),
            ];
            assert!(buffers.iter().all(|(_, capacity)| *capacity > 0));
            if let Some(previous) = first {
                assert_eq!(buffers, previous);
            }
            first = Some(buffers);
        }
    }

    #[test]
    fn dashed_subpaths_reuse_flat_segment_and_range_buffers() {
        let mut path = Path::new();
        path.move_to(pt(0, 0)).quad_to(pt(10, 12), pt(20, 0));
        path.move_to(pt(0, 20)).line_to(pt(20, 20));
        let mut outline = Path::new();
        let mut flattened = Vec::new();
        let mut subpaths = Vec::new();
        let mut semantic_joins = Vec::new();
        let mut normals = Vec::new();
        let mut right = Vec::new();
        let mut left = Vec::new();
        let mut arc = Vec::new();
        let mut dashed_segments = Vec::new();
        let mut dashed_subpaths = Vec::new();
        let mut dashed_semantic_joins = Vec::new();
        let mut first = None;

        for _ in 0..2 {
            offset_polygon_into(
                &path.cmds,
                None,
                Fixed::from_int(2),
                LineCap::Butt,
                LineJoin::Miter,
                Fixed::from_int(4),
                Some(&[Fixed::from_int(3), Fixed::from_int(2)]),
                &mut outline,
                &mut flattened,
                &mut subpaths,
                &mut semantic_joins,
                &mut normals,
                &mut right,
                &mut left,
                &mut arc,
                &mut dashed_segments,
                &mut dashed_subpaths,
                &mut dashed_semantic_joins,
            );
            let buffers = [
                (flattened.as_ptr().cast::<()>(), flattened.capacity()),
                (subpaths.as_ptr().cast::<()>(), subpaths.capacity()),
                (
                    dashed_segments.as_ptr().cast::<()>(),
                    dashed_segments.capacity(),
                ),
                (
                    dashed_subpaths.as_ptr().cast::<()>(),
                    dashed_subpaths.capacity(),
                ),
            ];
            assert!(buffers.iter().all(|(_, capacity)| *capacity > 0));
            if let Some(previous) = first {
                assert_eq!(buffers, previous);
            }
            first = Some(buffers);
        }
    }

    #[test]
    fn offset_polygon_zero_width_is_empty() {
        let mut path = Path::new();
        path.move_to(pt(0, 0)).line_to(pt(10, 0));
        let outline = offset_polygon_path(&path, Fixed::ZERO);
        assert!(outline.cmds.is_empty());
    }

    #[test]
    fn flatten_rounded_rect_produces_expected_count() {
        let p = Path::rounded_rect(
            Fixed::from_int(0),
            Fixed::from_int(0),
            Fixed::from_int(20),
            Fixed::from_int(20),
            Fixed::from_int(4),
        );
        let segs = flatten_path(&p);
        // rounded_rect = 4 lines + 4 quad corners (N=8 each) + possibly 1 close.
        // Layout: move, line, quad, line, quad, line, quad, line, quad, close.
        // Close may add 0 or 1 edge depending on whether final point equals start.
        let expected_min = 4 + 4 * QUAD_STEPS as usize;
        assert!(
            segs.len() >= expected_min,
            "got {} segs, expected ≥{}",
            segs.len(),
            expected_min
        );
    }

    #[test]
    fn projective_flatten_projects_curve_samples_in_path_space() {
        let mut path = Path::new();
        path.move_to(pt(0, 0)).quad_to(pt(8, 12), pt(16, 0));
        let projection =
            Transform3D::rotate_y_perspective(Fixed::from_int(25), Fixed::from_int(120));
        let mut segments = Vec::new();

        flatten_projective_into(&path.cmds, &projection, &mut segments).unwrap();

        let midpoint = quad_at(pt(0, 0), pt(8, 12), pt(16, 0), Fixed::from_ratio(1, 2));
        assert_eq!(segments[3].p2, projection.apply_point(midpoint).unwrap());
        assert_eq!(segments.len(), QUAD_STEPS as usize);
    }

    #[test]
    fn projective_flatten_rejects_points_behind_the_camera() {
        let path = Path::rect(
            Fixed::ZERO,
            Fixed::ZERO,
            Fixed::from_int(8),
            Fixed::from_int(8),
        );
        let projection = Transform3D {
            m22: crate::types::Fixed64::ZERO,
            ..Transform3D::IDENTITY
        };
        let mut segments = Vec::new();

        assert_eq!(
            flatten_projective_into(&path.cmds, &projection, &mut segments),
            Err(())
        );
    }

    fn rect_segs(x0: i32, y0: i32, x1: i32, y1: i32, ccw: bool) -> Vec<LineSeg> {
        // Outer ring CW (default) or CCW. Order matters because winding
        // direction is derived from p1.y vs p2.y in the rasterizer.
        if ccw {
            alloc::vec![
                LineSeg {
                    p1: pt(x0, y0),
                    p2: pt(x0, y1)
                },
                LineSeg {
                    p1: pt(x0, y1),
                    p2: pt(x1, y1)
                },
                LineSeg {
                    p1: pt(x1, y1),
                    p2: pt(x1, y0)
                },
                LineSeg {
                    p1: pt(x1, y0),
                    p2: pt(x0, y0)
                },
            ]
        } else {
            alloc::vec![
                LineSeg {
                    p1: pt(x0, y0),
                    p2: pt(x1, y0)
                },
                LineSeg {
                    p1: pt(x1, y0),
                    p2: pt(x1, y1)
                },
                LineSeg {
                    p1: pt(x1, y1),
                    p2: pt(x0, y1)
                },
                LineSeg {
                    p1: pt(x0, y1),
                    p2: pt(x0, y0)
                },
            ]
        }
    }

    fn count_filled(segs: &[LineSeg], rule: FillRule) -> i32 {
        let mut n = 0;
        let mut acc = Vec::new();
        let mut crossings = Vec::new();
        scanline_fill(
            segs,
            0,
            0,
            10,
            10,
            rule,
            &mut acc,
            &mut crossings,
            |_, _, cov| {
                if cov > Fixed::from_int(0) {
                    n += 1;
                }
            },
        );
        n
    }

    #[test]
    fn fill_rule_even_odd_carves_inner_ring_to_zero() {
        // Outer 0..10 + inner 2..8 (same direction). Even-odd makes the
        // inner ring punch a hole; non-zero would fill solid.
        let mut segs = rect_segs(0, 0, 10, 10, false);
        segs.extend(rect_segs(2, 2, 8, 8, false));
        let lit = count_filled(&segs, FillRule::EvenOdd);
        assert!(lit > 0, "outer ring still fills with hole");
        // Inside hole should be 0; only outer band lit.
        // 10x10 = 100 total, hole 6x6 = 36, ring = 64.
        assert!(lit < 70, "expected hole, got lit={}", lit);
    }

    #[test]
    fn fill_rule_non_zero_fills_overlapping_same_direction_solid() {
        // Outer + inner same winding. Non-zero stays inside (count = 2),
        // entire 10x10 should be lit. Even-odd would punch a hole.
        let mut segs = rect_segs(0, 0, 10, 10, false);
        segs.extend(rect_segs(2, 2, 8, 8, false));
        let lit = count_filled(&segs, FillRule::NonZero);
        assert_eq!(lit, 100, "non-zero with same-winding should fill solid");
    }

    #[test]
    fn fill_rule_non_zero_carves_hole_when_inner_reversed() {
        // Outer CW, inner CCW (opposite winding). Non-zero count cancels
        // inside the inner — TrueType "even-odd-like via path direction".
        let mut segs = rect_segs(0, 0, 10, 10, false);
        segs.extend(rect_segs(2, 2, 8, 8, true));
        let lit = count_filled(&segs, FillRule::NonZero);
        // 10x10 = 100, hole 6x6 = 36, ring = 64.
        assert!(
            (60..70).contains(&lit),
            "non-zero with opposite winding should carve a hole, got lit={}",
            lit
        );
    }
}