azul-core 0.0.10

Common datatypes used for the Azul document object model, shared across all azul-* crates
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
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
//! SVG rendering and path tessellation.
//!
//! This module provides functionality for parsing, manipulating, and rendering SVG paths.
//! It includes:
//!
//! - **Path tessellation**: Converts SVG paths into triangle meshes for GPU rendering
//! - **Stroke generation**: Creates stroked paths with various line join and cap styles
//! - **Transform support**: Applies CSS transforms to SVG elements
//! - **Style parsing**: Handles SVG fill, stroke, opacity, and other attributes
//!
//! The module uses Lyon for geometric tessellation and generates vertex/index buffers
//! that can be uploaded to WebRender for hardware-accelerated rendering.

use alloc::{
    string::{String, ToString},
    vec::Vec,
};
use core::fmt;

use azul_css::{
    props::{
        basic::{
            ColorF, ColorU, OptionColorU, OptionLayoutSize, PixelValue, SvgCubicCurve, SvgPoint,
            SvgQuadraticCurve, SvgRect, SvgVector,
        },
        style::{StyleTransform, StyleTransformOrigin, StyleTransformVec},
    },
    AzString, OptionString, StringVec, U32Vec,
};

use crate::{
    geom::PhysicalSizeU32,
    gl::{
        GlContextPtr, GlShader, IndexBufferFormat, Texture, Uniform, UniformType, VertexAttribute,
        VertexAttributeType, VertexBuffer, VertexLayout, VertexLayoutDescription,
    },
    transform::{ComputedTransform3D, RotationMode},
    xml::XmlError,
};

/// Default miter limit for stroke joins (ratio of miter length to stroke width)
const DEFAULT_MITER_LIMIT: f32 = 4.0;
/// Default stroke width in pixels
const DEFAULT_LINE_WIDTH: f32 = 1.0;
/// Default tessellation tolerance in pixels (smaller = more vertices, higher quality)
const DEFAULT_TOLERANCE: f32 = 0.1;

/// Represents the dimensions of an SVG viewport or element.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgSize {
    /// Width in SVG user units
    pub width: f32,
    /// Height in SVG user units
    pub height: f32,
}

/// A line segment in 2D space.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgLine {
    /// Start point of the line
    pub start: SvgPoint,
    /// End point of the line
    pub end: SvgPoint,
}

impl SvgLine {
    /// Creates a new line segment from start to end point
    #[inline]
    #[must_use] pub const fn new(start: SvgPoint, end: SvgPoint) -> Self {
        Self { start, end }
    }

    /// Computes the inward-facing normal vector for this line.
    ///
    /// The normal points 90 degrees to the right of the line direction.
    /// Returns `None` if the line has zero length.
    #[must_use] pub fn inwards_normal(&self) -> Option<SvgPoint> {
        let dx = self.end.x - self.start.x;
        let dy = self.end.y - self.start.y;
        let edge_length = dx.hypot(dy);
        let x = -dy / edge_length;
        let y = dx / edge_length;

        if x.is_finite() && y.is_finite() {
            Some(SvgPoint { x, y })
        } else {
            None
        }
    }

    /// Computes the outward-facing normal vector for this line (opposite of `inwards_normal`).
    #[must_use] pub fn outwards_normal(&self) -> Option<SvgPoint> {
        let inwards = self.inwards_normal()?;
        Some(SvgPoint {
            x: -inwards.x,
            y: -inwards.y,
        })
    }

    /// Reverses the direction of the line by swapping start and end points.
    pub const fn reverse(&mut self) {
        core::mem::swap(&mut self.start, &mut self.end);
    }
    /// Returns the start point of the line.
    #[must_use] pub const fn get_start(&self) -> SvgPoint {
        self.start
    }
    /// Returns the end point of the line.
    #[must_use] pub const fn get_end(&self) -> SvgPoint {
        self.end
    }

    /// Returns the parametric `t` value (0.0–1.0) at the given arc-length offset.
    #[must_use] pub fn get_t_at_offset(&self, offset: f64) -> f64 {
        offset / self.get_length()
    }

    /// Returns the tangent vector of the line.
    /// For a line, the tangent is constant (same direction everywhere),
    /// so no `t` parameter is needed.
    #[must_use] pub fn get_tangent_vector_at_t(&self) -> SvgVector {
        let dx = self.end.x - self.start.x;
        let dy = self.end.y - self.start.y;
        SvgVector {
            x: f64::from(dx),
            y: f64::from(dy),
        }
        .normalize()
    }

    /// Returns the X coordinate at parametric position `t` (0.0 = start, 1.0 = end).
    #[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
    #[must_use] pub fn get_x_at_t(&self, t: f64) -> f64 {
        f64::from(self.start.x) + (f64::from(self.end.x) - f64::from(self.start.x)) * t
    }

    /// Returns the Y coordinate at parametric position `t` (0.0 = start, 1.0 = end).
    #[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
    #[must_use] pub fn get_y_at_t(&self, t: f64) -> f64 {
        f64::from(self.start.y) + (f64::from(self.end.y) - f64::from(self.start.y)) * t
    }

    /// Returns the Euclidean length of the line segment.
    #[must_use] pub fn get_length(&self) -> f64 {
        let dx = self.end.x - self.start.x;
        let dy = self.end.y - self.start.y;
        f64::from(libm::hypotf(dx, dy))
    }

    /// Returns the axis-aligned bounding rectangle of this line segment.
    #[must_use] pub fn get_bounds(&self) -> SvgRect {
        let min_x = self.start.x.min(self.end.x);
        let max_x = self.start.x.max(self.end.x);

        let min_y = self.start.y.min(self.end.y);
        let max_y = self.start.y.max(self.end.y);

        let width = (max_x - min_x).abs();
        let height = (max_y - min_y).abs();

        SvgRect {
            width,
            height,
            x: min_x,
            y: min_y,
            radius_top_left: 0.0,
            radius_top_right: 0.0,
            radius_bottom_left: 0.0,
            radius_bottom_right: 0.0,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C, u8)]
pub enum SvgPathElement {
    Line(SvgLine),
    QuadraticCurve(SvgQuadraticCurve),
    CubicCurve(SvgCubicCurve),
}

impl_option!(
    SvgPathElement,
    OptionSvgPathElement,
    [Debug, Copy, Clone, PartialEq, PartialOrd]
);

impl SvgPathElement {
    /// Creates a line path element from a `SvgLine`
    #[inline]
    #[must_use] pub const fn line(l: SvgLine) -> Self {
        Self::Line(l)
    }

    /// Creates a quadratic curve path element from a `SvgQuadraticCurve`
    #[inline]
    #[must_use] pub const fn quadratic_curve(qc: SvgQuadraticCurve) -> Self {
        Self::QuadraticCurve(qc)
    }

    /// Creates a cubic curve path element from a `SvgCubicCurve`
    #[inline]
    #[must_use] pub const fn cubic_curve(cc: SvgCubicCurve) -> Self {
        Self::CubicCurve(cc)
    }

    /// Sets the end point of this path element.
    pub const fn set_last(&mut self, point: SvgPoint) {
        match self {
            Self::Line(l) => l.end = point,
            Self::QuadraticCurve(qc) => qc.end = point,
            Self::CubicCurve(cc) => cc.end = point,
        }
    }

    /// Sets the start point of this path element.
    pub const fn set_first(&mut self, point: SvgPoint) {
        match self {
            Self::Line(l) => l.start = point,
            Self::QuadraticCurve(qc) => qc.start = point,
            Self::CubicCurve(cc) => cc.start = point,
        }
    }

    /// Reverses the direction of this path element.
    pub const fn reverse(&mut self) {
        match self {
            Self::Line(l) => l.reverse(),
            Self::QuadraticCurve(qc) => qc.reverse(),
            Self::CubicCurve(cc) => cc.reverse(),
        }
    }
    /// Returns the start point of this path element.
    #[must_use] pub const fn get_start(&self) -> SvgPoint {
        match self {
            Self::Line(l) => l.get_start(),
            Self::QuadraticCurve(qc) => qc.get_start(),
            Self::CubicCurve(cc) => cc.get_start(),
        }
    }
    /// Returns the end point of this path element.
    #[must_use] pub const fn get_end(&self) -> SvgPoint {
        match self {
            Self::Line(l) => l.get_end(),
            Self::QuadraticCurve(qc) => qc.get_end(),
            Self::CubicCurve(cc) => cc.get_end(),
        }
    }
    /// Returns the axis-aligned bounding rectangle of this path element.
    #[must_use] pub fn get_bounds(&self) -> SvgRect {
        match self {
            Self::Line(l) => l.get_bounds(),
            Self::QuadraticCurve(qc) => qc.get_bounds(),
            Self::CubicCurve(cc) => cc.get_bounds(),
        }
    }
    /// Returns the arc length of this path element.
    #[must_use] pub fn get_length(&self) -> f64 {
        match self {
            Self::Line(l) => l.get_length(),
            Self::QuadraticCurve(qc) => qc.get_length(),
            Self::CubicCurve(cc) => cc.get_length(),
        }
    }
    /// Returns the parametric `t` value at the given arc-length offset.
    #[must_use] pub fn get_t_at_offset(&self, offset: f64) -> f64 {
        match self {
            Self::Line(l) => l.get_t_at_offset(offset),
            Self::QuadraticCurve(qc) => qc.get_t_at_offset(offset),
            Self::CubicCurve(cc) => cc.get_t_at_offset(offset),
        }
    }
    /// Returns the normalized tangent vector at parametric position `t`.
    #[must_use] pub fn get_tangent_vector_at_t(&self, t: f64) -> SvgVector {
        match self {
            Self::Line(l) => l.get_tangent_vector_at_t(),
            Self::QuadraticCurve(qc) => qc.get_tangent_vector_at_t(t),
            Self::CubicCurve(cc) => cc.get_tangent_vector_at_t(t),
        }
    }
    /// Returns the X coordinate at parametric position `t`.
    #[must_use] pub fn get_x_at_t(&self, t: f64) -> f64 {
        match self {
            Self::Line(l) => l.get_x_at_t(t),
            Self::QuadraticCurve(qc) => qc.get_x_at_t(t),
            Self::CubicCurve(cc) => cc.get_x_at_t(t),
        }
    }
    /// Returns the Y coordinate at parametric position `t`.
    #[must_use] pub fn get_y_at_t(&self, t: f64) -> f64 {
        match self {
            Self::Line(l) => l.get_y_at_t(t),
            Self::QuadraticCurve(qc) => qc.get_y_at_t(t),
            Self::CubicCurve(cc) => cc.get_y_at_t(t),
        }
    }
}

impl_vec!(SvgPathElement, SvgPathElementVec, SvgPathElementVecDestructor, SvgPathElementVecDestructorType, SvgPathElementVecSlice, OptionSvgPathElement);
impl_vec_debug!(SvgPathElement, SvgPathElementVec);
impl_vec_clone!(
    SvgPathElement,
    SvgPathElementVec,
    SvgPathElementVecDestructor
);
impl_vec_partialeq!(SvgPathElement, SvgPathElementVec);
impl_vec_partialord!(SvgPathElement, SvgPathElementVec);

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgPath {
    pub items: SvgPathElementVec,
}

impl_option!(
    SvgPath,
    OptionSvgPath,
    copy = false,
    [Debug, Clone, PartialEq, PartialOrd]
);

