teksilo-text 0.17.2

Text backend for Teksilo — shaping, line breaking and glyph rasterization via text-typeset.
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

use std::collections::HashMap;

use teksilo_canvas::GlyphQuad;
use teksilo_canvas::text_backend::{
    AtlasInfo, CharGeom, GlyphValidation, LineEnd, LineTruncation, TextBackend, TextDirection,
    TextGeometry, TextLayout, TextLayoutSpan, TextLine, TextLineSegment, TextLink, TextSpanKind,
};
use teksilo_tokens::TextStyle;

use crate::font_registrar::shared_static;
use text_typeset::atlas::cache::GlyphCacheKey;
use text_typeset::{
    DocumentFlow, FontFaceId, InlineMarkup, LaidOutSpanKind, ParagraphResult, SingleLineResult,
    TextFontService, TextFormat,
};

/// Which layout method produced the cache entry — separates the
/// single-line and paragraph caches so a single-line truncated result
/// never masquerades as a wrapped paragraph result (or vice versa).
#[derive(Clone, PartialEq, Eq, Hash)]
enum LayoutMode {
    SingleLine,
    /// `max_lines` cap expressed as `u32::MAX` when unbounded.
    Paragraph(u32),
    SingleLineMarkup,
    ParagraphMarkup(u32),
}

/// Cache key for text layout results.
#[derive(Clone, PartialEq, Eq, Hash)]
struct LayoutCacheKey {
    text: String,
    font_family: String,
    font_size_bits: u32, // f32 as bits for Hash/Eq
    font_weight: u32,
    max_width_bits: Option<u32>,
    mode: LayoutMode,
    /// Ambient raster scale at layout time (f32 bits). Metrics are
    /// identical across raster scales, but each entry's `layout_key`
    /// indexes glyph quads baked at a specific bitmap density — entries
    /// at different scales must not alias.
    raster_scale_bits: u32,
}

impl LayoutCacheKey {
    fn new_single_line(
        text: &str,
        style: &TextStyle,
        max_width: Option<f32>,
        scale_factor: f32,
        raster_scale: f32,
    ) -> Self {
        let scaled_size = style.size * scale_factor;
        Self {
            text: text.to_string(),
            font_family: style.family.clone(),
            font_size_bits: scaled_size.to_bits(),
            font_weight: style.weight.0 as u32,
            max_width_bits: max_width.map(|w| (w * scale_factor).to_bits()),
            mode: LayoutMode::SingleLine,
            raster_scale_bits: raster_scale.to_bits(),
        }
    }

    fn new_paragraph(
        text: &str,
        style: &TextStyle,
        max_width: f32,
        max_lines: Option<usize>,
        scale_factor: f32,
        raster_scale: f32,
    ) -> Self {
        let scaled_size = style.size * scale_factor;
        let cap = max_lines
            .map(|n| n.min(u32::MAX as usize) as u32)
            .unwrap_or(u32::MAX);
        Self {
            text: text.to_string(),
            font_family: style.family.clone(),
            font_size_bits: scaled_size.to_bits(),
            font_weight: style.weight.0 as u32,
            max_width_bits: Some((max_width * scale_factor).to_bits()),
            mode: LayoutMode::Paragraph(cap),
            raster_scale_bits: raster_scale.to_bits(),
        }
    }
}

// Only cache the metrics here — the glyph quads live in their own map,
// keyed by `layout_key` and resolved by `ensure_glyphs` during paint.

/// Bridge between teksilo-canvas's `TextBackend` trait and text-typeset.
///
/// Holds a shared [`TextFontService`] (font registry + glyph atlas +
/// shaper cache) plus a dedicated [`DocumentFlow`] used only for the
/// single-line / paragraph label API exposed via `TextBackend`. Rich
/// text widgets that need a full-document flow keep their OWN
/// `DocumentFlow` on the `RichTextEngine` side; they reach through
/// this bridge to grab a mutable borrow of the shared service when
/// they layout and render, so every widget's glyphs land in the same
/// GPU atlas.
pub struct TypesetterBridge {
    service: TextFontService,
    /// Dedicated flow used by the `TextBackend` label path
    /// (`layout_single_line` / `layout_paragraph` / their markup
    /// variants). Labels do not need a persistent flow-layout state,
    /// so this single flow is sufficient — every `layout_*` call
    /// reshapes from scratch through the shared service's atlas.
    label_flow: DocumentFlow,
    default_font: Option<FontFaceId>,
    next_layout_key: u64,
    /// Layout metrics cache: avoids re-shaping text just for size
    /// measurement.
    layout_cache: HashMap<LayoutCacheKey, TextLayout>,
    /// Glyph quads + per-glyph cache keys stored by opaque layout key
    /// so ensure_glyphs can be resolved independently for many text
    /// widgets in the same frame. The cache keys are used to touch
    /// glyphs in text-typeset's internal `GlyphCache` so they aren't
    /// evicted while still being rendered via paint-cache hits.
    glyph_cache: HashMap<u64, (Vec<GlyphQuad>, Vec<GlyphCacheKey>)>,
    /// Whether any text work (a `layout_*` call, or a `service_mut()`
    /// borrow) happened since the last `atlas_info()` call. Deliberately
    /// NOT set by `ensure_glyphs` — see the note there. When false we
    /// skip advancing the eviction generation to avoid aging out
    /// idle-but-visible glyphs.
    had_text_activity: bool,
    /// Sticky atlas-dirty flag set by rich-text widgets via
    /// `service_mut()`. text-typeset's `render()` path clears
    /// `atlas.dirty` after copying pixels into the returned
    /// `RenderFrame`. teksilo-app consumes the atlas through
    /// `atlas_info()` *after* `tree.render()`, so by then the
    /// typesetter's own dirty flag is already false and the atlas
    /// would never be uploaded, leaving rich-text glyphs invisible
    /// on the GPU. This flag closes the gap: anyone who took a
    /// mutable borrow of the service since the last `atlas_info()`
    /// call forces the bridge to report the atlas as dirty one
    /// more time.
    rich_text_atlas_dirty: bool,
    /// `TextFontService::eviction_epoch()` as of the previous
    /// `atlas_info()` call. Comparing against the live epoch reports
    /// evictions from EVERY internal path — the snapshot-driven scan,
    /// the scan at the start of every rich-text `render()`
    /// (`build_render_frame`), and the wholesale reset on scale-factor
    /// change — not just the snapshot's own `glyphs_evicted` flag.
    last_seen_eviction_epoch: u64,
    /// Monotonic atlas content version, bumped whenever `atlas_info()`
    /// observes changed pixels. Each window renderer records the version
    /// it last uploaded; see [`AtlasInfo::version`].
    atlas_version: u64,
    /// Ambient raster scale set by the paint walker for the widget
    /// currently painting (see [`TextBackend::set_raster_scale`]).
    /// Flows into every `label_flow.layout_*` call and into the layout
    /// cache key — flipping it costs nothing, entries at different
    /// scales coexist and age out via the normal touch/LRU machinery.
    raster_scale: f32,
    /// Monotonic generation bumped whenever `layout_cache` / `glyph_cache`
    /// are cleared wholesale (scale-factor reset or `invalidate_cache`).
    /// Surfaced via [`TextBackend::layout_cache_generation`] so widgets
    /// that retain a `TextLayout` across the layout→paint boundary can
    /// detect a dangling `layout_key` before drawing it. Independent of
    /// the eviction epoch (which also tracks LRU, leaving these maps
    /// intact) so it never perturbs the per-frame atlas-upload decision.
    layout_cache_generation: u64,
    /// Debug-only `layout_key -> source text` side map for diagnostics.
    ///
    /// Unlike `layout_cache` / `glyph_cache`, this is **not** cleared on a
    /// scale-factor change — the scale reset is the dominant trigger of
    /// the evicted-layout warning (a widget retains a `TextLayout` from
    /// before the reset, then paints it after both caches were wiped), so
    /// recovering the text for the warning requires a mapping that
    /// outlives the wipe. Bounded by [`DEBUG_TEXT_MAP_CAP`]; never read on
    /// a hot path (only [`debug_layout_text`](TextBackend::debug_layout_text)).
    #[cfg(debug_assertions)]
    debug_text_by_key: HashMap<u64, String>,
}

