deckcraft-render 0.6.1

DeckCraft renderer: slides, layouts and masters to pixels (vello_cpu): fills, outlines, effects, pictures, text, tables, charts
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
//! The DeckCraft renderer: draws slides, layouts and masters with `vello_cpu`.
//!
//! One code path serves the editor canvas, thumbnails, image/PDF-raster export and the slide show:
//! [`render_slide`] draws the background, the master and layout graphics, then the slide's shapes
//! (fills with theme shading, gradients, pictures, outlines with dashes and arrowheads, shadows,
//! glow and soft edges, text, tables, charts, groups), optionally with per-shape animation state.
#![deny(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::unimplemented,
    clippy::todo,
    clippy::unreachable
)]
#![forbid(unsafe_code)]

mod chart;
mod colr;

mod compound;
mod extent;
mod images;
mod metafile;
mod morph_path;
mod morph_text;
mod paint;
mod pattern;

mod pathgrad;
mod placed;
mod ramp;
mod table;
mod warp;

use std::collections::HashMap;
use std::sync::Arc;

use deckcraft_color::Rgba;
use deckcraft_fonts::FontDb;
use deckcraft_geom::preset::{self, FillMode, Geometry, SubPath};
use deckcraft_model::resolve::{self, Ctx};
use deckcraft_model::style::{Effects, Fill, Line};
use deckcraft_model::text::TextBody;
use deckcraft_model::{Geom, PhType, Presentation, Shape, ShapeId, ShapeKind, Slide};
use deckcraft_text::datetime::{self, DateTime};
use deckcraft_text::{Fields, Opts};
use kurbo::{Affine, BezPath, PathEl, Point, Rect, Shape as _, Vec2};
use vello_common::filter_effects::{EdgeMode, Filter, FilterPrimitive};
use vello_cpu::{RenderContext, Resources, peniko};

pub use extent::{Region, TILE, layout_extent, pasteboard_region, slide_extent};
pub use images::{decode as decode_image, gif_frame};
pub use morph_path::PathMorph;
pub use morph_text::TextMorph;
pub use placed::{Placed, PlacedLink, PlacedText, place_slide};
pub use table::row_heights as table_row_heights;

/// Largest pixmap side we render (vello_cpu uses u16 sizes).
pub const MAX_SIDE: u32 = 16_000;

/// A rendered image: premultiplied RGBA8.
#[derive(Clone, Debug, Default)]
pub struct Image {
    pub width: u32,
    pub height: u32,
    pub pixels: Vec<u8>,
}

impl Image {
    /// Straight (unpremultiplied) RGBA.
    pub fn to_straight(&self) -> Vec<u8> {
        let mut out = self.pixels.clone();
        for px in out.as_chunks_mut::<4>().0 {
            let a = px.get(3).copied().unwrap_or(255) as u32;
            if a != 0 && a != 255 {
                for c in px.iter_mut().take(3) {
                    *c = ((*c as u32 * 255 + a / 2) / a).min(255) as u8;
                }
            }
        }
        out
    }
    pub fn to_png(&self) -> Vec<u8> {
        let mut px = self.to_straight();
        px.resize(self.width as usize * self.height as usize * 4, 0);
        let Some(img) = image::RgbaImage::from_raw(self.width, self.height, px) else {
            return Vec::new();
        };
        let mut buf = Vec::new();
        if let Err(e) = img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png) {
            log::error!("PNG encode: {e}");
            return Vec::new();
        }
        buf
    }
    pub fn to_jpeg(&self, quality: u8) -> Vec<u8> {
        let straight = self.to_straight();
        let rgb: Vec<u8> = straight
            .as_chunks::<4>()
            .0
            .iter()
            .flat_map(|p| [p[0], p[1], p[2]])
            .collect();
        let Some(img) = image::RgbImage::from_raw(self.width, self.height, rgb) else {
            return Vec::new();
        };
        let mut buf = Vec::new();
        let enc =
            image::codecs::jpeg::JpegEncoder::new_with_quality(&mut buf, quality.clamp(1, 100));
        if let Err(e) = img.write_with_encoder(enc) {
            log::error!("JPEG encode: {e}");
            return Vec::new();
        }
        buf
    }
    /// Pixel at (x, y) as straight RGBA.
    pub fn pixel(&self, x: u32, y: u32) -> [u8; 4] {
        let i = (y as usize * self.width as usize + x as usize) * 4;
        let p = self.pixels.get(i..i + 4).unwrap_or(&[0, 0, 0, 0]);
        let a = p.get(3).copied().unwrap_or(0) as u32;
        let un = |c: u8| {
            (c as u32 * 255 + a / 2)
                .checked_div(a)
                .map(|v| v.min(255) as u8)
                .unwrap_or(0)
        };
        [un(p[0]), un(p[1]), un(p[2]), a as u8]
    }
}

/// Animation state of one shape during a slide show or preview.
#[derive(Clone, Debug)]
pub struct ShapeState {
    pub visible: bool,
    pub opacity: f64,
    /// Applied in slide space around the shape's centre: translation (pt), scale, rotation (deg).
    pub offset: Vec2,
    pub scale: (f64, f64),
    pub rotate: f64,
    /// Reveal clip in shape-local unit space (0..1): l, t, r, b.
    pub clip: Option<[f64; 4]>,
    /// Colour override for emphasis effects (fill).
    pub tint: Option<Rgba>,
    /// Per-paragraph states of by-paragraph text builds (paragraph index, state).
    pub paras: Vec<(usize, ParaState)>,
}

impl Default for ShapeState {
    fn default() -> Self {
        ShapeState {
            visible: true,
            opacity: 1.0,
            offset: Vec2::ZERO,
            scale: (1.0, 1.0),
            rotate: 0.0,
            clip: None,
            tint: None,
            paras: vec![],
        }
    }
}

/// Animation state of one paragraph, relative to its shape.
#[derive(Clone, Copy, Debug)]
pub struct ParaState {
    pub visible: bool,
    pub opacity: f64,
    /// Translation in shape-local points.
    pub offset: Vec2,
}

impl Default for ParaState {
    fn default() -> Self {
        ParaState {
            visible: true,
            opacity: 1.0,
            offset: Vec2::ZERO,
        }
    }
}

pub struct RenderOpts<'a> {
    /// Pixels per point.
    pub scale: f64,
    /// Editor view: placeholder prompts and dashed placeholder outlines.
    pub edit: bool,
    /// Draw shapes marked hidden (Selection pane) — the editor hides them like the show does.
    pub show_hidden: bool,
    /// Per-shape animation state.
    pub state: Option<&'a dyn Fn(ShapeId) -> ShapeState>,
    /// Don't draw these shapes (being edited in place, drawn by the UI).
    pub skip: &'a [ShapeId],
    /// Hide the text of this shape (the UI draws it while editing).
    pub skip_text: Option<ShapeId>,
    /// Animated GIF pictures and how far into their play they are (seconds); others show frame 1.
    pub gif_times: &'a [(ShapeId, f64)],
    /// Worker threads (0 = this thread; required when effects use filters).
    pub threads: u16,
    /// Translate the slide within the output (pixels) and output size override.
    pub offset: (f64, f64),
    pub size: Option<(u32, u32)>,
    /// Draw a background colour outside the slide (output larger than slide).
    pub clear: Option<Rgba>,
    /// Show this text in every date field instead of the date.
    pub date_text: Option<String>,
    /// The local date and time date fields show (`None`: the system clock).
    pub now: Option<DateTime>,
}

impl Default for RenderOpts<'_> {
    fn default() -> Self {
        RenderOpts {
            scale: 1.0,
            edit: false,
            show_hidden: false,
            state: None,
            skip: &[],
            skip_text: None,
            gif_times: &[],
            threads: 0,
            offset: (0.0, 0.0),
            size: None,
            clear: None,
            date_text: None,
            now: None,
        }
    }
}

struct SlideFields {
    num: u32,
    /// Text for every date field instead of the date ([`RenderOpts::date_text`]).
    date: Option<String>,
    footer: String,
    /// The local date and time date fields show; `None`: they keep their saved text.
    now: Option<DateTime>,
}

impl SlideFields {
    fn new(num: u32, footer: String, opts: &RenderOpts) -> Self {
        SlideFields {
            num,
            date: opts.date_text.clone(),
            footer,
            now: opts.now.or_else(DateTime::now_local),
        }
    }
}

impl Fields for SlideFields {
    fn field(&self, kind: &str) -> Option<String> {
        self.field_in(kind, None)
    }

    fn field_in(&self, kind: &str, lang: Option<&str>) -> Option<String> {
        if kind == "slidenum" {
            return Some(self.num.to_string());
        }
        if datetime::is_date_field(kind) {
            // An automatic date: today's, in the format of its type. (A fixed date is plain text
            // in the slide's placeholder, not a field.)
            return self
                .date
                .clone()
                .or_else(|| datetime::field_text(kind, lang, self.now?));
        }
        if kind == "footer" {
            return Some(self.footer.clone());
        }
        None
    }
}

pub struct Renderer {
    resources: Resources,
    /// Outlines morphing in the frame being drawn by `blend`.
    paths: Vec<PathMorph>,
    /// The background of the slide (layout, master) being drawn, its placeholder colour and the
    /// slide's view transform: shapes filled with the background (`useBgFill`) show it through.
    bg: Option<(Fill, Option<Rgba>, Affine)>,
    /// What the frame being drawn has painted so far, in output pixels: the editor learns from it
    /// how far a slide's drawing (text spilling out of its box too) reaches off the slide.
    drawn: Option<Rect>,
    /// The transform a top-level shape is drawn under (slide to pixels); a group's children draw
    /// under it combined with the group's child-coordinate mapping, which must not scale lines.
    view: Affine,
}