impl SvgPath {
    /// Creates a new `SvgPath` from a vector of path elements
    #[inline]
    #[must_use] pub const fn create(items: SvgPathElementVec) -> Self {
        Self { items }
    }

    /// Returns the start point of the first element, or `None` if the path is empty.
    #[must_use] pub fn get_start(&self) -> Option<SvgPoint> {
        self.items.as_ref().first().map(SvgPathElement::get_start)
    }

    /// Returns the end point of the last element, or `None` if the path is empty.
    #[must_use] pub fn get_end(&self) -> Option<SvgPoint> {
        self.items.as_ref().last().map(SvgPathElement::get_end)
    }

    /// Closes the path by appending a line from the last point to the first point, if needed.
    pub fn close(&mut self) {
        let Some(first) = self.items.as_ref().first() else {
            return;
        };
        let Some(last) = self.items.as_ref().last() else {
            return;
        };
        if first.get_start() != last.get_end() {
            let mut elements = self.items.as_slice().to_vec();
            elements.push(SvgPathElement::Line(SvgLine {
                start: last.get_end(),
                end: first.get_start(),
            }));
            self.items = elements.into();
        }
    }

    /// Returns `true` if the path's first start point equals its last end point.
    #[must_use] pub fn is_closed(&self) -> bool {
        let first = self.items.as_ref().first();
        let last = self.items.as_ref().last();
        match (first, last) {
            (Some(f), Some(l)) => (f.get_start() == l.get_end()),
            _ => false,
        }
    }

    /// Reverses the order and direction of all elements in the path.
    pub fn reverse(&mut self) {
        // swap self.items with a default vec
        let mut vec = SvgPathElementVec::from_const_slice(&[]);
        core::mem::swap(&mut vec, &mut self.items);
        let mut vec = vec.into_library_owned_vec();

        // reverse the order of items in the vec
        vec.reverse();

        // reverse the order inside the item itself
        // i.e. swap line.start and line.end
        for item in &mut vec {
            item.reverse();
        }

        // swap back
        let mut vec = SvgPathElementVec::from_vec(vec);
        core::mem::swap(&mut vec, &mut self.items);
    }

    /// Joins another path onto the end of this one, interpolating the join point.
    pub fn join_with(&mut self, mut path: Self) -> Option<()> {
        let self_last_point = self.items.as_ref().last()?.get_end();
        let other_start_point = path.items.as_ref().first()?.get_start();
        let interpolated_join_point = SvgPoint {
            x: f32::midpoint(self_last_point.x, other_start_point.x),
            y: f32::midpoint(self_last_point.y, other_start_point.y),
        };

        // swap self.items with a default vec
        let mut vec = SvgPathElementVec::from_const_slice(&[]);
        core::mem::swap(&mut vec, &mut self.items);
        let mut vec = vec.into_library_owned_vec();

        let mut other = SvgPathElementVec::from_const_slice(&[]);
        core::mem::swap(&mut other, &mut path.items);
        let mut other = other.into_library_owned_vec();

        let vec_len = vec.len() - 1;
        vec.get_mut(vec_len)?.set_last(interpolated_join_point);
        other.get_mut(0)?.set_first(interpolated_join_point);
        vec.append(&mut other);

        // swap back
        let mut vec = SvgPathElementVec::from_vec(vec);
        core::mem::swap(&mut vec, &mut self.items);

        Some(())
    }
    /// Returns the axis-aligned bounding rectangle of the entire path.
    #[must_use] pub fn get_bounds(&self) -> SvgRect {
        let mut first_bounds = match self.items.as_ref().first() {
            Some(s) => s.get_bounds(),
            None => return SvgRect::default(),
        };

        for mp in self.items.as_ref().iter().skip(1) {
            let mp_bounds = mp.get_bounds();
            first_bounds.union_with(&mp_bounds);
        }

        first_bounds
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgMultiPolygon {
    /// NOTE: If a ring represents a hole, simply reverse the order of points
    pub rings: SvgPathVec,
}

impl_option!(
    SvgMultiPolygon,
    OptionSvgMultiPolygon,
    copy = false,
    [Debug, Clone, PartialEq, PartialOrd]
);

impl SvgMultiPolygon {
    /// Creates a new `SvgMultiPolygon` from a vector of paths (rings)
    /// NOTE: If a ring represents a hole, simply reverse the order of points
    #[inline]
    #[must_use] pub const fn create(rings: SvgPathVec) -> Self {
        Self { rings }
    }

    /// Returns the axis-aligned bounding rectangle of all rings in this multi-polygon.
    #[must_use] pub fn get_bounds(&self) -> SvgRect {
        // Seed from the FIRST item found in ANY ring, not specifically rings[0].items[0]:
        // an empty first ring used to make the old seed-or-bail return SvgRect::default()
        // and silently drop every later ring's geometry.
        let mut bounds: Option<SvgRect> = None;
        for ring in &self.rings {
            for item in &ring.items {
                let item_bounds = item.get_bounds();
                match &mut bounds {
                    Some(b) => b.union_with(&item_bounds),
                    None => bounds = Some(item_bounds),
                }
            }
        }
        // Empty polygon (no items in any ring) has zero-sized bounds at origin.
        bounds.unwrap_or_default()
    }
}

impl_vec!(SvgPath, SvgPathVec, SvgPathVecDestructor, SvgPathVecDestructorType, SvgPathVecSlice, OptionSvgPath);
impl_vec_debug!(SvgPath, SvgPathVec);
impl_vec_clone!(SvgPath, SvgPathVec, SvgPathVecDestructor);
impl_vec_partialeq!(SvgPath, SvgPathVec);
impl_vec_partialord!(SvgPath, SvgPathVec);

impl_vec!(SvgMultiPolygon, SvgMultiPolygonVec, SvgMultiPolygonVecDestructor, SvgMultiPolygonVecDestructorType, SvgMultiPolygonVecSlice, OptionSvgMultiPolygon);
impl_vec_debug!(SvgMultiPolygon, SvgMultiPolygonVec);
impl_vec_clone!(
    SvgMultiPolygon,
    SvgMultiPolygonVec,
    SvgMultiPolygonVecDestructor
);
impl_vec_partialeq!(SvgMultiPolygon, SvgMultiPolygonVec);
impl_vec_partialord!(SvgMultiPolygon, SvgMultiPolygonVec);

/// One `SvgNode` corresponds to one SVG `<path></path>` element
#[derive(Debug, Clone, PartialOrd, PartialEq)]
#[repr(C, u8)]
pub enum SvgNode {
    /// Multiple multipolygons, merged to one CPU buf for efficient drawing
    MultiPolygonCollection(SvgMultiPolygonVec),
    MultiPolygon(SvgMultiPolygon),
    MultiShape(SvgSimpleNodeVec),
    Path(SvgPath),
    Circle(SvgCircle),
    Rect(SvgRect),
}

/// One `SvgSimpleNode` is either a path, a rect or a circle
#[derive(Debug, Clone, PartialOrd, PartialEq)]
#[repr(C, u8)]
pub enum SvgSimpleNode {
    Path(SvgPath),
    Circle(SvgCircle),
    Rect(SvgRect),
    CircleHole(SvgCircle),
    RectHole(SvgRect),
}

impl_option!(
    SvgSimpleNode,
    OptionSvgSimpleNode,
    copy = false,
    [Debug, Clone, PartialOrd, PartialEq]
);

impl_vec!(SvgSimpleNode, SvgSimpleNodeVec, SvgSimpleNodeVecDestructor, SvgSimpleNodeVecDestructorType, SvgSimpleNodeVecSlice, OptionSvgSimpleNode);
impl_vec_debug!(SvgSimpleNode, SvgSimpleNodeVec);
impl_vec_clone!(SvgSimpleNode, SvgSimpleNodeVec, SvgSimpleNodeVecDestructor);
impl_vec_partialeq!(SvgSimpleNode, SvgSimpleNodeVec);
impl_vec_partialord!(SvgSimpleNode, SvgSimpleNodeVec);

impl SvgSimpleNode {
    /// Returns the axis-aligned bounding rectangle of this node.
    // Same-body arms dispatch on differently-typed bindings (SvgPath vs SvgCircle),
    // so the identical `a.get_bounds()` bodies cannot be combined into one or-pattern.
    #[allow(clippy::match_same_arms)]
    #[must_use] pub fn get_bounds(&self) -> SvgRect {
        match self {
            Self::Path(a) => a.get_bounds(),
            Self::Circle(a) => a.get_bounds(),
            Self::Rect(a) => *a,
            Self::CircleHole(a) => a.get_bounds(),
            Self::RectHole(a) => *a,
        }
    }
    /// Returns `true` if this node represents a closed shape.
    #[must_use] pub fn is_closed(&self) -> bool {
        match self {
            Self::Path(a) => a.is_closed(),
            Self::Circle(_) | Self::Rect(_) | Self::CircleHole(_) | Self::RectHole(_) => true,
        }
    }
}

impl SvgNode {
    /// Returns the axis-aligned bounding rectangle of this SVG node.
    #[must_use] pub fn get_bounds(&self) -> SvgRect {
        match self {
            Self::MultiPolygonCollection(a) => {
                let mut first_mp_bounds = match a.get(0) {
                    Some(s) => s.get_bounds(),
                    None => return SvgRect::default(),
                };
                for mp in a.iter().skip(1) {
                    let mp_bounds = mp.get_bounds();
                    first_mp_bounds.union_with(&mp_bounds);
                }

                first_mp_bounds
            }
            Self::MultiPolygon(a) => a.get_bounds(),
            Self::MultiShape(a) => {
                let mut first_mp_bounds = match a.get(0) {
                    Some(s) => s.get_bounds(),
                    None => return SvgRect::default(),
                };
                for mp in a.iter().skip(1) {
                    let mp_bounds = mp.get_bounds();
                    first_mp_bounds.union_with(&mp_bounds);
                }

                first_mp_bounds
            }
            Self::Path(a) => a.get_bounds(),
            Self::Circle(a) => a.get_bounds(),
            Self::Rect(a) => *a,
        }
    }
    /// Returns `true` if all sub-paths in this node are closed.
    #[must_use] pub fn is_closed(&self) -> bool {
        match self {
            Self::MultiPolygonCollection(a) => {
                for mp in a {
                    for p in mp.rings.as_ref() {
                        if !p.is_closed() {
                            return false;
                        }
                    }
                }

                true
            }
            Self::MultiPolygon(a) => {
                for p in a.rings.as_ref() {
                    if !p.is_closed() {
                        return false;
                    }
                }

                true
            }
            Self::MultiShape(a) => {
                for p in a.as_ref() {
                    if !p.is_closed() {
                        return false;
                    }
                }

                true
            }
            Self::Path(a) => a.is_closed(),
            Self::Circle(_) | Self::Rect(_) => true,
        }
    }
}

/// An SVG node paired with its visual style (fill or stroke).
#[derive(Debug, Clone, PartialOrd, PartialEq)]
#[repr(C)]
pub struct SvgStyledNode {
    pub geometry: SvgNode,
    pub style: SvgStyle,
}

/// A 2D vertex used in tessellated SVG geometry.
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
#[repr(C)]
pub struct SvgVertex {
    pub x: f32,
    pub y: f32,
}

impl_option!(
    SvgVertex,
    OptionSvgVertex,
    [Debug, Copy, Clone, PartialOrd, PartialEq]
);

impl VertexLayoutDescription for SvgVertex {
    fn get_description() -> VertexLayout {
        VertexLayout {
            fields: vec![VertexAttribute {
                va_name: String::from("vAttrXY").into(),
                layout_location: None.into(),
                attribute_type: VertexAttributeType::Float,
                item_count: 2,
            }]
            .into(),
        }
    }
}