/// Cap on the debug `layout_key -> text` map. When exceeded the map is
/// cleared wholesale (oldest text is then simply unavailable to the
/// warning — it degrades to "no text", never to wrong text). Generous
/// enough that a normal session never trips it.
#[cfg(debug_assertions)]
const DEBUG_TEXT_MAP_CAP: usize = 8192;

impl TypesetterBridge {
    pub fn new() -> Self {
        let service = TextFontService::new();
        let last_seen_eviction_epoch = service.eviction_epoch();
        Self {
            service,
            label_flow: DocumentFlow::new(),
            default_font: None,
            next_layout_key: 1,
            layout_cache: HashMap::new(),
            glyph_cache: HashMap::new(),
            had_text_activity: false,
            rich_text_atlas_dirty: false,
            last_seen_eviction_epoch,
            atlas_version: 1,
            raster_scale: 1.0,
            layout_cache_generation: 0,
            #[cfg(debug_assertions)]
            debug_text_by_key: HashMap::new(),
        }
    }

    /// Clear the retained layout + glyph maps and bump
    /// [`layout_cache_generation`](Self::layout_cache_generation). The single
    /// path through which both maps are dropped, so the generation is the
    /// authoritative signal that any retained `layout_key` is now dangling.
    fn clear_layout_caches(&mut self) {
        self.layout_cache.clear();
        self.glyph_cache.clear();
        self.layout_cache_generation = self.layout_cache_generation.wrapping_add(1);
    }

    /// Record a `layout_key -> text` association for the debug
    /// evicted-layout warning. No-op in release. Bounded by
    /// [`DEBUG_TEXT_MAP_CAP`].
    #[cfg_attr(not(debug_assertions), allow(unused_variables))]
    #[inline]
    fn debug_remember_text(&mut self, key: u64, text: &str) {
        #[cfg(debug_assertions)]
        {
            if self.debug_text_by_key.len() >= DEBUG_TEXT_MAP_CAP {
                self.debug_text_by_key.clear();
            }
            self.debug_text_by_key.insert(key, text.to_string());
        }
    }

    /// Create a bridge with the bundled default font (Inter) plus
    /// every script-specific fallback font whose Cargo feature is
    /// enabled (`fonts-arabic`, `fonts-hebrew`, …).
    ///
    /// Inter is the primary font — it's what the default `TextStyle`
    /// asks for via `TypographyTokens::default().family = "Inter"` —
    /// and covers Latin, Cyrillic, Greek, and Vietnamese. The
    /// additional Noto Sans variable fonts are registered into the
    /// same font registry so that text-typeset's shaper-level
    /// `.notdef` fallback loop can cover mixed-script text without
    /// any locale awareness at the caller site.
    pub fn new_with_default_font() -> Self {
        let mut bridge = Self::new();
        bridge.register_default_font();
        bridge
    }

    /// Register a font from raw TTF/OTF data. Forwards to the
    /// shared [`TextFontService`].
    pub fn register_font(&mut self, data: &[u8]) -> FontFaceId {
        self.service.register_font(data)
    }

    /// Register Inter as the primary default and JetBrains Mono as the
    /// monospace face (the two families the default theme's typography
    /// tokens name), then register every feature-gated script-specific
    /// fallback font. The feature-gated registrations discard their
    /// `FontFaceId` because fallback eligibility only requires that a
    /// font be in the registry — text-typeset's `find_fallback_font`
    /// iterates every registered font and picks the first one whose
    /// charmap covers a `.notdef` glyph's codepoint.
    fn register_default_font(&mut self) {
        // Inter is the default/UI family. Register the upright variable
        // face as the default, then the italic variable face so italic
        // UI text shapes from real italic outlines instead of falling
        // back to the upright (an italic request would otherwise miss).
        let inter_data = include_bytes!("../fonts/InterVariable.ttf");
        let face_id = self.service.register_font_shared(shared_static(inter_data));
        self.service.set_default_font(face_id, 14.0);
        self.default_font = Some(face_id);
        let inter_italic = include_bytes!("../fonts/InterVariable-Italic.ttf");
        let _ = self
            .service
            .register_font_shared(shared_static(inter_italic));

        // The bundled Inter faces report their family name as "Inter
        // Variable" (name id 1; they have no typographic-family name
        // id 16), but the default theme's typography tokens request the
        // family "Inter". Alias the requested name to the registered one
        // so the explicit request resolves to the bundled faces instead
        // of silently falling through to the default font.
        self.service.set_generic_family("Inter", "Inter Variable");

        // JetBrains Mono is the monospace family the theme's `mono`
        // typography token names. Without it, code/mono text would fall
        // back to the proportional default font. Register both the
        // upright and italic variable faces (both under the family name
        // "JetBrains Mono"), so the theme's request — upright or italic —
        // resolves directly.
        let mono_data = include_bytes!("../fonts/JetBrainsMono.ttf");
        let _ = self.service.register_font_shared(shared_static(mono_data));
        let mono_italic = include_bytes!("../fonts/JetBrainsMono-Italic.ttf");
        let _ = self
            .service
            .register_font_shared(shared_static(mono_italic));

        #[cfg(font_arabic)]
        {
            let data = include_bytes!("../fonts/NotoSansArabic-VariableFont_wdth,wght.ttf");
            let _ = self.service.register_font_shared(shared_static(data));
        }
        #[cfg(font_hebrew)]
        {
            let data = include_bytes!("../fonts/NotoSansHebrew-VariableFont_wdth,wght.ttf");
            let _ = self.service.register_font_shared(shared_static(data));
        }
        #[cfg(font_thai)]
        {
            let data = include_bytes!("../fonts/NotoSansThai-VariableFont_wdth,wght.ttf");
            let _ = self.service.register_font_shared(shared_static(data));
        }
        #[cfg(font_devanagari)]
        {
            let data = include_bytes!("../fonts/NotoSansDevanagari-VariableFont_wdth,wght.ttf");
            let _ = self.service.register_font_shared(shared_static(data));
        }
        #[cfg(font_cjk_sc)]
        {
            let data = include_bytes!("../fonts/NotoSansSC-VariableFont_wght.ttf");
            let _ = self.service.register_font_shared(shared_static(data));
        }
        #[cfg(font_cjk_jp)]
        {
            let data = include_bytes!("../fonts/NotoSansJP-VariableFont_wght.ttf");
            let _ = self.service.register_font_shared(shared_static(data));
        }
        #[cfg(font_cjk_kr)]
        {
            let data = include_bytes!("../fonts/NotoSansKR-VariableFont_wght.ttf");
            let _ = self.service.register_font_shared(shared_static(data));
        }

        // Optional runtime color-emoji fallback. Reads the platform's
        // installed emoji font at startup when `system-emoji` is on.
        // Silent on miss so headless / embedded targets aren't noisy.
        #[cfg(feature = "system-emoji")]
        {
            let _ = self.register_system_emoji_font();
        }
    }