impl Default for Renderer {
    fn default() -> Self {
        Renderer {
            resources: Resources::new(),
            paths: Vec::new(),
            bg: None,
            drawn: None,
            view: Affine::IDENTITY,
        }
    }
}

thread_local! {
    static RENDERER: std::cell::RefCell<Renderer> = std::cell::RefCell::new(Renderer::default());
}

/// Does anything on this slide (or its layout/master graphics) use blur-based effects?
pub fn needs_filters(pres: &Presentation, slide: &Slide) -> bool {
    let Some(rctx) = Ctx::for_slide(pres, slide) else {
        return false;
    };
    let mut any = false;
    let mut check = |shapes: &[Shape]| {
        deckcraft_model::walk(shapes, &mut |s, _| {
            if any {
                return;
            }
            let (e, _) = resolve::effects(&rctx, s);
            if e.is_some_and(|e| !e.is_empty()) || text_has_effects(&rctx, s) {
                any = true;
            }
        });
    };
    check(&slide.shapes);
    if let Some(l) = rctx.layout {
        check(&l.shapes);
    }
    check(&rctx.master.shapes);
    any
}

/// Does the shape's text have a shadow or glow (on a run, in its list style, or in the master's
/// text styles it inherits)? Such text is blurred, which needs filters.
fn text_has_effects(rctx: &Ctx, s: &Shape) -> bool {
    use deckcraft_model::text::{ListStyle, RunProps};
    let fx = |r: &RunProps| {
        r.shadow.as_ref().is_some_and(|s| !s.inner)
            || r.glow.as_ref().is_some_and(|g| g.radius > 0.0)
    };
    let styled = |l: &ListStyle| l.levels.iter().flatten().any(|lv| fx(&lv.run));
    let Some(t) = &s.text else { return false };
    t.paragraphs
        .iter()
        .any(|p| p.runs.iter().any(|r| fx(&r.props)) || fx(&p.end_props))
        || styled(&t.list_style)
        || styled(&rctx.master.title_style)
        || styled(&rctx.master.body_style)
        || styled(&rctx.master.other_style)
}

/// Render slide `index` at `opts.scale` pixels per point. A panic inside the renderer becomes a
/// blank image (logged), never a crash.
pub fn render_slide(pres: &Presentation, index: usize, opts: &RenderOpts) -> Image {
    render_slide_drawn(pres, index, opts).0
}

/// [`render_slide`], also returning how far the drawing reached, in slide space (at least the
/// slide; off it, what the shapes, their effects and their text drew, the parts outside the output
/// included). The editor grows its pasteboard to it ([`pasteboard_region`]).
pub fn render_slide_drawn(pres: &Presentation, index: usize, opts: &RenderOpts) -> (Image, Rect) {
    let page = Rect::new(0.0, 0.0, pres.slide_size.width, pres.slide_size.height);
    let Some(slide) = pres.slides.get(index) else {
        return (Image::default(), page);
    };
    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        RENDERER.with(|r| {
            let mut r = r.borrow_mut();
            r.drawn = None;
            let img = r.slide(pres, slide, index, opts);
            (img, r.drawn.take())
        })
    })) {
        Ok((img, drawn)) => {
            let k = opts.scale;
            let to_slide = |d: Rect| {
                Rect::new(
                    (d.x0 - opts.offset.0) / k,
                    (d.y0 - opts.offset.1) / k,
                    (d.x1 - opts.offset.0) / k,
                    (d.y1 - opts.offset.1) / k,
                )
            };
            let extent = match drawn {
                Some(d) if k.is_finite() && k > 0.0 && to_slide(d).is_finite() => {
                    page.union(to_slide(d))
                }
                _ => page,
            };
            (img, extent)
        }
        Err(_) => {
            log::error!("rendering slide {index} panicked; showing a blank slide");
            RENDERER.with(|r| *r.borrow_mut() = Renderer::default());
            let (w, h) = output_size(pres, opts);
            (
                Image {
                    width: w as u32,
                    height: h as u32,
                    pixels: vec![255; w as usize * h as usize * 4],
                },
                page,
            )
        }
    }
}

/// Render a frame between slides `old` and `new` (indices into `pres.slides`), e.g. of Morph: the
/// backdrop (background, master and layout graphics) of `old` cross-fading into that of `new` by
/// `mix` (0..1), then `shapes` in order, each resolved against the slide it comes from (`true`:
/// `old`). Pairs of shapes in `text` draw their boxes as usual but their texts morph by words or
/// characters ([`TextMorph`]), on top of the later shape of the pair. Shapes listed in `paths` draw
/// an outline morphing from another geometry into their own ([`PathMorph`]). `opts.state` applies
/// to `shapes` only.
pub fn render_blend(
    pres: &Presentation,
    old: usize,
    new: usize,
    mix: f64,
    shapes: &[(Shape, bool)],
    text: &[TextMorph],
    paths: &[PathMorph],
    opts: &RenderOpts,
) -> Image {
    let (Some(a), Some(b)) = (pres.slides.get(old), pres.slides.get(new)) else {
        return Image::default();
    };
    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        RENDERER.with(|r| {
            r.borrow_mut()
                .blend(pres, (a, old), (b, new), mix, shapes, text, paths, opts)
        })
    })) {
        Ok(img) => img,
        Err(_) => {
            log::error!(
                "rendering a frame between slides {old} and {new} panicked; showing the new slide"
            );
            RENDERER.with(|r| *r.borrow_mut() = Renderer::default());
            render_slide(pres, new, opts)
        }
    }
}

/// Render a layout (Slide Master view thumbnail and canvas).
pub fn render_layout(
    pres: &Presentation,
    master: usize,
    layout: Option<usize>,
    opts: &RenderOpts,
) -> Image {
    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        RENDERER.with(|r| r.borrow_mut().layout(pres, master, layout, opts))
    })) {
        Ok(img) => img,
        Err(_) => {
            log::error!("rendering a layout panicked; showing a blank image");
            RENDERER.with(|r| *r.borrow_mut() = Renderer::default());
            Image::default()
        }
    }
}

fn output_size(pres: &Presentation, opts: &RenderOpts) -> (u16, u16) {
    let (w, h) = opts.size.unwrap_or((
        (pres.slide_size.width * opts.scale).round().max(1.0) as u32,
        (pres.slide_size.height * opts.scale).round().max(1.0) as u32,
    ));
    (w.clamp(1, MAX_SIDE) as u16, h.clamp(1, MAX_SIDE) as u16)
}

pub(crate) fn color(c: Rgba, alpha: f64) -> peniko::Color {
    let a = (c.a as f64 * alpha.clamp(0.0, 1.0))
        .round()
        .clamp(0.0, 255.0) as u8;
    peniko::Color::from_rgba8(c.r, c.g, c.b, a)
}

fn shade(c: Rgba, m: FillMode) -> Rgba {
    match m {
        FillMode::Lighten => c.lerp(Rgba::WHITE.with_alpha(c.a), 0.4),
        FillMode::LightenLess => c.lerp(Rgba::WHITE.with_alpha(c.a), 0.2),
        FillMode::Darken => c.lerp(Rgba::BLACK.with_alpha(c.a), 0.4),
        FillMode::DarkenLess => c.lerp(Rgba::BLACK.with_alpha(c.a), 0.2),
        _ => c,
    }
}

/// Geometry of a shape at its size.
pub fn shape_geometry(shape: &Shape, w: f64, h: f64) -> Geometry {
    match &shape.geom {
        Geom::Preset { name, adj } => preset::build(name, w, h, adj)
            .or_else(|| preset::build("rect", w, h, &[]))
            .unwrap_or(Geometry {
                paths: vec![],
                text_rect: Rect::new(0.0, 0.0, w, h),
                handles: vec![],
                sites: vec![],
            }),
        Geom::Custom { paths } => {
            let mut out = vec![];
            for p in paths {
                let (pw, ph) = (
                    if p.w > 0.0 { p.w } else { w.max(1e-9) },
                    if p.h > 0.0 { p.h } else { h.max(1e-9) },
                );
                let mut bp = parse_path(&p.d);
                bp.apply_affine(Affine::scale_non_uniform(w / pw, h / ph));
                out.push(SubPath {
                    path: bp,
                    fill: p.fill,
                    stroke: p.stroke,
                    even_odd: false,
                });
            }
            Geometry {
                paths: out,
                text_rect: Rect::new(0.0, 0.0, w, h),
                handles: vec![],
                sites: vec![],
            }
        }
    }
}

/// Parse our custom path syntax (`M x y L x y C … Q … Z`).
pub fn parse_path(d: &str) -> BezPath {
    let mut bp = BezPath::new();
    let toks: Vec<&str> = d
        .split(|c: char| c.is_whitespace() || c == ',')
        .filter(|t| !t.is_empty())
        .collect();
    let mut i = 0;
    let num = |i: usize| {
        toks.get(i)
            .and_then(|t| t.parse::<f64>().ok())
            .filter(|v| v.is_finite())
    };
    let mut started = false;
    while i < toks.len() {
        let t = toks.get(i).copied().unwrap_or("");
        match t {
            "M" => {
                if let (Some(x), Some(y)) = (num(i + 1), num(i + 2)) {
                    bp.move_to((x, y));
                    started = true;
                }
                i += 3;
            }
            "L" => {
                if let (Some(x), Some(y)) = (num(i + 1), num(i + 2)) {
                    if !started {
                        bp.move_to((x, y));
                        started = true;
                    } else {
                        bp.line_to((x, y));
                    }
                }
                i += 3;
            }
            "C" => {
                if let (Some(a), Some(b), Some(c), Some(d2), Some(e), Some(f)) = (
                    num(i + 1),
                    num(i + 2),
                    num(i + 3),
                    num(i + 4),
                    num(i + 5),
                    num(i + 6),
                ) && started
                {
                    bp.curve_to((a, b), (c, d2), (e, f));
                }
                i += 7;
            }
            "Q" => {
                if let (Some(a), Some(b), Some(c), Some(d2)) =
                    (num(i + 1), num(i + 2), num(i + 3), num(i + 4))
                    && started
                {
                    bp.quad_to((a, b), (c, d2));
                }
                i += 5;
            }
            "Z" | "z" => {
                if started {
                    bp.close_path();
                }
                i += 1;
            }
            _ => i += 1,
        }
    }
    bp
}

