wilhelm_renderer 0.12.0

A minimalist 2D graphics engine
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
use crate::core::engine::opengl::{
    GL_POINTS, GL_TRIANGLE_FAN, GL_TRIANGLE_STRIP, GL_TRIANGLES, GLfloat, Vec2,
};
use crate::core::{
    Attribute, Color, FontAtlas, Geometry, Mesh, Renderable, Renderer, Shader,
    generate_texture_from_image, load_image,
};
use crate::graphics2d::shapes::{
    Arc as ArcShape, Circle, Ellipse, Image, Line, MultiPoint, Polygon, Polyline, Rectangle,
    RoundedRectangle, ShapeKind, Text, Triangle,
};
use crate::core::math::Mat4;
use std::cell::{OnceCell, RefCell};
use std::collections::HashMap;
use std::f32::consts::PI;
use std::rc::Rc;

const MIN_STROKE_WIDTH: f32 = 1.0;

/// Anchor point used for positioning, rotation, and scaling.
///
/// The anchor is the single point on the shape that:
/// - is placed at the coordinates passed to [`ShapeRenderable::set_position`],
/// - acts as the pivot for rotation,
/// - acts as the origin for scaling.
///
/// `Default` uses each shape's natural anchor. Compass variants resolve
/// against the shape's axis-aligned bounding box (North = top edge,
/// East = right edge, etc. in screen-space Y-down convention).
///
/// `Custom(x, y)` specifies an arbitrary point in the shape's local
/// coordinate space.
///
/// ## Natural defaults
///
/// - `Point`, `Circle`, `Ellipse`, `Image`, `Arc`: center
/// - `Rectangle`, `RoundedRectangle`: north-west corner (top-left)
/// - `Line`, `Polyline`, `Polygon`, `MultiPoint`: first vertex
/// - `Triangle`: centroid
/// - `Text`: north-west corner of the text cell
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Anchor {
    Default,
    Center,
    North,
    NorthEast,
    East,
    SouthEast,
    South,
    SouthWest,
    West,
    NorthWest,
    Custom(f32, f32),
}

/// Shared anchor resolution for Rectangle / RoundedRectangle (bbox = (0,0)..(w,h)).
/// Default is NorthWest (0, 0) in Y-down screen space.
fn rectangle_anchor(width: f32, height: f32, anchor: Anchor) -> (f32, f32) {
    resolve_anchor(
        anchor,
        (0.0, 0.0),
        (width, height),
        (0.0, 0.0),
    )
}

/// Axis-aligned bounding box over a slice of points. Panics on empty input.
fn bbox_of_points(points: &[(f32, f32)]) -> ((f32, f32), (f32, f32)) {
    let mut min_x = f32::INFINITY;
    let mut min_y = f32::INFINITY;
    let mut max_x = f32::NEG_INFINITY;
    let mut max_y = f32::NEG_INFINITY;
    for &(x, y) in points {
        if x < min_x { min_x = x; }
        if y < min_y { min_y = y; }
        if x > max_x { max_x = x; }
        if y > max_y { max_y = y; }
    }
    ((min_x, min_y), (max_x, max_y))
}

/// Resolve an `Anchor` to a point in the shape's local coordinate space,
/// given the shape's bbox and its natural default.
fn resolve_anchor(
    anchor: Anchor,
    bbox_min: (f32, f32),
    bbox_max: (f32, f32),
    default: (f32, f32),
) -> (f32, f32) {
    let (min_x, min_y) = bbox_min;
    let (max_x, max_y) = bbox_max;
    let cx = (min_x + max_x) * 0.5;
    let cy = (min_y + max_y) * 0.5;
    match anchor {
        Anchor::Default => default,
        Anchor::Center => (cx, cy),
        Anchor::North => (cx, min_y),
        Anchor::NorthEast => (max_x, min_y),
        Anchor::East => (max_x, cy),
        Anchor::SouthEast => (max_x, max_y),
        Anchor::South => (cx, max_y),
        Anchor::SouthWest => (min_x, max_y),
        Anchor::West => (min_x, cy),
        Anchor::NorthWest => (min_x, min_y),
        Anchor::Custom(x, y) => (x, y),
    }
}

#[derive(Clone, Debug)]
pub struct ShapeStyle {
    pub fill: Option<Color>,
    pub stroke_color: Option<Color>,
    pub stroke_width: Option<f32>,
    pub dash_pattern: Option<(f32, f32)>,
}

impl Default for ShapeStyle {
    fn default() -> Self {
        Self {
            fill: Some(Color::from_rgb(1.0, 1.0, 1.0)),
            stroke_color: None,
            stroke_width: None,
            dash_pattern: None,
        }
    }
}

impl ShapeStyle {
    pub fn fill(fill: Color) -> Self {
        Self {
            fill: Some(fill),
            stroke_color: None,
            stroke_width: None,
            dash_pattern: None,
        }
    }

    pub fn stroke(color: Color, width: f32) -> Self {
        Self {
            fill: None,
            stroke_color: Some(color),
            stroke_width: Some(width),
            dash_pattern: None,
        }
    }

    pub fn fill_and_stroke(fill: Color, stroke: Color, width: f32) -> Self {
        Self {
            fill: Some(fill),
            stroke_color: Some(stroke),
            stroke_width: Some(width),
            dash_pattern: None,
        }
    }

    pub fn dashed_stroke(color: Color, width: f32, dash: f32, gap: f32) -> Self {
        Self {
            fill: None,
            stroke_color: Some(color),
            stroke_width: Some(width),
            dash_pattern: Some((dash, gap)),
        }
    }

    pub fn with_dash(mut self, dash: f32, gap: f32) -> Self {
        self.dash_pattern = Some((dash, gap));
        self
    }
}

thread_local! {
    static DEFAULT_SHADER: OnceCell<Rc<Shader>> = OnceCell::new();
}

fn default_shader() -> Rc<Shader> {
    DEFAULT_SHADER.with(|cell| {
        cell.get_or_init(|| {
            let vert_src = include_str!("../shaders/shape.vert");
            let frag_src = include_str!("../shaders/shape.frag");
            Rc::new(
                Shader::compile(vert_src, frag_src, None)
                    .expect("Failed to compile default shader"),
            )
        })
        .clone()
    })
}

thread_local! {
    static DASHED_SHADER: OnceCell<Rc<Shader>> = OnceCell::new();
}

fn dashed_shader() -> Rc<Shader> {
    DASHED_SHADER.with(|cell| {
        cell.get_or_init(|| {
            let vert_src = include_str!("../shaders/dashed.vert");
            let frag_src = include_str!("../shaders/dashed.frag");
            Rc::new(
                Shader::compile(vert_src, frag_src, None)
                    .expect("Failed to compile dashed shader"),
            )
        })
        .clone()
    })
}

thread_local! {
    static POINT_SHADER: OnceCell<Rc<Shader>> = OnceCell::new();
}

fn point_shader() -> Rc<Shader> {
    POINT_SHADER.with(|cell| {
        cell.get_or_init(|| {
            let vert_src = include_str!("../shaders/shape.vert");
            let frag_src = include_str!("../shaders/point.frag");
            Rc::new(
                Shader::compile(vert_src, frag_src, None).expect("Failed to compile point shader"),
            )
        })
        .clone()
    })
}

thread_local! {
    static IMAGE_SHADER: OnceCell<Rc<Shader>> = OnceCell::new();
}
fn image_shader() -> Rc<Shader> {
    IMAGE_SHADER.with(|cell| {
        cell.get_or_init(|| {
            let vert_src = include_str!("../shaders/image.vert");
            let frag_src = include_str!("../shaders/image.frag");
            Rc::new(
                Shader::compile(vert_src, frag_src, None).expect("Failed to compile image shader"),
            )
        })
        .clone()
    })
}

thread_local! {
    static TEXT_SHADER: OnceCell<Rc<Shader>> = OnceCell::new();
}
fn text_shader() -> Rc<Shader> {
    TEXT_SHADER.with(|cell| {
        cell.get_or_init(|| {
            let vert_src = include_str!("../shaders/text.vert");
            let frag_src = include_str!("../shaders/text.frag");
            Rc::new(
                Shader::compile(vert_src, frag_src, None).expect("Failed to compile text shader"),
            )
        })
        .clone()
    })
}

/// Font cache key: (font_path, font_size)
type FontCacheKey = (String, u32);