    /// Try to register the platform's default color-emoji font as a
    /// fallback. Returns the registered [`FontFaceId`] on success, or
    /// `None` if no emoji font was found at a known system path.
    ///
    /// Called automatically by [`new_with_default_font`](Self::new_with_default_font).
    /// Exposed publicly so apps that construct a bare bridge via
    /// [`new`](Self::new) can still opt in without replicating the
    /// per-OS path list.
    ///
    /// Requires the `system-emoji` feature.
    #[cfg(feature = "system-emoji")]
    pub fn register_system_emoji_font(&mut self) -> Option<FontFaceId> {
        // `register_font_shared` keeps the mmap handle alive inside
        // the font registry without copying the bytes to the heap —
        // the kernel pages glyph tables in on demand.
        crate::system_emoji::load_system_emoji_data()
            .map(|data| self.service.register_font_shared(data))
    }

    /// Set the default font and size. Forwards to the shared
    /// [`TextFontService`].
    pub fn set_default_font(&mut self, face_id: FontFaceId, size_px: f32) {
        self.service.set_default_font(face_id, size_px);
        self.default_font = Some(face_id);
    }

    /// Get atlas information for GPU upload.
    ///
    /// Only advances the glyph cache generation and runs eviction
    /// when text work happened since the last call — this prevents
    /// aging out glyphs that are still visible but cached (idle
    /// app scenario).
    ///
    /// `seen_version` is the [`AtlasInfo::version`] the caller last
    /// uploaded (0 for a fresh consumer). Pixels are cloned into the
    /// result only when the caller is behind, so several windows can
    /// each pull the current atlas at their own redraw without the
    /// first caller consuming it for everyone — and a clean-atlas
    /// frame still skips the ~1 MB memcpy (profiling the animations
    /// example showed an unconditional clone dominating self-time at
    /// ~6 % during shader-driven animations).
    ///
    /// `glyphs_evicted` is derived from
    /// [`TextFontService::eviction_epoch`], so it reports evictions
    /// from every path — the snapshot scan below, the scan inside
    /// every rich-text `render()`, and scale-factor resets — not just
    /// the snapshot's own flag. On `true`, the caller must invalidate
    /// every retained paint cache in every window.
    pub fn atlas_info(&mut self, seen_version: u64) -> AtlasInfo {
        let (snapshot_dirty, width, height) = {
            let snapshot = self.service.atlas_snapshot(self.had_text_activity);
            (snapshot.dirty, snapshot.width, snapshot.height)
        };
        let rich_dirty = self.rich_text_atlas_dirty;
        self.rich_text_atlas_dirty = false;
        self.had_text_activity = false;
        let dirty = snapshot_dirty || rich_dirty;
        if dirty {
            self.atlas_version = self.atlas_version.wrapping_add(1);
        }
        let pixels = if seen_version != self.atlas_version {
            self.service.atlas_pixels().to_vec()
        } else {
            Vec::new()
        };

        let epoch = self.service.eviction_epoch();
        let glyphs_evicted = epoch != self.last_seen_eviction_epoch;
        self.last_seen_eviction_epoch = epoch;

        AtlasInfo {
            dirty,
            width,
            height,
            pixels,
            version: self.atlas_version,
            glyphs_evicted,
        }
    }

    /// Current atlas content version (see [`AtlasInfo::version`]),
    /// without consuming any pending dirty/eviction state. Used when a
    /// consumer primes itself outside the per-frame `atlas_info` flow —
    /// e.g. a freshly created window uploading the current atlas pixels
    /// directly — and needs the matching version stamp.
    pub fn atlas_version(&self) -> u64 {
        self.atlas_version
    }

    /// Convert a teksilo-tokens TextStyle to a text-typeset TextFormat.
    fn to_text_format(style: &TextStyle) -> TextFormat {
        TextFormat {
            font_family: Some(style.family.clone()),
            font_weight: Some(style.weight.0 as u32),
            font_bold: None,
            font_italic: None,
            font_size: Some(style.size),
            color: None,
            ..Default::default()
        }
    }

    /// Invalidate the layout cache (e.g. on scale factor change).
    pub fn invalidate_cache(&mut self) {
        self.clear_layout_caches();
    }

    /// Borrow the underlying [`TextFontService`] immutably.
    ///
    /// Rich-text widgets read this to shape and lay out against
    /// the shared font registry without taking a mutable borrow.
    pub fn service(&self) -> &TextFontService {
        &self.service
    }

    /// Borrow the underlying [`TextFontService`] mutably.
    ///
    /// Rich-text widgets (`teksilo_text::RichTextEngine` driving the
    /// `RichTextEditor` widget) keep their own `DocumentFlow` and
    /// call `flow.layout_full(&bridge.service, ...)` +
    /// `flow.render(&mut bridge.service, ...)` through this
    /// accessor, so every widget's glyphs land in the same GPU
    /// atlas. Marks the atlas as dirty for the next
    /// [`atlas_info`](Self::atlas_info) call.
    pub fn service_mut(&mut self) -> &mut TextFontService {
        self.had_text_activity = true;
        self.rich_text_atlas_dirty = true;
        &mut self.service
    }

    // ── Font enumeration (font picker) ──────────────────────────

    /// Enumerate every installed font family, deduplicated and sorted.
    ///
    /// Cheap: reads only font metadata (family name + monospaced flag), so
    /// no font file is opened. This is the item source for a `FontPicker`.
    /// Reachable from a widget's `build()` via
    /// `ctx.app_state::<SharedTypesetter>()?.bridge().borrow().families()`.
    pub fn families(&self) -> Vec<text_typeset::FontFamilyInfo> {
        self.service.families()
    }