/// The rectangle a picture's image occupies given its crop (fractions; negative = padding).
pub fn crop_dest(bounds: Rect, crop: [f64; 4]) -> Rect {
    let [l, t, r, b] = crop.map(|v| {
        if v.is_finite() {
            v.clamp(-10.0, 0.99)
        } else {
            0.0
        }
    });
    let vis_w = (1.0 - l - r).max(1e-6);
    let vis_h = (1.0 - t - b).max(1e-6);
    let fw = bounds.width() / vis_w;
    let fh = bounds.height() / vis_h;
    Rect::new(
        bounds.x0 - l * fw,
        bounds.y0 - t * fh,
        bounds.x0 - l * fw + fw,
        bounds.y0 - t * fh + fh,
    )
}

struct Frame<'a> {
    pres: &'a Presentation,
    opts: &'a RenderOpts<'a>,
    fields: SlideFields,
}

impl Renderer {
    fn canvas(
        pres: &Presentation,
        opts: &RenderOpts,
        filters: bool,
    ) -> (RenderContext, u16, u16, Affine) {
        let (w, h) = output_size(pres, opts);
        let mut ctx = RenderContext::new_with(
            w,
            h,
            vello_cpu::RenderSettings {
                num_threads: if cfg!(target_arch = "wasm32") || filters {
                    0
                } else {
                    opts.threads
                },
                ..Default::default()
            },
        );
        if let Some(c) = opts.clear {
            ctx.set_paint(color(c, 1.0));
            ctx.fill_rect(&Rect::new(0.0, 0.0, w as f64, h as f64));
        }
        let view = Affine::translate((opts.offset.0, opts.offset.1)) * Affine::scale(opts.scale);
        (ctx, w, h, view)
    }

    /// Note that `r` (local space, placed by `m`) was drawn.
    fn track(&mut self, m: Affine, r: Rect) {
        let b = m.transform_rect_bbox(r);
        if b.is_finite() {
            self.drawn = Some(self.drawn.map_or(b, |d| d.union(b)));
        }
    }

    fn fields(pres: &Presentation, index: usize, opts: &RenderOpts) -> SlideFields {
        SlideFields::new(
            pres.slide_number(index),
            pres.header_footer.footer_text.clone(),
            opts,
        )
    }

    /// Background plus the master and layout graphics the slide shows.
    fn backdrop(
        &mut self,
        ctx: &mut RenderContext,
        f: &Frame,
        rctx: &Ctx,
        slide: &Slide,
        view: Affine,
    ) {
        self.background(ctx, f, rctx, Some(slide), view);
        let (show_layout, show_master) = resolve::show_master_shapes(slide, rctx.layout);
        if show_master {
            let mctx = Ctx::for_master(f.pres, rctx.master);
            for s in &rctx.master.shapes {
                if s.ph.is_none() {
                    self.shape(ctx, f, &mctx, s, view, 0);
                }
            }
        }
        if show_layout && let Some(l) = rctx.layout {
            let lctx = Ctx::for_layout(f.pres, rctx.master, l);
            for s in &l.shapes {
                if s.ph.is_none() {
                    self.shape(ctx, f, &lctx, s, view, 0);
                }
            }
        }
    }

    fn slide(
        &mut self,
        pres: &Presentation,
        slide: &Slide,
        index: usize,
        opts: &RenderOpts,
    ) -> Image {
        let (mut ctx, w, h, view) = Self::canvas(pres, opts, needs_filters(pres, slide));
        let f = Frame {
            pres,
            opts,
            fields: Self::fields(pres, index, opts),
        };
        if let Some(rctx) = Ctx::for_slide(pres, slide) {
            self.backdrop(&mut ctx, &f, &rctx, slide, view);
            for s in &slide.shapes {
                self.shape(&mut ctx, &f, &rctx, s, view, 0);
            }
        }
        self.finish(ctx, w, h)
    }

    #[allow(clippy::too_many_arguments)]
    fn blend(
        &mut self,
        pres: &Presentation,
        old: (&Slide, usize),
        new: (&Slide, usize),
        mix: f64,
        shapes: &[(Shape, bool)],
        text: &[TextMorph],
        paths: &[PathMorph],
        opts: &RenderOpts,
    ) -> Image {
        let mix = if mix.is_finite() {
            mix.clamp(0.0, 1.0)
        } else {
            1.0
        };
        let (mut ctx, w, h, view) = Self::canvas(
            pres,
            opts,
            needs_filters(pres, old.0) || needs_filters(pres, new.0),
        );
        let (Some(co), Some(cn)) = (Ctx::for_slide(pres, old.0), Ctx::for_slide(pres, new.0))
        else {
            return self.finish(ctx, w, h);
        };
        // Backdrop graphics never take shape states: their ids may clash with the frame's shapes.
        let bopts = RenderOpts {
            state: None,
            date_text: opts.date_text.clone(),
            ..*opts
        };
        if mix < 1.0 {
            let f = Frame {
                pres,
                opts: &bopts,
                fields: Self::fields(pres, old.1, opts),
            };
            self.backdrop(&mut ctx, &f, &co, old.0, view);
        }
        if mix > 0.0 {
            let f = Frame {
                pres,
                opts: &bopts,
                fields: Self::fields(pres, new.1, opts),
            };
            let layered = mix < 1.0;
            if layered {
                ctx.push_layer(None, None, Some(mix as f32), None, None);
            }
            self.backdrop(&mut ctx, &f, &cn, new.0, view);
            if layered {
                ctx.pop_layer();
            }
        }
        let fo = Frame {
            pres,
            opts,
            fields: Self::fields(pres, old.1, opts),
        };
        let fnew = Frame {
            pres,
            opts,
            fields: Self::fields(pres, new.1, opts),
        };
        let mut drawn: Vec<ShapeId> = Vec::new();
        // Set only now: the backdrop's ids may clash with the frame's.
        self.paths = paths.to_vec();
        for (s, from_old) in shapes {
            let (f, c) = if *from_old { (&fo, &co) } else { (&fnew, &cn) };
            let Some(tm) = text.iter().find(|m| m.old == s.id || m.new == s.id) else {
                self.shape(&mut ctx, f, c, s, view, 0);
                continue;
            };
            self.shape(
                &mut ctx,
                f,
                c,
                &Shape {
                    text: None,
                    ..s.clone()
                },
                view,
                0,
            );
            drawn.push(s.id);
            if !(drawn.contains(&tm.old) && drawn.contains(&tm.new)) {
                continue;
            }
            let find = |id: ShapeId| shapes.iter().map(|(s, _)| s).find(|s| s.id == id);
            if let (Some(a), Some(b)) = (find(tm.old), find(tm.new)) {
                let now = b.xfrm.unwrap_or_else(|| resolve::xfrm(&cn, b));
                morph_text::draw(
                    &mut ctx,
                    (&co, a, &fo.fields),
                    (&cn, b, &fnew.fields),
                    tm,
                    now,
                    view,
                );
            }
        }
        self.paths.clear();
        self.finish(ctx, w, h)
    }

    fn layout(
        &mut self,
        pres: &Presentation,
        mi: usize,
        li: Option<usize>,
        opts: &RenderOpts,
    ) -> Image {
        let (w, h) = output_size(pres, opts);
        let mut ctx = RenderContext::new_with(
            w,
            h,
            vello_cpu::RenderSettings {
                num_threads: 0,
                ..Default::default()
            },
        );
        let view = Affine::translate((opts.offset.0, opts.offset.1)) * Affine::scale(opts.scale);
        let f = Frame {
            pres,
            opts,
            fields: SlideFields::new(1, String::new(), opts),
        };
        if let Some(m) = pres.masters.get(mi) {
            match li.and_then(|i| m.layouts.get(i)) {
                Some(l) => {
                    let c = Ctx::for_layout(pres, m, l);
                    self.background(&mut ctx, &f, &c, None, view);
                    if l.show_master_shapes {
                        let mc = Ctx::for_master(pres, m);
                        for s in m.shapes.iter().filter(|s| s.ph.is_none()) {
                            self.shape(&mut ctx, &f, &mc, s, view, 0);
                        }
                    }
                    for s in &l.shapes {
                        self.shape(&mut ctx, &f, &c, s, view, 0);
                    }
                }
                None => {
                    let c = Ctx::for_master(pres, m);
                    self.background(&mut ctx, &f, &c, None, view);
                    for s in &m.shapes {
                        self.shape(&mut ctx, &f, &c, s, view, 0);
                    }
                }
            }
        }
        self.finish(ctx, w, h)
    }

    fn finish(&mut self, mut ctx: RenderContext, w: u16, h: u16) -> Image {
        ctx.flush();
        let mut pixels = vec![0u8; w as usize * h as usize * 4];
        if let Some(pm) = vello_cpu::PixmapMut::new(w, h, &mut pixels) {
            ctx.render(pm, &mut self.resources);
        }
        Image {
            width: w as u32,
            height: h as u32,
            pixels,
        }
    }

