blinc_text 0.5.0

High-quality text rendering for Blinc UI framework
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
//! Text renderer
//!
//! High-level text rendering that combines font loading, shaping,
//! rasterization, atlas management, and glyph instance generation.
//!
//! Supports automatic emoji font fallback - when the primary font doesn't
//! have a glyph for an emoji character, the system emoji font is used.

use crate::atlas::{ColorGlyphAtlas, GlyphAtlas, GlyphInfo};
use crate::emoji::{is_emoji, is_variation_selector, is_zwj};
use crate::font::FontFace;
use crate::layout::{LayoutOptions, PositionedGlyph, TextLayoutEngine};
use crate::rasterizer::GlyphRasterizer;
use crate::registry::{FontRegistry, GenericFont};
use crate::shaper::TextShaper;
use crate::{Result, TextError};
use lru::LruCache;
use std::num::NonZeroUsize;
use std::sync::Arc;

/// Maximum number of glyphs to keep in the grayscale glyph cache
const GLYPH_CACHE_CAPACITY: usize = 512;

/// Maximum number of color glyphs (emoji) to keep in cache
const COLOR_GLYPH_CACHE_CAPACITY: usize = 128;

/// A GPU glyph instance for rendering
#[derive(Debug, Clone, Copy)]
pub struct GlyphInstance {
    /// Position and size in pixels (x, y, width, height)
    pub bounds: [f32; 4],
    /// UV coordinates in atlas (u_min, v_min, u_max, v_max)
    pub uv_bounds: [f32; 4],
    /// Text color (RGBA, 0.0-1.0)
    pub color: [f32; 4],
    /// Whether this glyph is from the color atlas (emoji)
    pub is_color: bool,
}

/// Result of preparing text for rendering
#[derive(Debug)]
pub struct PreparedText {
    /// Glyph instances ready for GPU rendering
    pub glyphs: Vec<GlyphInstance>,
    /// Total width of the text
    pub width: f32,
    /// Total height of the text (line height)
    pub height: f32,
    /// Ascender in pixels (distance from baseline to top of em box)
    pub ascender: f32,
    /// Descender in pixels (typically negative, distance from baseline to bottom)
    pub descender: f32,
}

/// A color span for styled text rendering
#[derive(Debug, Clone, Copy)]
pub struct ColorSpan {
    /// Start byte index in text
    pub start: usize,
    /// End byte index in text (exclusive)
    pub end: usize,
    /// RGBA color
    pub color: [f32; 4],
}

/// Text renderer that manages fonts, atlas, and glyph rendering
pub struct TextRenderer {
    /// Default font (legacy support)
    default_font: Option<FontFace>,
    /// Font registry for system font discovery and caching
    /// Can be shared with other components (like text measurement)
    font_registry: Arc<std::sync::Mutex<FontRegistry>>,
    /// Glyph atlas (grayscale for regular text)
    atlas: GlyphAtlas,
    /// Color glyph atlas (RGBA for color emoji)
    color_atlas: ColorGlyphAtlas,
    /// Glyph rasterizer
    rasterizer: GlyphRasterizer,
    /// Text layout engine
    layout_engine: TextLayoutEngine,
    /// LRU cache for grayscale glyphs: (font_id, glyph_id, quantized_size) -> atlas info
    /// font_id is hash of font name or 0 for default
    glyph_cache: LruCache<(u32, u16, u16), GlyphInfo>,
    /// LRU cache for color glyphs (emoji) - same key format
    color_glyph_cache: LruCache<(u32, u16, u16), GlyphInfo>,
}

impl TextRenderer {
    /// Create a new text renderer with default atlas size.
    ///
    /// Uses the global shared font registry to minimize memory usage.
    /// Apple Color Emoji alone is 180MB - sharing prevents loading it multiple times.
    pub fn new() -> Self {
        Self {
            default_font: None,
            font_registry: crate::global_font_registry(),
            atlas: GlyphAtlas::default(),
            color_atlas: ColorGlyphAtlas::default(),
            rasterizer: GlyphRasterizer::new(),
            layout_engine: TextLayoutEngine::new(),
            glyph_cache: LruCache::new(NonZeroUsize::new(GLYPH_CACHE_CAPACITY).unwrap()),
            color_glyph_cache: LruCache::new(
                NonZeroUsize::new(COLOR_GLYPH_CACHE_CAPACITY).unwrap(),
            ),
        }
    }