    /// Enumerate installed font family names, deduplicated and sorted — the
    /// simple projection of [`families`](Self::families).
    pub fn family_names(&self) -> Vec<String> {
        self.service.family_names()
    }

    /// True if any face of the named family is monospaced (font metadata,
    /// no bytes loaded). Case-insensitive on the family name.
    pub fn family_is_monospaced(&self, family: &str) -> bool {
        self.service.family_is_monospaced(family)
    }

    /// Build a `Send` snapshot of every family's face byte-sources, to be
    /// moved to a background thread and turned into a writing-system
    /// coverage map (see [`WritingSystemIndexBuilder`]).
    ///
    /// Cheap on the calling thread; the expensive per-face OS/2 parsing runs
    /// in [`WritingSystemIndexBuilder::build`] off-thread. `FontPicker` uses
    /// this to build its writing-system filter index without stalling the UI.
    ///
    /// [`WritingSystemIndexBuilder`]: text_typeset::WritingSystemIndexBuilder
    /// [`WritingSystemIndexBuilder::build`]: text_typeset::WritingSystemIndexBuilder::build
    pub fn writing_system_index_builder(&self) -> text_typeset::WritingSystemIndexBuilder {
        self.service.writing_system_index_builder()
    }

    /// Current HiDPI display scale factor as last set by
    /// [`TextBackend::set_scale_factor`]. Reads through to the
    /// shared [`TextFontService`].
    pub fn display_scale_factor(&self) -> f32 {
        self.service.scale_factor()
    }

    /// Ambient raster scale as last set by
    /// [`TextBackend::set_raster_scale`] (the paint walker's
    /// accumulated transform scale for the widget currently
    /// painting). Rich-text widgets sync this into their own
    /// `DocumentFlow` (`flow.set_raster_scale`) before `render`, so
    /// document glyphs densify under a scene zoom exactly like the
    /// label path.
    pub fn ambient_raster_scale(&self) -> f32 {
        self.raster_scale
    }

    /// Line height (in logical pixels) of the registry's default
    /// font + size — `ascent + descent + leading`. Useful for
    /// widgets that need to size against an intrinsic line height
    /// before any content has been laid out (e.g.
    /// `RichTextEditor::min_lines` / `max_lines`).
    ///
    /// Returns `0.0` if no default font is registered. Does not
    /// apply any per-block `line_height_multiplier`.
    pub fn default_line_height(&self) -> f32 {
        self.service.default_line_height()
    }

    /// Line height (in logical pixels) for a specific [`TextStyle`].
    /// Same calculation as [`default_line_height`](Self::default_line_height)
    /// but resolves the explicit family / weight / size first.
    ///
    /// Returns `0.0` when the style cannot be resolved.
    pub fn measure_line_height(&self, style: &TextStyle) -> f32 {
        self.service
            .measure_line_height(&Self::to_text_format(style))
    }
}

impl Default for TypesetterBridge {
    fn default() -> Self {
        Self::new()
    }
}

/// Translate text-typeset's layout geometry into the canvas DTOs.
///
/// The two shapes are deliberately identical; the copy is what keeps
/// `teksilo-canvas` free of a text-typeset dependency, so a host can
/// supply its own backend without pulling in the shaper.
pub(crate) fn to_canvas_geometry(geometry: text_typeset::LayoutGeometry) -> TextGeometry {
    TextGeometry {
        lines: geometry.lines.iter().map(to_canvas_line).collect(),
        dropped_lines: geometry.dropped_lines,
        source_len: geometry.source_len,
        rendered_text: geometry.rendered_text,
        links: geometry
            .links
            .into_iter()
            .map(|l| TextLink {
                rendered_byte_range: l.rendered_byte_range,
                url: l.url,
            })
            .collect(),
    }
}

pub(crate) fn to_canvas_line(line: &text_typeset::LineGeometry) -> TextLine {
    TextLine {
        index: line.index,
        byte_range: line.byte_range.clone(),
        char_range: line.char_range.clone(),
        rect: line.rect,
        baseline: line.baseline,
        caret_x: line.caret_x,
        segments: line
            .segments
            .iter()
            .map(|s| TextLineSegment {
                byte_range: s.byte_range.clone(),
                char_range: s.char_range.clone(),
                direction: match s.direction {
                    text_typeset::GeometryDirection::LeftToRight => TextDirection::LeftToRight,
                    text_typeset::GeometryDirection::RightToLeft => TextDirection::RightToLeft,
                },
                rect: s.rect,
                characters: s
                    .characters
                    .iter()
                    .map(|c| CharGeom {
                        position: c.position,
                        width: c.width,
                    })
                    .collect(),
            })
            .collect(),
        end: match line.end {
            text_typeset::LineEnd::SoftWrap => LineEnd::SoftWrap,
            text_typeset::LineEnd::HardBreak { chars, bytes } => {
                LineEnd::HardBreak { chars, bytes }
            }
            text_typeset::LineEnd::EndOfText => LineEnd::EndOfText,
        },
        truncation: line.truncation.map(|t| LineTruncation {
            ellipsis_x: t.ellipsis_x,
            ellipsis_width: t.ellipsis_width,
        }),
    }
}

impl TextBackend for TypesetterBridge {
    fn set_scale_factor(&mut self, scale_factor: f32) {
        if (self.service.scale_factor() - scale_factor).abs() > 0.001 {
            // Push the new factor to the shared service. The
            // service clears its glyph cache and atlas in place
            // and bumps its `scale_generation`. Our own layout +
            // glyph caches still need to drop because they key
            // on the scale factor.
            self.service.set_scale_factor(scale_factor);
            self.clear_layout_caches();
        }
    }

    fn set_raster_scale(&mut self, raster_scale: f32) {
        // No cache clearing: the layout cache keys on the raster scale,
        // so entries at different scales coexist. Stale-scale glyph
        // entries age out of text-typeset's atlas via the normal LRU.
        self.raster_scale = if raster_scale > 0.0 {
            raster_scale
        } else {
            1.0
        };
    }

    fn raster_scale(&self) -> f32 {
        self.raster_scale
    }