    fn background(
        &mut self,
        ctx: &mut RenderContext,
        f: &Frame,
        rctx: &Ctx,
        slide: Option<&Slide>,
        view: Affine,
    ) {
        let (fill, ph) = resolve::background(rctx, slide);
        let r = Rect::new(0.0, 0.0, f.pres.slide_size.width, f.pres.slide_size.height);
        ctx.set_transform(view);
        let path = r.to_path(0.1);
        // PowerPoint lays every background over white: a picture stretched into part of the
        // slide, or a see-through colour, leaves white around and under it.
        ctx.set_paint(color(Rgba::WHITE, 1.0));
        ctx.fill_path(&path);
        paint::fill_path(
            ctx,
            rctx,
            &fill,
            ph,
            &path,
            r,
            view,
            FillMode::Norm,
            1.0,
            f.pres,
        );
        self.bg = Some((fill, ph, view));
    }

    fn shape(
        &mut self,
        ctx: &mut RenderContext,
        f: &Frame,
        rctx: &Ctx,
        s: &Shape,
        parent: Affine,
        depth: usize,
    ) {
        if depth == 0 {
            let outer = std::mem::replace(&mut self.view, parent);
            self.shape_at(ctx, f, rctx, s, parent, depth);
            self.view = outer;
        } else {
            self.shape_at(ctx, f, rctx, s, parent, depth);
        }
    }

    /// How lines are drawn for a shape under `m` at `depth`: the transform to stroke under and the
    /// one that takes the shape's outline into it. Inside a group they are the slide-level view and
    /// the rest of the chain, so the group's child-coordinate scale moves and sizes the outline but
    /// never the line width; outside a group, `m` itself and no change to the outline.
    fn line_space(&self, m: Affine, depth: usize) -> (Affine, Affine) {
        let view = self.view;
        if depth > 0 && view.determinant().abs() > 1e-12 && view.is_finite() {
            (view, view.inverse() * m)
        } else {
            (m, Affine::IDENTITY)
        }
    }

    /// The size and transform a shape's text is set in: inside a group whose child coordinates
    /// scale the shape, the shape's size in slide points and `m` with that scale taken out, so
    /// insets and font sizes stay in points; otherwise `w`, `h` and `m` themselves.
    fn text_space(&self, m: Affine, depth: usize, w: f64, h: f64) -> (f64, f64, Affine) {
        let (_, rel) = self.line_space(m, depth);
        let c = rel.as_coeffs();
        let (sx, sy) = (c[0].hypot(c[1]), c[2].hypot(c[3]));
        let ok = |k: f64| k.is_finite() && k > 1e-9;
        if depth == 0 || !ok(sx) || !ok(sy) || ((sx - 1.0).abs() < 1e-9 && (sy - 1.0).abs() < 1e-9)
        {
            return (w, h, m);
        }
        let (tw, th) = (w * sx, h * sy);
        if !tw.is_finite() || !th.is_finite() {
            return (w, h, m);
        }
        (tw, th, m * Affine::scale_non_uniform(1.0 / sx, 1.0 / sy))
    }