    /// Create a new text renderer with a shared font registry
    ///
    /// Use this to share fonts between text measurement and rendering,
    /// ensuring consistent text layout.
    pub fn with_shared_registry(registry: Arc<std::sync::Mutex<FontRegistry>>) -> Self {
        Self {
            default_font: None,
            font_registry: registry,
            atlas: GlyphAtlas::default(),
            color_atlas: ColorGlyphAtlas::default(),
            rasterizer: GlyphRasterizer::new(),
            layout_engine: TextLayoutEngine::new(),
            glyph_cache: LruCache::new(NonZeroUsize::new(GLYPH_CACHE_CAPACITY).unwrap()),
            color_glyph_cache: LruCache::new(
                NonZeroUsize::new(COLOR_GLYPH_CACHE_CAPACITY).unwrap(),
            ),
        }
    }

    /// Create with custom atlas size.
    ///
    /// Uses the global shared font registry to minimize memory usage.
    pub fn with_atlas_size(width: u32, height: u32) -> Self {
        Self {
            default_font: None,
            font_registry: crate::global_font_registry(),
            atlas: GlyphAtlas::new(width, height),
            color_atlas: ColorGlyphAtlas::default(),
            rasterizer: GlyphRasterizer::new(),
            layout_engine: TextLayoutEngine::new(),
            glyph_cache: LruCache::new(NonZeroUsize::new(GLYPH_CACHE_CAPACITY).unwrap()),
            color_glyph_cache: LruCache::new(
                NonZeroUsize::new(COLOR_GLYPH_CACHE_CAPACITY).unwrap(),
            ),
        }
    }

    /// Get the shared font registry
    ///
    /// This can be used to share the registry with other components
    /// like text measurement.
    pub fn font_registry(&self) -> Arc<std::sync::Mutex<FontRegistry>> {
        self.font_registry.clone()
    }

    /// Set the default font
    pub fn set_default_font(&mut self, font: FontFace) {
        self.default_font = Some(font);
    }

    /// Load and set the default font from file
    pub fn load_default_font(&mut self, path: &std::path::Path) -> Result<()> {
        let font = FontFace::from_file(path)?;
        self.default_font = Some(font);
        Ok(())
    }

    /// Load and set the default font from data
    pub fn load_default_font_data(&mut self, data: Vec<u8>) -> Result<()> {
        let font = FontFace::from_data(data)?;
        self.default_font = Some(font);
        Ok(())
    }

    /// Load font data into the registry (used by the rendering system)
    ///
    /// This adds fonts to the registry where they can be found by name
    /// during text rendering. Returns the number of font faces loaded.
    pub fn load_font_data_to_registry(&mut self, data: Vec<u8>) -> usize {
        let mut registry = self.font_registry.lock().unwrap();
        registry.load_font_data(data)
    }

    /// Get the glyph atlas (grayscale)
    pub fn atlas(&self) -> &GlyphAtlas {
        &self.atlas
    }

    /// Get mutable atlas (for GPU upload checking)
    pub fn atlas_mut(&mut self) -> &mut GlyphAtlas {
        &mut self.atlas
    }

    /// Get the color glyph atlas (RGBA for emoji)
    pub fn color_atlas(&self) -> &ColorGlyphAtlas {
        &self.color_atlas
    }

    /// Get mutable color atlas
    pub fn color_atlas_mut(&mut self) -> &mut ColorGlyphAtlas {
        &mut self.color_atlas
    }

    /// Check if atlas needs GPU upload
    pub fn atlas_is_dirty(&self) -> bool {
        self.atlas.is_dirty()
    }

    /// Check if color atlas needs GPU upload
    pub fn color_atlas_is_dirty(&self) -> bool {
        self.color_atlas.is_dirty()
    }

    /// Mark atlas as clean after GPU upload
    pub fn mark_atlas_clean(&mut self) {
        self.atlas.mark_clean();
    }

    /// Mark color atlas as clean after GPU upload
    pub fn mark_color_atlas_clean(&mut self) {
        self.color_atlas.mark_clean();
    }

    /// Number of cached glyph rasterization entries (grayscale LRU)
    pub fn glyph_cache_len(&self) -> usize {
        self.glyph_cache.len()
    }