    fn layout_single_line(
        &mut self,
        text: &str,
        style: &TextStyle,
        max_width: Option<f32>,
    ) -> TextLayout {
        let cache_key = LayoutCacheKey::new_single_line(
            text,
            style,
            max_width,
            self.service.scale_factor(),
            self.raster_scale,
        );

        if let Some(cached) = self.layout_cache.get(&cache_key) {
            return cached.clone();
        }

        self.had_text_activity = true;
        let format = Self::to_text_format(style);
        let (result, geometry): (SingleLineResult, _) =
            self.label_flow.layout_single_line_with_geometry(
                &mut self.service,
                text,
                &format,
                max_width,
                self.raster_scale,
            );

        let key = self.next_layout_key;
        self.next_layout_key += 1;
        self.debug_remember_text(key, text);

        let layout = TextLayout {
            width: result.width,
            height: result.height,
            ascent: result.baseline,
            descent: result.height - result.baseline,
            underline_offset: result.underline_offset,
            underline_thickness: result.underline_thickness,
            layout_key: key,
            line_count: 1,
            spans: Vec::new(),
            raster_scale: self.raster_scale,
            geometry: Some(std::rc::Rc::new(to_canvas_geometry(geometry))),
        };

        self.layout_cache.insert(cache_key.clone(), layout.clone());
        self.glyph_cache.insert(
            key,
            (
                result
                    .glyphs
                    .iter()
                    .map(|g| GlyphQuad {
                        screen: g.screen,
                        atlas: g.atlas,
                        color: g.color,
                        is_color: g.is_color,
                    })
                    .collect(),
                result.glyph_keys.clone(),
            ),
        );
        layout
    }

    fn layout_paragraph(
        &mut self,
        text: &str,
        style: &TextStyle,
        max_width: f32,
        max_lines: Option<usize>,
    ) -> TextLayout {
        let cache_key = LayoutCacheKey::new_paragraph(
            text,
            style,
            max_width,
            max_lines,
            self.service.scale_factor(),
            self.raster_scale,
        );

        if let Some(cached) = self.layout_cache.get(&cache_key) {
            return cached.clone();
        }

        self.had_text_activity = true;
        let format = Self::to_text_format(style);
        let (result, geometry): (ParagraphResult, _) =
            self.label_flow.layout_paragraph_with_geometry(
                &mut self.service,
                text,
                &format,
                max_width,
                max_lines,
                self.raster_scale,
            );

        let key = self.next_layout_key;
        self.next_layout_key += 1;
        self.debug_remember_text(key, text);

        let layout = TextLayout {
            width: result.width,
            height: result.height,
            ascent: result.baseline_first,
            descent: (result.height - result.baseline_first).max(0.0),
            underline_offset: result.underline_offset,
            underline_thickness: result.underline_thickness,
            layout_key: key,
            line_count: result.line_count.max(1),
            spans: Vec::new(),
            raster_scale: self.raster_scale,
            geometry: Some(std::rc::Rc::new(to_canvas_geometry(geometry))),
        };

        self.layout_cache.insert(cache_key.clone(), layout.clone());
        self.glyph_cache.insert(
            key,
            (
                result
                    .glyphs
                    .iter()
                    .map(|g| GlyphQuad {
                        screen: g.screen,
                        atlas: g.atlas,
                        color: g.color,
                        is_color: g.is_color,
                    })
                    .collect(),
                result.glyph_keys.clone(),
            ),
        );
        layout
    }

    fn layout_single_line_markup(
        &mut self,
        source: &str,
        style: &TextStyle,
        max_width: Option<f32>,
    ) -> TextLayout {
        let cache_key = LayoutCacheKey {
            text: source.to_string(),
            font_family: style.family.clone(),
            font_size_bits: (style.size * self.service.scale_factor()).to_bits(),
            font_weight: style.weight.0 as u32,
            max_width_bits: max_width.map(|w| (w * self.service.scale_factor()).to_bits()),
            mode: LayoutMode::SingleLineMarkup,
            raster_scale_bits: self.raster_scale.to_bits(),
        };
        if let Some(cached) = self.layout_cache.get(&cache_key) {
            return cached.clone();
        }

        self.had_text_activity = true;
        let format = Self::to_text_format(style);
        let tt_markup = InlineMarkup::parse(source);
        let (result, geometry): (SingleLineResult, _) =
            self.label_flow.layout_single_line_markup_with_geometry(
                &mut self.service,
                &tt_markup,
                &format,
                max_width,
                self.raster_scale,
            );

        let key = self.next_layout_key;
        self.next_layout_key += 1;
        self.debug_remember_text(key, source);

        let layout = TextLayout {
            width: result.width,
            height: result.height,
            ascent: result.baseline,
            descent: (result.height - result.baseline).max(0.0),
            underline_offset: result.underline_offset,
            underline_thickness: result.underline_thickness,
            layout_key: key,
            line_count: 1,
            spans: result
                .spans
                .iter()
                .map(|s| TextLayoutSpan {
                    kind: match &s.kind {
                        LaidOutSpanKind::Text => TextSpanKind::Text,
                        LaidOutSpanKind::Link { url } => TextSpanKind::Link { url: url.clone() },
                    },
                    line_index: s.line_index,
                    rect: s.rect,
                    byte_range: s.byte_range.clone(),
                })
                .collect(),
            raster_scale: self.raster_scale,
            geometry: Some(std::rc::Rc::new(to_canvas_geometry(geometry))),
        };

        self.layout_cache.insert(cache_key, layout.clone());
        self.glyph_cache.insert(
            key,
            (
                result
                    .glyphs
                    .iter()
                    .map(|g| GlyphQuad {
                        screen: g.screen,
                        atlas: g.atlas,
                        color: g.color,
                        is_color: g.is_color,
                    })
                    .collect(),
                result.glyph_keys.clone(),
            ),
        );
        layout
    }

    fn layout_paragraph_markup(
        &mut self,
        source: &str,
        style: &TextStyle,
        max_width: f32,
        max_lines: Option<usize>,
    ) -> TextLayout {
        let cap = max_lines
            .map(|n| n.min(u32::MAX as usize) as u32)
            .unwrap_or(u32::MAX);
        let cache_key = LayoutCacheKey {
            text: source.to_string(),
            font_family: style.family.clone(),
            font_size_bits: (style.size * self.service.scale_factor()).to_bits(),
            font_weight: style.weight.0 as u32,
            max_width_bits: Some((max_width * self.service.scale_factor()).to_bits()),
            mode: LayoutMode::ParagraphMarkup(cap),
            raster_scale_bits: self.raster_scale.to_bits(),
        };
        if let Some(cached) = self.layout_cache.get(&cache_key) {
            return cached.clone();
        }

        self.had_text_activity = true;
        let format = Self::to_text_format(style);
        let tt_markup = InlineMarkup::parse(source);
        let (result, geometry): (ParagraphResult, _) =
            self.label_flow.layout_paragraph_markup_with_geometry(
                &mut self.service,
                &tt_markup,
                &format,
                max_width,
                max_lines,
                self.raster_scale,
            );

        let key = self.next_layout_key;
        self.next_layout_key += 1;
        self.debug_remember_text(key, source);

        let layout = TextLayout {
            width: result.width,
            height: result.height,
            ascent: result.baseline_first,
            descent: (result.height - result.baseline_first).max(0.0),
            underline_offset: result.underline_offset,
            underline_thickness: result.underline_thickness,
            layout_key: key,
            line_count: result.line_count.max(1),
            spans: result
                .spans
                .iter()
                .map(|s| TextLayoutSpan {
                    kind: match &s.kind {
                        LaidOutSpanKind::Text => TextSpanKind::Text,
                        LaidOutSpanKind::Link { url } => TextSpanKind::Link { url: url.clone() },
                    },
                    line_index: s.line_index,
                    rect: s.rect,
                    byte_range: s.byte_range.clone(),
                })
                .collect(),
            raster_scale: self.raster_scale,
            geometry: Some(std::rc::Rc::new(to_canvas_geometry(geometry))),
        };

        self.layout_cache.insert(cache_key, layout.clone());
        self.glyph_cache.insert(
            key,
            (
                result
                    .glyphs
                    .iter()
                    .map(|g| GlyphQuad {
                        screen: g.screen,
                        atlas: g.atlas,
                        color: g.color,
                        is_color: g.is_color,
                    })
                    .collect(),
                result.glyph_keys.clone(),
            ),
        );
        layout
    }