    fn shape_at(
        &mut self,
        ctx: &mut RenderContext,
        f: &Frame,
        rctx: &Ctx,
        s: &Shape,
        parent: Affine,
        depth: usize,
    ) {
        if depth > 32 || (s.hidden && !f.opts.show_hidden) || f.opts.skip.contains(&s.id) {
            return;
        }
        // Footer placeholders on layouts/masters show only on slides that include them.
        let st = f.opts.state.map(|g| g(s.id)).unwrap_or_default();
        if !st.visible || st.opacity <= 0.001 {
            return;
        }
        let x = resolve::xfrm(rctx, s);
        if !x.is_finite() {
            return;
        }
        let mut m = parent * x.affine();
        if st.offset != Vec2::ZERO || st.scale != (1.0, 1.0) || st.rotate != 0.0 {
            let c = Point::new(x.w / 2.0, x.h / 2.0);
            let around = Affine::translate(c.to_vec2())
                * Affine::rotate(st.rotate.to_radians())
                * Affine::scale_non_uniform(st.scale.0, st.scale.1)
                * Affine::translate(-c.to_vec2());
            m = parent * Affine::translate(st.offset) * x.affine() * around;
        }
        let layered = st.opacity < 0.999 || st.clip.is_some();
        if layered {
            ctx.set_transform(m);
            let clip = st
                .clip
                .map(|[l, t, r, b]| Rect::new(l * x.w, t * x.h, r * x.w, b * x.h).to_path(0.1));
            ctx.push_layer(
                clip.as_ref(),
                None,
                Some(st.opacity.clamp(0.0, 1.0) as f32),
                None,
                None,
            );
        }
        match &s.kind {
            ShapeKind::Group { children, child } => {
                let ch = Rect::new(child.x, child.y, child.x + child.w, child.y + child.h);
                let gm = parent * deckcraft_geom::group_child_affine(&x, ch);
                for c in children {
                    self.shape(ctx, f, rctx, c, gm, depth + 1);
                }
            }
            ShapeKind::Table(t) => {
                // Rows grow with their text: what the cells cover, borders included.
                let cells = table::draw(ctx, f.pres, rctx, t, m, x.w, x.h, &f.fields);
                self.track(
                    m,
                    cells
                        .unwrap_or(Rect::new(0.0, 0.0, x.w, x.h))
                        .inflate(2.0, 2.0),
                );
            }
            ShapeKind::Chart(c) => {
                self.track(m, Rect::new(0.0, 0.0, x.w, x.h));
                chart::draw(ctx, rctx, c, m, x.w, x.h)
            }
            ShapeKind::Ink { strokes } => {
                for sk in strokes {
                    let w = if sk.width.is_finite() {
                        sk.width.abs()
                    } else {
                        0.0
                    };
                    for (px, py, _) in &sk.points {
                        self.track(parent, Rect::new(*px, *py, *px, *py).inflate(w, w));
                    }
                    let mut bp = BezPath::new();
                    for (i, (px, py, _)) in sk.points.iter().enumerate() {
                        if i == 0 {
                            bp.move_to((*px, *py));
                        } else {
                            bp.line_to((*px, *py));
                        }
                    }
                    let (base, rel) = self.line_space(parent, depth);
                    bp.apply_affine(rel);
                    ctx.set_transform(base);
                    ctx.set_paint(color(sk.color, if sk.highlighter { 0.4 } else { 1.0 }));
                    ctx.set_stroke(
                        kurbo::Stroke::new(sk.width.max(0.1))
                            .with_caps(kurbo::Cap::Round)
                            .with_join(kurbo::Join::Round),
                    );
                    ctx.stroke_path(&bp);
                }
            }
            _ => self.geometry_shape(ctx, f, rctx, s, m, x.w, x.h, &st, depth),
        }
        if layered {
            ctx.pop_layer();
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn geometry_shape(
        &mut self,
        ctx: &mut RenderContext,
        f: &Frame,
        rctx: &Ctx,
        s: &Shape,
        m: Affine,
        w: f64,
        h: f64,
        st: &ShapeState,
        depth: usize,
    ) {
        let (geo, fades) = match self.paths.iter().find(|p| p.id == s.id) {
            Some(pm) => morph_path::geometry(pm, s, w, h),
            None => (shape_geometry(s, w, h), Vec::new()),
        };
        let fade = |i: usize| {
            fades
                .get(i)
                .copied()
                .unwrap_or(deckcraft_geom::morph::Fade {
                    fill: 1.0,
                    stroke: 1.0,
                })
        };
        let (fill, fill_ph) = resolve::fill(rctx, s);
        let (line, line_ph) = resolve::line(rctx, s);
        let (effects, fx_ph) = resolve::effects(rctx, s);
        let reach = extent::spill(Some(&line), effects.as_ref(), w, h);
        self.track(m, Rect::new(0.0, 0.0, w, h).inflate(reach, reach));
        let is_pic = matches!(s.kind, ShapeKind::Picture { .. });
        let outline = geo.outline();
        // Placeholders in the editor: dashed outline and prompt, nothing else when empty.
        let empty_ph = s.ph.is_some()
            && s.text.as_ref().is_none_or(TextBody::is_empty)
            && !is_pic
            && !matches!(s.kind, ShapeKind::Media(_));
        let fill = if let Some(t) = st.tint {
            Some(Fill::solid(deckcraft_model::ColorRef::rgb(t)))
        } else {
            fill
        };
        // Effects below the shape.
        if let Some(e) = &effects {
            // A picture (or picture fill) casts its shadow from its pixels, not its frame.
            let content = match (&s.kind, &fill) {
                (ShapeKind::Picture { fill: pf }, _) | (_, Some(Fill::Picture(pf))) => {
                    Some(paint::resolved(rctx, pf, None).into_owned())
                }
                _ => None,
            };
            self.effects_below(ctx, rctx, e, fx_ph, &geo, &fill, content.as_ref(), m, w, h);
        }
        let soft = effects
            .as_ref()
            .and_then(|e| e.soft_edge)
            .filter(|r| *r > 0.0);
        if let Some(r) = soft {
            ctx.set_transform(Affine::IDENTITY);
            ctx.push_layer(None, None, None, None, None);
            let _ = r;
        }
        // Fill.
        match &s.kind {
            ShapeKind::Picture { fill: pf } => {
                ctx.set_transform(m);
                let bounds = Rect::new(0.0, 0.0, w, h);
                ctx.push_clip_layer(&outline);
                let t = f.opts.gif_times.iter().find(|g| g.0 == s.id).map(|g| g.1);
                paint::picture(ctx, f.pres, &paint::resolved(rctx, pf, None), bounds, m, t);
                ctx.pop_layer();
            }
            // An object DeckCraft doesn't model (an embedded OLE object, an unknown graphic): its
            // preview picture, stretched over the frame, as PowerPoint draws it.
            ShapeKind::Opaque {
                preview: Some(media),
                ..
            } => {
                ctx.set_transform(m);
                let pf = deckcraft_model::style::PictureFill {
                    media: *media,
                    ..Default::default()
                };
                paint::picture(ctx, f.pres, &pf, Rect::new(0.0, 0.0, w, h), m, None);
            }
            ShapeKind::Media(mc) => {
                ctx.set_transform(m);
                let bounds = Rect::new(0.0, 0.0, w, h);
                if let Some(p) = mc.poster {
                    let pf = deckcraft_model::style::PictureFill {
                        media: p,
                        ..Default::default()
                    };
                    paint::picture(ctx, f.pres, &pf, bounds, m, None);
                } else if mc.video {
                    ctx.set_paint(peniko::Color::from_rgba8(20, 20, 24, 255));
                    ctx.fill_rect(&bounds);
                }
                if !mc.video || mc.poster.is_none() {
                    paint::media_glyph(ctx, mc.video, bounds);
                }
            }
            _ => {
                if let Some(fl) = &fill
                    && !(empty_ph && f.opts.edit && s.fill.is_none())
                {
                    for (i, sp) in geo.paths.iter().enumerate() {
                        if sp.fill == FillMode::None || fade(i).fill <= 0.0 {
                            continue;
                        }
                        ctx.set_transform(m);
                        ctx.set_fill_rule(if sp.even_odd {
                            peniko::Fill::EvenOdd
                        } else {
                            peniko::Fill::NonZero
                        });
                        match (fl, &self.bg) {
                            // `useBgFill`: the slide's background, laid out on the slide (not the
                            // shape), shows through the shape.
                            (Fill::Background, Some((bg, ph, view))) => {
                                let slide = Rect::new(
                                    0.0,
                                    0.0,
                                    f.pres.slide_size.width,
                                    f.pres.slide_size.height,
                                );
                                let path = (view.inverse() * m) * sp.path.clone();
                                paint::fill_path(
                                    ctx,
                                    rctx,
                                    bg,
                                    *ph,
                                    &path,
                                    slide,
                                    *view,
                                    sp.fill,
                                    fade(i).fill,
                                    f.pres,
                                );
                            }
                            _ => paint::fill_path(
                                ctx,
                                rctx,
                                fl,
                                fill_ph,
                                &sp.path,
                                Rect::new(0.0, 0.0, w, h),
                                m,
                                sp.fill,
                                fade(i).fill,
                                f.pres,
                            ),
                        }
                        ctx.set_fill_rule(peniko::Fill::NonZero);
                    }
                }
            }
        }
        // `a:fillOverlay`: a fill blended over the shape's fill (PowerPoint's "Flow" themes multiply
        // a gradient over a tiled picture), inside the filled outline, under the line.
        if let Some(ov) = effects.as_ref().and_then(|e| e.fill_overlay.as_ref())
            && fill.as_ref().is_some_and(|f| !f.is_none())
        {
            let mix = match ov.blend.as_str() {
                "mult" => peniko::Mix::Multiply,
                "screen" => peniko::Mix::Screen,
                "darken" => peniko::Mix::Darken,
                "lighten" => peniko::Mix::Lighten,
                _ => peniko::Mix::Normal,
            };
            for (i, sp) in geo
                .paths
                .iter()
                .enumerate()
                .filter(|(i, sp)| sp.fill != FillMode::None && fade(*i).fill > 0.0)
            {
                ctx.set_transform(m);
                ctx.push_layer(
                    Some(&sp.path),
                    Some(peniko::BlendMode::new(mix, peniko::Compose::SrcOver)),
                    None,
                    None,
                    None,
                );
                paint::fill_path(
                    ctx,
                    rctx,
                    &ov.fill,
                    fx_ph,
                    &sp.path,
                    Rect::new(0.0, 0.0, w, h),
                    m,
                    FillMode::Norm,
                    fade(i).fill,
                    f.pres,
                );
                ctx.pop_layer();
            }
        }
        // Outline.
        if line.fill.as_ref().is_some_and(|f| !f.is_none()) {
            let lw = line.width.unwrap_or(0.75).max(0.0);
            let (base, rel) = self.line_space(m, depth);
            for (i, sp) in geo
                .paths
                .iter()
                .enumerate()
                .filter(|(i, p)| p.stroke && fade(*i).stroke > 0.0)
            {
                let path = if rel == Affine::IDENTITY {
                    std::borrow::Cow::Borrowed(&sp.path)
                } else {
                    let mut moved = sp.path.clone();
                    moved.apply_affine(rel);
                    std::borrow::Cow::Owned(moved)
                };
                stroke(ctx, rctx, &line, line_ph, &path, base, lw, fade(i).stroke);
            }
            if s.is_line() || geo.is_open() {
                arrowheads(ctx, rctx, &line, line_ph, &geo, rel, base, lw);
            }
        }
        if let Some(r) = soft {
            // Feather: the shape (fill and line) fades to nothing at its outline. Measured on
            // PowerPoint's export: the mask is the outline shrunk by 0.85 r, blurred by a third of
            // r, so it's half opaque 0.85 r inside the edge and transparent at the edge itself.
            ctx.set_transform(m);
            ctx.push_layer(
                None,
                Some(peniko::BlendMode::new(
                    peniko::Mix::Normal,
                    peniko::Compose::DestIn,
                )),
                None,
                None,
                Some(blur(r / 3.0 * scale_of(m))),
            );
            ctx.set_paint(peniko::Color::from_rgba8(0, 0, 0, 255));
            ctx.fill_path(&outline);
            ctx.push_blend_layer(peniko::BlendMode::new(
                peniko::Mix::Normal,
                peniko::Compose::DestOut,
            ));
            ctx.set_stroke(kurbo::Stroke::new(r * 1.7).with_join(kurbo::Join::Round));
            ctx.stroke_path(&outline);
            ctx.pop_layer();
            ctx.pop_layer();
            ctx.pop_layer();
        }
        // Inner shadow.
        if let Some(e) = &effects
            && let Some(sh) = &e.inner_shadow
        {
            ctx.set_transform(m);
            ctx.push_clip_layer(&outline);
            let c = rctx.color(&sh.color, fx_ph);
            let d = Vec2::new(sh.dir.to_radians().cos(), sh.dir.to_radians().sin()) * sh.dist;
            // As PowerPoint draws it (measured on its export): the shadow falls inside along the
            // edges it points to (45 degrees: the bottom and right), blurred by a third of the
            // blur radius.
            ctx.push_layer(
                None,
                None,
                None,
                None,
                Some(blur(sh.blur / 3.0 * scale_of(m))),
            );
            ctx.set_paint(color(c, 1.0));
            let mut ring = Rect::new(0.0, 0.0, w, h)
                .inflate(w.max(h), w.max(h))
                .to_path(0.1);
            let mut hole = outline.clone();
            hole.apply_affine(Affine::translate(-d));
            ring.extend(hole.iter());
            ctx.set_fill_rule(peniko::Fill::EvenOdd);
            ctx.fill_path(&ring);
            ctx.set_fill_rule(peniko::Fill::NonZero);
            ctx.pop_layer();
            ctx.pop_layer();
        }
        // Text (or the placeholder prompt in the editor).
        let skip_text = f.opts.skip_text == Some(s.id);
        if let Some(body) = &s.text
            && !skip_text
        {
            // Text in a group is set at the shape's size on the slide, in points: a group's
            // child-coordinate scale sizes and places the box, never the type (#233's twin).
            let (tw, th, m) = self.text_space(m, depth, w, h);
            let text_geo;
            let geo = if (tw, th) == (w, h) {
                &geo
            } else {
                text_geo = shape_geometry(s, tw, th);
                &text_geo
            };
            let tr = geo.text_rect;
            if !body.is_empty() {
                let l = deckcraft_text::layout(
                    rctx,
                    s,
                    body,
                    &Opts {
                        rect: tr,
                        fields: &f.fields,
                        prompt_color: None,
                        no_shrink: false,
                    },
                );
                let tm = text_transform(m, tr, l.rotation);
                for b in l.lines.iter().filter_map(line_bounds) {
                    // Glyphs reach a little past their carets and line box (italics, swashes).
                    let pad = b.height() * 0.3;
                    self.track(tm, b.inflate(pad, pad));
                }
                match text_warp(rctx, s, tr).filter(|_| l.rotation == 0.0) {
                    Some(w) => warp::draw(ctx, &l, tr, m, &st.paras, &w),
                    None => draw_layout_with(ctx, &l, tr, m, &st.paras),
                }
            } else if f.opts.edit && s.ph.is_some() {
                let kind = s.ph_type().unwrap_or(PhType::Body);
                let prompt_body = prompt_body(rctx, s, kind);
                if !prompt_body.is_empty() {
                    let grey = Rgba::rgb(0x59, 0x59, 0x59);
                    draw_text(ctx, rctx, s, &prompt_body, tr, m, &f.fields, Some(grey));
                }
            }
        }
        if empty_ph && f.opts.edit {
            // Dashed placeholder outline.
            ctx.set_transform(m);
            let px = 1.0 / scale_of(m).max(1e-6);
            ctx.set_paint(peniko::Color::from_rgba8(0xA6, 0xA6, 0xA6, 255));
            ctx.set_stroke(kurbo::Stroke::new(px).with_dashes(0.0, [3.0 * px, 2.0 * px]));
            ctx.stroke_path(&Rect::new(0.0, 0.0, w, h).to_path(0.1));
            if matches!(
                s.ph_type(),
                Some(
                    PhType::Picture
                        | PhType::Chart
                        | PhType::Table
                        | PhType::Media
                        | PhType::Diagram
                )
            ) {
                paint::placeholder_icon(
                    ctx,
                    s.ph_type().unwrap_or(PhType::Picture),
                    Rect::new(0.0, 0.0, w, h),
                );
            }
        }
    }

    fn effects_below(
        &mut self,
        ctx: &mut RenderContext,
        rctx: &Ctx,
        e: &Effects,
        ph: Option<Rgba>,
        geo: &Geometry,
        fill: &Option<Fill>,
        content: Option<&deckcraft_model::style::PictureFill>,
        m: Affine,
        w: f64,
        h: f64,
    ) {
        let silhouette = {
            let mut p = BezPath::new();
            for sp in &geo.paths {
                if sp.fill != FillMode::None || fill.is_none() {
                    p.extend(sp.path.iter());
                }
            }
            if p.elements().is_empty() {
                geo.outline()
            } else {
                p
            }
        };
        let k = scale_of(m);
        if let Some(sh) = &e.outer_shadow {
            let c = rctx.color(&sh.color, ph);
            let d = Vec2::new(sh.dir.to_radians().cos(), sh.dir.to_radians().sin()) * sh.dist;
            let c0 = Point::new(w / 2.0, h / 2.0);
            let local = Affine::translate(c0.to_vec2())
                * Affine::new([
                    sh.sx,
                    sh.ky.to_radians().tan(),
                    sh.kx.to_radians().tan(),
                    sh.sy,
                    0.0,
                    0.0,
                ])
                * Affine::translate(-c0.to_vec2());
            // Shadow offset is in slide space (doesn't rotate with the shape unless asked).
            let sm = if sh.rotate_with_shape {
                m * Affine::translate(d) * local
            } else {
                pre_translate(m, d * k) * local
            };
            ctx.set_transform(Affine::IDENTITY);
            // Blurred by a third of the blur radius, as PowerPoint's export shows.
            ctx.push_layer(
                None,
                None,
                Some((c.a as f32) / 255.0),
                None,
                Some(blur(sh.blur / 3.0 * k)),
            );
            ctx.set_transform(sm);
            match content {
                // As PowerPoint draws it: the shadow is the picture's own coverage (a logo on a
                // transparent background casts the logo's shadow): the picture, clipped to the
                // shape, recoloured to the shadow colour.
                Some(pf) => {
                    ctx.push_clip_layer(&silhouette);
                    paint::picture(ctx, rctx.pres, pf, Rect::new(0.0, 0.0, w, h), sm, None);
                    ctx.push_layer(
                        None,
                        Some(peniko::BlendMode::new(
                            peniko::Mix::Normal,
                            peniko::Compose::SrcIn,
                        )),
                        None,
                        None,
                        None,
                    );
                    ctx.set_paint(color(c.with_alpha(255), 1.0));
                    ctx.fill_rect(&Rect::new(0.0, 0.0, w, h).inflate(1.0, 1.0));
                    ctx.pop_layer();
                    ctx.pop_layer();
                }
                None => {
                    ctx.set_paint(color(c.with_alpha(255), 1.0));
                    ctx.fill_path(&silhouette);
                }
            }
            ctx.pop_layer();
        }
        if let Some(g) = &e.glow {
            let c = rctx.color(&g.color, ph);
            ctx.set_transform(Affine::IDENTITY);
            // Measured on PowerPoint's export: a glow of radius r is half strength about half of r
            // out from the outline and gone by r.
            ctx.push_layer(
                None,
                None,
                Some((c.a as f32) / 255.0),
                None,
                Some(blur(g.radius * 0.17 * k)),
            );
            ctx.set_transform(m);
            ctx.set_paint(color(c.with_alpha(255), 1.0));
            ctx.set_stroke(
                kurbo::Stroke::new(g.radius.max(0.0) * 0.95).with_join(kurbo::Join::Round),
            );
            ctx.stroke_path(&silhouette);
            ctx.fill_path(&silhouette);
            ctx.pop_layer();
        }
        if let Some(rf) = &e.reflection {
            // Mirror below the shape, fading out.
            let mm = m
                * Affine::translate((0.0, 2.0 * h + rf.dist))
                * Affine::scale_non_uniform(1.0, -1.0);
            ctx.set_transform(Affine::IDENTITY);
            ctx.push_layer(
                None,
                None,
                Some(rf.start_alpha.clamp(0.0, 1.0) as f32 * 0.6),
                None,
                Some(blur(rf.blur * 0.5 * k)),
            );
            ctx.set_transform(mm);
            if let Some(fl) = fill {
                paint::fill_path(
                    ctx,
                    rctx,
                    fl,
                    ph,
                    &silhouette,
                    Rect::new(0.0, 0.0, w, h),
                    mm,
                    FillMode::Norm,
                    1.0,
                    rctx.pres,
                );
            }
            // Fade mask.
            ctx.push_layer(
                None,
                Some(peniko::BlendMode::new(
                    peniko::Mix::Normal,
                    peniko::Compose::DestIn,
                )),
                None,
                None,
                None,
            );
            let grad = peniko::Gradient::new_linear(
                Point::new(0.0, h),
                Point::new(0.0, h * (1.0 - rf.end_pos.clamp(0.01, 1.0))),
            )
            .with_stops(
                [
                    (0.0, peniko::Color::from_rgba8(0, 0, 0, 255)),
                    (1.0, peniko::Color::from_rgba8(0, 0, 0, 0)),
                ]
                .as_slice(),
            );
            ctx.set_paint(grad);
            ctx.fill_rect(&Rect::new(0.0, 0.0, w, h).inflate(4.0, 4.0));
            ctx.pop_layer();
            ctx.pop_layer();
        }
    }
}

fn pre_translate(m: Affine, d: Vec2) -> Affine {
    Affine::translate(d) * m
}

pub(crate) fn scale_of(m: Affine) -> f64 {
    m.determinant().abs().sqrt()
}

fn blur(sigma: f64) -> Filter {
    Filter::from_primitive(FilterPrimitive::GaussianBlur {
        std_deviation: sigma.clamp(0.0, 500.0) as f32,
        edge_mode: EdgeMode::None,
    })
}

fn stroke(
    ctx: &mut RenderContext,
    rctx: &Ctx,
    line: &Line,
    ph: Option<Rgba>,
    path: &BezPath,
    m: Affine,
    lw: f64,
    alpha: f64,
) {
    use deckcraft_model::style::{LineCap, LineJoin};
    let cap = match line.cap.unwrap_or_default() {
        LineCap::Flat => kurbo::Cap::Butt,
        LineCap::Round => kurbo::Cap::Round,
        LineCap::Square => kurbo::Cap::Square,
    };
    let join = match line.join.unwrap_or_default() {
        LineJoin::Round => kurbo::Join::Round,
        LineJoin::Bevel => kurbo::Join::Bevel,
        LineJoin::Miter => kurbo::Join::Miter,
    };
    // Hairlines stay visible at any zoom.
    let min_w = 0.5 / scale_of(m).max(1e-6);
    let mut sk = kurbo::Stroke::new(lw.max(min_w))
        .with_caps(cap)
        .with_join(join);
    if let Some(d) = &line.dash {
        let pat: Vec<f64> = d.pattern().iter().map(|v| v * lw.max(0.75)).collect();
        if !pat.is_empty() && pat.iter().all(|v| *v > 0.0) {
            sk = sk.with_dashes(0.0, pat);
        }
    }
    ctx.set_transform(m);
    match &line.fill {
        Some(Fill::Solid { color: c }) => {
            ctx.set_paint(color(rctx.color(c, ph), alpha));
        }
        Some(Fill::Gradient(g)) => {
            let c = g
                .stops
                .first()
                .map(|s| rctx.color(&s.color, ph))
                .unwrap_or(Rgba::BLACK);
            ctx.set_paint(color(c, alpha));
        }
        _ => ctx.set_paint(color(Rgba::BLACK, alpha)),
    }
    // Compound lines: each band stroked along the path moved to its place across the width.
    let compound = line.compound.unwrap_or_default();
    if compound != deckcraft_model::style::Compound::Single && lw * scale_of(m) >= 1.5 {
        let tolerance = 0.1 / scale_of(m).max(1e-6);
        for &(a, b) in compound::bands(compound) {
            let d = (-0.5 + (a + b) / 2.0) * lw;
            let mut band = sk.clone();
            band.width = (b - a) * lw;
            ctx.set_stroke(band);
            ctx.stroke_path(&compound::offset(path, d, tolerance));
        }
        return;
    }
    ctx.set_stroke(sk);
    ctx.stroke_path(path);
}

fn end_points(path: &BezPath) -> Option<((Point, Point), (Point, Point))> {
    let els: Vec<PathEl> = path.elements().to_vec();
    let mut pts: Vec<Point> = vec![];
    for e in &els {
        match e {
            PathEl::MoveTo(p) | PathEl::LineTo(p) => pts.push(*p),
            PathEl::QuadTo(a, b) => {
                pts.push(*a);
                pts.push(*b);
            }
            PathEl::CurveTo(a, b, c) => {
                pts.push(*a);
                pts.push(*b);
                pts.push(*c);
            }
            PathEl::ClosePath => {}
        }
    }
    if pts.len() < 2 {
        return None;
    }
    let first = *pts.first()?;
    let second = pts
        .iter()
        .skip(1)
        .find(|p| (**p - first).hypot() > 1e-6)
        .copied()
        .unwrap_or(*pts.get(1)?);
    let last = *pts.last()?;
    let prev = pts
        .iter()
        .rev()
        .skip(1)
        .find(|p| (**p - last).hypot() > 1e-6)
        .copied()
        .unwrap_or(*pts.get(pts.len() - 2)?);
    Some(((first, second), (last, prev)))
}

#[allow(clippy::too_many_arguments)]
fn arrowheads(
    ctx: &mut RenderContext,
    rctx: &Ctx,
    line: &Line,
    ph: Option<Rgba>,
    geo: &Geometry,
    rel: Affine,
    m: Affine,
    lw: f64,
) {
    let Some(sp) = geo.paths.iter().find(|p| p.stroke) else {
        return;
    };
    let Some(((a, a2), (b, b2))) = end_points(&sp.path) else {
        return;
    };
    // The outline's ends in the space the arrowheads are sized and drawn in.
    let (a, a2, b, b2) = (rel * a, rel * a2, rel * b, rel * b2);
    let col = match &line.fill {
        Some(Fill::Solid { color: c }) => rctx.color(c, ph),
        _ => Rgba::BLACK,
    };
    let lw = lw.max(0.75);
    for (end, tip, from) in [(&line.head, a, a2), (&line.tail, b, b2)] {
        let Some(e) = end else { continue };
        if e.kind == "none" {
            continue;
        }
        let sz = |s: &str| match s {
            "sm" => 2.0,
            "lg" => 5.0,
            _ => 3.0,
        };
        let (wl, ll) = (sz(&e.w) * lw, sz(&e.len) * lw);
        let dir = (tip - from).normalize();
        if !dir.x.is_finite() {
            continue;
        }
        let n = Vec2::new(-dir.y, dir.x);
        let base = tip - dir * ll;
        let mut p = BezPath::new();
        match e.kind.as_str() {
            "oval" => {
                let c = kurbo::Ellipse::new(tip, (ll / 2.0, wl / 2.0), dir.y.atan2(dir.x));
                p = c.to_path(0.05);
            }
            "diamond" => {
                p.move_to(tip + dir * ll / 2.0);
                p.line_to(tip + n * wl / 2.0);
                p.line_to(tip - dir * ll / 2.0);
                p.line_to(tip - n * wl / 2.0);
                p.close_path();
            }
            "stealth" => {
                p.move_to(tip);
                p.line_to(base + n * wl / 2.0);
                p.line_to(tip - dir * ll * 0.6);
                p.line_to(base - n * wl / 2.0);
                p.close_path();
            }
            "arrow" => {
                let mut o = BezPath::new();
                o.move_to(base + n * wl / 2.0);
                o.line_to(tip);
                o.line_to(base - n * wl / 2.0);
                ctx.set_transform(m);
                ctx.set_paint(color(col, 1.0));
                ctx.set_stroke(kurbo::Stroke::new(lw).with_join(kurbo::Join::Miter));
                ctx.stroke_path(&o);
                continue;
            }
            _ => {
                p.move_to(tip);
                p.line_to(base + n * wl / 2.0);
                p.line_to(base - n * wl / 2.0);
                p.close_path();
            }
        }
        ctx.set_transform(m);
        ctx.set_paint(color(col, 1.0));
        ctx.fill_path(&p);
    }
}

/// The prompt shown in an empty placeholder, formatted like its content would be.
fn prompt_body(rctx: &Ctx, s: &Shape, kind: PhType) -> TextBody {
    // Layout/master prompt text when the layout supplies a custom one.
    let (lp, _) = resolve::parents(rctx, s);
    let custom = lp
        .and_then(|l| {
            l.ph.as_ref()
                .filter(|p| p.has_custom_prompt)
                .and(l.text.as_ref())
        })
        .filter(|t| !t.is_empty());
    if let Some(t) = custom {
        return t.clone();
    }
    if matches!(rctx.owner, resolve::Owner::Layout | resolve::Owner::Master) {
        return TextBody::default();
    }
    let txt = kind.prompt();
    if txt.is_empty() || kind.is_footer_kind() {
        return TextBody::default();
    }
    let mut t = TextBody::from_text(txt);
    if matches!(
        kind,
        PhType::Picture
            | PhType::Chart
            | PhType::Table
            | PhType::Media
            | PhType::Diagram
            | PhType::ClipArt
    ) {
        t.body.anchor = Some(deckcraft_model::text::Anchor::Bottom);
        if let Some(p) = t.paragraphs.first_mut() {
            p.props.align = Some(deckcraft_model::text::Align::Center);
            p.props.bullet = Some(deckcraft_model::text::Bullet::None);
            p.props.margin_left = Some(0.0);
            p.props.indent = Some(0.0);
            for r in &mut p.runs {
                r.props.size = Some(14.0);
            }
        }
    }
    t
}

thread_local! {
    static GLYPH_CACHE: std::cell::RefCell<HashMap<(u32, u32), Arc<BezPath>>> = std::cell::RefCell::new(HashMap::new());
}

fn glyph(face: &deckcraft_fonts::FontFace, gid: u32) -> Arc<BezPath> {
    let key = (face.id(), gid);
    if let Some(p) = GLYPH_CACHE.with(|c| c.borrow().get(&key).cloned()) {
        return p;
    }
    let p = FontDb::global().outline(face, gid);
    GLYPH_CACHE.with(|c| {
        let mut c = c.borrow_mut();
        if c.len() > 200_000 {
            c.clear();
        }
        c.insert(key, p.clone());
    });
    p
}

/// Draw a text body into shape space `m` within the text rect.
pub fn draw_text(
    ctx: &mut RenderContext,
    rctx: &Ctx,
    s: &Shape,
    body: &TextBody,
    tr: Rect,
    m: Affine,
    fields: &dyn Fields,
    prompt: Option<Rgba>,
) {
    let l = deckcraft_text::layout(
        rctx,
        s,
        body,
        &Opts {
            rect: tr,
            fields,
            prompt_color: prompt,
            no_shrink: false,
        },
    );
    draw_layout(ctx, &l, tr, m);
}

/// Where text laid out in text rect `tr` is drawn, for a shape drawn with `m` (shape-local →
/// output): `m`, without mirroring, then the layout's `rotation` about the rect's centre.
///
/// Flips mirror a shape's geometry, never its text. When `m` reflects (flipH or flipV, the shape's
/// own or a group's) the text rect keeps its mirrored place and the text is mirrored back about
/// the rect's centre: a flipH shape's text stays readable and a flipV shape's text is turned 180°
/// (both flips: 180°), as in PowerPoint. Rendering, hit testing and the caret all use this.
pub fn text_transform(m: Affine, tr: Rect, rotation: f64) -> Affine {
    let c = tr.center().to_vec2();
    let mut t = m;
    if m.determinant() < 0.0 && c.is_finite() {
        t = t * Affine::translate(c) * Affine::scale_non_uniform(-1.0, 1.0) * Affine::translate(-c);
    }
    if rotation != 0.0 && rotation.is_finite() && c.is_finite() {
        t = t
            * Affine::translate(c)
            * Affine::rotate(rotation.to_radians())
            * Affine::translate(-c);
    }
    t
}

/// The WordArt warp of `s`'s text over its text rectangle `tr`, if it has one DeckCraft draws.
pub fn text_warp(rctx: &Ctx, s: &Shape, tr: Rect) -> Option<deckcraft_geom::warp::Warp> {
    let bp = resolve::body(rctx, s);
    let name = bp.warp.as_deref()?;
    deckcraft_geom::warp::warp(name, &bp.warp_adj, tr)
}

/// Draw an already laid-out text block.
pub fn draw_layout(ctx: &mut RenderContext, l: &deckcraft_text::TextLayout, tr: Rect, m: Affine) {
    draw_layout_with(ctx, l, tr, m, &[]);
}

/// Draw a laid-out text block with per-paragraph animation states.
pub fn draw_layout_with(
    ctx: &mut RenderContext,
    l: &deckcraft_text::TextLayout,
    tr: Rect,
    m: Affine,
    paras: &[(usize, ParaState)],
) {
    let para = |i: usize| {
        paras
            .iter()
            .find(|(p, _)| *p == i)
            .map(|(_, s)| *s)
            .unwrap_or_default()
    };
    let m = text_transform(m, tr, l.rotation);
    let deco = |ctx: &mut RenderContext, d: &deckcraft_text::Deco| {
        let ps = para(d.para);
        if !ps.visible || ps.opacity <= 0.001 {
            return;
        }
        ctx.set_transform(m * Affine::translate(ps.offset));
        ctx.set_paint(color(d.color, ps.opacity));
        ctx.fill_rect(&d.rect);
    };
    for d in l.decos.iter().filter(|d| d.behind) {
        deco(ctx, d);
    }
    // Text shadows, then glows, under all the text.
    for pass in [TextEffect::Shadow, TextEffect::Glow] {
        for run in &l.runs {
            let ps = para(run.para);
            if ps.visible && ps.opacity > 0.001 {
                text_effect(ctx, run, pass, m * Affine::translate(ps.offset), ps.opacity);
            }
        }
    }
    for run in &l.runs {
        if (run.alpha <= 0.0 || run.color.a == 0) && run.outline.is_none() {
            continue;
        }
        let ps = para(run.para);
        if !ps.visible || ps.opacity <= 0.001 {
            continue;
        }
        let m = m * Affine::translate(ps.offset);
        // A gradient fill starts over on every line and spans that line's text (from its top to
        // its bottom, from its first to its last character), as in PowerPoint.
        let mut grads: Vec<(usize, peniko::Gradient)> = Vec::new();
        for (gid, x, y) in &run.glyphs {
            let at = m * Affine::translate((*x, *y));
            let Some(g) = run.gradient.as_ref().filter(|g| !g.stops.is_empty()) else {
                draw_glyph(ctx, run, *gid, at, ps.opacity);
                continue;
            };
            let li = line_at(l, run.para, *y);
            if !grads.iter().any(|(i, _)| *i == li) {
                let scheme = deckcraft_color::ColorScheme {
                    name: String::new(),
                    colors: [Rgba::BLACK; 12],
                };
                let stops = g
                    .stops
                    .iter()
                    .map(|s| (s.pos, s.color.resolve(&scheme, None)))
                    .collect();
                let bounds = l.lines.get(li).and_then(line_bounds).unwrap_or(tr);
                grads.push((
                    li,
                    paint::gradient(&g.shape, stops, bounds, run.alpha * ps.opacity),
                ));
            }
            let fill = grads
                .iter()
                .find(|(i, _)| *i == li)
                .map(|(_, g)| (g, Affine::translate((-*x, -*y))));
            draw_glyph_with(ctx, run, *gid, at, ps.opacity, fill);
        }
    }
    for d in l.decos.iter().filter(|d| !d.behind) {
        deco(ctx, d);
    }
}

/// The line of paragraph `para` holding a glyph on baseline `y` (the nearest baseline).
fn line_at(l: &deckcraft_text::TextLayout, para: usize, y: f64) -> usize {
    l.lines
        .iter()
        .enumerate()
        .filter(|(_, li)| li.para == para)
        .min_by(|a, b| {
            (a.1.baseline - y)
                .abs()
                .total_cmp(&(b.1.baseline - y).abs())
        })
        .map(|(i, _)| i)
        .unwrap_or(0)
}

/// The box a line's text covers: its top and bottom, its first and last caret.
fn line_bounds(li: &deckcraft_text::LineInfo) -> Option<Rect> {
    let (x0, x1) = li
        .caret_x
        .iter()
        .fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), x| {
            (a.min(*x), b.max(*x))
        });
    let r = Rect::new(x0, li.top, x1, li.bottom);
    (r.is_finite() && r.width() > 0.0 && r.height() > 0.0).then_some(r)
}