    /// Number of cached color glyph rasterization entries (emoji LRU)
    pub fn color_glyph_cache_len(&self) -> usize {
        self.color_glyph_cache.len()
    }

    /// Capacity of the grayscale glyph LRU cache
    pub fn glyph_cache_capacity(&self) -> usize {
        GLYPH_CACHE_CAPACITY
    }

    /// Capacity of the color glyph LRU cache
    pub fn color_glyph_cache_capacity(&self) -> usize {
        COLOR_GLYPH_CACHE_CAPACITY
    }

    /// Get atlas pixel data for GPU upload (grayscale)
    pub fn atlas_pixels(&self) -> &[u8] {
        self.atlas.pixels()
    }

    /// Get color atlas pixel data for GPU upload (RGBA)
    pub fn color_atlas_pixels(&self) -> &[u8] {
        self.color_atlas.pixels()
    }

    /// Get atlas dimensions
    pub fn atlas_dimensions(&self) -> (u32, u32) {
        self.atlas.dimensions()
    }

    /// Get color atlas dimensions
    pub fn color_atlas_dimensions(&self) -> (u32, u32) {
        self.color_atlas.dimensions()
    }

    /// Prepare text for rendering, rasterizing glyphs as needed
    pub fn prepare_text(
        &mut self,
        text: &str,
        font_size: f32,
        color: [f32; 4],
        options: &LayoutOptions,
    ) -> Result<PreparedText> {
        self.prepare_text_internal(
            text,
            font_size,
            color,
            options,
            None,
            GenericFont::System,
            400,
            false,
        )
    }

    /// Prepare text for rendering with a specific font family
    ///
    /// # Arguments
    /// * `text` - The text to render
    /// * `font_size` - Font size in pixels
    /// * `color` - Text color (RGBA, 0.0-1.0)
    /// * `options` - Layout options
    /// * `font_name` - Optional font name (e.g., "Fira Code", "Inter")
    /// * `generic` - Generic font fallback category
    pub fn prepare_text_with_font(
        &mut self,
        text: &str,
        font_size: f32,
        color: [f32; 4],
        options: &LayoutOptions,
        font_name: Option<&str>,
        generic: GenericFont,
    ) -> Result<PreparedText> {
        self.prepare_text_internal(
            text, font_size, color, options, font_name, generic, 400, false,
        )
    }

    /// Prepare text for rendering with a specific font family, weight, and style
    ///
    /// # Arguments
    /// * `text` - The text to render
    /// * `font_size` - Font size in pixels
    /// * `color` - Text color (RGBA, 0.0-1.0)
    /// * `options` - Layout options
    /// * `font_name` - Optional font name (e.g., "Fira Code", "Inter")
    /// * `generic` - Generic font fallback category
    /// * `weight` - Font weight (100-900, where 400 is normal, 700 is bold)
    /// * `italic` - Whether to use italic variant
    #[allow(clippy::too_many_arguments)]
    pub fn prepare_text_with_style(
        &mut self,
        text: &str,
        font_size: f32,
        color: [f32; 4],
        options: &LayoutOptions,
        font_name: Option<&str>,
        generic: GenericFont,
        weight: u16,
        italic: bool,
    ) -> Result<PreparedText> {
        self.prepare_text_internal(
            text, font_size, color, options, font_name, generic, weight, italic,
        )
    }