thread_local! {
    /// Global font cache - shares FontAtlas instances across text renderables.
    /// Properly dropped when thread exits, no memory leaks.
    static FONT_CACHE: RefCell<HashMap<FontCacheKey, Rc<RefCell<FontAtlas>>>> = RefCell::new(HashMap::new());
}

/// Get or create a FontAtlas from the cache
fn get_or_create_font_atlas(font_path: &str, font_size: u32) -> Rc<RefCell<FontAtlas>> {
    FONT_CACHE.with(|cache| {
        let mut cache = cache.borrow_mut();
        let key = (font_path.to_string(), font_size);

        if let Some(atlas) = cache.get(&key) {
            return atlas.clone();
        }

        // Create new FontAtlas and cache it
        let atlas = FontAtlas::new(font_path, font_size, 512)
            .expect("Failed to create font atlas");
        let atlas_rc = Rc::new(RefCell::new(atlas));
        cache.insert(key, atlas_rc.clone());
        atlas_rc
    })
}

/// Clear the font cache, releasing all FontAtlas resources.
/// Call this when changing scenes or when fonts are no longer needed.
/// Safe to call at any time - new text will recreate atlases as needed.
pub fn clear_font_cache() {
    FONT_CACHE.with(|cache| {
        cache.borrow_mut().clear();
    });
}

fn ortho_2d(width: f32, height: f32) -> Mat4 {
    Mat4::orthographic_rh_gl(0.0, width, height, 0.0, -1.0, 1.0)
}
pub struct ShapeRenderable {
    x: f32,
    y: f32,
    scale: f32,
    rotation: f32,
    z_order: i32,
    mesh: Mesh,
    stroke_mesh: Option<Mesh>,
    shape: ShapeKind,
}
impl Renderable for ShapeRenderable {
    fn render(&mut self, renderer: &Renderer) {
        let (window_width, window_height) = renderer.window_handle.size();
        let transform = ortho_2d(window_width as f32, window_height as f32);
        self.mesh.set_transform(transform);
        self.mesh.set_scale(self.scale);
        self.mesh.set_rotation(self.rotation);

        if self.mesh.geometry.instance_count() > 0 {
            // instanced: u_offset = (0,0), positions come from attrib 1
            renderer.draw_mesh_instanced(&self.mesh);
        } else {
            // single: use u_offset
            self.mesh.set_screen_offset(self.x, self.y);
            renderer.draw_mesh(&self.mesh);
        }

        // Render stroke on top if present
        if let Some(stroke) = &mut self.stroke_mesh {
            stroke.set_transform(transform);
            stroke.set_scale(self.scale);
            stroke.set_rotation(self.rotation);

            if stroke.geometry.instance_count() > 0 {
                renderer.draw_mesh_instanced(stroke);
            } else {
                stroke.set_screen_offset(self.x, self.y);
                renderer.draw_mesh(stroke);
            }
        }
    }
}

impl ShapeRenderable {
    fn new(mesh: Mesh, shape: ShapeKind) -> Self {
        Self { x: 0.0, y: 0.0, scale: 1.0, rotation: 0.0, z_order: 0, mesh, stroke_mesh: None, shape }
    }

    fn new_with_stroke(mesh: Mesh, stroke_mesh: Mesh, shape: ShapeKind) -> Self {
        Self { x: 0.0, y: 0.0, scale: 1.0, rotation: 0.0, z_order: 0, mesh, stroke_mesh: Some(stroke_mesh), shape }
    }

    pub fn set_position(&mut self, x: f32, y: f32) -> &mut Self {
        self.x = x;
        self.y = y;
        self
    }

    pub fn x(&self) -> f32 {
        self.x
    }

    pub fn y(&self) -> f32 {
        self.y
    }

    pub fn position(&self) -> (f32, f32) {
        (self.x, self.y)
    }

    pub fn set_scale(&mut self, scale: f32) -> &mut Self {
        self.scale = scale;
        self
    }

    pub fn scale(&self) -> f32 {
        self.scale
    }

    pub fn set_rotation(&mut self, angle: f32) -> &mut Self {
        self.rotation = angle;
        self
    }

    pub fn rotation(&self) -> f32 {
        self.rotation
    }

    pub fn set_z_order(&mut self, z_order: i32) -> &mut Self {
        self.z_order = z_order;
        self
    }

    pub fn z_order(&self) -> i32 {
        self.z_order
    }

    pub fn set_fill_color(&mut self, color: Color) -> &mut Self {
        self.mesh.color = Some(color);
        self
    }

    pub fn set_stroke_color(&mut self, color: Color) -> &mut Self {
        if let Some(stroke) = &mut self.stroke_mesh {
            stroke.color = Some(color);
        }
        self
    }

    pub fn fill_color(&self) -> Option<Color> {
        self.mesh.color
    }

    pub fn stroke_color(&self) -> Option<Color> {
        self.stroke_mesh.as_ref().and_then(|s| s.color)
    }
    /// Construct a `ShapeRenderable` with the shape's default anchor.
    ///
    /// Equivalent to `ShapeRenderable::builder(shape, style).build()`.
    pub fn from_shape(shape: ShapeKind, style: ShapeStyle) -> Self {
        Self::builder(shape, style).build()
    }

    /// Start a builder that lets you override the anchor (and, later, other
    /// per-shape parameters) before constructing the `ShapeRenderable`.
    pub fn builder(shape: ShapeKind, style: ShapeStyle) -> ShapeRenderableBuilder {
        ShapeRenderableBuilder {
            shape,
            style,
            anchor: Anchor::Default,
        }
    }