/// A 3D vertex with per-vertex RGBA color, used in multi-colored SVG tessellation.
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
#[repr(C)]
pub struct SvgColoredVertex {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl_option!(
    SvgColoredVertex,
    OptionSvgColoredVertex,
    [Debug, Copy, Clone, PartialOrd, PartialEq]
);

impl VertexLayoutDescription for SvgColoredVertex {
    fn get_description() -> VertexLayout {
        VertexLayout {
            fields: vec![
                VertexAttribute {
                    va_name: String::from("vAttrXY").into(),
                    layout_location: None.into(),
                    attribute_type: VertexAttributeType::Float,
                    item_count: 3,
                },
                VertexAttribute {
                    va_name: String::from("vColor").into(),
                    layout_location: None.into(),
                    attribute_type: VertexAttributeType::Float,
                    item_count: 4,
                },
            ]
            .into(),
        }
    }
}

/// A circle defined by center coordinates and radius.
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
#[repr(C)]
pub struct SvgCircle {
    pub center_x: f32,
    pub center_y: f32,
    pub radius: f32,
}

impl SvgCircle {
    /// Returns `true` if the given point lies inside the circle.
    #[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma; keep explicit a*b+c
    #[must_use] pub fn contains_point(&self, x: f32, y: f32) -> bool {
        let x_diff = libm::fabsf(x - self.center_x);
        let y_diff = libm::fabsf(y - self.center_y);
        (x_diff * x_diff) + (y_diff * y_diff) < (self.radius * self.radius)
    }
    /// Returns the axis-aligned bounding rectangle of this circle.
    #[must_use] pub fn get_bounds(&self) -> SvgRect {
        SvgRect {
            width: self.radius * 2.0,
            height: self.radius * 2.0,
            x: self.center_x - self.radius,
            y: self.center_y - self.radius,
            radius_top_left: 0.0,
            radius_top_right: 0.0,
            radius_bottom_left: 0.0,
            radius_bottom_right: 0.0,
        }
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct TessellatedSvgNode {
    pub vertices: SvgVertexVec,
    pub indices: U32Vec,
}

impl_option!(
    TessellatedSvgNode,
    OptionTessellatedSvgNode,
    copy = false,
    [Debug, Clone, PartialEq, PartialOrd]
);

impl Default for TessellatedSvgNode {
    fn default() -> Self {
        Self {
            vertices: Vec::new().into(),
            indices: Vec::new().into(),
        }
    }
}

impl_vec!(TessellatedSvgNode, TessellatedSvgNodeVec, TessellatedSvgNodeVecDestructor, TessellatedSvgNodeVecDestructorType, TessellatedSvgNodeVecSlice, OptionTessellatedSvgNode);
impl_vec_debug!(TessellatedSvgNode, TessellatedSvgNodeVec);
impl_vec_partialord!(TessellatedSvgNode, TessellatedSvgNodeVec);
impl_vec_clone!(
    TessellatedSvgNode,
    TessellatedSvgNodeVec,
    TessellatedSvgNodeVecDestructor
);
impl_vec_partialeq!(TessellatedSvgNode, TessellatedSvgNodeVec);

impl TessellatedSvgNode {
    #[must_use] pub fn empty() -> Self {
        Self::default()
    }
}

impl TessellatedSvgNodeVec {
    #[must_use] pub fn get_ref(&self) -> TessellatedSvgNodeVecRef {
        let slice = self.as_ref();
        TessellatedSvgNodeVecRef {
            ptr: slice.as_ptr(),
            len: slice.len(),
        }
    }
}

impl fmt::Debug for TessellatedSvgNodeVecRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_slice().fmt(f)
    }
}

// C ABI wrapper over &[TessellatedSvgNode]
#[repr(C)]
pub struct TessellatedSvgNodeVecRef {
    pub ptr: *const TessellatedSvgNode,
    pub len: usize,
}

impl Clone for TessellatedSvgNodeVecRef {
    fn clone(&self) -> Self {
        Self {
            ptr: self.ptr,
            len: self.len,
        }
    }
}

impl TessellatedSvgNodeVecRef {
    #[must_use] pub const fn as_slice(&self) -> &[TessellatedSvgNode] {
        unsafe { core::slice::from_raw_parts(self.ptr, self.len) }
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct TessellatedColoredSvgNode {
    pub vertices: SvgColoredVertexVec,
    pub indices: U32Vec,
}

impl_option!(
    TessellatedColoredSvgNode,
    OptionTessellatedColoredSvgNode,
    copy = false,
    [Debug, Clone, PartialEq, PartialOrd]
);

impl Default for TessellatedColoredSvgNode {
    fn default() -> Self {
        Self {
            vertices: Vec::new().into(),
            indices: Vec::new().into(),
        }
    }
}

impl_vec!(TessellatedColoredSvgNode, TessellatedColoredSvgNodeVec, TessellatedColoredSvgNodeVecDestructor, TessellatedColoredSvgNodeVecDestructorType, TessellatedColoredSvgNodeVecSlice, OptionTessellatedColoredSvgNode);
impl_vec_debug!(TessellatedColoredSvgNode, TessellatedColoredSvgNodeVec);
impl_vec_partialord!(TessellatedColoredSvgNode, TessellatedColoredSvgNodeVec);
impl_vec_clone!(
    TessellatedColoredSvgNode,
    TessellatedColoredSvgNodeVec,
    TessellatedColoredSvgNodeVecDestructor
);
impl_vec_partialeq!(TessellatedColoredSvgNode, TessellatedColoredSvgNodeVec);

impl TessellatedColoredSvgNode {
    #[must_use] pub fn empty() -> Self {
        Self::default()
    }
}

impl TessellatedColoredSvgNodeVec {
    #[must_use] pub fn get_ref(&self) -> TessellatedColoredSvgNodeVecRef {
        let slice = self.as_ref();
        TessellatedColoredSvgNodeVecRef {
            ptr: slice.as_ptr(),
            len: slice.len(),
        }
    }
}

impl fmt::Debug for TessellatedColoredSvgNodeVecRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_slice().fmt(f)
    }
}

// C ABI wrapper over &[TessellatedColoredSvgNode]
#[repr(C)]
pub struct TessellatedColoredSvgNodeVecRef {
    pub ptr: *const TessellatedColoredSvgNode,
    pub len: usize,
}

impl Clone for TessellatedColoredSvgNodeVecRef {
    fn clone(&self) -> Self {
        Self {
            ptr: self.ptr,
            len: self.len,
        }
    }
}

impl TessellatedColoredSvgNodeVecRef {
    #[must_use] pub const fn as_slice(&self) -> &[TessellatedColoredSvgNode] {
        unsafe { core::slice::from_raw_parts(self.ptr, self.len) }
    }
}

impl_vec!(SvgVertex, SvgVertexVec, SvgVertexVecDestructor, SvgVertexVecDestructorType, SvgVertexVecSlice, OptionSvgVertex);
impl_vec_debug!(SvgVertex, SvgVertexVec);
impl_vec_partialord!(SvgVertex, SvgVertexVec);
impl_vec_clone!(SvgVertex, SvgVertexVec, SvgVertexVecDestructor);
impl_vec_partialeq!(SvgVertex, SvgVertexVec);

impl_vec!(SvgColoredVertex, SvgColoredVertexVec, SvgColoredVertexVecDestructor, SvgColoredVertexVecDestructorType, SvgColoredVertexVecSlice, OptionSvgColoredVertex);
impl_vec_debug!(SvgColoredVertex, SvgColoredVertexVec);
impl_vec_partialord!(SvgColoredVertex, SvgColoredVertexVec);
impl_vec_clone!(
    SvgColoredVertex,
    SvgColoredVertexVec,
    SvgColoredVertexVecDestructor
);
impl_vec_partialeq!(SvgColoredVertex, SvgColoredVertexVec);

/// Computes the bbox size and transform matrix uniforms shared by SVG draw methods.
///
/// Converts `StyleTransform` list into column-major `[f32; 16]` for OpenGL,
/// and packages it along with the bbox size uniform.
// target_size is physical pixel dimensions (u32); GL uniforms are f32. Pixel
// counts are always well within f32's exact-integer range (2^24), so the
// precision loss the lint warns about cannot occur for any real render target.
#[allow(clippy::cast_precision_loss)]
fn compute_svg_transform_uniforms(
    target_size: PhysicalSizeU32,
    transforms: &[StyleTransform],
) -> (Uniform, Uniform) {
    let transform_origin = StyleTransformOrigin {
        x: PixelValue::px(target_size.width as f32 / 2.0),
        y: PixelValue::px(target_size.height as f32 / 2.0),
    };

    let computed_transform = ComputedTransform3D::from_style_transform_vec(
        transforms,
        &transform_origin,
        target_size.width as f32,
        target_size.height as f32,
        RotationMode::ForWebRender,
    );

    // NOTE: OpenGL draws are column-major, while ComputedTransform3D
    // is row-major! Need to transpose the matrix!
    let m = computed_transform.get_column_major().m;
    let matrix: [f32; 16] = core::array::from_fn(|i| m[i / 4][i % 4]);

    let bbox_uniform = Uniform {
        uniform_name: "vBboxSize".into(),
        uniform_type: UniformType::FloatVec2([
            target_size.width as f32,
            target_size.height as f32,
        ]),
    };

    let transform_uniform = Uniform {
        uniform_name: "vTransformMatrix".into(),
        uniform_type: UniformType::Matrix4 {
            transpose: false,
            matrix,
        },
    };

    (bbox_uniform, transform_uniform)
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
#[repr(C)]
pub struct TessellatedGPUSvgNode {
    pub vertex_index_buffer: VertexBuffer,
}

impl TessellatedGPUSvgNode {
    /// Uploads the tesselated SVG node to GPU memory
    #[must_use] pub fn new(node: &TessellatedSvgNode, gl: GlContextPtr) -> Self {
        let svg_shader_id = gl.ptr.svg_shader;
        Self {
            vertex_index_buffer: VertexBuffer::new(
                gl,
                svg_shader_id,
                node.vertices.as_ref(),
                node.indices.as_ref(),
                IndexBufferFormat::Triangles,
            ),
        }
    }

    /// Draw the vertex buffer to the texture with the given color and transform
    #[allow(clippy::needless_pass_by_value)] // owned azul value taken by value (public API / ownership-transfer convention)
    pub fn draw(
        &self,
        texture: &mut Texture,
        target_size: PhysicalSizeU32,
        color: ColorU,
        transforms: StyleTransformVec,
    ) -> bool {
        let (bbox_uniform, transform_uniform) =
            compute_svg_transform_uniforms(target_size, transforms.as_ref());

        let color: ColorF = color.into();

        let uniforms = [
            bbox_uniform,
            Uniform {
                uniform_name: "fDrawColor".into(),
                uniform_type: UniformType::FloatVec4([color.r, color.g, color.b, color.a]),
            },
            transform_uniform,
        ];

        GlShader::draw(
            texture.gl_context.ptr.svg_shader,
            texture,
            &[(&self.vertex_index_buffer, &uniforms[..])],
        );

        true
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
#[repr(C)]
pub struct TessellatedColoredGPUSvgNode {
    pub vertex_index_buffer: VertexBuffer,
}

impl TessellatedColoredGPUSvgNode {
    /// Uploads the tesselated SVG node to GPU memory
    #[must_use] pub fn new(node: &TessellatedColoredSvgNode, gl: GlContextPtr) -> Self {
        let svg_shader_id = gl.ptr.svg_multicolor_shader;
        Self {
            vertex_index_buffer: VertexBuffer::new(
                gl,
                svg_shader_id,
                node.vertices.as_ref(),
                node.indices.as_ref(),
                IndexBufferFormat::Triangles,
            ),
        }
    }

    /// Draw the vertex buffer to the texture with the given color and transform
    #[allow(clippy::needless_pass_by_value)] // owned azul value taken by value (public API / ownership-transfer convention)
    pub fn draw(
        &self,
        texture: &mut Texture,
        target_size: PhysicalSizeU32,
        transforms: StyleTransformVec,
    ) -> bool {
        let (bbox_uniform, transform_uniform) =
            compute_svg_transform_uniforms(target_size, transforms.as_ref());

        // two separately-named GL uniforms collected into the draw-call array;
        // not a tuple->array conversion.
        #[allow(clippy::tuple_array_conversions)]
        let uniforms = [bbox_uniform, transform_uniform];

        GlShader::draw(
            texture.gl_context.ptr.svg_multicolor_shader,
            texture,
            &[(&self.vertex_index_buffer, &uniforms[..])],
        );

        true
    }
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C, u8)]
pub enum SvgStyle {
    Fill(SvgFillStyle),
    Stroke(SvgStrokeStyle),
}

