euv_core/font/system/trait.rs
1use super::*;
2
3/// Abstraction for native OS and deterministic glyph rasterization providers.
4pub trait SystemFontProvider {
5 /// Returns metadata describing this font provider's active backend and capabilities.
6 ///
7 /// # Arguments
8 ///
9 /// - `&Self` - Font provider reference.
10 ///
11 /// # Returns
12 ///
13 /// - `SystemFontInfo` - Provider capabilities and family metadata.
14 fn font_info(&self) -> SystemFontInfo;
15
16 /// Rasterizes a quantized `GlyphKey` into an alpha8 coverage bitmap.
17 ///
18 /// # Arguments
19 ///
20 /// - `&Self` - Font provider reference.
21 /// - `GlyphKey` - Quantized character, font size, bold flag, and family.
22 ///
23 /// # Returns
24 ///
25 /// - `Option<RasterizedGlyph>` - Rasterized glyph bitmap if supported by this provider.
26 fn rasterize(&self, key: GlyphKey) -> Option<RasterizedGlyph>;
27
28 /// Rasterizes a batch of missing `GlyphKey` entries in a single provider pass to amortize OS/Web readbacks.
29 ///
30 /// # Arguments
31 ///
32 /// - `&Self` - Font provider reference.
33 /// - `&[GlyphKey]` - Slice of missing glyph keys to rasterize.
34 ///
35 /// # Returns
36 ///
37 /// - `Vec<Option<RasterizedGlyph>>` - Rasterized glyph results aligned 1:1 with `keys`.
38 fn rasterize_batch(&self, keys: &[GlyphKey]) -> Vec<Option<RasterizedGlyph>> {
39 let mut results: Vec<Option<RasterizedGlyph>> = Vec::with_capacity(keys.len());
40 for &key in keys.iter() {
41 results.push(self.rasterize(key));
42 }
43 results
44 }
45
46 /// Rasterizes `key` directly into a caller-owned reusable `dst` alpha8 staging buffer without allocating a fresh bitmap vector.
47 ///
48 /// # Arguments
49 ///
50 /// - `&Self` - Font provider reference.
51 /// - `GlyphKey` - Quantized glyph descriptor.
52 /// - `&mut Vec<u8>` - Reusable destination staging buffer cleared and overwritten with row-major alpha8 pixels.
53 ///
54 /// # Returns
55 ///
56 /// - `Option<(u32, u32, u32)>` - Tuple `(width, height, advance)` when rasterization succeeded.
57 fn rasterize_into(&self, key: GlyphKey, dst: &mut Vec<u8>) -> Option<(u32, u32, u32)> {
58 let glyph: RasterizedGlyph = self.rasterize(key)?;
59 dst.clear();
60 dst.extend_from_slice(glyph.get_coverage().as_slice());
61 Some((glyph.get_width(), glyph.get_height(), glyph.get_advance()))
62 }
63}