Skip to main content

euv_core/font/system/
impl.rs

1use super::*;
2
3/// Default `NativeSystemFont` uses deterministic cross-platform rasterization.
4impl Default for NativeSystemFont {
5    /// Creates a deterministic `NativeSystemFont`.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Deterministic font provider.
10    fn default() -> Self {
11        Self::deterministic()
12    }
13}
14
15/// Construction and mode helpers for `NativeSystemFont`.
16impl NativeSystemFont {
17    /// Creates a `NativeSystemFont` probing the active operating system's native font engine.
18    ///
19    /// # Returns
20    ///
21    /// - `Self` - Native OS font provider with automatic deterministic fallback.
22    #[must_use]
23    pub fn system() -> Self {
24        Self {
25            info: probe_system_font(),
26            deterministic_only: false,
27        }
28    }
29
30    /// Creates a deterministic cross-platform font provider guaranteeing identical pixels on every OS.
31    ///
32    /// # Returns
33    ///
34    /// - `Self` - Deterministic bitmap/stroke font provider.
35    #[must_use]
36    pub fn deterministic() -> Self {
37        Self {
38            info: SystemFontInfo::new(
39                FontRenderBackend::DeterministicBitmap,
40                FONT_FAMILY_DETERMINISTIC.to_string(),
41                true,
42                false,
43            ),
44            deterministic_only: true,
45        }
46    }
47
48    /// Returns the number of lazy native fallback font loads (such as CJK or symbol cascades) performed on the current thread.
49    ///
50    /// # Returns
51    ///
52    /// - `usize` - Cumulative lazy fallback font load count on this thread.
53    #[must_use]
54    pub fn thread_fallback_font_loads() -> usize {
55        THREAD_FALLBACK_FONT_LOADS.with(Cell::get)
56    }
57}
58
59/// `SystemFontProvider` implementation delegating to OS native APIs with deterministic fallback.
60impl SystemFontProvider for NativeSystemFont {
61    /// Returns the active `SystemFontInfo`.
62    ///
63    /// # Arguments
64    ///
65    /// - `&Self` - Font provider reference.
66    ///
67    /// # Returns
68    ///
69    /// - `SystemFontInfo` - Provider capability descriptor.
70    fn font_info(&self) -> SystemFontInfo {
71        self.get_info().clone()
72    }
73
74    /// Rasterizes `key` via the native OS font engine, falling back to deterministic rasterization.
75    ///
76    /// # Arguments
77    ///
78    /// - `&Self` - Font provider reference.
79    /// - `GlyphKey` - Quantized glyph descriptor.
80    ///
81    /// # Returns
82    ///
83    /// - `Option<RasterizedGlyph>` - Rasterized alpha8 coverage bitmap.
84    fn rasterize(&self, key: GlyphKey) -> Option<RasterizedGlyph> {
85        if !self.get_deterministic_only()
86            && let Some(native_glyph) = rasterize_native_glyph(key)
87        {
88            return Some(native_glyph);
89        }
90        Some(rasterize_glyph(key))
91    }
92}
93
94/// Construction helpers for the thread-local Apple CoreText handle cache.
95#[cfg(any(target_os = "macos", target_os = "ios"))]
96/// Inherent implementation of [`CoreTextFontCache`].
97impl CoreTextFontCache {
98    /// Creates an empty, zero-allocation CoreText font handle cache.
99    ///
100    /// # Returns
101    ///
102    /// - `Self` - Empty thread-local CoreText handle cache.
103    #[must_use]
104    pub(crate) const fn new() -> Self {
105        Self {
106            gray_space: null_mut(),
107            scratch_ctx: null_mut(),
108            scratch_buf: Vec::new(),
109            scratch_w: 0,
110            scratch_h: 0,
111            fonts: Vec::new(),
112        }
113    }
114}
115
116/// Automatic CoreFoundation and CoreGraphics handle release on thread exit.
117#[cfg(any(target_os = "macos", target_os = "ios"))]
118/// Implements [`Drop`] for [`CoreTextFontCache`].
119impl Drop for CoreTextFontCache {
120    /// Releases the scratch `CGBitmapContextRef`, every cached `CTFontRef`, and the singleton `CGColorSpaceRef`.
121    ///
122    /// # Arguments
123    ///
124    /// - `&Self` - Dropping CoreText cache.
125    fn drop(&mut self) {
126        let Self {
127            gray_space,
128            scratch_ctx,
129            fonts,
130            ..
131        } = self;
132        release_coretext_cache(*gray_space, *scratch_ctx, fonts);
133        *scratch_ctx = null_mut();
134        *gray_space = null_mut();
135    }
136}
137
138/// Construction helpers for lazily resolved FreeType face slots.
139#[cfg(any(target_os = "linux", target_os = "android"))]
140/// Inherent implementation of [`FtLazyFace`].
141impl FtLazyFace {
142    /// Creates an unprobed lazy face slot incurring zero filesystem or FreeType overhead until first demand.
143    ///
144    /// # Returns
145    ///
146    /// - `Self` - Empty unprobed FreeType face slot.
147    #[must_use]
148    pub(crate) const fn unprobed() -> Self {
149        Self {
150            probed: false,
151            face: null_mut(),
152            last_size_px: 0,
153        }
154    }
155
156    /// Wraps an already opened FreeType face pointer.
157    ///
158    /// # Arguments
159    ///
160    /// - `*mut FtFaceRec` - Opened FreeType face handle.
161    ///
162    /// # Returns
163    ///
164    /// - `Self` - Ready FreeType face slot.
165    #[must_use]
166    pub(crate) const fn loaded(face: *mut FtFaceRec) -> Self {
167        Self {
168            probed: true,
169            face,
170            last_size_px: 0,
171        }
172    }
173}
174
175/// Construction helpers for the thread-local Web offscreen glyph canvas scratchpad.
176#[cfg(target_arch = "wasm32")]
177/// Inherent implementation of [`WebFontScratch`].
178impl WebFontScratch {
179    /// Creates an empty, zero-DOM-allocation Web font scratchpad.
180    ///
181    /// # Returns
182    ///
183    /// - `Self` - Empty lazy Web font scratchpad.
184    #[must_use]
185    pub(crate) const fn new() -> Self {
186        Self {
187            canvas: None,
188            ctx2d: None,
189            width: 0,
190            height: 0,
191            last_font: String::new(),
192        }
193    }
194}
195
196/// Construction helpers for the thread-local Win32 GDI font handle cache.
197#[cfg(target_os = "windows")]
198/// Inherent implementation of [`Win32FontCache`].
199impl Win32FontCache {
200    /// Creates an empty, zero-allocation Win32 GDI font handle cache.
201    ///
202    /// # Returns
203    ///
204    /// - `Self` - Empty thread-local Win32 GDI handle cache.
205    #[must_use]
206    pub(crate) const fn new() -> Self {
207        Self {
208            hdc: null_mut(),
209            fonts: Vec::new(),
210            raw_buf: Vec::new(),
211        }
212    }
213}
214
215/// Automatic Win32 GDI `HFONT` and memory `HDC` release on thread exit.
216#[cfg(target_os = "windows")]
217/// Implements [`Drop`] for [`Win32FontCache`].
218impl Drop for Win32FontCache {
219    /// Deletes every cached `HFONT` and the persistent memory `HDC`.
220    ///
221    /// # Arguments
222    ///
223    /// - `&Self` - Dropping Win32 GDI cache.
224    fn drop(&mut self) {
225        let Self { hdc, fonts, .. } = self;
226        release_win32_cache(*hdc, fonts);
227        *hdc = null_mut();
228    }
229}