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
49/// `SystemFontProvider` implementation delegating to OS native APIs with deterministic fallback.
50impl SystemFontProvider for NativeSystemFont {
51    /// Returns the active `SystemFontInfo`.
52    ///
53    /// # Arguments
54    ///
55    /// - `&Self` - Font provider reference.
56    ///
57    /// # Returns
58    ///
59    /// - `SystemFontInfo` - Provider capability descriptor.
60    fn font_info(&self) -> SystemFontInfo {
61        self.get_info().clone()
62    }
63
64    /// Rasterizes `key` via the native OS font engine, falling back to deterministic rasterization.
65    ///
66    /// # Arguments
67    ///
68    /// - `&Self` - Font provider reference.
69    /// - `GlyphKey` - Quantized glyph descriptor.
70    ///
71    /// # Returns
72    ///
73    /// - `Option<RasterizedGlyph>` - Rasterized alpha8 coverage bitmap.
74    fn rasterize(&self, key: GlyphKey) -> Option<RasterizedGlyph> {
75        if !self.get_deterministic_only()
76            && let Some(native_glyph) = rasterize_native_glyph(key)
77        {
78            return Some(native_glyph);
79        }
80        Some(rasterize_glyph(key))
81    }
82}
83
84/// Construction helpers for the thread-local Apple CoreText handle cache.
85#[cfg(any(target_os = "macos", target_os = "ios"))]
86impl CoreTextFontCache {
87    /// Creates an empty, zero-allocation CoreText font handle cache.
88    ///
89    /// # Returns
90    ///
91    /// - `Self` - Empty thread-local CoreText handle cache.
92    #[must_use]
93    pub(crate) const fn new() -> Self {
94        Self {
95            gray_space: null_mut(),
96            fonts: Vec::new(),
97        }
98    }
99}
100
101/// Automatic CoreFoundation and CoreGraphics handle release on thread exit.
102#[cfg(any(target_os = "macos", target_os = "ios"))]
103impl Drop for CoreTextFontCache {
104    /// Releases every cached `CTFontRef` and the singleton `CGColorSpaceRef`.
105    ///
106    /// # Arguments
107    ///
108    /// - `&Self` - Dropping CoreText cache.
109    fn drop(&mut self) {
110        let Self { gray_space, fonts } = self;
111        release_coretext_cache(*gray_space, fonts);
112        *gray_space = null_mut();
113    }
114}
115
116/// Construction helpers for the thread-local Win32 GDI font handle cache.
117#[cfg(target_os = "windows")]
118impl Win32FontCache {
119    /// Creates an empty, zero-allocation Win32 GDI font handle cache.
120    ///
121    /// # Returns
122    ///
123    /// - `Self` - Empty thread-local Win32 GDI handle cache.
124    #[must_use]
125    pub(crate) const fn new() -> Self {
126        Self {
127            hdc: null_mut(),
128            fonts: Vec::new(),
129            raw_buf: Vec::new(),
130        }
131    }
132}
133
134/// Automatic Win32 GDI `HFONT` and memory `HDC` release on thread exit.
135#[cfg(target_os = "windows")]
136impl Drop for Win32FontCache {
137    /// Deletes every cached `HFONT` and the persistent memory `HDC`.
138    ///
139    /// # Arguments
140    ///
141    /// - `&Self` - Dropping Win32 GDI cache.
142    fn drop(&mut self) {
143        let Self { hdc, fonts, .. } = self;
144        release_win32_cache(*hdc, fonts);
145        *hdc = null_mut();
146    }
147}