#[derive(Clone, Copy, PartialEq)]
enum TextEffect {
    Shadow,
    Glow,
}

/// Draw `run`'s shadow or glow (if it has one) under its glyphs, placed by `m`.
fn text_effect(
    ctx: &mut RenderContext,
    run: &deckcraft_text::GlyphRun,
    which: TextEffect,
    m: Affine,
    opacity: f64,
) {
    let Some(fx) = run.effects else { return };
    let k = run.size / run.face.upem.max(1.0);
    let skew = if run.fake_italic {
        Affine::new([1.0, 0.0, -0.2, 1.0, 0.0, 0.0])
    } else {
        Affine::IDENTITY
    };
    let px = scale_of(m);
    let (c, sigma, offset, grow) = match (which, fx.shadow, fx.glow) {
        (TextEffect::Shadow, Some(s), _) => {
            (s.color, s.blur * 0.5 * px, Vec2::new(s.dx, s.dy), 0.0)
        }
        // Measured on PowerPoint's export: a text glow of radius r is solid to about a third of r
        // past the glyph's edge and fades out by about two thirds of r.
        (TextEffect::Glow, _, Some((gc, r))) => (gc, r * 0.15 * px, Vec2::ZERO, r * 0.68),
        _ => return,
    };
    let alpha = (c.a as f64 / 255.0) * opacity;
    if alpha <= 0.0 {
        return;
    }
    ctx.set_transform(Affine::IDENTITY);
    ctx.push_layer(None, None, Some(alpha as f32), None, Some(blur(sigma)));
    ctx.set_paint(color(c.with_alpha(255), 1.0));
    for (gid, x, y) in &run.glyphs {
        let path = glyph(&run.face, *gid);
        if path.elements().is_empty() {
            continue;
        }
        // The offset is in slide space: a shadow falls the same way whatever the text's rotation.
        let at =
            pre_translate(m * Affine::translate((*x, *y)), offset * px) * skew * Affine::scale(k);
        ctx.set_transform(at);
        ctx.fill_path(&path);
        if grow > 0.0 {
            ctx.set_stroke(kurbo::Stroke::new(grow / k.max(1e-9)).with_join(kurbo::Join::Round));
            ctx.stroke_path(&path);
        }
    }
    ctx.pop_layer();
}