    fn from_shape_with_anchor(shape: ShapeKind, style: ShapeStyle, anchor: Anchor) -> Self {
        match shape {
            ShapeKind::Point => {
                ShapeRenderable::point(style.fill.unwrap_or(Color::white()), anchor)
            }
            ShapeKind::MultiPoint(mp) => {
                ShapeRenderable::multi_points(mp, style.fill.unwrap_or(Color::white()), anchor)
            }
            ShapeKind::Line(line) => ShapeRenderable::line(
                line,
                style.stroke_color.unwrap_or_else(Color::white),
                style.stroke_width.unwrap_or(1.0),
                anchor,
                style.dash_pattern,
            ),
            ShapeKind::Polyline(poly_line) => ShapeRenderable::polyline(
                poly_line,
                style.stroke_color.unwrap_or(Color::white()),
                style.stroke_width.unwrap_or(1.0),
                anchor,
                style.dash_pattern,
            ),
            ShapeKind::Triangle(triangle) => match (style.fill, style.stroke_color) {
                (Some(fill), Some(stroke)) => ShapeRenderable::triangle_fill_and_stroke(
                    triangle, fill, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (None, Some(stroke)) => ShapeRenderable::triangle_outline(
                    triangle, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (fill, None) => ShapeRenderable::triangle(
                    triangle, fill.unwrap_or(Color::white()), anchor,
                ),
            },
            ShapeKind::Rectangle(rect) => match (style.fill, style.stroke_color) {
                (Some(fill), Some(stroke)) => ShapeRenderable::rectangle_fill_and_stroke(
                    rect,
                    fill,
                    stroke,
                    style.stroke_width.unwrap_or(1.0),
                    anchor,
                    style.dash_pattern,
                ),
                (None, Some(stroke)) => ShapeRenderable::rectangle_outline(
                    rect,
                    stroke,
                    style.stroke_width.unwrap_or(1.0),
                    anchor,
                    style.dash_pattern,
                ),
                (fill, None) => ShapeRenderable::rectangle(
                    rect,
                    fill.unwrap_or(Color::white()),
                    anchor,
                ),
            },
            ShapeKind::RoundedRectangle(rr) => match (style.fill, style.stroke_color) {
                (Some(fill), Some(stroke)) => ShapeRenderable::rounded_rectangle_fill_and_stroke(
                    rr, fill, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (None, Some(stroke)) => ShapeRenderable::rounded_rectangle_outline(
                    rr, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (fill, None) => ShapeRenderable::rounded_rectangle(
                    rr, fill.unwrap_or(Color::white()), anchor,
                ),
            },
            ShapeKind::Polygon(polygon) => match (style.fill, style.stroke_color) {
                (Some(fill), Some(stroke)) => ShapeRenderable::polygon_fill_and_stroke(
                    polygon, fill, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (None, Some(stroke)) => ShapeRenderable::polygon_outline(
                    polygon, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (fill, None) => ShapeRenderable::polygon(
                    polygon, fill.unwrap_or(Color::white()), anchor,
                ),
            },
            ShapeKind::Circle(circle) => match (style.fill, style.stroke_color) {
                (Some(fill), Some(stroke)) => ShapeRenderable::circle_fill_and_stroke(
                    circle, fill, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (None, Some(stroke)) => ShapeRenderable::circle_outline(
                    circle, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (fill, None) => ShapeRenderable::circle(
                    circle, fill.unwrap_or(Color::white()), anchor,
                ),
            },
            ShapeKind::Ellipse(ellipse) => match (style.fill, style.stroke_color) {
                (Some(fill), Some(stroke)) => ShapeRenderable::ellipse_fill_and_stroke(
                    ellipse, fill, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (None, Some(stroke)) => ShapeRenderable::ellipse_outline(
                    ellipse, stroke, style.stroke_width.unwrap_or(1.0), anchor, style.dash_pattern,
                ),
                (fill, None) => ShapeRenderable::ellipse(
                    ellipse, fill.unwrap_or(Color::white()), anchor,
                ),
            },
            ShapeKind::Arc(arc) => ShapeRenderable::arc(
                arc,
                style.stroke_color.unwrap_or(Color::white()),
                style.stroke_width.unwrap_or(1.0),
                anchor,
                style.dash_pattern,
            ),
            ShapeKind::Image(_) => {
                unimplemented!("ShapeRenderable::from_shape cannot create Image without path")
            }
            ShapeKind::Text(text) => ShapeRenderable::text(
                text,
                style.fill.unwrap_or(Color::white()),
                anchor,
            ),
        }
    }

    pub fn create_multiple_instances(&mut self, capacity: usize) {
        self.mesh.geometry.enable_instancing_xy(capacity);
        if let Some(stroke) = &mut self.stroke_mesh {
            stroke.geometry.enable_instancing_xy(capacity);
        }
    }

    pub fn set_instance_positions(&mut self, positions: &[Vec2]) -> &mut Self {
        self.mesh.geometry.update_instance_xy(positions);
        if let Some(stroke) = &mut self.stroke_mesh {
            stroke.geometry.update_instance_xy(positions);
        }
        self
    }

    pub fn set_instance_colors(&mut self, colors: &[Color]) -> &mut Self {
        self.mesh.geometry.update_instance_colors(colors);
        self
    }

    pub fn set_instance_stroke_colors(&mut self, colors: &[Color]) -> &mut Self {
        if let Some(stroke) = &mut self.stroke_mesh {
            stroke.geometry.update_instance_colors(colors);
        }
        self
    }

    pub fn clear_instances(&mut self) {
        self.mesh.geometry.clear_instancing();
        if let Some(stroke) = &mut self.stroke_mesh {
            stroke.geometry.clear_instancing();
        }
    }

    fn point(color: Color, _anchor: Anchor) -> Self {
        // Point has only one vertex at (0, 0); anchor is trivially that point
        // for every variant (bbox is degenerate). Ignore the anchor.
        let geometry = ShapeRenderable::point_geometry();
        let mesh = Mesh::with_color(point_shader(), geometry, Some(color));
        ShapeRenderable::new(mesh, ShapeKind::Point)
    }

    fn multi_points(multi_point: MultiPoint, color: Color, anchor: Anchor) -> Self {
        assert!(!multi_point.points.is_empty(), "MultiPoint requires at least one point");

        let (bbox_min, bbox_max) = bbox_of_points(&multi_point.points);
        let default = multi_point.points[0];
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let rel_points: Vec<(GLfloat, GLfloat)> = multi_point
            .points
            .iter()
            .map(|(px, py)| (px - ax, py - ay))
            .collect();

        let geometry = ShapeRenderable::point_list_geometry(&rel_points);
        let mesh = Mesh::with_color(point_shader(), geometry, Some(color));

        let mut s = ShapeRenderable::new(mesh, ShapeKind::MultiPoint(multi_point));
        s.x = ax;
        s.y = ay;
        s
    }

    fn line(shape: Line, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let (x1, y1) = shape.start;
        let (x2, y2) = shape.end;
        let bbox_min = (x1.min(x2), y1.min(y2));
        let bbox_max = (x1.max(x2), y1.max(y2));
        let default = (x1, y1); // start point
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let (geometry, shader) = if let Some(_) = dash_pattern {
            (ShapeRenderable::line_geometry_dashed(x1 - ax, y1 - ay, x2 - ax, y2 - ay, stroke_width), dashed_shader())
        } else {
            (ShapeRenderable::line_geometry(x1 - ax, y1 - ay, x2 - ax, y2 - ay, stroke_width), default_shader())
        };
        let mut mesh = Mesh::with_color(shader, geometry, Some(stroke));
        if let Some((dash, gap)) = dash_pattern {
            mesh.dash_pattern = Some((dash, gap));
        }

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Line(shape));
        s.x = ax;
        s.y = ay;
        s
    }

    fn polyline(polyline: Polyline, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        assert!(polyline.points.len() >= 2, "Polyline requires at least two points");

        let (bbox_min, bbox_max) = bbox_of_points(&polyline.points);
        let default = polyline.points[0];
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let rel_points: Vec<(f32, f32)> = polyline
            .points
            .iter()
            .map(|(px, py)| (px - ax, py - ay))
            .collect();

        let (geometry, shader) = if let Some(_) = dash_pattern {
            (ShapeRenderable::polyline_geometry_dashed(&rel_points, stroke_width), dashed_shader())
        } else {
            (ShapeRenderable::polyline_geometry(&rel_points, stroke_width), default_shader())
        };
        let mut mesh = Mesh::with_color(shader, geometry, Some(stroke));
        if let Some((dash, gap)) = dash_pattern {
            mesh.dash_pattern = Some((dash, gap));
        }

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Polyline(polyline));
        s.x = ax;
        s.y = ay;
        s
    }

    fn arc(arc: ArcShape, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        use std::f32::consts::TAU;

        let segments = 64;

        // Normalize sweep to [0, TAU)
        let mut sweep = arc.end_angle - arc.start_angle;
        if sweep < 0.0 {
            sweep += TAU;
        }

        // Generate points around the arc's center (local origin).
        let mut points = Vec::with_capacity(segments + 1);
        for i in 0..=segments {
            let t = i as f32 / segments as f32;
            let theta = arc.start_angle + t * sweep;
            let px = arc.radius * theta.cos();
            let py = -arc.radius * theta.sin();
            points.push((px, py));
        }

        // Bbox over the curve points; default anchor is the arc's circle center (0, 0).
        let (bbox_min, bbox_max) = bbox_of_points(&points);
        let default = (0.0, 0.0);
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let shifted: Vec<(f32, f32)> =
            points.iter().map(|(x, y)| (x - ax, y - ay)).collect();
        let (geometry, shader) = if let Some(_) = dash_pattern {
            (ShapeRenderable::polyline_geometry_dashed(&shifted, stroke_width), dashed_shader())
        } else {
            (ShapeRenderable::polyline_geometry(&shifted, stroke_width), default_shader())
        };
        let mut mesh = Mesh::with_color(shader, geometry, Some(stroke));
        if let Some((dash, gap)) = dash_pattern {
            mesh.dash_pattern = Some((dash, gap));
        }

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Arc(arc));
        s.x = ax;
        s.y = ay;
        s
    }

    /// Build a stroke mesh from a closed polyline, choosing solid or dashed rendering.
    fn stroke_mesh_from_outline(
        points: &mut Vec<(f32, f32)>,
        stroke: Color,
        stroke_width: f32,
        dash_pattern: Option<(f32, f32)>,
    ) -> Mesh {
        // For solid strokes, add a direction hint (second point) for proper miter at closing corner.
        // For dashed strokes, skip it — the overlap would fill in gaps.
        if dash_pattern.is_none() {
            if let Some(&second) = points.get(1) {
                points.push(second);
            }
        }

        let (geometry, shader) = if let Some(_) = dash_pattern {
            (ShapeRenderable::polyline_geometry_dashed(points, stroke_width), dashed_shader())
        } else {
            (ShapeRenderable::polyline_geometry(points, stroke_width), default_shader())
        };
        let mut mesh = Mesh::with_color(shader, geometry, Some(stroke));
        if let Some((dash, gap)) = dash_pattern {
            mesh.dash_pattern = Some((dash, gap));
        }
        mesh
    }

    fn triangle(triangle: Triangle, color: Color, anchor: Anchor) -> Self {
        let [v0, v1, v2] = triangle.vertices;
        let bbox_min = (v0.0.min(v1.0).min(v2.0), v0.1.min(v1.1).min(v2.1));
        let bbox_max = (v0.0.max(v1.0).max(v2.0), v0.1.max(v1.1).max(v2.1));
        let default = triangle.centroid();
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let shifted = [
            (v0.0 - ax, v0.1 - ay),
            (v1.0 - ax, v1.1 - ay),
            (v2.0 - ax, v2.1 - ay),
        ];
        let geometry = ShapeRenderable::triangle_geometry(&shifted);
        let mesh = Mesh::with_color(default_shader(), geometry, Some(color));

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Triangle(triangle));
        s.x = ax;
        s.y = ay;
        s
    }

    fn triangle_outline(triangle: Triangle, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let [v0, v1, v2] = triangle.vertices;
        let bbox_min = (v0.0.min(v1.0).min(v2.0), v0.1.min(v1.1).min(v2.1));
        let bbox_max = (v0.0.max(v1.0).max(v2.0), v0.1.max(v1.1).max(v2.1));
        let default = triangle.centroid();
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let mut points = vec![
            (v0.0 - ax, v0.1 - ay),
            (v1.0 - ax, v1.1 - ay),
            (v2.0 - ax, v2.1 - ay),
            (v0.0 - ax, v0.1 - ay), // close
        ];
        let mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Triangle(triangle));
        s.x = ax;
        s.y = ay;
        s
    }

    fn triangle_fill_and_stroke(triangle: Triangle, fill: Color, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let [v0, v1, v2] = triangle.vertices;
        let bbox_min = (v0.0.min(v1.0).min(v2.0), v0.1.min(v1.1).min(v2.1));
        let bbox_max = (v0.0.max(v1.0).max(v2.0), v0.1.max(v1.1).max(v2.1));
        let default = triangle.centroid();
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let shifted = [
            (v0.0 - ax, v0.1 - ay),
            (v1.0 - ax, v1.1 - ay),
            (v2.0 - ax, v2.1 - ay),
        ];
        let fill_geometry = ShapeRenderable::triangle_geometry(&shifted);
        let fill_mesh = Mesh::with_color(default_shader(), fill_geometry, Some(fill));

        let mut points = vec![shifted[0], shifted[1], shifted[2], shifted[0]];
        let stroke_mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new_with_stroke(fill_mesh, stroke_mesh, ShapeKind::Triangle(triangle));
        s.x = ax;
        s.y = ay;
        s
    }

    fn rectangle(rect: Rectangle, color: Color, anchor: Anchor) -> Self {
        let (ax, ay) = rectangle_anchor(rect.width, rect.height, anchor);
        let geometry = ShapeRenderable::rectangle_geometry(rect.width, rect.height, ax, ay);
        let mesh = Mesh::with_color(default_shader(), geometry, Some(color));

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Rectangle(rect));
        s.x = ax;
        s.y = ay;
        s
    }

    fn rectangle_outline(rect: Rectangle, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let (ax, ay) = rectangle_anchor(rect.width, rect.height, anchor);
        // Closed polyline in the same local frame as the fill, shifted by anchor.
        // The direction hint (6th point) retraces the top edge for a proper miter join.
        // For dashed lines, skip it — the overlap would fill in the gaps on the top edge.
        let mut points = vec![
            (0.0 - ax, 0.0 - ay),
            (rect.width - ax, 0.0 - ay),
            (rect.width - ax, rect.height - ay),
            (0.0 - ax, rect.height - ay),
            (0.0 - ax, 0.0 - ay),                // close the loop
        ];
        if dash_pattern.is_none() {
            points.push((rect.width - ax, 0.0 - ay)); // direction hint for join at closing corner
        }

        let (geometry, shader) = if let Some(_) = dash_pattern {
            (ShapeRenderable::polyline_geometry_dashed(&points, stroke_width), dashed_shader())
        } else {
            (ShapeRenderable::polyline_geometry(&points, stroke_width), default_shader())
        };
        let mut mesh = Mesh::with_color(shader, geometry, Some(stroke));
        if let Some((dash, gap)) = dash_pattern {
            mesh.dash_pattern = Some((dash, gap));
        }

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Rectangle(rect));
        s.x = ax;
        s.y = ay;
        s
    }

    fn rectangle_fill_and_stroke(
        rect: Rectangle,
        fill: Color,
        stroke: Color,
        stroke_width: f32,
        anchor: Anchor,
        dash_pattern: Option<(f32, f32)>,
    ) -> Self {
        let (ax, ay) = rectangle_anchor(rect.width, rect.height, anchor);

        let fill_geometry = ShapeRenderable::rectangle_geometry(rect.width, rect.height, ax, ay);
        let fill_mesh = Mesh::with_color(default_shader(), fill_geometry, Some(fill));

        let mut points = vec![
            (0.0 - ax, 0.0 - ay),
            (rect.width - ax, 0.0 - ay),
            (rect.width - ax, rect.height - ay),
            (0.0 - ax, rect.height - ay),
            (0.0 - ax, 0.0 - ay),
        ];
        if dash_pattern.is_none() {
            points.push((rect.width - ax, 0.0 - ay));
        }
        let (stroke_geometry, shader) = if let Some(_) = dash_pattern {
            (ShapeRenderable::polyline_geometry_dashed(&points, stroke_width), dashed_shader())
        } else {
            (ShapeRenderable::polyline_geometry(&points, stroke_width), default_shader())
        };
        let mut stroke_mesh = Mesh::with_color(shader, stroke_geometry, Some(stroke));
        if let Some((dash, gap)) = dash_pattern {
            stroke_mesh.dash_pattern = Some((dash, gap));
        }

        let mut s = ShapeRenderable::new_with_stroke(
            fill_mesh,
            stroke_mesh,
            ShapeKind::Rectangle(rect),
        );
        s.x = ax;
        s.y = ay;
        s
    }

    fn rounded_rectangle(rr: RoundedRectangle, color: Color, anchor: Anchor) -> Self {
        let (ax, ay) = rectangle_anchor(rr.width, rr.height, anchor);
        let geometry =
            ShapeRenderable::rounded_rectangle_geometry(rr.width, rr.height, rr.radius, 8, ax, ay);
        let mesh = Mesh::with_color(default_shader(), geometry, Some(color));

        let mut s = ShapeRenderable::new(mesh, ShapeKind::RoundedRectangle(rr));
        s.x = ax;
        s.y = ay;
        s
    }

    fn rounded_rectangle_outline(rr: RoundedRectangle, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let (ax, ay) = rectangle_anchor(rr.width, rr.height, anchor);
        let mut points = ShapeRenderable::rounded_rectangle_outline_points(rr.width, rr.height, rr.radius, 8, ax, ay);
        let mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new(mesh, ShapeKind::RoundedRectangle(rr));
        s.x = ax;
        s.y = ay;
        s
    }

    fn rounded_rectangle_fill_and_stroke(rr: RoundedRectangle, fill: Color, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let (ax, ay) = rectangle_anchor(rr.width, rr.height, anchor);

        let fill_geometry = ShapeRenderable::rounded_rectangle_geometry(rr.width, rr.height, rr.radius, 8, ax, ay);
        let fill_mesh = Mesh::with_color(default_shader(), fill_geometry, Some(fill));

        let mut points = ShapeRenderable::rounded_rectangle_outline_points(rr.width, rr.height, rr.radius, 8, ax, ay);
        let stroke_mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new_with_stroke(fill_mesh, stroke_mesh, ShapeKind::RoundedRectangle(rr));
        s.x = ax;
        s.y = ay;
        s
    }

    fn polygon(polygon: Polygon, color: Color, anchor: Anchor) -> Self {
        assert!(polygon.points.len() >= 3, "Polygon requires at least 3 points");

        let (bbox_min, bbox_max) = bbox_of_points(&polygon.points);
        let default = polygon.points[0];
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let rel_points: Vec<(f32, f32)> = polygon
            .points
            .iter()
            .map(|(px, py)| (px - ax, py - ay))
            .collect();
        let triangles = polygon.triangulate();

        let geometry = ShapeRenderable::polygon_geometry(&rel_points, &triangles);
        let mesh = Mesh::with_color(default_shader(), geometry, Some(color));

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Polygon(polygon));
        s.x = ax;
        s.y = ay;
        s
    }

    fn polygon_outline(polygon: Polygon, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        assert!(polygon.points.len() >= 3, "Polygon requires at least 3 points");

        let (bbox_min, bbox_max) = bbox_of_points(&polygon.points);
        let default = polygon.points[0];
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let mut points: Vec<(f32, f32)> = polygon.points.iter().map(|(px, py)| (px - ax, py - ay)).collect();
        points.push(points[0]); // close
        let mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Polygon(polygon));
        s.x = ax;
        s.y = ay;
        s
    }