impl SvgStyle {
    #[must_use] pub const fn get_antialias(&self) -> bool {
        match self {
            Self::Fill(f) => f.anti_alias,
            Self::Stroke(s) => s.anti_alias,
        }
    }
    #[must_use] pub const fn get_high_quality_aa(&self) -> bool {
        match self {
            Self::Fill(f) => f.high_quality_aa,
            Self::Stroke(s) => s.high_quality_aa,
        }
    }
    #[must_use] pub const fn get_transform(&self) -> SvgTransform {
        match self {
            Self::Fill(f) => f.transform,
            Self::Stroke(s) => s.transform,
        }
    }
}
/// SVG fill rule for determining the interior of a shape.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd)]
#[repr(C)]
#[derive(Default)]
pub enum SvgFillRule {
    #[default]
    Winding,
    EvenOdd,
}


#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgTransform {
    pub sx: f32,
    pub kx: f32,
    pub ky: f32,
    pub sy: f32,
    pub tx: f32,
    pub ty: f32,
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgFillStyle {
    /// See the SVG specification.
    ///
    /// Default value: `LineJoin::Miter`.
    pub line_join: SvgLineJoin,
    /// See the SVG specification.
    ///
    /// Must be greater than or equal to 1.0.
    /// Default value: `StrokeOptions::DEFAULT_MITER_LIMIT`.
    pub miter_limit: f32,
    /// Maximum allowed distance to the path when building an approximation.
    ///
    /// See [Flattening and tolerance](index.html#flattening-and-tolerance).
    /// Default value: `StrokeOptions::DEFAULT_TOLERANCE`.
    pub tolerance: f32,
    /// Whether to use the "winding" or "even / odd" fill rule when tesselating the path
    pub fill_rule: SvgFillRule,
    /// Whether to apply a transform to the points in the path (warning: will be done on the CPU -
    /// expensive)
    pub transform: SvgTransform,
    /// Whether the fill is intended to be anti-aliased (default: true)
    pub anti_alias: bool,
    /// Whether the anti-aliasing has to be of high quality (default: false)
    pub high_quality_aa: bool,
}