/// Draw glyph `gid` of `run` with its origin (on the baseline) mapped by `at`, faded by `opacity`.
pub(crate) fn draw_glyph(
    ctx: &mut RenderContext,
    run: &deckcraft_text::GlyphRun,
    gid: u32,
    at: Affine,
    opacity: f64,
) {
    draw_glyph_with(ctx, run, gid, at, opacity, None);
}

/// [`draw_glyph`], filled with `fill` (a gradient laid out in the text's space, and the map from
/// the glyph's origin to that space) instead of the run's colour.
fn draw_glyph_with(
    ctx: &mut RenderContext,
    run: &deckcraft_text::GlyphRun,
    gid: u32,
    at: Affine,
    opacity: f64,
    fill: Option<(&peniko::Gradient, Affine)>,
) {
    let alpha = run.alpha * opacity;
    if (alpha <= 0.0 || run.color.a == 0) && run.outline.is_none() {
        return;
    }
    let k = run.size / run.face.upem.max(1.0);
    // Colour glyphs (emoji) paint their own colours; an outline (text outline effect) still
    // strokes the plain glyph below.
    if alpha > 0.0
        && run.outline.is_none()
        && run.face.is_color()
        && colr::paint(ctx, &run.face, gid, at, k, run.color, alpha)
    {
        return;
    }
    let path = glyph(&run.face, gid);
    if path.elements().is_empty() {
        return;
    }
    let skew = if run.fake_italic {
        Affine::new([1.0, 0.0, -0.2, 1.0, 0.0, 0.0])
    } else {
        Affine::IDENTITY
    };
    ctx.set_transform(at * skew * Affine::scale(k));
    if let Some((g, to_text)) = fill
        && alpha > 0.0
    {
        ctx.set_paint(g.clone());
        ctx.set_paint_transform((skew * Affine::scale(k)).inverse() * to_text);
        ctx.fill_path(&path);
        ctx.reset_paint_transform();
    } else if alpha > 0.0 && run.color.a > 0 {
        ctx.set_paint(color(run.color, alpha));
        ctx.fill_path(&path);
        if run.fake_bold {
            ctx.set_stroke(kurbo::Stroke::new(run.face.upem * 0.025).with_join(kurbo::Join::Round));
            ctx.stroke_path(&path);
        }
    }
    if let Some((oc, ow)) = run.outline {
        ctx.set_paint(color(oc, opacity));
        ctx.set_stroke(kurbo::Stroke::new(ow / k.max(1e-9)).with_join(kurbo::Join::Round));
        ctx.stroke_path(&path);
    }
}