    fn debug_layout_text(&self, layout_key: u64) -> Option<String> {
        #[cfg(debug_assertions)]
        {
            self.debug_text_by_key.get(&layout_key).cloned()
        }
        #[cfg(not(debug_assertions))]
        {
            let _ = layout_key;
            None
        }
    }

    fn ensure_glyphs(&mut self, layout: &TextLayout) -> Vec<GlyphQuad> {
        if let Some((quads, keys)) = self.glyph_cache.get(&layout.layout_key) {
            // Touch the glyphs in text-typeset's internal cache so they
            // aren't evicted while still being rendered via paint-cache
            // hits (where layout_single_line is not called and the
            // internal glyph_cache.get() that refreshes last_used is
            // never reached).
            //
            // Note: do NOT set `had_text_activity` here — touch_glyphs
            // directly updates the glyph timestamps, which is sufficient
            // to protect them when eviction does run (triggered by real
            // text work on other frames). Setting the flag here would
            // advance the atlas generation every frame, defeating the
            // idle-protection mechanism.
            self.service.touch_glyphs(keys);
            quads.clone()
        } else {
            Vec::new()
        }
    }

    fn touch_layout(&mut self, layout_key: u64) {
        // Fast path used by the widget-tree renderer when a widget's
        // `cached_paint` is reused. The cached frame references atlas
        // positions by baked-in UVs, so if the underlying glyphs age
        // out and their slots get reused by newer glyphs, the cached
        // quads render the wrong pixels. Refreshing timestamps here
        // mirrors what `ensure_glyphs` does on a cache hit, but without
        // reconstructing or cloning the quad list.
        if let Some((_, keys)) = self.glyph_cache.get(&layout_key) {
            self.service.touch_glyphs(keys);
        }
    }

    fn glyph_epoch(&self) -> u64 {
        self.service.eviction_epoch()
    }

    fn layout_cache_generation(&self) -> u64 {
        self.layout_cache_generation
    }