    /// Internal method for preparing text with optional font family
    #[allow(clippy::too_many_arguments)]
    fn prepare_text_internal(
        &mut self,
        text: &str,
        font_size: f32,
        color: [f32; 4],
        options: &LayoutOptions,
        font_name: Option<&str>,
        generic: GenericFont,
        weight: u16,
        italic: bool,
    ) -> Result<PreparedText> {
        // Resolve the font to use
        let font = self.resolve_font_with_style(font_name, generic, weight, italic)?;
        let font_id = self.font_id_with_style(font_name, generic, weight, italic);

        // Get font metrics for the PreparedText result
        let (ascender, descender) = {
            let metrics = font.metrics();
            (
                metrics.ascender_px(font_size),
                metrics.descender_px(font_size),
            )
        };

        // Lazy-loaded fallback fonts: only load emoji/symbol fonts when actually needed
        // This saves ~180MB of memory when text doesn't contain emoji
        // Emoji font and symbol font are loaded separately - symbol font is small,
        // but emoji font (Apple Color Emoji) is ~180MB, so we only load it for actual emoji
        let mut emoji_font: Option<Arc<FontFace>> = None;
        let mut symbol_font: Option<Arc<FontFace>> = None;
        let mut emoji_font_id: u32 = 0;
        let mut symbol_font_id: u32 = 0;
        let mut emoji_font_loaded = false;
        let mut symbol_font_loaded = false;

        // Layout the text
        let layout = self.layout_engine.layout(text, &font, font_size, options);

        // Collect positioned glyphs for processing
        let positioned_glyphs: Vec<_> = layout.glyphs().cloned().collect();

        // Convert to GPU glyph instances
        let mut glyphs = Vec::with_capacity(positioned_glyphs.len());

        // Track glyph info along with whether it's a color glyph
        // (GlyphInfo, PositionedGlyph, is_color)
        struct RasterizedGlyphData {
            info: GlyphInfo,
            positioned: PositionedGlyph,
            is_color: bool,
        }

        let mut glyph_infos: Vec<Option<RasterizedGlyphData>> =
            Vec::with_capacity(positioned_glyphs.len());

        // Track advance correction when using fallback fonts
        // This accumulates the difference between what the primary font gave us
        // and what the fallback font's actual advance is
        let mut x_offset: f32 = 0.0;

        for (i, positioned) in positioned_glyphs.iter().enumerate() {
            if positioned.codepoint.is_whitespace() {
                glyph_infos.push(None);
                continue;
            }

            // Skip invisible combining characters
            // - Variation selectors (U+FE00-U+FE0F) modify the previous character's style
            // - ZWJ (U+200D) joins emoji into sequences
            // These are handled by the shaper but shouldn't render as visible glyphs
            if is_variation_selector(positioned.codepoint) || is_zwj(positioned.codepoint) {
                glyph_infos.push(None);
                continue;
            }

            // Check if this is an emoji or if the primary font doesn't have this glyph
            let is_emoji_char = is_emoji(positioned.codepoint);

            // For emoji characters, check if we've already processed this exact codepoint
            // at a previous position. This handles cases where HarfBuzz produces multiple
            // glyphs for a single emoji sequence (e.g., ☀️ = sun + variation selector).
            // The shaper may report both glyphs with the same codepoint due to cluster mapping.
            if is_emoji_char {
                // Check if the previous glyph was the same emoji codepoint
                // If so, this is likely a duplicate from cluster mapping and should be skipped
                if i > 0 {
                    let prev = &positioned_glyphs[i - 1];
                    if prev.codepoint == positioned.codepoint && is_emoji(prev.codepoint) {
                        // Skip this duplicate emoji glyph
                        glyph_infos.push(None);
                        continue;
                    }
                }
            }

            // Check if fallback is needed:
            // - Primary font doesn't have this glyph (glyph_id == 0 or has_glyph returns false)
            // - For emoji, always try emoji font to get color rendering (even if primary has glyph)
            let primary_has_glyph =
                positioned.glyph_id != 0 && font.has_glyph(positioned.codepoint);
            let needs_fallback = !primary_has_glyph || is_emoji_char;

            if needs_fallback {
                // Lazy load symbol font for non-emoji fallback (small, fast to load)
                if !symbol_font_loaded {
                    let mut registry = self.font_registry.lock().unwrap();
                    symbol_font = registry.load_generic(GenericFont::Symbol).ok();
                    drop(registry);
                    symbol_font_id = self.font_id(None, GenericFont::Symbol);
                    symbol_font_loaded = true;
                }

                // Only load emoji font (~180MB) when we actually encounter an emoji character
                if is_emoji_char && !emoji_font_loaded {
                    let mut registry = self.font_registry.lock().unwrap();
                    emoji_font = registry.load_generic(GenericFont::Emoji).ok();
                    drop(registry);
                    emoji_font_id = self.font_id(None, GenericFont::Emoji);
                    emoji_font_loaded = true;
                }

                // Build fallback font chain: try emoji first (for emoji), then symbol (for Unicode symbols)
                // For non-emoji characters, prefer symbol font to get text-colored glyphs
                let fallback_fonts: Vec<(&Arc<FontFace>, u32, bool)> = if is_emoji_char {
                    // Emoji: try emoji font first (color), then symbol (grayscale)
                    [
                        emoji_font.as_ref().map(|f| (f, emoji_font_id, true)),
                        symbol_font.as_ref().map(|f| (f, symbol_font_id, false)),
                    ]
                    .into_iter()
                    .flatten()
                    .collect()
                } else {
                    // Non-emoji: only use symbol font (don't load emoji font for non-emoji characters)
                    [symbol_font.as_ref().map(|f| (f, symbol_font_id, false))]
                        .into_iter()
                        .flatten()
                        .collect()
                };

                let mut found_fallback = false;
                for (fallback_font, fallback_font_id, use_color) in &fallback_fonts {
                    if let Some(fallback_glyph_id) = fallback_font.glyph_id(positioned.codepoint) {
                        if fallback_glyph_id != 0 {
                            // Shape just this character with the fallback font to get correct metrics
                            let shaper = TextShaper::new();
                            // Use stack-allocated buffer instead of heap String
                            let mut char_buf = [0u8; 4];
                            let char_str = positioned.codepoint.encode_utf8(&mut char_buf);
                            let shaped = shaper.shape(char_str, fallback_font, font_size);

                            if let Some(shaped_glyph) = shaped.glyphs.first() {
                                // Create a new positioned glyph with fallback font metrics
                                // Apply the accumulated x_offset from previous fallback corrections
                                let fallback_positioned = PositionedGlyph {
                                    glyph_id: shaped_glyph.glyph_id,
                                    codepoint: positioned.codepoint,
                                    x: positioned.x + x_offset,
                                    y: positioned.y,
                                };

                                // Use color rasterization for emoji font
                                let (glyph_info, is_color) = if *use_color && is_emoji_char {
                                    let info = self.rasterize_color_glyph_for_font(
                                        fallback_font,
                                        *fallback_font_id,
                                        shaped_glyph.glyph_id,
                                        font_size,
                                    )?;
                                    (info, true)
                                } else {
                                    let info = self.rasterize_glyph_for_font(
                                        fallback_font,
                                        *fallback_font_id,
                                        shaped_glyph.glyph_id,
                                        font_size,
                                    )?;
                                    (info, false)
                                };

                                // Calculate advance correction
                                // The fallback font's advance tells us how much space this glyph needs
                                let fallback_advance = glyph_info.advance as f32;

                                // Calculate what advance the primary font thought this character had
                                // by looking at the distance to the next glyph
                                let primary_advance = if i + 1 < positioned_glyphs.len() {
                                    positioned_glyphs[i + 1].x - positioned.x
                                } else {
                                    // Last character - use layout width
                                    (layout.width - positioned.x).max(0.0)
                                };

                                // Accumulate the difference
                                x_offset += fallback_advance - primary_advance;

                                glyph_infos.push(Some(RasterizedGlyphData {
                                    info: glyph_info,
                                    positioned: fallback_positioned,
                                    is_color,
                                }));
                                found_fallback = true;
                                break;
                            }
                        }
                    }
                }

                if found_fallback {
                    continue;
                }
            }

            // Use primary font (apply accumulated x_offset)
            let glyph_info =
                self.rasterize_glyph_for_font(&font, font_id, positioned.glyph_id, font_size)?;
            let mut adjusted_positioned = *positioned;
            adjusted_positioned.x += x_offset;
            glyph_infos.push(Some(RasterizedGlyphData {
                info: glyph_info,
                positioned: adjusted_positioned,
                is_color: false,
            }));
        }

        // Re-read atlas dimensions after rasterization — the atlas may have grown
        let atlas_dims = self.atlas.dimensions();
        let color_atlas_dims = self.color_atlas.dimensions();

        // Second pass: build glyph instances
        for glyph_data in &glyph_infos {
            let data = match glyph_data {
                Some(d) => d,
                None => continue,
            };

            // Skip glyphs with no bitmap (empty glyphs)
            if data.info.region.width == 0 || data.info.region.height == 0 {
                continue;
            }

            // Calculate screen position
            // positioned.x is the pen position from the shaper (includes advance)
            // bearing_x is the offset from pen position to the glyph's left edge
            let x = data.positioned.x + data.info.bearing_x as f32;
            let y = data.positioned.y - data.info.bearing_y as f32;
            let w = data.info.region.width as f32;
            let h = data.info.region.height as f32;

            // Get UV coordinates from the appropriate atlas
            let uv = if data.is_color {
                data.info
                    .region
                    .uv_bounds(color_atlas_dims.0, color_atlas_dims.1)
            } else {
                data.info.region.uv_bounds(atlas_dims.0, atlas_dims.1)
            };

            glyphs.push(GlyphInstance {
                bounds: [x, y, w, h],
                uv_bounds: uv,
                color,
                is_color: data.is_color,
            });
        }

        Ok(PreparedText {
            glyphs,
            width: layout.width,
            height: layout.height,
            ascender,
            descender,
        })
    }

