Skip to main content

euv_core/font/atlas/
impl.rs

1use super::*;
2
3/// Caching lookup operations for `GlyphAtlas`.
4impl GlyphAtlas {
5    /// Returns the cached `RasterizedGlyph` for `key`, rasterizing and inserting it on a miss.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Mutable reference to the glyph atlas.
10    /// - `GlyphKey` - Quantized glyph lookup key.
11    ///
12    /// # Returns
13    ///
14    /// - `&RasterizedGlyph` - Cached coverage bitmap and pixel metrics.
15    pub fn get_or_rasterize(&mut self, key: GlyphKey) -> &RasterizedGlyph {
16        if !self.get_glyphs().contains_key(&key) {
17            let glyph: RasterizedGlyph = rasterize_glyph(key);
18            self.get_mut_glyphs().insert(key, glyph);
19            let next_rev: u64 = self.get_revision().wrapping_add(1);
20            self.set_revision(next_rev);
21        }
22        &self.get_glyphs()[&key]
23    }
24
25    /// Returns the total number of cached glyph entries.
26    ///
27    /// # Arguments
28    ///
29    /// - `&Self` - Glyph atlas reference.
30    ///
31    /// # Returns
32    ///
33    /// - `usize` - Number of cached glyphs.
34    #[must_use]
35    pub fn len(&self) -> usize {
36        self.get_glyphs().len()
37    }
38
39    /// Returns true when the atlas contains no cached glyphs.
40    ///
41    /// # Arguments
42    ///
43    /// - `&Self` - Glyph atlas reference.
44    ///
45    /// # Returns
46    ///
47    /// - `bool` - True if empty.
48    #[must_use]
49    pub fn is_empty(&self) -> bool {
50        self.get_glyphs().is_empty()
51    }
52
53    /// Returns the cached `RasterizedGlyph` for `key`, rasterizing through `provider` on a miss.
54    ///
55    /// # Arguments
56    ///
57    /// - `&Self` - Mutable reference to the glyph atlas.
58    /// - `GlyphKey` - Quantized glyph lookup key.
59    /// - `&dyn SystemFontProvider` - Native or deterministic font rasterization provider.
60    ///
61    /// # Returns
62    ///
63    /// - `&RasterizedGlyph` - Cached coverage bitmap and pixel metrics.
64    pub fn get_or_rasterize_with(
65        &mut self,
66        key: GlyphKey,
67        provider: &dyn SystemFontProvider,
68    ) -> &RasterizedGlyph {
69        if !self.get_glyphs().contains_key(&key) {
70            let glyph: RasterizedGlyph = provider
71                .rasterize(key)
72                .unwrap_or_else(|| rasterize_glyph(key));
73            self.get_mut_glyphs().insert(key, glyph);
74            let next_rev: u64 = self.get_revision().wrapping_add(1);
75            self.set_revision(next_rev);
76        }
77        &self.get_glyphs()[&key]
78    }
79
80    /// Shelf-packs all cached glyphs into a single GPU-uploadable RGBA8 texture atlas.
81    ///
82    /// # Arguments
83    ///
84    /// - `&Self` - Glyph atlas reference.
85    ///
86    /// # Returns
87    ///
88    /// - `PackedGlyphAtlas` - Packed RGBA8 texture, per-glyph UV rects, and revision tag.
89    #[must_use]
90    pub fn pack_rgba_atlas(&self) -> PackedGlyphAtlas {
91        let atlas_w: u32 = ATLAS_DEFAULT_WIDTH;
92        let glyph_count: usize = self.get_glyphs().len();
93        let mut cursor_x: u32 = 1;
94        let mut cursor_y: u32 = 1;
95        let mut shelf_h: u32 = 0;
96        let mut regions: HashMap<GlyphKey, (u32, u32, u32, u32)> =
97            HashMap::with_capacity(glyph_count);
98        let mut placements: Vec<(&RasterizedGlyph, u32, u32)> = Vec::with_capacity(glyph_count);
99        for (key, glyph) in self.get_glyphs().iter() {
100            let gw: u32 = glyph.get_width();
101            let gh: u32 = glyph.get_height();
102            if cursor_x + gw + 1 > atlas_w {
103                cursor_x = 1;
104                cursor_y += shelf_h + 1;
105                shelf_h = 0;
106            }
107            regions.insert(*key, (cursor_x, cursor_y, gw, gh));
108            placements.push((glyph, cursor_x, cursor_y));
109            cursor_x += gw + 1;
110            shelf_h = shelf_h.max(gh);
111        }
112        let atlas_h: u32 = (cursor_y + shelf_h + 1).max(16);
113        let atlas_w_usize: usize = atlas_w as usize;
114        let mut pixels: Vec<u8> = vec![0; atlas_w_usize * (atlas_h as usize) * 4];
115        for (glyph, rx, ry) in placements.into_iter() {
116            let gw: usize = glyph.get_width() as usize;
117            let gh: usize = glyph.get_height() as usize;
118            let cov: &[u8] = glyph.get_coverage().as_slice();
119            for py in 0..gh {
120                let src_row: &[u8] = &cov[py * gw..(py + 1) * gw];
121                let dst_start: usize = (((ry as usize) + py) * atlas_w_usize + (rx as usize)) * 4;
122                let dst_row: &mut [u8] = &mut pixels[dst_start..dst_start + gw * 4];
123                for (alpha, dst_px) in src_row
124                    .iter()
125                    .copied()
126                    .zip(dst_row.as_chunks_mut::<4>().0.iter_mut())
127                {
128                    *dst_px = [255, 255, 255, alpha];
129                }
130            }
131        }
132        PackedGlyphAtlas::new(atlas_w, atlas_h, pixels, regions, self.get_revision())
133    }
134
135    /// Clears all cached glyphs and increments the atlas revision.
136    ///
137    /// # Arguments
138    ///
139    /// - `&Self` - Mutable reference to the glyph atlas.
140    pub fn clear(&mut self) {
141        self.get_mut_glyphs().clear();
142        let next_rev: u64 = self.get_revision().wrapping_add(1);
143        self.set_revision(next_rev);
144    }
145}