impl Default for SvgFillStyle {
    fn default() -> Self {
        Self {
            line_join: SvgLineJoin::Miter,
            miter_limit: DEFAULT_MITER_LIMIT,
            tolerance: DEFAULT_TOLERANCE,
            fill_rule: SvgFillRule::default(),
            transform: SvgTransform::default(),
            anti_alias: true,
            high_quality_aa: false,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgStrokeStyle {
    /// Dash pattern
    pub dash_pattern: OptionSvgDashPattern,
    /// Whether to apply a transform to the points in the path (warning: will be done on the CPU -
    /// expensive)
    pub transform: SvgTransform,
    /// What cap to use at the start of each sub-path.
    ///
    /// Default value: `LineCap::Butt`.
    pub start_cap: SvgLineCap,
    /// What cap to use at the end of each sub-path.
    ///
    /// Default value: `LineCap::Butt`.
    pub end_cap: SvgLineCap,
    /// See the SVG specification.
    ///
    /// Default value: `LineJoin::Miter`.
    pub line_join: SvgLineJoin,
    /// Line width
    ///
    /// Default value: `StrokeOptions::DEFAULT_LINE_WIDTH`.
    pub line_width: f32,
    /// See the SVG specification.
    ///
    /// Must be greater than or equal to 1.0.
    /// Default value: `StrokeOptions::DEFAULT_MITER_LIMIT`.
    pub miter_limit: f32,
    /// Maximum allowed distance to the path when building an approximation.
    ///
    /// See [Flattening and tolerance](index.html#flattening-and-tolerance).
    /// Default value: `StrokeOptions::DEFAULT_TOLERANCE`.
    pub tolerance: f32,
    /// Apply line width
    ///
    /// When set to false, the generated vertices will all be positioned in the centre
    /// of the line. The width can be applied later on (eg in a vertex shader) by adding
    /// the vertex normal multiplied by the line with to each vertex position.
    ///
    /// Default value: `true`. NOTE: currently unused!
    pub apply_line_width: bool,
    /// Whether the fill is intended to be anti-aliased (default: true)
    pub anti_alias: bool,
    /// Whether the anti-aliasing has to be of high quality (default: false)
    pub high_quality_aa: bool,
}

impl Default for SvgStrokeStyle {
    fn default() -> Self {
        Self {
            dash_pattern: OptionSvgDashPattern::None,
            transform: SvgTransform::default(),
            start_cap: SvgLineCap::default(),
            end_cap: SvgLineCap::default(),
            line_join: SvgLineJoin::default(),
            line_width: DEFAULT_LINE_WIDTH,
            miter_limit: DEFAULT_MITER_LIMIT,
            tolerance: DEFAULT_TOLERANCE,
            apply_line_width: true,
            anti_alias: true,
            high_quality_aa: false,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgDashPattern {
    pub offset: f32,
    pub length_1: f32,
    pub gap_1: f32,
    pub length_2: f32,
    pub gap_2: f32,
    pub length_3: f32,
    pub gap_3: f32,
}

impl_option!(
    SvgDashPattern,
    OptionSvgDashPattern,
    [Debug, Copy, Clone, PartialEq, PartialOrd]
);

/// The shape used at the end of open sub-paths when they are stroked.
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
#[repr(C)]
#[derive(Default)]
pub enum SvgLineCap {
    #[default]
    Butt,
    Square,
    Round,
}


/// The shape used at the corners of stroked paths.
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
#[repr(C)]
#[derive(Default)]
pub enum SvgLineJoin {
    #[default]
    Miter,
    MiterClip,
    Round,
    Bevel,
}


pub use core::ffi::c_void;

#[derive(Debug, Clone)]
#[repr(C)]
pub struct SvgXmlNode {
    pub node: *const c_void, // usvg::Node
    pub run_destructor: bool,
}

#[derive(Debug, Clone)]
#[repr(C)]
pub struct Svg {
    pub tree: *const c_void, // *mut usvg::Tree,
    pub run_destructor: bool,
}

/// SVG `shape-rendering` property controlling quality vs speed tradeoffs.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[repr(C)]
pub enum ShapeRendering {
    OptimizeSpeed,
    CrispEdges,
    GeometricPrecision,
}

/// SVG `image-rendering` property controlling image quality vs speed.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[repr(C)]
pub enum ImageRendering {
    OptimizeQuality,
    OptimizeSpeed,
}

/// SVG `text-rendering` property controlling text quality vs speed.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[repr(C)]
pub enum TextRendering {
    OptimizeSpeed,
    OptimizeLegibility,
    GeometricPrecision,
}

/// Font database source for SVG text rendering.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[repr(C)]
pub enum FontDatabase {
    Empty,
    System,
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgRenderOptions {
    pub target_size: OptionLayoutSize,
    pub background_color: OptionColorU,
    pub fit: SvgFitTo,
    pub transform: SvgRenderTransform,
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgRenderTransform {
    pub sx: f32,
    pub kx: f32,
    pub ky: f32,
    pub sy: f32,
    pub tx: f32,
    pub ty: f32,
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[repr(C, u8)]
#[derive(Default)]
pub enum SvgFitTo {
    #[default]
    Original,
    Width(u32),
    Height(u32),
    Zoom(f32),
}


#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[repr(C)]
pub struct SvgParseOptions {
    /// SVG image path. Used to resolve relative image paths.
    pub relative_image_path: OptionString,
    /// Default font family. Will be used when no font-family attribute is set in the SVG. Default:
    /// Times New Roman
    pub default_font_family: AzString,
    /// A list of languages. Will be used to resolve a systemLanguage conditional attribute.
    /// Format: en, en-US. Default: [en]
    pub languages: StringVec,
    /// Target DPI. Impact units conversion. Default: 96.0
    pub dpi: f32,
    /// A default font size. Will be used when no font-size attribute is set in the SVG. Default:
    /// 12
    pub font_size: f32,
    /// Specifies the default shape rendering method. Will be used when an SVG element's
    /// shape-rendering property is set to auto. Default: `GeometricPrecision`
    pub shape_rendering: ShapeRendering,
    /// Specifies the default text rendering method. Will be used when an SVG element's
    /// text-rendering property is set to auto. Default: `OptimizeLegibility`
    pub text_rendering: TextRendering,
    /// Specifies the default image rendering method. Will be used when an SVG element's
    /// image-rendering property is set to auto. Default: `OptimizeQuality`
    pub image_rendering: ImageRendering,
    /// When empty, text elements will be skipped. Default: `System`
    pub fontdb: FontDatabase,
    /// Keep named groups. If set to true, all non-empty groups with id attribute will not be
    /// removed. Default: false
    pub keep_named_groups: bool,
}

impl Default for SvgParseOptions {
    fn default() -> Self {
        let lang_vec: Vec<AzString> = vec![String::from("en").into()];
        Self {
            relative_image_path: OptionString::None,
            default_font_family: "Times New Roman".to_string().into(),
            languages: lang_vec.into(),
            dpi: 96.0,
            font_size: 12.0,
            shape_rendering: ShapeRendering::GeometricPrecision,
            text_rendering: TextRendering::OptimizeLegibility,
            image_rendering: ImageRendering::OptimizeQuality,
            fontdb: FontDatabase::System,
            keep_named_groups: false,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
#[repr(C)]
pub struct SvgXmlOptions {
    pub use_single_quote: bool,
    pub indent: Indent,
    pub attributes_indent: Indent,
}

impl Default for SvgXmlOptions {
    fn default() -> Self {
        Self {
            use_single_quote: false,
            indent: Indent::Spaces(2),
            attributes_indent: Indent::Spaces(2),
        }
    }
}
#[allow(variant_size_differences)] // repr(C,u8) FFI enum: boxing the large variant would change the C ABI (api.json bindings); size disparity accepted
#[derive(Debug, PartialEq, Eq, PartialOrd, Clone)]
#[repr(C, u8)]
pub enum SvgParseError {
    NoParserAvailable,
    ElementsLimitReached,
    NotAnUtf8Str,
    MalformedGZip,
    InvalidSize,
    ParsingFailed(XmlError),
}

impl fmt::Display for SvgParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use self::SvgParseError::{NoParserAvailable, ElementsLimitReached, NotAnUtf8Str, MalformedGZip, InvalidSize, ParsingFailed};
        match self {
            NoParserAvailable => write!(
                f,
                "Library was compiled without SVG support (no parser available)"
            ),
            ElementsLimitReached => write!(f, "Error parsing SVG: Elements limit reached"),
            NotAnUtf8Str => write!(f, "Error parsing SVG: Not an UTF-8 String"),
            MalformedGZip => write!(
                f,
                "Error parsing SVG: SVG is compressed with a malformed GZIP compression"
            ),
            InvalidSize => write!(f, "Error parsing SVG: Invalid size"),
            ParsingFailed(e) => write!(f, "Error parsing SVG: Parsing SVG as XML failed: {e}"),
        }
    }
}

impl_result!(
    SvgXmlNode,
    SvgParseError,
    ResultSvgXmlNodeSvgParseError,
    copy = false,
    [Debug, Clone]
);
impl_result!(
    Svg,
    SvgParseError,
    ResultSvgSvgParseError,
    copy = false,
    [Debug, Clone]
);

/// Indentation style for SVG XML serialization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
#[repr(C, u8)]
pub enum Indent {
    None,
    Spaces(u8),
    Tabs,
}

#[cfg(test)]
#[allow(clippy::pedantic, clippy::nursery, clippy::float_cmp)]
mod autotest_generated {
    use super::*;

    // ---------------------------------------------------------------- helpers

    fn pt(x: f32, y: f32) -> SvgPoint {
        SvgPoint { x, y }
    }

    fn line_el(x1: f32, y1: f32, x2: f32, y2: f32) -> SvgPathElement {
        SvgPathElement::line(SvgLine::new(pt(x1, y1), pt(x2, y2)))
    }

    fn make_path(items: Vec<SvgPathElement>) -> SvgPath {
        SvgPath::create(SvgPathElementVec::from_vec(items))
    }

    fn quad() -> SvgQuadraticCurve {
        SvgQuadraticCurve {
            start: pt(0.0, 0.0),
            ctrl: pt(5.0, 10.0),
            end: pt(10.0, 0.0),
        }
    }

    fn cubic() -> SvgCubicCurve {
        SvgCubicCurve {
            start: pt(0.0, 0.0),
            ctrl_1: pt(0.0, 10.0),
            ctrl_2: pt(10.0, 10.0),
            end: pt(10.0, 0.0),
        }
    }

    /// `true` if `outer` fully contains `inner` (used to check bounding-box invariants).
    fn rect_contains(outer: &SvgRect, inner: &SvgRect) -> bool {
        outer.x <= inner.x
            && outer.y <= inner.y
            && outer.x + outer.width >= inner.x + inner.width
            && outer.y + outer.height >= inner.y + inner.height
    }

    // ------------------------------------------------------- SvgLine :: basic

    #[test]
    fn svgline_new_keeps_fields_including_extreme_values() {
        let l = SvgLine::new(pt(1.0, 2.0), pt(3.0, 4.0));
        assert_eq!(l.get_start(), pt(1.0, 2.0));
        assert_eq!(l.get_end(), pt(3.0, 4.0));

        let extreme = SvgLine::new(pt(f32::MIN, f32::MAX), pt(f32::MAX, f32::MIN));
        assert_eq!(extreme.start.x, f32::MIN);
        assert_eq!(extreme.end.x, f32::MAX);

        // NaN survives construction untouched (no normalization happens)
        let nan = SvgLine::new(pt(f32::NAN, 0.0), pt(0.0, f32::NAN));
        assert!(nan.get_start().x.is_nan());
        assert!(nan.get_end().y.is_nan());
    }

    #[test]
    fn svgline_reverse_is_an_involution() {
        let orig = SvgLine::new(pt(-1.5, 2.5), pt(7.0, -9.0));
        let mut l = orig;
        l.reverse();
        assert_eq!(l.get_start(), orig.get_end());
        assert_eq!(l.get_end(), orig.get_start());
        l.reverse();
        assert_eq!(l, orig);
    }

    #[test]
    fn svgline_reverse_does_not_panic_on_extreme_values() {
        let mut l = SvgLine::new(pt(f32::INFINITY, f32::NAN), pt(f32::NEG_INFINITY, f32::MAX));
        l.reverse();
        assert!(l.get_start().x.is_infinite() && l.get_start().x < 0.0);
        assert!(l.get_end().y.is_nan());
    }

    // ----------------------------------------------------- SvgLine :: normals

    #[test]
    fn svgline_inwards_normal_is_unit_length_and_90deg_right() {
        // horizontal line pointing +x -> normal points -y (90deg to the right in SVG coords)
        let l = SvgLine::new(pt(0.0, 0.0), pt(10.0, 0.0));
        let n = l.inwards_normal().expect("non-degenerate line has a normal");
        assert!((n.x - 0.0).abs() < 1e-6);
        assert!((n.y - 1.0).abs() < 1e-6);
        let len = (n.x * n.x + n.y * n.y).sqrt();
        assert!((len - 1.0).abs() < 1e-6, "normal must be unit length");
    }

    #[test]
    fn svgline_outwards_normal_is_the_negated_inwards_normal() {
        let l = SvgLine::new(pt(3.0, -4.0), pt(-7.0, 11.0));
        let i = l.inwards_normal().expect("non-degenerate line has a normal");
        let o = l.outwards_normal().expect("non-degenerate line has a normal");
        assert!((i.x + o.x).abs() < 1e-6);
        assert!((i.y + o.y).abs() < 1e-6);
    }

    #[test]
    fn svgline_normals_are_none_for_zero_length_line() {
        // division by a zero edge length must not produce a bogus point
        let l = SvgLine::new(pt(5.0, 5.0), pt(5.0, 5.0));
        assert_eq!(l.inwards_normal(), None);
        assert_eq!(l.outwards_normal(), None);
    }

    #[test]
    fn svgline_normals_are_none_for_nan_and_infinite_coords() {
        let nan = SvgLine::new(pt(f32::NAN, 0.0), pt(1.0, 1.0));
        assert_eq!(nan.inwards_normal(), None);
        assert_eq!(nan.outwards_normal(), None);

        // dy/hypot == inf/inf == NaN -> not finite -> None
        let inf = SvgLine::new(pt(f32::NEG_INFINITY, f32::NEG_INFINITY), pt(1.0, 1.0));
        assert_eq!(inf.inwards_normal(), None);
        assert_eq!(inf.outwards_normal(), None);
    }

    #[test]
    fn svgline_inwards_normal_on_overflowing_line_stays_defined() {
        // dx/dy overflow f32 -> hypot is +inf; the result must be either None
        // or finite, never a silent NaN/inf leaking into the point.
        let l = SvgLine::new(pt(-f32::MAX, -f32::MAX), pt(f32::MAX, f32::MAX));
        match l.inwards_normal() {
            None => {}
            Some(n) => assert!(
                n.x.is_finite() && n.y.is_finite(),
                "inwards_normal returned a non-finite point: {n:?}"
            ),
        }
    }

    // ---------------------------------------------------- SvgLine :: numerics

    #[test]
    fn svgline_get_x_y_at_t_hit_the_endpoints_exactly() {
        let l = SvgLine::new(pt(2.0, -3.0), pt(12.0, 17.0));
        assert_eq!(l.get_x_at_t(0.0), 2.0);
        assert_eq!(l.get_y_at_t(0.0), -3.0);
        assert_eq!(l.get_x_at_t(1.0), 12.0);
        assert_eq!(l.get_y_at_t(1.0), 17.0);
        assert!((l.get_x_at_t(0.5) - 7.0).abs() < 1e-9);
        assert!((l.get_y_at_t(0.5) - 7.0).abs() < 1e-9);
    }

    #[test]
    fn svgline_get_x_at_t_extrapolates_for_out_of_range_t() {
        // t is NOT clamped to [0, 1] - document the extrapolating behaviour
        let l = SvgLine::new(pt(0.0, 0.0), pt(10.0, 10.0));
        assert!((l.get_x_at_t(-1.0) - -10.0).abs() < 1e-9);
        assert!((l.get_y_at_t(2.0) - 20.0).abs() < 1e-9);
    }

    #[test]
    fn svgline_get_x_y_at_t_nan_and_inf_do_not_panic() {
        let l = SvgLine::new(pt(0.0, 0.0), pt(10.0, 10.0));
        assert!(l.get_x_at_t(f64::NAN).is_nan());
        assert!(l.get_y_at_t(f64::NAN).is_nan());
        assert!(l.get_x_at_t(f64::INFINITY).is_infinite());
        assert!(l.get_y_at_t(f64::NEG_INFINITY).is_infinite());

        // 0-length in x: (end.x - start.x) == 0, so 0 * inf == NaN
        let vertical = SvgLine::new(pt(4.0, 0.0), pt(4.0, 10.0));
        assert!(vertical.get_x_at_t(f64::INFINITY).is_nan());
    }

    #[test]
    fn svgline_get_x_at_t_at_f32_extremes_saturates_to_inf_not_panic() {
        let l = SvgLine::new(pt(-f32::MAX, 0.0), pt(f32::MAX, 0.0));
        // f64 has the range to hold 2 * f32::MAX, so this must stay finite
        assert!(l.get_x_at_t(0.5).abs() < 1e-9);
        assert!(l.get_x_at_t(1.0).is_finite());
        assert!(l.get_x_at_t(f64::MAX).is_infinite());
    }

    #[test]
    fn svgline_get_length_is_euclidean_and_direction_independent() {
        let l = SvgLine::new(pt(0.0, 0.0), pt(3.0, 4.0));
        assert!((l.get_length() - 5.0).abs() < 1e-6);
        let mut r = l;
        r.reverse();
        assert!((r.get_length() - l.get_length()).abs() < 1e-9);
        assert_eq!(SvgLine::new(pt(1.0, 1.0), pt(1.0, 1.0)).get_length(), 0.0);
    }

    #[test]
    fn svgline_get_length_overflow_and_nan_are_defined() {
        // dx overflows f32 -> +inf, hypot(+inf, 0) == +inf
        let huge = SvgLine::new(pt(-f32::MAX, 0.0), pt(f32::MAX, 0.0));
        let len = huge.get_length();
        assert!(len.is_infinite() && len > 0.0);

        let nan = SvgLine::new(pt(f32::NAN, 0.0), pt(0.0, 0.0));
        assert!(nan.get_length().is_nan());
    }

    #[test]
    fn svgline_get_t_at_offset_maps_arc_length_to_t() {
        let l = SvgLine::new(pt(0.0, 0.0), pt(10.0, 0.0));
        assert_eq!(l.get_t_at_offset(0.0), 0.0);
        assert!((l.get_t_at_offset(5.0) - 0.5).abs() < 1e-6);
        assert!((l.get_t_at_offset(10.0) - 1.0).abs() < 1e-6);
        // negative + past-the-end offsets are NOT clamped
        assert!((l.get_t_at_offset(-5.0) + 0.5).abs() < 1e-6);
        assert!((l.get_t_at_offset(20.0) - 2.0).abs() < 1e-6);
    }

    #[test]
    fn svgline_get_t_at_offset_on_zero_length_line_is_nan_or_inf_not_a_panic() {
        // offset / 0.0 -- must not panic; 0/0 is NaN, x/0 is +-inf
        let l = SvgLine::new(pt(1.0, 1.0), pt(1.0, 1.0));
        assert!(l.get_t_at_offset(0.0).is_nan());
        assert!(l.get_t_at_offset(5.0).is_infinite());
        assert!(l.get_t_at_offset(-5.0).is_infinite());
        assert!(l.get_t_at_offset(f64::NAN).is_nan());
    }

    #[test]
    fn svgline_get_t_at_offset_round_trips_through_get_x_at_t() {
        let l = SvgLine::new(pt(2.0, 2.0), pt(2.0, 12.0));
        let t = l.get_t_at_offset(l.get_length());
        assert!((l.get_y_at_t(t) - 12.0).abs() < 1e-6);
        assert!((l.get_x_at_t(t) - 2.0).abs() < 1e-6);
    }

    #[test]
    fn svgline_tangent_vector_is_normalized_and_zero_for_degenerate_lines() {
        let l = SvgLine::new(pt(0.0, 0.0), pt(0.0, 5.0));
        let t = l.get_tangent_vector_at_t();
        assert!((t.x - 0.0).abs() < 1e-9);
        assert!((t.y - 1.0).abs() < 1e-9);

        // normalize() defines the zero-length case as the zero vector
        let degenerate = SvgLine::new(pt(3.0, 3.0), pt(3.0, 3.0));
        let t = degenerate.get_tangent_vector_at_t();
        assert_eq!(t, SvgVector { x: 0.0, y: 0.0 });
    }

    // ------------------------------------------------------ SvgLine :: bounds

    #[test]
    fn svgline_get_bounds_is_orientation_independent() {
        let l = SvgLine::new(pt(10.0, 20.0), pt(-5.0, 4.0));
        let mut r = l;
        r.reverse();
        assert_eq!(l.get_bounds(), r.get_bounds());

        let b = l.get_bounds();
        assert_eq!(b.x, -5.0);
        assert_eq!(b.y, 4.0);
        assert_eq!(b.width, 15.0);
        assert_eq!(b.height, 16.0);
        assert_eq!(b.radius_top_left, 0.0);
    }

    #[test]
    fn svgline_get_bounds_of_degenerate_line_is_zero_sized() {
        let b = SvgLine::new(pt(7.0, 8.0), pt(7.0, 8.0)).get_bounds();
        assert_eq!((b.x, b.y, b.width, b.height), (7.0, 8.0, 0.0, 0.0));
    }

    #[test]
    fn svgline_get_bounds_overflows_to_inf_width_without_panicking() {
        // max_x - min_x overflows f32 -> +inf rather than a wrapped/negative width
        let b = SvgLine::new(pt(-f32::MAX, 0.0), pt(f32::MAX, 1.0)).get_bounds();
        assert!(b.width.is_infinite() && b.width > 0.0);
        assert_eq!(b.x, -f32::MAX);
        assert_eq!(b.height, 1.0);
    }

    // ------------------------------------------------- SvgPathElement :: ctors

    #[test]
    fn svgpathelement_constructors_wrap_the_right_variant() {
        let l = SvgLine::new(pt(0.0, 0.0), pt(1.0, 1.0));
        assert!(matches!(SvgPathElement::line(l), SvgPathElement::Line(_)));
        assert!(matches!(
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::QuadraticCurve(_)
        ));
        assert!(matches!(
            SvgPathElement::cubic_curve(cubic()),
            SvgPathElement::CubicCurve(_)
        ));
    }

    #[test]
    fn svgpathelement_constructors_accept_extreme_geometry() {
        let l = SvgLine::new(pt(f32::NAN, f32::INFINITY), pt(f32::MIN, f32::MAX));
        let el = SvgPathElement::line(l);
        assert!(el.get_start().x.is_nan());
        assert_eq!(el.get_end().x, f32::MIN);
    }

    // ------------------------------------------- SvgPathElement :: set / get

    #[test]
    fn svgpathelement_set_first_last_round_trip_for_every_variant() {
        let a = pt(-1.0, -2.0);
        let b = pt(3.0, 4.0);

        for mut el in [
            SvgPathElement::line(SvgLine::new(pt(0.0, 0.0), pt(1.0, 1.0))),
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::cubic_curve(cubic()),
        ] {
            el.set_first(a);
            el.set_last(b);
            assert_eq!(el.get_start(), a);
            assert_eq!(el.get_end(), b);
        }
    }

    #[test]
    fn svgpathelement_set_first_last_accept_zero_min_max_and_nan() {
        let mut el = SvgPathElement::cubic_curve(cubic());

        el.set_first(pt(0.0, 0.0));
        el.set_last(pt(0.0, 0.0));
        assert_eq!(el.get_start(), pt(0.0, 0.0));
        assert_eq!(el.get_end(), pt(0.0, 0.0));

        el.set_first(pt(f32::MIN, f32::MIN));
        el.set_last(pt(f32::MAX, f32::MAX));
        assert_eq!(el.get_start().x, f32::MIN);
        assert_eq!(el.get_end().x, f32::MAX);

        el.set_first(pt(f32::NAN, f32::NEG_INFINITY));
        assert!(el.get_start().x.is_nan());
        assert!(el.get_start().y.is_infinite());
    }

    #[test]
    fn svgpathelement_reverse_is_an_involution_for_every_variant() {
        for el in [
            SvgPathElement::line(SvgLine::new(pt(0.0, 0.0), pt(1.0, 1.0))),
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::cubic_curve(cubic()),
        ] {
            let mut r = el;
            r.reverse();
            assert_eq!(r.get_start(), el.get_end());
            assert_eq!(r.get_end(), el.get_start());
            r.reverse();
            assert_eq!(r, el, "reverse() applied twice must be the identity");
        }
    }

    // --------------------------------------------- SvgPathElement :: numerics

    #[test]
    fn svgpathelement_get_length_is_non_negative_for_every_variant() {
        for el in [
            SvgPathElement::line(SvgLine::new(pt(0.0, 0.0), pt(3.0, 4.0))),
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::cubic_curve(cubic()),
        ] {
            let len = el.get_length();
            assert!(len.is_finite() && len >= 0.0, "bad length: {len}");
        }
    }

    #[test]
    fn svgpathelement_get_x_y_at_t_hit_endpoints_for_every_variant() {
        for el in [
            SvgPathElement::line(SvgLine::new(pt(0.0, 0.0), pt(10.0, 0.0))),
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::cubic_curve(cubic()),
        ] {
            assert!((el.get_x_at_t(0.0) - f64::from(el.get_start().x)).abs() < 1e-6);
            assert!((el.get_y_at_t(0.0) - f64::from(el.get_start().y)).abs() < 1e-6);
            assert!((el.get_x_at_t(1.0) - f64::from(el.get_end().x)).abs() < 1e-6);
            assert!((el.get_y_at_t(1.0) - f64::from(el.get_end().y)).abs() < 1e-6);
        }
    }

    #[test]
    fn svgpathelement_get_x_y_at_t_nan_inf_do_not_panic() {
        for el in [
            SvgPathElement::line(SvgLine::new(pt(0.0, 0.0), pt(10.0, 0.0))),
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::cubic_curve(cubic()),
        ] {
            assert!(el.get_x_at_t(f64::NAN).is_nan());
            assert!(el.get_y_at_t(f64::NAN).is_nan());
            // +-inf must not panic; any non-panicking value is acceptable here
            let _ = el.get_x_at_t(f64::INFINITY);
            let _ = el.get_y_at_t(f64::NEG_INFINITY);
            let _ = el.get_x_at_t(f64::MIN);
            let _ = el.get_y_at_t(f64::MAX);
        }
    }

    #[test]
    fn svgpathelement_line_tangent_ignores_t_even_when_t_is_nan() {
        // SvgLine has a constant tangent, so the `t` argument is discarded.
        let el = SvgPathElement::line(SvgLine::new(pt(0.0, 0.0), pt(0.0, 8.0)));
        let at_half = el.get_tangent_vector_at_t(0.5);
        assert_eq!(el.get_tangent_vector_at_t(f64::NAN), at_half);
        assert_eq!(el.get_tangent_vector_at_t(f64::INFINITY), at_half);
        assert_eq!(at_half, SvgVector { x: 0.0, y: 1.0 });
    }

    #[test]
    fn svgpathelement_curve_tangent_at_nan_is_nan_not_a_panic() {
        let el = SvgPathElement::quadratic_curve(quad());
        let v = el.get_tangent_vector_at_t(f64::NAN);
        assert!(v.x.is_nan() && v.y.is_nan());
    }

    #[test]
    fn svgpathelement_curve_tangent_is_unit_length_in_range() {
        for el in [
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::cubic_curve(cubic()),
        ] {
            for t in [0.0_f64, 0.25, 0.5, 0.75, 1.0] {
                let v = el.get_tangent_vector_at_t(t);
                let len = (v.x * v.x + v.y * v.y).sqrt();
                // either the zero vector (degenerate derivative) or unit length
                assert!(
                    len.abs() < 1e-9 || (len - 1.0).abs() < 1e-6,
                    "tangent at t={t} has length {len}"
                );
            }
        }
    }

    #[test]
    fn svgpathelement_curve_t_at_offset_saturates_at_1_past_the_end() {
        // the sampling loop never triggers for an out-of-range offset,
        // so it must fall through to the final t (== 1.0), not overshoot
        for el in [
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::cubic_curve(cubic()),
        ] {
            assert!((el.get_t_at_offset(f64::MAX) - 1.0).abs() < 1e-9);
            assert!((el.get_t_at_offset(1.0e30) - 1.0).abs() < 1e-9);
            // NaN never compares greater, so it also falls through
            assert!((el.get_t_at_offset(f64::NAN) - 1.0).abs() < 1e-9);
        }
    }

    #[test]
    fn svgpathelement_curve_t_at_offset_is_monotonic_and_in_range() {
        let el = SvgPathElement::cubic_curve(cubic());
        let len = el.get_length();
        let t_quarter = el.get_t_at_offset(len * 0.25);
        let t_half = el.get_t_at_offset(len * 0.5);
        assert!(t_quarter.is_finite() && t_half.is_finite());
        assert!((0.0..=1.0).contains(&t_quarter), "t={t_quarter}");
        assert!((0.0..=1.0).contains(&t_half), "t={t_half}");
        assert!(t_quarter <= t_half, "t must not decrease with offset");
    }

    #[test]
    fn svgpathelement_curve_t_at_offset_negative_offset_is_non_positive() {
        let el = SvgPathElement::cubic_curve(cubic());
        let t = el.get_t_at_offset(-10.0);
        assert!(t.is_finite(), "negative offset produced {t}");
        assert!(t <= 0.0, "negative offset must not map to a forward t: {t}");
    }

    #[test]
    fn svgpathelement_get_bounds_contains_both_endpoints() {
        for el in [
            SvgPathElement::line(SvgLine::new(pt(-3.0, 2.0), pt(9.0, -4.0))),
            SvgPathElement::quadratic_curve(quad()),
            SvgPathElement::cubic_curve(cubic()),
        ] {
            let b = el.get_bounds();
            let (s, e) = (el.get_start(), el.get_end());
            assert!(b.width >= 0.0 && b.height >= 0.0);
            assert!(s.x >= b.x && s.x <= b.x + b.width);
            assert!(e.x >= b.x && e.x <= b.x + b.width);
            assert!(s.y >= b.y && s.y <= b.y + b.height);
            assert!(e.y >= b.y && e.y <= b.y + b.height);
        }
    }

    // -------------------------------------------------------------- SvgPath

    #[test]
    fn svgpath_empty_getters_are_none_and_bounds_are_default() {
        let p = make_path(Vec::new());
        assert_eq!(p.get_start(), None);
        assert_eq!(p.get_end(), None);
        assert!(!p.is_closed(), "an empty path is not closed");
        assert_eq!(p.get_bounds(), SvgRect::default());
    }

    #[test]
    fn svgpath_start_and_end_come_from_first_and_last_element() {
        let p = make_path(vec![
            line_el(0.0, 0.0, 1.0, 1.0),
            line_el(1.0, 1.0, 5.0, 5.0),
            line_el(5.0, 5.0, 9.0, 2.0),
        ]);
        assert_eq!(p.get_start(), Some(pt(0.0, 0.0)));
        assert_eq!(p.get_end(), Some(pt(9.0, 2.0)));
    }

    #[test]
    fn svgpath_close_makes_is_closed_true_and_is_idempotent() {
        let mut p = make_path(vec![
            line_el(0.0, 0.0, 10.0, 0.0),
            line_el(10.0, 0.0, 10.0, 10.0),
        ]);
        assert!(!p.is_closed());
        p.close();
        assert!(p.is_closed(), "close() must establish is_closed()");
        assert_eq!(p.items.len(), 3);
        assert_eq!(p.get_end(), p.get_start());

        // closing an already-closed path must not append anything
        p.close();
        assert_eq!(p.items.len(), 3);
    }

    #[test]
    fn svgpath_close_on_empty_path_is_a_noop() {
        let mut p = make_path(Vec::new());
        p.close();
        assert_eq!(p.items.len(), 0);
        assert!(!p.is_closed());
    }

    #[test]
    fn svgpath_close_with_nan_coords_appends_once_and_does_not_panic() {
        // NaN != NaN, so the "already closed?" check can never be satisfied.
        // close() must still terminate and append exactly one element.
        let mut p = make_path(vec![line_el(f32::NAN, 0.0, 10.0, 0.0)]);
        p.close();
        assert_eq!(p.items.len(), 2);
        assert!(!p.is_closed(), "a NaN start point can never compare equal");
    }

    #[test]
    fn svgpath_is_closed_for_single_degenerate_element() {
        let p = make_path(vec![line_el(4.0, 4.0, 4.0, 4.0)]);
        assert!(p.is_closed(), "start == end for the only element");

        let open = make_path(vec![line_el(4.0, 4.0, 5.0, 4.0)]);
        assert!(!open.is_closed());
    }

    #[test]
    fn svgpath_reverse_is_an_involution_and_swaps_endpoints() {
        let orig = make_path(vec![
            line_el(0.0, 0.0, 1.0, 1.0),
            SvgPathElement::cubic_curve(cubic()),
            line_el(10.0, 0.0, 20.0, 5.0),
        ]);

        let mut p = orig.clone();
        p.reverse();
        assert_eq!(p.get_start(), orig.get_end());
        assert_eq!(p.get_end(), orig.get_start());
        assert_eq!(p.items.len(), orig.items.len());

        p.reverse();
        assert_eq!(p, orig, "reverse() applied twice must be the identity");
    }

    #[test]
    fn svgpath_reverse_on_empty_path_does_not_panic() {
        let mut p = make_path(Vec::new());
        p.reverse();
        assert_eq!(p.items.len(), 0);
    }

    #[test]
    fn svgpath_join_with_interpolates_the_join_point() {
        let mut a = make_path(vec![line_el(0.0, 0.0, 10.0, 0.0)]);
        let b = make_path(vec![line_el(20.0, 10.0, 30.0, 10.0)]);

        assert_eq!(a.join_with(b), Some(()));
        assert_eq!(a.items.len(), 2);

        // join point is the midpoint of (10,0) and (20,10)
        let mid = pt(15.0, 5.0);
        assert_eq!(a.items.as_ref()[0].get_end(), mid);
        assert_eq!(a.items.as_ref()[1].get_start(), mid);
        assert_eq!(a.get_start(), Some(pt(0.0, 0.0)));
        assert_eq!(a.get_end(), Some(pt(30.0, 10.0)));
    }

    #[test]
    fn svgpath_join_with_empty_other_returns_none_and_leaves_self_intact() {
        let mut a = make_path(vec![line_el(0.0, 0.0, 10.0, 0.0)]);
        let before = a.clone();
        assert_eq!(a.join_with(make_path(Vec::new())), None);
        assert_eq!(a, before, "a failed join must not corrupt the receiver");
    }

    #[test]
    fn svgpath_join_with_on_empty_self_returns_none_without_underflow() {
        // `vec.len() - 1` would underflow on an empty receiver; the `?` on
        // `last()` must short-circuit first.
        let mut a = make_path(Vec::new());
        let b = make_path(vec![line_el(0.0, 0.0, 1.0, 1.0)]);
        assert_eq!(a.join_with(b), None);
        assert_eq!(a.items.len(), 0);
    }

    #[test]
    fn svgpath_join_with_extreme_coords_does_not_panic() {
        let mut a = make_path(vec![line_el(0.0, 0.0, f32::MAX, f32::MAX)]);
        let b = make_path(vec![line_el(-f32::MAX, -f32::MAX, 0.0, 0.0)]);
        assert_eq!(a.join_with(b), Some(()));
        // midpoint of MAX and -MAX must not overflow to inf
        let join = a.items.as_ref()[0].get_end();
        assert!(join.x.is_finite() && join.y.is_finite(), "join: {join:?}");
    }

    #[test]
    fn svgpath_get_bounds_unions_every_element() {
        let p = make_path(vec![
            line_el(0.0, 0.0, 10.0, 0.0),
            line_el(10.0, 0.0, 10.0, 20.0),
            line_el(10.0, 20.0, -5.0, -8.0),
        ]);
        let b = p.get_bounds();
        assert_eq!(b.x, -5.0);
        assert_eq!(b.y, -8.0);
        assert_eq!(b.width, 15.0);
        assert_eq!(b.height, 28.0);

        for el in p.items.as_ref() {
            assert!(
                rect_contains(&b, &el.get_bounds()),
                "path bounds must contain every element's bounds"
            );
        }
    }

    // ------------------------------------------------------ SvgMultiPolygon

    #[test]
    fn svgmultipolygon_empty_has_default_bounds() {
        let mp = SvgMultiPolygon::create(SvgPathVec::from_vec(Vec::new()));
        assert_eq!(mp.get_bounds(), SvgRect::default());
    }

    #[test]
    fn svgmultipolygon_bounds_union_all_rings() {
        let mp = SvgMultiPolygon::create(SvgPathVec::from_vec(vec![
            make_path(vec![line_el(0.0, 0.0, 10.0, 10.0)]),
            make_path(vec![line_el(-4.0, 30.0, 2.0, 33.0)]),
        ]));
        let b = mp.get_bounds();
        assert_eq!(b.x, -4.0);
        assert_eq!(b.y, 0.0);
        assert_eq!(b.width, 14.0);
        assert_eq!(b.height, 33.0);
    }

    #[test]
    fn svgmultipolygon_bounds_must_contain_geometry_after_an_empty_first_ring() {
        // BUG: get_bounds() seeds from rings[0].items[0]; when the FIRST ring is
        // empty it bails out to SvgRect::default() and silently drops every
        // later ring's geometry.
        let mp = SvgMultiPolygon::create(SvgPathVec::from_vec(vec![
            make_path(Vec::new()),
            make_path(vec![line_el(100.0, 100.0, 200.0, 200.0)]),
        ]));
        let b = mp.get_bounds();
        let expected = SvgRect {
            width: 100.0,
            height: 100.0,
            x: 100.0,
            y: 100.0,
            ..SvgRect::default()
        };
        assert!(
            rect_contains(&b, &expected),
            "bounds {b:?} must contain the geometry of the non-empty ring {expected:?}"
        );
    }

    // ------------------------------------------------------- SvgSimpleNode

    #[test]
    fn svgsimplenode_is_closed_per_variant() {
        let circle = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: 5.0,
        };
        assert!(SvgSimpleNode::Circle(circle).is_closed());
        assert!(SvgSimpleNode::CircleHole(circle).is_closed());
        assert!(SvgSimpleNode::Rect(SvgRect::default()).is_closed());
        assert!(SvgSimpleNode::RectHole(SvgRect::default()).is_closed());

        assert!(!SvgSimpleNode::Path(make_path(vec![line_el(0.0, 0.0, 1.0, 0.0)])).is_closed());
        assert!(SvgSimpleNode::Path(make_path(vec![line_el(2.0, 2.0, 2.0, 2.0)])).is_closed());
        // an empty path is not a closed shape
        assert!(!SvgSimpleNode::Path(make_path(Vec::new())).is_closed());
    }

    #[test]
    fn svgsimplenode_get_bounds_per_variant() {
        let circle = SvgCircle {
            center_x: 10.0,
            center_y: 10.0,
            radius: 2.0,
        };
        let b = SvgSimpleNode::Circle(circle).get_bounds();
        assert_eq!((b.x, b.y, b.width, b.height), (8.0, 8.0, 4.0, 4.0));
        assert_eq!(SvgSimpleNode::CircleHole(circle).get_bounds(), b);

        let rect = SvgRect {
            width: 3.0,
            height: 4.0,
            x: 1.0,
            y: 2.0,
            ..SvgRect::default()
        };
        assert_eq!(SvgSimpleNode::Rect(rect).get_bounds(), rect);
        assert_eq!(SvgSimpleNode::RectHole(rect).get_bounds(), rect);

        // empty path -> default bounds, no panic
        assert_eq!(
            SvgSimpleNode::Path(make_path(Vec::new())).get_bounds(),
            SvgRect::default()
        );
    }

    // ------------------------------------------------------------- SvgNode

    #[test]
    fn svgnode_get_bounds_of_empty_collections_is_default() {
        assert_eq!(
            SvgNode::MultiPolygonCollection(SvgMultiPolygonVec::from_vec(Vec::new())).get_bounds(),
            SvgRect::default()
        );
        assert_eq!(
            SvgNode::MultiShape(SvgSimpleNodeVec::from_vec(Vec::new())).get_bounds(),
            SvgRect::default()
        );
        assert_eq!(
            SvgNode::Path(make_path(Vec::new())).get_bounds(),
            SvgRect::default()
        );
        assert_eq!(
            SvgNode::MultiPolygon(SvgMultiPolygon::create(SvgPathVec::from_vec(Vec::new())))
                .get_bounds(),
            SvgRect::default()
        );
    }

    #[test]
    fn svgnode_is_closed_is_vacuously_true_for_empty_collections() {
        assert!(SvgNode::MultiPolygonCollection(SvgMultiPolygonVec::from_vec(Vec::new())).is_closed());
        assert!(SvgNode::MultiShape(SvgSimpleNodeVec::from_vec(Vec::new())).is_closed());
        assert!(
            SvgNode::MultiPolygon(SvgMultiPolygon::create(SvgPathVec::from_vec(Vec::new())))
                .is_closed()
        );
        // ... but an empty *path* is still open
        assert!(!SvgNode::Path(make_path(Vec::new())).is_closed());
    }

    #[test]
    fn svgnode_is_closed_false_when_any_subpath_is_open() {
        let open = make_path(vec![line_el(0.0, 0.0, 1.0, 0.0)]);
        let mut closed = make_path(vec![
            line_el(0.0, 0.0, 1.0, 0.0),
            line_el(1.0, 0.0, 1.0, 1.0),
        ]);
        closed.close();

        let mp = SvgMultiPolygon::create(SvgPathVec::from_vec(vec![closed.clone(), open.clone()]));
        assert!(!SvgNode::MultiPolygon(mp.clone()).is_closed());
        assert!(!SvgNode::MultiPolygonCollection(SvgMultiPolygonVec::from_vec(vec![mp])).is_closed());
        assert!(!SvgNode::MultiShape(SvgSimpleNodeVec::from_vec(vec![
            SvgSimpleNode::Path(open),
            SvgSimpleNode::Circle(SvgCircle {
                center_x: 0.0,
                center_y: 0.0,
                radius: 1.0,
            }),
        ]))
        .is_closed());

        let all_closed = SvgMultiPolygon::create(SvgPathVec::from_vec(vec![closed]));
        assert!(SvgNode::MultiPolygon(all_closed).is_closed());
    }

    #[test]
    fn svgnode_get_bounds_contains_all_children() {
        let a = make_path(vec![line_el(0.0, 0.0, 10.0, 10.0)]);
        let b = make_path(vec![line_el(100.0, 100.0, 200.0, 200.0)]);
        let node = SvgNode::MultiShape(SvgSimpleNodeVec::from_vec(vec![
            SvgSimpleNode::Path(a.clone()),
            SvgSimpleNode::Path(b.clone()),
        ]));
        let bounds = node.get_bounds();
        assert!(rect_contains(&bounds, &a.get_bounds()));
        assert!(rect_contains(&bounds, &b.get_bounds()));
    }

    #[test]
    fn svgnode_rect_and_circle_bounds_are_passthrough() {
        let rect = SvgRect {
            width: 5.0,
            height: 6.0,
            x: -1.0,
            y: -2.0,
            ..SvgRect::default()
        };
        assert_eq!(SvgNode::Rect(rect).get_bounds(), rect);
        assert!(SvgNode::Rect(rect).is_closed());

        let circle = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: 1.0,
        };
        assert_eq!(SvgNode::Circle(circle).get_bounds(), circle.get_bounds());
        assert!(SvgNode::Circle(circle).is_closed());
    }

    // ----------------------------------------------------------- SvgCircle

    #[test]
    fn svgcircle_contains_point_is_strict_and_correct() {
        let c = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: 1.0,
        };
        assert!(c.contains_point(0.0, 0.0));
        assert!(c.contains_point(0.5, 0.5));
        // exactly on the perimeter -> NOT contained (strict `<`)
        assert!(!c.contains_point(1.0, 0.0));
        assert!(!c.contains_point(0.0, -1.0));
        assert!(!c.contains_point(2.0, 0.0));
        assert!(!c.contains_point(-0.8, -0.8));
    }

    #[test]
    fn svgcircle_zero_radius_contains_nothing_not_even_its_center() {
        let c = SvgCircle {
            center_x: 3.0,
            center_y: 3.0,
            radius: 0.0,
        };
        assert!(!c.contains_point(3.0, 3.0));
        assert!(!c.contains_point(0.0, 0.0));
    }

    #[test]
    fn svgcircle_negative_radius_behaves_like_its_absolute_value() {
        // r*r discards the sign, so a negative radius is NOT treated as empty.
        // Documented here so a future fix has to update this test deliberately.
        let neg = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: -2.0,
        };
        let pos = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: 2.0,
        };
        assert_eq!(neg.contains_point(0.0, 0.0), pos.contains_point(0.0, 0.0));
        assert_eq!(neg.contains_point(1.9, 0.0), pos.contains_point(1.9, 0.0));
        assert_eq!(neg.contains_point(5.0, 0.0), pos.contains_point(5.0, 0.0));
    }

    #[test]
    fn svgcircle_contains_point_nan_and_inf_are_false_not_a_panic() {
        let c = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: 1.0,
        };
        // every comparison against NaN is false
        assert!(!c.contains_point(f32::NAN, 0.0));
        assert!(!c.contains_point(0.0, f32::NAN));
        assert!(!c.contains_point(f32::INFINITY, 0.0));
        assert!(!c.contains_point(f32::NEG_INFINITY, f32::NEG_INFINITY));

        let nan_r = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: f32::NAN,
        };
        assert!(!nan_r.contains_point(0.0, 0.0));
    }

    #[test]
    fn svgcircle_contains_point_at_f32_extremes_does_not_panic() {
        let c = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: f32::MAX,
        };
        // x_diff*x_diff overflows to +inf, which is never < radius^2 (+inf)
        assert!(!c.contains_point(f32::MAX, f32::MAX));
        assert!(c.contains_point(1.0, 1.0));
        assert!(!c.contains_point(f32::MIN, 0.0));
    }

    #[test]
    fn svgcircle_get_bounds_is_the_enclosing_square() {
        let c = SvgCircle {
            center_x: 5.0,
            center_y: -5.0,
            radius: 2.5,
        };
        let b = c.get_bounds();
        assert_eq!((b.x, b.y, b.width, b.height), (2.5, -7.5, 5.0, 5.0));
        // A bbox CORNER is outside the inscribed circle. (Mid-edge is not: the
        // previous probe sat on the horizontal diameter, 2.4 from the centre, i.e.
        // strictly inside the radius-2.5 circle.)
        assert!(!c.contains_point(b.x + 0.1, b.y + 0.1));
        assert!(c.contains_point(c.center_x, c.center_y));
    }

    #[test]
    fn svgcircle_get_bounds_with_nan_and_huge_radius_does_not_panic() {
        let nan = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: f32::NAN,
        };
        let b = nan.get_bounds();
        assert!(b.width.is_nan() && b.x.is_nan());

        let huge = SvgCircle {
            center_x: 0.0,
            center_y: 0.0,
            radius: f32::MAX,
        };
        let b = huge.get_bounds();
        // radius * 2.0 overflows f32 -> +inf (saturates, does not wrap negative)
        assert!(b.width.is_infinite() && b.width > 0.0);
        assert_eq!(b.x, -f32::MAX);
    }

    // ------------------------------------------------ Tessellated* wrappers

    #[test]
    fn tessellated_svg_node_empty_is_a_neutral_value() {
        let e = TessellatedSvgNode::empty();
        assert!(e.vertices.as_ref().is_empty());
        assert!(e.indices.as_ref().is_empty());
        assert_eq!(e, TessellatedSvgNode::default());
    }

    #[test]
    fn tessellated_colored_svg_node_empty_is_a_neutral_value() {
        let e = TessellatedColoredSvgNode::empty();
        assert!(e.vertices.as_ref().is_empty());
        assert!(e.indices.as_ref().is_empty());
        assert_eq!(e, TessellatedColoredSvgNode::default());
    }

    #[test]
    fn tessellated_svg_node_vec_ref_round_trips_through_as_slice() {
        let node = TessellatedSvgNode {
            vertices: vec![SvgVertex { x: 1.0, y: 2.0 }].into(),
            indices: vec![0_u32].into(),
        };
        let v = TessellatedSvgNodeVec::from_vec(vec![node.clone(), TessellatedSvgNode::empty()]);
        let r = v.get_ref();
        assert_eq!(r.len, 2);
        let slice = r.as_slice();
        assert_eq!(slice.len(), 2);
        assert_eq!(slice[0], node);
        assert_eq!(slice[1], TessellatedSvgNode::empty());
    }

    #[test]
    fn tessellated_svg_node_vec_ref_on_empty_vec_yields_an_empty_slice() {
        // as_slice() calls slice::from_raw_parts - an empty vec must still
        // produce a valid (dangling but aligned) pointer, never a null deref.
        let v = TessellatedSvgNodeVec::from_vec(Vec::new());
        let r = v.get_ref();
        assert_eq!(r.len, 0);
        assert!(r.as_slice().is_empty());
        assert!(!r.ptr.is_null(), "raw ptr must never be null");
    }

    #[test]
    fn tessellated_colored_svg_node_vec_ref_round_trips_through_as_slice() {
        let node = TessellatedColoredSvgNode {
            vertices: vec![SvgColoredVertex {
                x: 1.0,
                y: 2.0,
                z: 3.0,
                r: 1.0,
                g: 0.5,
                b: 0.25,
                a: 1.0,
            }]
            .into(),
            indices: vec![0_u32, 1, 2].into(),
        };
        let v = TessellatedColoredSvgNodeVec::from_vec(vec![node.clone()]);
        let r = v.get_ref();
        assert_eq!(r.len, 1);
        assert_eq!(r.as_slice().len(), 1);
        assert_eq!(r.as_slice()[0], node);
    }

    #[test]
    fn tessellated_colored_svg_node_vec_ref_on_empty_vec_yields_an_empty_slice() {
        let v = TessellatedColoredSvgNodeVec::from_vec(Vec::new());
        let r = v.get_ref();
        assert_eq!(r.len, 0);
        assert!(r.as_slice().is_empty());
        assert!(!r.ptr.is_null(), "raw ptr must never be null");
    }

    // ------------------------------------- compute_svg_transform_uniforms

    fn matrix_of(u: &Uniform) -> [f32; 16] {
        match u.uniform_type {
            UniformType::Matrix4 { transpose, matrix } => {
                assert!(!transpose, "SVG shaders expect a pre-transposed matrix");
                matrix
            }
            _ => panic!("expected a Matrix4 uniform"),
        }
    }

    fn bbox_of(u: &Uniform) -> [f32; 2] {
        match u.uniform_type {
            UniformType::FloatVec2(v) => v,
            _ => panic!("expected a FloatVec2 uniform"),
        }
    }

    #[test]
    fn compute_svg_transform_uniforms_zero_size_no_transforms_is_finite() {
        let (bbox, tf) = compute_svg_transform_uniforms(
            PhysicalSizeU32 {
                width: 0,
                height: 0,
            },
            &[],
        );
        assert_eq!(bbox.uniform_name.as_str(), "vBboxSize");
        assert_eq!(bbox_of(&bbox), [0.0, 0.0]);

        assert_eq!(tf.uniform_name.as_str(), "vTransformMatrix");
        let m = matrix_of(&tf);
        assert!(
            m.iter().all(|f| f.is_finite()),
            "a zero-sized target must not produce NaN/inf in the matrix: {m:?}"
        );
    }

    #[test]
    fn compute_svg_transform_uniforms_at_u32_max_does_not_panic() {
        let (bbox, tf) = compute_svg_transform_uniforms(
            PhysicalSizeU32 {
                width: u32::MAX,
                height: u32::MAX,
            },
            &[],
        );
        let b = bbox_of(&bbox);
        assert!(b[0].is_finite() && b[0] > 0.0);
        assert_eq!(b[0], u32::MAX as f32);
        assert_eq!(b[1], u32::MAX as f32);
        let m = matrix_of(&tf);
        assert!(m.iter().all(|f| f.is_finite()), "matrix: {m:?}");
    }

    #[test]
    fn compute_svg_transform_uniforms_translation_changes_the_matrix() {
        let size = PhysicalSizeU32 {
            width: 800,
            height: 600,
        };
        let identity = matrix_of(&compute_svg_transform_uniforms(size, &[]).1);
        let translated = matrix_of(
            &compute_svg_transform_uniforms(
                size,
                &[
                    StyleTransform::TranslateX(PixelValue::px(10.0)),
                    StyleTransform::TranslateY(PixelValue::px(-20.0)),
                ],
            )
            .1,
        );
        assert!(
            translated.iter().all(|f| f.is_finite()),
            "matrix: {translated:?}"
        );
        assert!(
            identity != translated,
            "a translation must actually alter the transform matrix"
        );
    }

    #[test]
    fn compute_svg_transform_uniforms_extreme_translation_does_not_panic() {
        let size = PhysicalSizeU32 {
            width: 1,
            height: 1,
        };
        let (bbox, tf) = compute_svg_transform_uniforms(
            size,
            &[
                StyleTransform::TranslateX(PixelValue::px(f32::MAX)),
                StyleTransform::TranslateY(PixelValue::px(-f32::MAX)),
            ],
        );
        assert_eq!(bbox_of(&bbox), [1.0, 1.0]);
        // no assertion on finiteness here: the transform itself is degenerate,
        // we only require that building the uniform does not panic
        let _ = matrix_of(&tf);
    }

    // ------------------------------------------------------------ SvgStyle

    #[test]
    fn svgstyle_getters_read_through_to_both_variants() {
        let fill = SvgStyle::Fill(SvgFillStyle::default());
        assert!(fill.get_antialias());
        assert!(!fill.get_high_quality_aa());
        assert_eq!(fill.get_transform(), SvgTransform::default());

        let stroke = SvgStyle::Stroke(SvgStrokeStyle::default());
        assert!(stroke.get_antialias());
        assert!(!stroke.get_high_quality_aa());
        assert_eq!(stroke.get_transform(), SvgTransform::default());
    }

    #[test]
    fn svgstyle_getters_reflect_non_default_fields() {
        let transform = SvgTransform {
            sx: 2.0,
            kx: 0.5,
            ky: -0.5,
            sy: 3.0,
            tx: 10.0,
            ty: -10.0,
        };
        let fill = SvgStyle::Fill(SvgFillStyle {
            anti_alias: false,
            high_quality_aa: true,
            transform,
            ..SvgFillStyle::default()
        });
        assert!(!fill.get_antialias());
        assert!(fill.get_high_quality_aa());
        assert_eq!(fill.get_transform(), transform);

        let stroke = SvgStyle::Stroke(SvgStrokeStyle {
            anti_alias: false,
            high_quality_aa: true,
            transform,
            ..SvgStrokeStyle::default()
        });
        assert!(!stroke.get_antialias());
        assert!(stroke.get_high_quality_aa());
        assert_eq!(stroke.get_transform(), transform);
    }

    // ------------------------------------------------------ SvgParseError

    #[test]
    fn svgparseerror_display_is_non_empty_for_every_variant() {
        let variants = [
            SvgParseError::NoParserAvailable,
            SvgParseError::ElementsLimitReached,
            SvgParseError::NotAnUtf8Str,
            SvgParseError::MalformedGZip,
            SvgParseError::InvalidSize,
            SvgParseError::ParsingFailed(XmlError::NoRootNode),
        ];

        for v in &variants {
            let s = v.to_string();
            assert!(!s.is_empty(), "empty Display output for {v:?}");
            assert!(
                !s.contains("\u{0}"),
                "Display output must not contain NUL bytes"
            );
        }
    }

    #[test]
    fn svgparseerror_display_of_parsing_failed_embeds_the_inner_error() {
        let inner = XmlError::NoRootNode;
        let s = SvgParseError::ParsingFailed(inner.clone()).to_string();
        assert!(s.starts_with("Error parsing SVG:"), "got: {s}");
        assert!(
            s.contains(&inner.to_string()),
            "outer message {s:?} must embed the inner XmlError message"
        );
    }
}