    /// Prepare styled text with multiple color spans
    ///
    /// This renders text as a single unit but applies different colors to different ranges.
    /// Unlike creating separate text elements, this ensures proper character spacing.
    #[allow(clippy::too_many_arguments)]
    pub fn prepare_styled_text(
        &mut self,
        text: &str,
        font_size: f32,
        default_color: [f32; 4],
        color_spans: &[ColorSpan],
        options: &LayoutOptions,
        font_name: Option<&str>,
        generic: GenericFont,
    ) -> Result<PreparedText> {
        // Resolve the font to use
        let font = self.resolve_font(font_name, generic)?;
        let font_id = self.font_id(font_name, generic);

        // Get font metrics
        let (ascender, descender) = {
            let metrics = font.metrics();
            (
                metrics.ascender_px(font_size),
                metrics.descender_px(font_size),
            )
        };

        // Layout the text (this gives us proper positions from HarfBuzz)
        let layout = self.layout_engine.layout(text, &font, font_size, options);

        // Collect positioned glyphs
        let positioned_glyphs: Vec<_> = layout.glyphs().cloned().collect();

        // Build a map of byte position to color
        // For each character, find which span it belongs to
        let get_color_for_byte_pos = |byte_pos: usize| -> [f32; 4] {
            for span in color_spans {
                if byte_pos >= span.start && byte_pos < span.end {
                    return span.color;
                }
            }
            default_color
        };

        // Convert to GPU glyph instances
        let mut glyphs = Vec::with_capacity(positioned_glyphs.len());

        // First pass: rasterize all glyphs
        let mut glyph_infos: Vec<Option<GlyphInfo>> = Vec::with_capacity(positioned_glyphs.len());
        for positioned in &positioned_glyphs {
            if positioned.codepoint.is_whitespace() {
                glyph_infos.push(None);
                continue;
            }

            // Skip invisible combining characters
            if is_variation_selector(positioned.codepoint) || is_zwj(positioned.codepoint) {
                glyph_infos.push(None);
                continue;
            }

            let glyph_info =
                self.rasterize_glyph_for_font(&font, font_id, positioned.glyph_id, font_size)?;
            glyph_infos.push(Some(glyph_info));
        }

        // Re-read atlas dimensions after rasterization — the atlas may have grown
        let atlas_dims = self.atlas.dimensions();

        // Second pass: build glyph instances with per-glyph colors
        // We need to map glyph cluster (byte position) to color
        let byte_positions: Vec<usize> = text.char_indices().map(|(i, _)| i).collect();

        for (i, (positioned, glyph_info)) in
            positioned_glyphs.iter().zip(glyph_infos.iter()).enumerate()
        {
            let glyph_info = match glyph_info {
                Some(info) => *info,
                None => continue,
            };

            if glyph_info.region.width == 0 || glyph_info.region.height == 0 {
                continue;
            }

            // Get the byte position for this glyph's cluster to determine color
            let byte_pos = byte_positions.get(i).copied().unwrap_or(0);
            let color = get_color_for_byte_pos(byte_pos);

            // positioned.x is the pen position from the shaper
            // bearing_x is the offset from pen position to the glyph's left edge
            let x = positioned.x + glyph_info.bearing_x as f32;
            let y = positioned.y - glyph_info.bearing_y as f32;
            let w = glyph_info.region.width as f32;
            let h = glyph_info.region.height as f32;

            let uv = glyph_info.region.uv_bounds(atlas_dims.0, atlas_dims.1);

            glyphs.push(GlyphInstance {
                bounds: [x, y, w, h],
                uv_bounds: uv,
                color,
                is_color: false,
            });
        }

        Ok(PreparedText {
            glyphs,
            width: layout.width,
            height: layout.height,
            ascender,
            descender,
        })
    }