    fn debug_validate_layout(&self, layout_key: u64) -> GlyphValidation {
        let Some((quads, keys)) = self.glyph_cache.get(&layout_key) else {
            // The bridge's caches were cleared (eviction recovery or
            // scale-factor change) after these quads were baked. A
            // retained paint replaying them outlived an invalidation
            // that should have reached it.
            return GlyphValidation::StaleKey;
        };
        // `quads` and `keys` are parallel arrays: every layout_* path
        // stores `result.glyphs` mapped 1:1 alongside
        // `result.glyph_keys`, both produced by the same shaping pass.
        debug_assert_eq!(quads.len(), keys.len());
        for (quad, key) in quads.iter().zip(keys.iter()) {
            // Zero-size quads (whitespace / .notdef placeholders) have
            // no atlas residency requirement.
            if quad.atlas[2] <= 0.0 || quad.atlas[3] <= 0.0 {
                continue;
            }
            let Some(rect) = self.service.peek_glyph_rect(key) else {
                return GlyphValidation::RectMismatch;
            };
            let baked = [
                quad.atlas[0] as u32,
                quad.atlas[1] as u32,
                quad.atlas[2] as u32,
                quad.atlas[3] as u32,
            ];
            if rect != baked {
                return GlyphValidation::RectMismatch;
            }
        }
        GlyphValidation::Valid
    }
}

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

    #[test]
    fn layout_single_line_returns_nonzero_size() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let layout = bridge.layout_single_line("Hello", &TextStyle::default(), None);
        assert!(layout.width > 0.0, "width was {}", layout.width);
        assert!(layout.height > 0.0, "height was {}", layout.height);
    }

    #[test]
    fn ensure_glyphs_returns_quads() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let layout = bridge.layout_single_line("Hi", &TextStyle::default(), None);
        let glyphs = bridge.ensure_glyphs(&layout);
        assert!(
            glyphs.len() >= 2,
            "expected >= 2 glyphs, got {}",
            glyphs.len()
        );
    }

    #[test]
    fn empty_text_returns_zero_width() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let layout = bridge.layout_single_line("", &TextStyle::default(), None);
        assert_eq!(layout.width, 0.0);
    }

    #[test]
    fn set_raster_scale_produces_separate_cache_entries_and_layout_keys() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let style = TextStyle::default();

        let at_one = bridge.layout_single_line("Hello", &style, None);
        bridge.set_raster_scale(2.0);
        let at_two = bridge.layout_single_line("Hello", &style, None);

        // Metrics are raster-scale-independent…
        assert!((at_one.width - at_two.width).abs() < 1e-3);
        assert!((at_one.height - at_two.height).abs() < 1e-3);
        // …but each scale owns its own cache entry / glyph set.
        assert_ne!(at_one.layout_key, at_two.layout_key);
        assert_eq!(at_one.raster_scale, 1.0);
        assert_eq!(at_two.raster_scale, 2.0);

        let q1 = bridge.ensure_glyphs(&at_one);
        let q2 = bridge.ensure_glyphs(&at_two);
        assert_eq!(q1.len(), q2.len());
        assert!(!q1.is_empty());
        // The 2x entry samples a denser bitmap: atlas rects grow, screen
        // rects stay logical.
        let (a1, a2) = (&q1[0], &q2[0]);
        assert!(
            a2.atlas[2] > a1.atlas[2] * 1.5,
            "atlas w should roughly double: {} -> {}",
            a1.atlas[2],
            a2.atlas[2]
        );
        assert!((a1.screen[2] - a2.screen[2]).abs() <= 1.01);
    }

    #[test]
    fn raster_scale_back_to_one_hits_original_cache() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let style = TextStyle::default();

        let first = bridge.layout_single_line("Hello", &style, None);
        bridge.set_raster_scale(2.0);
        let scaled = bridge.layout_single_line("Hello", &style, None);
        bridge.set_raster_scale(1.0);
        let back = bridge.layout_single_line("Hello", &style, None);

        // Returning to 1.0 must hit the original entry (no cache churn —
        // set_raster_scale clears nothing).
        assert_eq!(first.layout_key, back.layout_key);
        assert_ne!(first.layout_key, scaled.layout_key);
    }

    /// Minimal BlockLayoutParams for driving a rich-text `DocumentFlow`
    /// through the bridge's shared service, the way `RichTextEngine` does.
    fn make_test_block(id: usize, text: &str) -> text_typeset::layout::block::BlockLayoutParams {
        use text_typeset::layout::block::{BlockLayoutParams, FragmentParams};
        BlockLayoutParams {
            base_direction: Default::default(),
            block_id: id,
            position: 0,
            text: text.to_string(),
            fragments: vec![FragmentParams {
                text: text.to_string(),
                offset: 0,
                length: text.len(),
                font_family: None,
                font_weight: None,
                font_bold: None,
                font_italic: None,
                font_point_size: None,
                underline_style: text_typeset::UnderlineStyle::None,
                overline: false,
                strikeout: false,
                is_link: false,
                letter_spacing: 0.0,
                word_spacing: 0.0,
                foreground_color: None,
                underline_color: None,
                background_color: None,
                anchor_href: None,
                tooltip: None,
                vertical_alignment: text_typeset::VerticalAlignment::Normal,
                image_name: None,
                image_width: 0.0,
                image_height: 0.0,
                footnote_marker: None,
                features: Vec::new(),
            }],
            alignment: text_typeset::layout::paragraph::Alignment::Left,
            top_margin: 0.0,
            bottom_margin: 0.0,
            left_margin: 0.0,
            right_margin: 0.0,
            text_indent: 0.0,
            list_marker: String::new(),
            list_indent: 0.0,
            tab_positions: vec![],
            line_height_multiplier: None,
            non_breakable_lines: false,
            hyphenation: None,
            checkbox: None,
            background_color: None,
        }
    }

    /// Regression test for the corruption root cause: evictions that
    /// happen inside `build_render_frame` (every rich-text `render()` —
    /// i.e. every keystroke in any text field) used to bump only the
    /// service's eviction epoch; `atlas_info` passed through just the
    /// snapshot's own `glyphs_evicted` flag, so the app-level recovery
    /// (invalidate every retained paint) never fired and stale glyph
    /// UVs stayed on screen until a forced repaint.
    #[test]
    fn atlas_info_reports_render_path_evictions() {
        let mut bridge = TypesetterBridge::new_with_default_font();

        // A label whose glyphs live in the shared atlas…
        let layout = bridge.layout_single_line("Hello", &TextStyle::default(), None);
        let _ = bridge.ensure_glyphs(&layout);
        // …with the baseline atlas state consumed.
        let baseline = bridge.atlas_info(0);
        assert!(
            !baseline.glyphs_evicted,
            "nothing can have been evicted yet"
        );

        // Drive a rich-text DocumentFlow through the shared service,
        // exactly like RichTextEngine, far past the LRU idle window
        // (120 generations) + scan cadence (60). The label's glyphs are
        // never touched, so build_render_frame evicts them mid-loop.
        let mut flow = DocumentFlow::new();
        flow.set_viewport(800.0, 600.0);
        flow.layout_blocks(bridge.service_mut(), vec![make_test_block(1, "zzzz")]);
        for _ in 0..250 {
            let _ = flow.render(bridge.service_mut());
        }

        let info = bridge.atlas_info(baseline.version);
        assert!(
            info.glyphs_evicted,
            "evictions performed inside build_render_frame (rich-text render path) \
             must surface through AtlasInfo::glyphs_evicted"
        );
    }

    #[test]
    fn atlas_info_version_semantics() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let layout = bridge.layout_single_line("Hi", &TextStyle::default(), None);
        let _ = bridge.ensure_glyphs(&layout);

        // First consumer, never uploaded: gets pixels + a version stamp.
        let first = bridge.atlas_info(0);
        assert!(first.version > 0);
        assert!(
            !first.pixels.is_empty(),
            "a consumer at version 0 must receive the atlas pixels"
        );

        // Same consumer, up to date, no new text work: no pixels.
        let second = bridge.atlas_info(first.version);
        assert_eq!(second.version, first.version, "clean frame must not bump");
        assert!(
            second.pixels.is_empty(),
            "an up-to-date consumer must not pay the pixel memcpy"
        );

        // A second window that never uploaded still gets pixels — the
        // first consumer must not have consumed them for everyone
        // (the old consume-once dirty-flag bug).
        let lagging = bridge.atlas_info(0);
        assert_eq!(lagging.version, first.version);
        assert!(
            !lagging.pixels.is_empty(),
            "a lagging consumer must receive pixels even after another \
             consumer already pulled this version"
        );

        // New rasterization dirties the atlas: version bumps, pixels flow.
        let layout2 = bridge.layout_single_line("WXYZ", &TextStyle::default(), None);
        let _ = bridge.ensure_glyphs(&layout2);
        let third = bridge.atlas_info(first.version);
        assert!(third.version > first.version);
        assert!(!third.pixels.is_empty());
    }

    #[test]
    fn debug_validate_layout_catches_stale_rect() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let layout = bridge.layout_single_line("Hi", &TextStyle::default(), None);

        // Untouched layout: every glyph resident at its baked rect.
        assert_eq!(
            bridge.debug_validate_layout(layout.layout_key),
            GlyphValidation::Valid
        );

        // Move one resident glyph's atlas rect underneath the baked
        // quads (what slot reuse after eviction does).
        let key = {
            let (quads, keys) = bridge
                .glyph_cache
                .get(&layout.layout_key)
                .expect("layout entry exists");
            quads
                .iter()
                .zip(keys.iter())
                .find(|(q, _)| q.atlas[2] > 0.0 && q.atlas[3] > 0.0)
                .map(|(_, k)| *k)
                .expect("at least one glyph with atlas residency")
        };
        assert!(
            bridge
                .service_mut()
                .debug_set_glyph_rect(&key, [499, 499, 1, 1])
        );
        assert_eq!(
            bridge.debug_validate_layout(layout.layout_key),
            GlyphValidation::RectMismatch,
            "a moved atlas rect under baked quads is definite corruption"
        );

        // After a wholesale cache clear, the key is unknown — the
        // retained-frame-outlived-a-clear signature.
        bridge.invalidate_cache();
        assert_eq!(
            bridge.debug_validate_layout(layout.layout_key),
            GlyphValidation::StaleKey
        );
    }

    #[test]
    fn longer_text_is_wider() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let short = bridge.layout_single_line("Hi", &TextStyle::default(), None);
        let long = bridge.layout_single_line("Hello World", &TextStyle::default(), None);
        assert!(long.width > short.width);
    }

    #[test]
    fn ascent_and_descent_are_positive() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let layout = bridge.layout_single_line("Test", &TextStyle::default(), None);
        assert!(layout.ascent > 0.0);
        assert!(layout.descent > 0.0);
        assert!((layout.ascent + layout.descent - layout.height).abs() < 0.01);
    }

    /// Arabic text renders with visible glyphs — regression test for
    /// the default-font gap that a future font-coverage fix addresses. Before
    /// the fix, `register_default_font` loaded only Inter, which has
    /// no Arabic glyph coverage, so every shaped codepoint produced a
    /// `.notdef` with a zero-size atlas rect. After the fix,
    /// `fonts-arabic` (default) registers Noto Sans Arabic as a
    /// fallback font, and text-typeset's codepoint-based fallback
    /// loop picks it up automatically.
    ///
    /// The test shapes an Arabic greeting and asserts (a) the total
    /// advance is positive and (b) at least one glyph in the layout
    /// rasterizes to a non-zero atlas rect, proving a real glyph was
    /// found (not an invisible `.notdef`).
    #[cfg(font_arabic)]
    #[test]
    fn arabic_text_renders_with_visible_glyphs() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let layout = bridge.layout_single_line("مرحبا", &TextStyle::default(), None);
        assert!(
            layout.width > 0.0,
            "Arabic text should produce a non-zero advance, got {}",
            layout.width
        );
        let glyphs = bridge.ensure_glyphs(&layout);
        assert!(
            !glyphs.is_empty(),
            "Arabic text should produce at least one glyph"
        );
        let visible = glyphs.iter().any(|g| g.atlas[2] > 0.0 && g.atlas[3] > 0.0);
        assert!(
            visible,
            "no Arabic glyph rasterized to a visible atlas rect — \
             is `fonts-arabic` enabled and the Noto Sans Arabic font \
             registered via `register_default_font`?"
        );
    }

    /// Regression test for a text-typeset bidi bug: Latin text
    /// embedded in an Arabic string must not be visually reversed.
    #[cfg(font_arabic)]
    #[test]
    fn latin_in_arabic_is_not_visually_reversed() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let style = TextStyle::default();

        let pure = bridge.layout_single_line("Alice", &style, None);
        let pure_glyphs = bridge.ensure_glyphs(&pure);
        assert_eq!(
            pure_glyphs.len(),
            5,
            "expected 5 glyphs for 'Alice', got {}",
            pure_glyphs.len()
        );
        let pure_widths: Vec<f32> = pure_glyphs.iter().map(|g| g.screen[2]).collect();

        let mixed = bridge.layout_single_line("مرحبا Alice", &style, None);
        let mixed_glyphs = bridge.ensure_glyphs(&mixed);
        assert!(
            mixed_glyphs.len() > pure_glyphs.len(),
            "mixed layout should contain at least the Latin glyphs plus Arabic"
        );

        for (i, (pw, mg)) in pure_widths
            .iter()
            .zip(mixed_glyphs.iter())
            .take(5)
            .enumerate()
        {
            let mw = mg.screen[2];
            assert!(
                (pw - mw).abs() < 0.5,
                "Latin glyph {} width mismatch: pure={:.2}, mixed={:.2} \
                 (Latin cluster in RTL paragraph is reversed — text-typeset bidi bug)",
                i,
                pw,
                mw,
            );
        }
    }

    #[cfg(font_hebrew)]
    #[test]
    fn hebrew_text_renders_with_visible_glyphs() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let layout = bridge.layout_single_line("שלום", &TextStyle::default(), None);
        assert!(layout.width > 0.0);
        let glyphs = bridge.ensure_glyphs(&layout);
        let visible = glyphs.iter().any(|g| g.atlas[2] > 0.0 && g.atlas[3] > 0.0);
        assert!(
            visible,
            "no Hebrew glyph rasterized to a visible atlas rect — \
             is `fonts-hebrew` enabled and the Noto Sans Hebrew font \
             registered via `register_default_font`?"
        );
    }

    #[test]
    fn max_width_truncates() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let unconstrained = bridge.layout_single_line(
            "A very long text that should be truncated",
            &TextStyle::default(),
            None,
        );
        let constrained = bridge.layout_single_line(
            "A very long text that should be truncated",
            &TextStyle::default(),
            Some(50.0),
        );
        assert!(constrained.width <= 51.0);
        assert!(constrained.width < unconstrained.width);
    }

    #[test]
    fn cache_hit_returns_same_result() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let first = bridge.layout_single_line("Hello", &TextStyle::default(), None);
        let second = bridge.layout_single_line("Hello", &TextStyle::default(), None);
        assert!((first.width - second.width).abs() < 0.001);
        assert!((first.height - second.height).abs() < 0.001);
    }

    #[test]
    fn different_max_width_is_separate_cache_entry() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let wide = bridge.layout_single_line("Hello World", &TextStyle::default(), None);
        let narrow = bridge.layout_single_line("Hello World", &TextStyle::default(), Some(30.0));
        assert!(narrow.width < wide.width || narrow.width <= 31.0);
    }

    /// Reproduces the stale-glyph bug: after a layout pass (many
    /// layout_single_line calls without ensure_glyphs), the first
    /// paint call's ensure_glyphs should return glyphs for the
    /// correct text, not for whatever was last measured.
    #[test]
    fn ensure_glyphs_after_layout_pass_returns_correct_text() {
        let mut bridge = TypesetterBridge::new_with_default_font();
        let style = TextStyle::default();

        let _l1 = bridge.layout_single_line("First text", &style, None);
        let _l2 = bridge.layout_single_line("Second text", &style, None);
        let _l3 = bridge.layout_single_line("Third text is the last measured", &style, None);

        let layout = bridge.layout_single_line("First text", &style, None);
        let glyphs = bridge.ensure_glyphs(&layout);

        assert!(!glyphs.is_empty(), "should produce glyphs for 'First text'");
        assert!(
            glyphs.len() <= 15,
            "got {} glyphs — expected ~10 for 'First text', not ~31 for the stale last_result",
            glyphs.len()
        );
    }

    /// A bundled face is gated on its `font_*` cfg, never on its feature.
    ///
    /// The difference is the whole reason `build.rs` exists. `include_bytes!`
    /// resolves at compile time, so gating on the *feature* makes a face whose
    /// file is absent a hard build error — which is what made five features,
    /// both meta-features and `--all-features` unbuildable on every revision of
    /// this workspace. Gating on the cfg the probe emits makes the same case a
    /// warning.
    ///
    /// Scanning the source is the only way to hold this: the wrong gate
    /// compiles perfectly whenever the file happens to be present, so it is
    /// invisible in a normal build and surfaces only under `--all-features`, or
    /// on the day someone adds a feature without its file.
    #[test]
    fn a_bundled_face_is_gated_on_its_probe_cfg_and_never_on_its_feature() {
        let src = include_str!("typesetter_bridge.rs");
        let offenders: Vec<&str> = src
            .lines()
            .map(str::trim)
            .filter(|l| l.starts_with("#[cfg(") && l.contains("feature = \"fonts-"))
            .collect();
        assert!(
            offenders.is_empty(),
            "gate a bundled face on its `font_*` cfg from build.rs, not on its \
             feature — `include_bytes!` of an absent file is a build error, and \
             that is what broke `--all-features`. Offending: {offenders:?}"
        );
    }
}