    fn polygon_fill_and_stroke(polygon: Polygon, fill: Color, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        assert!(polygon.points.len() >= 3, "Polygon requires at least 3 points");

        let (bbox_min, bbox_max) = bbox_of_points(&polygon.points);
        let default = polygon.points[0];
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, default);

        let rel_points: Vec<(f32, f32)> = polygon.points.iter().map(|(px, py)| (px - ax, py - ay)).collect();
        let triangles = polygon.triangulate();
        let fill_geometry = ShapeRenderable::polygon_geometry(&rel_points, &triangles);
        let fill_mesh = Mesh::with_color(default_shader(), fill_geometry, Some(fill));

        let mut outline = rel_points.clone();
        outline.push(outline[0]); // close
        let stroke_mesh = ShapeRenderable::stroke_mesh_from_outline(&mut outline, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new_with_stroke(fill_mesh, stroke_mesh, ShapeKind::Polygon(polygon));
        s.x = ax;
        s.y = ay;
        s
    }

    fn circle(circle: Circle, color: Color, anchor: Anchor) -> Self {
        let r = circle.radius;
        let (ax, ay) = resolve_anchor(anchor, (-r, -r), (r, r), (0.0, 0.0));
        let geometry = ShapeRenderable::circle_geometry(r, 100, ax, ay);
        let mesh = Mesh::with_color(default_shader(), geometry, Some(color));

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Circle(circle));
        s.x = ax;
        s.y = ay;
        s
    }

    fn circle_outline(circle: Circle, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let r = circle.radius;
        let (ax, ay) = resolve_anchor(anchor, (-r, -r), (r, r), (0.0, 0.0));
        let mut points = ShapeRenderable::circle_outline_points(r, 100, ax, ay);
        let mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Circle(circle));
        s.x = ax;
        s.y = ay;
        s
    }

    fn circle_fill_and_stroke(circle: Circle, fill: Color, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let r = circle.radius;
        let (ax, ay) = resolve_anchor(anchor, (-r, -r), (r, r), (0.0, 0.0));

        let fill_geometry = ShapeRenderable::circle_geometry(r, 100, ax, ay);
        let fill_mesh = Mesh::with_color(default_shader(), fill_geometry, Some(fill));

        let mut points = ShapeRenderable::circle_outline_points(r, 100, ax, ay);
        let stroke_mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new_with_stroke(fill_mesh, stroke_mesh, ShapeKind::Circle(circle));
        s.x = ax;
        s.y = ay;
        s
    }

    fn ellipse(ellipse: Ellipse, color: Color, anchor: Anchor) -> Self {
        let rx = ellipse.radius_x;
        let ry = ellipse.radius_y;
        let (ax, ay) = resolve_anchor(anchor, (-rx, -ry), (rx, ry), (0.0, 0.0));
        let geometry = ShapeRenderable::ellipse_geometry(rx, ry, 64, ax, ay);
        let mesh = Mesh::with_color(default_shader(), geometry, Some(color));

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Ellipse(ellipse));
        s.x = ax;
        s.y = ay;
        s
    }

    fn ellipse_outline(ellipse: Ellipse, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let rx = ellipse.radius_x;
        let ry = ellipse.radius_y;
        let (ax, ay) = resolve_anchor(anchor, (-rx, -ry), (rx, ry), (0.0, 0.0));
        let mut points = ShapeRenderable::ellipse_outline_points(rx, ry, 64, ax, ay);
        let mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Ellipse(ellipse));
        s.x = ax;
        s.y = ay;
        s
    }

    fn ellipse_fill_and_stroke(ellipse: Ellipse, fill: Color, stroke: Color, stroke_width: f32, anchor: Anchor, dash_pattern: Option<(f32, f32)>) -> Self {
        let rx = ellipse.radius_x;
        let ry = ellipse.radius_y;
        let (ax, ay) = resolve_anchor(anchor, (-rx, -ry), (rx, ry), (0.0, 0.0));

        let fill_geometry = ShapeRenderable::ellipse_geometry(rx, ry, 64, ax, ay);
        let fill_mesh = Mesh::with_color(default_shader(), fill_geometry, Some(fill));

        let mut points = ShapeRenderable::ellipse_outline_points(rx, ry, 64, ax, ay);
        let stroke_mesh = ShapeRenderable::stroke_mesh_from_outline(&mut points, stroke, stroke_width, dash_pattern);

        let mut s = ShapeRenderable::new_with_stroke(fill_mesh, stroke_mesh, ShapeKind::Ellipse(ellipse));
        s.x = ax;
        s.y = ay;
        s
    }

    fn text(text: Text, color: Color, anchor: Anchor) -> Self {
        let font_atlas = get_or_create_font_atlas(&text.font_path, text.font_size);

        // Generate raw glyph vertices and compute the bbox in one pass.
        let (mut vertices, bbox_min, bbox_max, texture_id) = {
            let mut atlas = font_atlas.borrow_mut();
            let (vs, bmin, bmax) = ShapeRenderable::text_raw_vertices(&text.content, &mut atlas);
            let tex = atlas.texture_id();
            (vs, bmin, bmax, tex)
        };

        // Default anchor for Text is the top-left of the text cell (raw origin).
        let (ax, ay) = resolve_anchor(anchor, bbox_min, bbox_max, (0.0, 0.0));

        // Apply the anchor shift to the position components (x, y at stride 4).
        if ax != 0.0 || ay != 0.0 {
            let stride = 4usize;
            let mut i = 0;
            while i + 1 < vertices.len() {
                vertices[i] -= ax;
                vertices[i + 1] -= ay;
                i += stride;
            }
        }

        let mut geometry = Geometry::new(GL_TRIANGLES);
        geometry.add_buffer(&vertices, 4);
        geometry.add_vertex_attribute(Attribute::new(0, 2, 4, 0));
        geometry.add_vertex_attribute(Attribute::new(1, 2, 4, 2));

        let shader = text_shader();
        let mut mesh = Mesh::with_texture(shader, geometry, Some(texture_id));
        mesh.color = Some(color);

        let mut s = ShapeRenderable::new(mesh, ShapeKind::Text(text));
        s.x = ax;
        s.y = ay;
        s
    }

    pub fn image_with_size(path: &str, width: f32, height: f32) -> ShapeRenderable {
        Self::image_with_size_and_anchor(path, width, height, Anchor::Default)
    }

    fn image_with_size_and_anchor(
        path: &str,
        width: f32,
        height: f32,
        anchor: Anchor,
    ) -> ShapeRenderable {
        let image = load_image(path);
        let texture_id = generate_texture_from_image(&image);

        // Image geometry is built centered on origin, so bbox = (-w/2..w/2, -h/2..h/2)
        let hw = width * 0.5;
        let hh = height * 0.5;
        let (ax, ay) = resolve_anchor(anchor, (-hw, -hh), (hw, hh), (0.0, 0.0));

        let geometry = ShapeRenderable::image_geometry(width, height, ax, ay);
        let shader = image_shader();
        let mesh = Mesh::with_texture(shader, geometry, Some(texture_id));

        let mut s =
            ShapeRenderable::new(mesh, ShapeKind::Image(Image::new(width, height)));
        s.x = ax;
        s.y = ay;
        s
    }

    pub fn image(path: &str) -> Self {
        let image = load_image(path);
        Self::image_with_size(path, image.width as f32, image.height as f32)
    }

    fn point_geometry() -> Geometry {
        let vertex = vec![0.0, 0.0];
        let mut geometry = Geometry::new(GL_POINTS);
        geometry.add_buffer(&vertex, 2);

        geometry.add_vertex_attribute(Attribute::new(0, 2, 2, 0));

        geometry
    }

    fn point_list_geometry(points: &[(GLfloat, GLfloat)]) -> Geometry {
        let mut vertices = Vec::with_capacity(points.len() * 2);

        for &(x, y) in points {
            vertices.push(x);
            vertices.push(y);
        }

        let values_per_vertex = 2;

        let mut geometry = Geometry::new(GL_POINTS);
        geometry.add_buffer(&vertices, values_per_vertex);

        geometry.add_vertex_attribute(Attribute::new(
            0, // position
            values_per_vertex,
            values_per_vertex as usize,
            0,
        ));

        geometry
    }

    fn line_geometry(
        x1: GLfloat,
        y1: GLfloat,
        x2: GLfloat,
        y2: GLfloat,
        stroke_width: f32,
    ) -> Geometry {
        let stroke_width = stroke_width.max(MIN_STROKE_WIDTH);
        let dx = x2 - x1;
        let dy = y2 - y1;
        let length = (dx * dx + dy * dy).sqrt();

        if length == 0.0 {
            return Geometry::new(GL_TRIANGLES);
        }

        // Unit perpendicular vector
        let nx = -dy / length;
        let ny = dx / length;
        let half_thickness = stroke_width / 2.0;

        // Offset vector
        let ox = nx * half_thickness;
        let oy = ny * half_thickness;

        // Four corners of the quad
        let v0 = [x1 - ox, y1 - oy];
        let v1 = [x2 - ox, y2 - oy];
        let v2 = [x2 + ox, y2 + oy];
        let v3 = [x1 + ox, y1 + oy];

        let vertices: Vec<GLfloat> = vec![
            v0[0], v0[1], v1[0], v1[1], v2[0], v2[1], v2[0], v2[1], v3[0], v3[1], v0[0], v0[1],
        ];

        let position_values_per_vertex = 2;

        let mut geometry = Geometry::new(GL_TRIANGLES);
        geometry.add_buffer(&vertices, position_values_per_vertex);
        geometry.add_vertex_attribute(Attribute::new(
            0,
            position_values_per_vertex,
            position_values_per_vertex as usize,
            0,
        ));

        geometry
    }

    /// Polyline triangulation adapted from JVPolyline by Julien Vernay (2025)
    ///
    /// Original C implementation:
    /// https://jvernay.fr/en/blog/polyline-triangulation/
    /// Source: https://git.sr.ht/~jvernay/JV/tree/main/item/src/jv_polyline/jv_polyline.c
    ///
    /// This implementation is based on the original algorithm,
    /// restructured and translated to idiomatic Rust for use in wilhelm_renderer.
    fn polyline_geometry(points: &[(GLfloat, GLfloat)], stroke_width: f32) -> Geometry {
        const MITER_LIMIT: f32 = 4.0; // Equivalent to JV default

        if points.len() < 2 {
            return Geometry::new(GL_TRIANGLES);
        }

        let half_thickness = stroke_width.max(1.0) / 2.0;
        let miter_limit_squared = (stroke_width * MITER_LIMIT).powi(2) / 4.0;
        let mut vertices: Vec<GLfloat> = Vec::new();

        let mut a = points[0];
        let mut b = points[1];

        let mut idx = 1;
        while idx < points.len() && (b.0 - a.0).hypot(b.1 - a.1) == 0.0 {
            idx += 1;
            if idx < points.len() {
                b = points[idx];
            }
        }
        if (b.0 - a.0).hypot(b.1 - a.1) == 0.0 {
            return Geometry::new(GL_TRIANGLES);
        }

        for i in idx + 1..=points.len() {
            let c = if i < points.len() { points[i] } else { a }; // fake point if last

            let ab = (b.0 - a.0, b.1 - a.1);
            let len_ab = (ab.0 * ab.0 + ab.1 * ab.1).sqrt();
            let normal_ab = (
                -ab.1 / len_ab * half_thickness,
                ab.0 / len_ab * half_thickness,
            );

            let a1 = (a.0 + normal_ab.0, a.1 + normal_ab.1);
            let a2 = (a.0 - normal_ab.0, a.1 - normal_ab.1);
            let b1 = (b.0 + normal_ab.0, b.1 + normal_ab.1);
            let b2 = (b.0 - normal_ab.0, b.1 - normal_ab.1);

            // segment quad
            vertices.extend_from_slice(&[
                a1.0, a1.1, a2.0, a2.1, b1.0, b1.1, a2.0, a2.1, b1.0, b1.1, b2.0, b2.1,
            ]);

            let bc = (c.0 - b.0, c.1 - b.1);
            let len_bc = (bc.0 * bc.0 + bc.1 * bc.1).sqrt();
            if len_bc > 0.0 {
                let normal_bc = (
                    -bc.1 / len_bc * half_thickness,
                    bc.0 / len_bc * half_thickness,
                );
                let b3 = (b.0 + normal_bc.0, b.1 + normal_bc.1);
                let b4 = (b.0 - normal_bc.0, b.1 - normal_bc.1);

                // turn direction
                let z = ab.0 * bc.1 - ab.1 * bc.0;

                // bevel join
                if z < 0.0 {
                    vertices.extend_from_slice(&[b.0, b.1, b1.0, b1.1, b3.0, b3.1]);
                } else if z > 0.0 {
                    vertices.extend_from_slice(&[b.0, b.1, b2.0, b2.1, b4.0, b4.1]);
                }

                // optional miter
                if z != 0.0 {
                    let (a_j, b_j, norm_j) = if z < 0.0 { (a1, b3, ab) } else { (a2, b4, ab) };

                    let denom = z;
                    let alpha = (bc.1 * (b_j.0 - a_j.0) + bc.0 * (a_j.1 - b_j.1)) / denom;
                    let mx = a_j.0 + alpha * norm_j.0;
                    let my = a_j.1 + alpha * norm_j.1;

                    let dist2 = (mx - b.0).powi(2) + (my - b.1).powi(2);
                    if dist2 <= miter_limit_squared {
                        if z < 0.0 {
                            vertices.extend_from_slice(&[mx, my, b1.0, b1.1, b3.0, b3.1]);
                        } else {
                            vertices.extend_from_slice(&[mx, my, b2.0, b2.1, b4.0, b4.1]);
                        }
                    }
                }
            }

            a = b;
            b = c;
        }

        let mut geometry = Geometry::new(GL_TRIANGLES);
        geometry.add_buffer(&vertices, 2);
        geometry.add_vertex_attribute(Attribute::new(0, 2, 2, 0));
        geometry
    }

    fn line_geometry_dashed(
        x1: GLfloat,
        y1: GLfloat,
        x2: GLfloat,
        y2: GLfloat,
        stroke_width: f32,
    ) -> Geometry {
        let stroke_width = stroke_width.max(MIN_STROKE_WIDTH);
        let dx = x2 - x1;
        let dy = y2 - y1;
        let length = (dx * dx + dy * dy).sqrt();

        if length == 0.0 {
            return Geometry::new(GL_TRIANGLES);
        }

        let nx = -dy / length;
        let ny = dx / length;
        let half_thickness = stroke_width / 2.0;

        let ox = nx * half_thickness;
        let oy = ny * half_thickness;

        // Four corners with distance: start=0, end=length
        // Layout: x, y, dist per vertex
        let vertices: Vec<GLfloat> = vec![
            x1 - ox, y1 - oy, 0.0,
            x2 - ox, y2 - oy, length,
            x2 + ox, y2 + oy, length,
            x2 + ox, y2 + oy, length,
            x1 + ox, y1 + oy, 0.0,
            x1 - ox, y1 - oy, 0.0,
        ];

        let mut geometry = Geometry::new(GL_TRIANGLES);
        geometry.add_buffer(&vertices, 3);
        geometry.add_vertex_attribute(Attribute::new(0, 2, 3, 0)); // vec2 position
        geometry.add_vertex_attribute(Attribute::new(3, 1, 3, 2)); // float distance
        geometry
    }

    fn polyline_geometry_dashed(points: &[(GLfloat, GLfloat)], stroke_width: f32) -> Geometry {
        const MITER_LIMIT: f32 = 4.0;

        if points.len() < 2 {
            return Geometry::new(GL_TRIANGLES);
        }

        let half_thickness = stroke_width.max(1.0) / 2.0;
        let miter_limit_squared = (stroke_width * MITER_LIMIT).powi(2) / 4.0;
        let mut vertices: Vec<GLfloat> = Vec::new();

        // Pre-compute cumulative distances
        let mut cum_dist = vec![0.0f32; points.len()];
        for i in 1..points.len() {
            let dx = points[i].0 - points[i - 1].0;
            let dy = points[i].1 - points[i - 1].1;
            cum_dist[i] = cum_dist[i - 1] + (dx * dx + dy * dy).sqrt();
        }

        let mut a = points[0];
        let mut b = points[1];
        let mut a_idx = 0usize;
        let mut b_idx = 1usize;

        let mut idx = 1;
        while idx < points.len() && (b.0 - a.0).hypot(b.1 - a.1) == 0.0 {
            idx += 1;
            if idx < points.len() {
                b = points[idx];
                b_idx = idx;
            }
        }
        if (b.0 - a.0).hypot(b.1 - a.1) == 0.0 {
            return Geometry::new(GL_TRIANGLES);
        }

        for i in idx + 1..=points.len() {
            let c = if i < points.len() { points[i] } else { a };

            let ab = (b.0 - a.0, b.1 - a.1);
            let len_ab = (ab.0 * ab.0 + ab.1 * ab.1).sqrt();
            let normal_ab = (
                -ab.1 / len_ab * half_thickness,
                ab.0 / len_ab * half_thickness,
            );

            let a1 = (a.0 + normal_ab.0, a.1 + normal_ab.1);
            let a2 = (a.0 - normal_ab.0, a.1 - normal_ab.1);
            let b1 = (b.0 + normal_ab.0, b.1 + normal_ab.1);
            let b2 = (b.0 - normal_ab.0, b.1 - normal_ab.1);

            let da = cum_dist[a_idx];
            let db = cum_dist[b_idx];

            // segment quad with distance
            vertices.extend_from_slice(&[
                a1.0, a1.1, da, a2.0, a2.1, da, b1.0, b1.1, db,
                a2.0, a2.1, da, b1.0, b1.1, db, b2.0, b2.1, db,
            ]);

            let bc = (c.0 - b.0, c.1 - b.1);
            let len_bc = (bc.0 * bc.0 + bc.1 * bc.1).sqrt();
            if len_bc > 0.0 {
                let normal_bc = (
                    -bc.1 / len_bc * half_thickness,
                    bc.0 / len_bc * half_thickness,
                );
                let b3 = (b.0 + normal_bc.0, b.1 + normal_bc.1);
                let b4 = (b.0 - normal_bc.0, b.1 - normal_bc.1);

                let z = ab.0 * bc.1 - ab.1 * bc.0;

                // bevel join — all vertices at junction distance
                if z < 0.0 {
                    vertices.extend_from_slice(&[b.0, b.1, db, b1.0, b1.1, db, b3.0, b3.1, db]);
                } else if z > 0.0 {
                    vertices.extend_from_slice(&[b.0, b.1, db, b2.0, b2.1, db, b4.0, b4.1, db]);
                }

                // optional miter
                if z != 0.0 {
                    let (a_j, b_j, norm_j) = if z < 0.0 { (a1, b3, ab) } else { (a2, b4, ab) };

                    let denom = z;
                    let alpha = (bc.1 * (b_j.0 - a_j.0) + bc.0 * (a_j.1 - b_j.1)) / denom;
                    let mx = a_j.0 + alpha * norm_j.0;
                    let my = a_j.1 + alpha * norm_j.1;

                    let dist2 = (mx - b.0).powi(2) + (my - b.1).powi(2);
                    if dist2 <= miter_limit_squared {
                        if z < 0.0 {
                            vertices.extend_from_slice(&[mx, my, db, b1.0, b1.1, db, b3.0, b3.1, db]);
                        } else {
                            vertices.extend_from_slice(&[mx, my, db, b2.0, b2.1, db, b4.0, b4.1, db]);
                        }
                    }
                }
            }

            a = b;
            a_idx = b_idx;
            b = c;
            if i < points.len() {
                b_idx = i;
            }
        }

        let mut geometry = Geometry::new(GL_TRIANGLES);
        geometry.add_buffer(&vertices, 3);
        geometry.add_vertex_attribute(Attribute::new(0, 2, 3, 0)); // vec2 position
        geometry.add_vertex_attribute(Attribute::new(3, 1, 3, 2)); // float distance
        geometry
    }

    /// Generate outline points for a circle as a closed polyline.
    fn circle_outline_points(radius: f32, segments: usize, ax: f32, ay: f32) -> Vec<(f32, f32)> {
        let mut points = Vec::with_capacity(segments + 2);
        for i in 0..=segments {
            let theta = (i as f32 / segments as f32) * std::f32::consts::TAU;
            points.push((radius * theta.cos() - ax, radius * theta.sin() - ay));
        }
        points
    }

    /// Generate outline points for an ellipse as a closed polyline.
    fn ellipse_outline_points(rx: f32, ry: f32, segments: usize, ax: f32, ay: f32) -> Vec<(f32, f32)> {
        let mut points = Vec::with_capacity(segments + 2);
        for i in 0..=segments {
            let angle = std::f32::consts::TAU * (i as f32) / (segments as f32);
            points.push((rx * angle.cos() - ax, ry * angle.sin() - ay));
        }
        points
    }

    /// Generate outline points for a rounded rectangle as a closed polyline.
    fn rounded_rectangle_outline_points(
        width: f32,
        height: f32,
        corner_radius: f32,
        segments: usize,
        ax: f32,
        ay: f32,
    ) -> Vec<(f32, f32)> {
        let r = corner_radius;
        let corners = [
            (r, r, PI, 1.5 * PI),                      // top-left
            (width - r, r, 1.5 * PI, 2.0 * PI),        // top-right
            (width - r, height - r, 0.0, 0.5 * PI),    // bottom-right
            (r, height - r, 0.5 * PI, PI),              // bottom-left
        ];

        let total = (segments + 1) * 4 + 1; // 4 arcs + closing point
        let mut points = Vec::with_capacity(total);

        for &(cx, cy, start, end) in &corners {
            for i in 0..=segments {
                let theta = start + (end - start) * (i as f32) / (segments as f32);
                points.push((cx + r * theta.cos() - ax, cy + r * theta.sin() - ay));
            }
        }

        // Close the loop
        if let Some(&first) = points.first() {
            points.push(first);
        }
        points
    }

    fn triangle_geometry(vertices: &[(f32, f32); 3]) -> Geometry {
        let mut geometry = Geometry::new(GL_TRIANGLES);
        let flattened: Vec<f32> = vertices.iter().flat_map(|(x, y)| [*x, *y]).collect();

        geometry.add_buffer(&flattened, 2);
        geometry.add_vertex_attribute(Attribute::new(0, 2, 2, 0));

        geometry
    }

    fn rectangle_geometry(width: GLfloat, height: GLfloat, ox: GLfloat, oy: GLfloat) -> Geometry {
        let vertices: Vec<GLfloat> = vec![
            0.0 - ox, 0.0 - oy,
            width - ox, 0.0 - oy,
            0.0 - ox, height - oy,
            width - ox, height - oy,
        ];

        let values_per_vertex = 2;
        let mut geometry = Geometry::new(GL_TRIANGLE_STRIP);
        geometry.add_buffer(&vertices, values_per_vertex);
        geometry.add_vertex_attribute(Attribute::new(0, 2, values_per_vertex as usize, 0));
        geometry
    }

    fn circle_geometry(radius: GLfloat, segments: usize, ox: GLfloat, oy: GLfloat) -> Geometry {
        let mut vertices: Vec<GLfloat> = Vec::with_capacity((segments + 2) * 2);

        // Center of the circle
        vertices.extend_from_slice(&[0.0 - ox, 0.0 - oy]);

        for i in 0..=segments {
            let theta = (i as f32 / segments as f32) * std::f32::consts::TAU;
            let x = radius * theta.cos() - ox;
            let y = radius * theta.sin() - oy;
            vertices.extend_from_slice(&[x, y]);
        }

        let values_per_vertex = 2;
        let mut geometry = Geometry::new(GL_TRIANGLE_FAN);
        geometry.add_buffer(&vertices, values_per_vertex);
        geometry.add_vertex_attribute(Attribute::new(0, 2, values_per_vertex as usize, 0));
        geometry
    }

    fn ellipse_geometry(rx: f32, ry: f32, segments: usize, ox: f32, oy: f32) -> Geometry {
        use std::f32::consts::PI;

        let mut vertices: Vec<GLfloat> = Vec::with_capacity((segments + 2) * 2);

        vertices.extend_from_slice(&[0.0 - ox, 0.0 - oy]);

        for i in 0..=segments {
            let angle = 2.0 * PI * (i as f32) / (segments as f32);
            let x = rx * angle.cos() - ox;
            let y = ry * angle.sin() - oy;
            vertices.extend_from_slice(&[x, y]);
        }

        let values_per_vertex = 2;
        let mut geometry = Geometry::new(GL_TRIANGLE_FAN);
        geometry.add_buffer(&vertices, values_per_vertex);
        geometry.add_vertex_attribute(Attribute::new(0, 2, values_per_vertex as usize, 0));
        geometry
    }

    pub fn rounded_rectangle_geometry(
        width: f32,
        height: f32,
        radius: f32,
        segments: usize,
        ox: f32,
        oy: f32,
    ) -> Geometry {
        assert!(radius * 2.0 <= width && radius * 2.0 <= height);

        let mut vertices: Vec<GLfloat> = Vec::new();

        // 1. Add center point for triangle fan
        let center_x = width / 2.0 - ox;
        let center_y = height / 2.0 - oy;
        vertices.push(center_x);
        vertices.push(center_y);

        // 2. Define arcs for each corner: (cx, cy, start_angle, end_angle)
        let corners = [
            (radius, radius, PI, 1.5 * PI),
            (width - radius, radius, 1.5 * PI, 2.0 * PI),
            (width - radius, height - radius, 0.0, 0.5 * PI),
            (radius, height - radius, 0.5 * PI, PI),
        ];

        let mut first_arc_x = 0.0;
        let mut first_arc_y = 0.0;
        let mut is_first = true;

        for &(cx, cy, start_angle, end_angle) in &corners {
            for i in 0..=segments {
                let theta =
                    start_angle + (end_angle - start_angle) * (i as f32) / (segments as f32);
                let x = cx + radius * theta.cos() - ox;
                let y = cy + radius * theta.sin() - oy;

                if is_first {
                    first_arc_x = x;
                    first_arc_y = y;
                    is_first = false;
                }

                vertices.push(x);
                vertices.push(y);
            }
        }

        vertices.push(first_arc_x);
        vertices.push(first_arc_y);

        let values_per_vertex = 2;
        let mut geometry = Geometry::new(GL_TRIANGLE_FAN);
        geometry.add_buffer(&vertices, values_per_vertex);
        geometry.add_vertex_attribute(Attribute::new(0, 2, values_per_vertex as usize, 0));
        geometry
    }

    fn polygon_geometry(
        points: &[(GLfloat, GLfloat)],
        triangles: &[[usize; 3]],
    ) -> Geometry {
        assert!(points.len() >= 3, "Polygon requires at least 3 points");
        assert!(
            !triangles.is_empty(),
            "Polygon triangulation produced no triangles"
        );

        // Expand the index list into a flat vertex buffer of triangles so the
        // geometry can be drawn with GL_TRIANGLES (works for concave polygons,
        // unlike GL_TRIANGLE_FAN which only renders convex shapes correctly).
        let mut vertices = Vec::with_capacity(triangles.len() * 3 * 2);
        for &[a, b, c] in triangles {
            let (ax, ay) = points[a];
            let (bx, by) = points[b];
            let (cx, cy) = points[c];
            vertices.extend_from_slice(&[ax, ay, bx, by, cx, cy]);
        }

        let values_per_vertex = 2;
        let mut geometry = Geometry::new(GL_TRIANGLES);
        geometry.add_buffer(&vertices, values_per_vertex);
        geometry.add_vertex_attribute(Attribute::new(0, 2, values_per_vertex as usize, 0));
        geometry
    }

    pub fn image_geometry(width: f32, height: f32, ox: f32, oy: f32) -> Geometry {
        // Vertex format: [x, y, u, v]. Geometry built centered at origin,
        // then shifted by (-ox, -oy) so the resolved anchor sits at local (0, 0).
        let hw = width / 2.0;
        let hh = height / 2.0;
        let vertices: Vec<f32> = vec![
            // Triangle 1
            -hw - ox, -hh - oy, 0.0, 0.0, // bottom-left
             hw - ox, -hh - oy, 1.0, 0.0, // bottom-right
             hw - ox,  hh - oy, 1.0, 1.0, // top-right
            // Triangle 2
            -hw - ox, -hh - oy, 0.0, 0.0, // bottom-left
             hw - ox,  hh - oy, 1.0, 1.0, // top-right
            -hw - ox,  hh - oy, 0.0, 1.0, // top-left
        ];

        let values_per_vertex = 4;

        let mut geometry = Geometry::new(GL_TRIANGLES);
        geometry.add_buffer(&vertices, values_per_vertex);
        geometry.add_vertex_attribute(Attribute::new(0, 2, values_per_vertex as usize, 0));
        geometry.add_vertex_attribute(Attribute::new(1, 2, values_per_vertex as usize, 2));

        geometry
    }

    /// Build raw textured-quad vertices for a string of text and compute the
    /// bounding box over all glyph quads. Returns `(vertices, bbox_min, bbox_max)`.
    /// Local origin is the top-left of the text cell (cursor_x = 0, y = 0).
    fn text_raw_vertices(
        text: &str,
        font_atlas: &mut FontAtlas,
    ) -> (Vec<f32>, (f32, f32), (f32, f32)) {
        let mut vertices: Vec<f32> = Vec::new();
        let mut cursor_x: f32 = 0.0;
        let baseline_y: f32 = font_atlas.font_size() as f32;

        let mut min_x = f32::INFINITY;
        let mut min_y = f32::INFINITY;
        let mut max_x = f32::NEG_INFINITY;
        let mut max_y = f32::NEG_INFINITY;

        for ch in text.chars() {
            if let Some(glyph) = font_atlas.get_glyph(ch) {
                if glyph.width == 0 || glyph.height == 0 {
                    cursor_x += glyph.advance;
                    continue;
                }

                let x0 = cursor_x + glyph.bearing_x as f32;
                let y0 = baseline_y - glyph.bearing_y as f32;
                let x1 = x0 + glyph.width as f32;
                let y1 = y0 + glyph.height as f32;

                if x0 < min_x { min_x = x0; }
                if y0 < min_y { min_y = y0; }
                if x1 > max_x { max_x = x1; }
                if y1 > max_y { max_y = y1; }

                let u0 = glyph.uv_x;
                let v0 = glyph.uv_y;
                let u1 = glyph.uv_x + glyph.uv_width;
                let v1 = glyph.uv_y + glyph.uv_height;

                // Triangle 1: bottom-left, bottom-right, top-right
                vertices.extend_from_slice(&[
                    x0, y1, u0, v1,
                    x1, y1, u1, v1,
                    x1, y0, u1, v0,
                ]);
                // Triangle 2: bottom-left, top-right, top-left
                vertices.extend_from_slice(&[
                    x0, y1, u0, v1,
                    x1, y0, u1, v0,
                    x0, y0, u0, v0,
                ]);

                cursor_x += glyph.advance;
            }
        }

        let (bbox_min, bbox_max) = if min_x.is_finite() {
            ((min_x, min_y), (max_x, max_y))
        } else {
            ((0.0, 0.0), (0.0, 0.0))
        };

        (vertices, bbox_min, bbox_max)
    }

}

/// Builder for `ShapeRenderable` that lets callers override the anchor point
/// before the mesh is created. Obtained via [`ShapeRenderable::builder`].
///
/// ```ignore
/// let rect = ShapeRenderable::builder(
///     ShapeKind::Rectangle(Rectangle::new(6.0, 6.0)),
///     ShapeStyle::fill(Color::white()),
/// )
/// .anchor(Anchor::Center)
/// .build();
/// ```
pub struct ShapeRenderableBuilder {
    shape: ShapeKind,
    style: ShapeStyle,
    anchor: Anchor,
}

impl ShapeRenderableBuilder {
    /// Set the anchor for the shape. Defaults to `Anchor::Default` if not called.
    pub fn anchor(mut self, anchor: Anchor) -> Self {
        self.anchor = anchor;
        self
    }

    /// Build the `ShapeRenderable`, consuming the builder.
    pub fn build(self) -> ShapeRenderable {
        ShapeRenderable::from_shape_with_anchor(self.shape, self.style, self.anchor)
    }
}