    /// Resolve font by name or generic category, with fallback to default
    /// Uses only cached fonts - fonts should be preloaded at app startup
    fn resolve_font(
        &mut self,
        font_name: Option<&str>,
        generic: GenericFont,
    ) -> Result<Arc<FontFace>> {
        self.resolve_font_with_style(font_name, generic, 400, false)
    }

    /// Resolve font by name or generic category with specific weight and style
    /// Loads fonts on demand if not cached
    fn resolve_font_with_style(
        &mut self,
        font_name: Option<&str>,
        generic: GenericFont,
        weight: u16,
        italic: bool,
    ) -> Result<Arc<FontFace>> {
        let mut registry = self.font_registry.lock().unwrap();

        // First try cache lookup
        if let Some(font) = registry.get_for_render_with_style(font_name, generic, weight, italic) {
            return Ok(font);
        }

        // Try loading the font with style on demand
        if let Some(name) = font_name {
            if let Ok(font) = registry.load_font_with_style(name, weight, italic) {
                return Ok(font);
            }
        }

        // Try loading generic font with style
        if let Ok(font) = registry.load_generic_with_style(generic, weight, italic) {
            return Ok(font);
        }

        // If styled font not found, fall back to normal style
        if weight != 400 || italic {
            if let Some(font) = registry.get_for_render_with_style(font_name, generic, 400, false) {
                return Ok(font);
            }
            // Try loading normal style
            if let Ok(font) = registry.load_generic_with_style(generic, 400, false) {
                return Ok(font);
            }
        }

        // Ultimate fallback to SansSerif normal
        if let Some(font) = registry.get_cached_generic(GenericFont::SansSerif) {
            return Ok(font);
        }
        if let Ok(font) = registry.load_generic(GenericFont::SansSerif) {
            return Ok(font);
        }

        Err(TextError::FontLoadError("No fonts available".to_string()))
    }