/// Render one shape alone onto a transparent canvas (drag previews, copy as picture).
pub fn render_shape(pres: &Presentation, slide: &Slide, id: ShapeId, scale: f64) -> Option<Image> {
    let rctx = Ctx::for_slide(pres, slide)?;
    let s = slide.shape(id)?;
    let x = resolve::xfrm(&rctx, s);
    let b = x.bounds().inflate(8.0, 8.0);
    let w = (b.width() * scale).ceil().clamp(1.0, MAX_SIDE as f64) as u16;
    let h = (b.height() * scale).ceil().clamp(1.0, MAX_SIDE as f64) as u16;
    let mut ctx = RenderContext::new_with(
        w,
        h,
        vello_cpu::RenderSettings {
            num_threads: 0,
            ..Default::default()
        },
    );
    let opts = RenderOpts {
        scale,
        ..Default::default()
    };
    let f = Frame {
        pres,
        opts: &opts,
        fields: SlideFields::new(1, String::new(), &opts),
    };
    let view = Affine::scale(scale) * Affine::translate(-b.origin().to_vec2());
    RENDERER.with(|r| {
        let mut r = r.borrow_mut();
        r.shape(&mut ctx, &f, &rctx, s, view, 0);
        Some(r.finish(ctx, w, h))
    })
}

#[cfg(test)]
mod tests;

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

    /// Regression: multithreaded rendering of a slide with shadows panicked inside vello_cpu.
    #[test]
    fn effects_render_with_threads_requested() {
        let mut s = deckcraft_model::Presentation::default();
        let sl = std::sync::Arc::make_mut(&mut s.slides[0]);
        let mut sh = deckcraft_model::Shape {
            id: ShapeId(900),
            xfrm: Some(deckcraft_geom::Xfrm::new(10.0, 10.0, 100.0, 100.0)),
            style: Some(deckcraft_model::ShapeStyle::accent(
                deckcraft_color::SchemeSlot::Accent1,
            )),
            ..Default::default()
        };
        sh.effects = Some(deckcraft_model::Effects {
            soft_edge: Some(5.0),
            ..Default::default()
        });
        sl.shapes.push(sh);
        assert!(needs_filters(&s, &s.slides[0]));
        let img = RENDERER.with(|r| {
            r.borrow_mut().slide(
                &s,
                &s.slides[0],
                0,
                &RenderOpts {
                    scale: 0.5,
                    threads: 4,
                    ..Default::default()
                },
            )
        });
        assert_eq!(img.width, 480);
        // The shape itself is drawn (not a blank fallback).
        assert_ne!(img.pixel(30, 30), [255, 255, 255, 255]);
    }
}