    /// Preload fonts that your app uses (call at startup)
    pub fn preload_fonts(&mut self, names: &[&str]) {
        let mut registry = self.font_registry.lock().unwrap();
        registry.preload_fonts(names);
    }

    /// Preload fonts with specific weights and styles
    pub fn preload_fonts_with_styles(&mut self, specs: &[(&str, u16, bool)]) {
        let mut registry = self.font_registry.lock().unwrap();
        for (name, weight, italic) in specs {
            let _ = registry.load_font_with_style(name, *weight, *italic);
        }
    }

    /// Preload generic font variants (weights and italic)
    pub fn preload_generic_styles(&mut self, generic: GenericFont, weights: &[u16], italic: bool) {
        let mut registry = self.font_registry.lock().unwrap();
        for weight in weights {
            let _ = registry.load_generic_with_style(generic, *weight, italic);
        }
    }

    /// Generate a unique font ID for cache keys
    fn font_id(&self, font_name: Option<&str>, generic: GenericFont) -> u32 {
        self.font_id_with_style(font_name, generic, 400, false)
    }

    /// Generate a unique font ID for cache keys with style
    fn font_id_with_style(
        &self,
        font_name: Option<&str>,
        generic: GenericFont,
        weight: u16,
        italic: bool,
    ) -> u32 {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        font_name.hash(&mut hasher);
        generic.hash(&mut hasher);
        weight.hash(&mut hasher);
        italic.hash(&mut hasher);
        hasher.finish() as u32
    }

    /// Rasterize a glyph for a specific font
    fn rasterize_glyph_for_font(
        &mut self,
        font: &FontFace,
        font_id: u32,
        glyph_id: u16,
        font_size: f32,
    ) -> Result<GlyphInfo> {
        // Quantize font size for cache key (0.5px granularity)
        let size_key = (font_size * 2.0).round() as u16;
        let cache_key = (font_id, glyph_id, size_key);

        // Check cache first (LruCache::get promotes to most-recently-used)
        if let Some(info) = self.glyph_cache.get(&cache_key) {
            return Ok(*info);
        }

        // Rasterize the glyph
        let rasterized = self.rasterizer.rasterize(font, glyph_id, font_size)?;

        // Handle empty glyphs (like space)
        if rasterized.width == 0 || rasterized.height == 0 {
            let info = GlyphInfo {
                region: crate::atlas::AtlasRegion {
                    x: 0,
                    y: 0,
                    width: 0,
                    height: 0,
                },
                bearing_x: rasterized.bearing_x,
                bearing_y: rasterized.bearing_y,
                advance: rasterized.advance,
                font_size,
            };
            // LruCache::put evicts oldest entry if at capacity
            self.glyph_cache.put(cache_key, info);
            return Ok(info);
        }

        // Insert into atlas, growing if full
        let info = match self.atlas.insert_glyph(
            font_id,
            glyph_id,
            font_size,
            rasterized.width,
            rasterized.height,
            rasterized.bearing_x,
            rasterized.bearing_y,
            rasterized.advance,
            &rasterized.bitmap,
        ) {
            Ok(info) => info,
            Err(TextError::AtlasFull) => {
                if !self.atlas.grow() {
                    return Err(TextError::AtlasFull);
                }
                let (nw, nh) = self.atlas.dimensions();
                tracing::info!("Glyph atlas grew to {}x{}", nw, nh);
                self.atlas.insert_glyph(
                    font_id,
                    glyph_id,
                    font_size,
                    rasterized.width,
                    rasterized.height,
                    rasterized.bearing_x,
                    rasterized.bearing_y,
                    rasterized.advance,
                    &rasterized.bitmap,
                )?
            }
            Err(e) => return Err(e),
        };

        self.glyph_cache.put(cache_key, info);
        Ok(info)
    }

    /// Rasterize a color glyph (emoji) for a specific font
    fn rasterize_color_glyph_for_font(
        &mut self,
        font: &FontFace,
        font_id: u32,
        glyph_id: u16,
        font_size: f32,
    ) -> Result<GlyphInfo> {
        // Quantize font size for cache key (0.5px granularity)
        let size_key = (font_size * 2.0).round() as u16;
        let cache_key = (font_id, glyph_id, size_key);

        // Check color cache first (LruCache::get promotes to most-recently-used)
        if let Some(info) = self.color_glyph_cache.get(&cache_key) {
            return Ok(*info);
        }

        // Rasterize the glyph as color (RGBA)
        let rasterized = self.rasterizer.rasterize_color(font, glyph_id, font_size)?;

        // Handle empty glyphs
        if rasterized.width == 0 || rasterized.height == 0 {
            let info = GlyphInfo {
                region: crate::atlas::AtlasRegion {
                    x: 0,
                    y: 0,
                    width: 0,
                    height: 0,
                },
                bearing_x: rasterized.bearing_x,
                bearing_y: rasterized.bearing_y,
                advance: rasterized.advance,
                font_size,
            };
            // LruCache::put evicts oldest entry if at capacity
            self.color_glyph_cache.put(cache_key, info);
            return Ok(info);
        }

        // Insert into color atlas, growing if full
        let info = match self.color_atlas.insert_glyph(
            font_id,
            glyph_id,
            font_size,
            rasterized.width,
            rasterized.height,
            rasterized.bearing_x,
            rasterized.bearing_y,
            rasterized.advance,
            &rasterized.bitmap,
        ) {
            Ok(info) => info,
            Err(TextError::AtlasFull) => {
                if !self.color_atlas.grow() {
                    return Err(TextError::AtlasFull);
                }
                let (nw, nh) = self.color_atlas.dimensions();
                tracing::info!("Color glyph atlas grew to {}x{}", nw, nh);
                self.color_atlas.insert_glyph(
                    font_id,
                    glyph_id,
                    font_size,
                    rasterized.width,
                    rasterized.height,
                    rasterized.bearing_x,
                    rasterized.bearing_y,
                    rasterized.advance,
                    &rasterized.bitmap,
                )?
            }
            Err(e) => return Err(e),
        };

        self.color_glyph_cache.put(cache_key, info);
        Ok(info)
    }

    /// Legacy method for backward compatibility - uses system font from registry
    #[allow(dead_code)]
    fn rasterize_glyph_if_needed(&mut self, glyph_id: u16, font_size: f32) -> Result<GlyphInfo> {
        let font = {
            let mut registry = self.font_registry.lock().unwrap();
            registry.load_generic(GenericFont::SansSerif)?
        };
        self.rasterize_glyph_for_font(&font, 0, glyph_id, font_size)
    }

    /// Clear the glyph cache and atlas
    pub fn clear(&mut self) {
        self.atlas.clear();
        self.color_atlas.clear();
        self.glyph_cache.clear();
        self.color_glyph_cache.clear();
    }
}

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