Skip to main content

euv_core/font/system/
struct.rs

1use super::*;
2
3/// Metadata describing the active native or deterministic font rasterization provider.
4#[derive(Clone, Debug, Eq, Getter, New, PartialEq)]
5pub struct SystemFontInfo {
6    /// Active font backend discriminant.
7    #[get(type(copy))]
8    pub(crate) backend: FontRenderBackend,
9    /// Resolved human-readable system font family name.
10    pub(crate) family_name: String,
11    /// Whether full Unicode / CJK glyph shaping is supported.
12    #[get(type(copy))]
13    pub(crate) supports_unicode: bool,
14    /// Whether smooth subpixel/alpha anti-aliasing is enabled.
15    #[get(type(copy))]
16    pub(crate) supports_antialiasing: bool,
17}
18
19/// Cross-platform native system font provider backed by direct OS font APIs.
20#[derive(Clone, Debug, Getter)]
21pub struct NativeSystemFont {
22    /// Probed system font capability descriptor.
23    pub(crate) info: SystemFontInfo,
24    /// When `true`, bypasses OS font engines for bit-identical deterministic output.
25    #[get(type(copy))]
26    pub(crate) deterministic_only: bool,
27}
28
29/// Shelf-packed alpha8/RGBA8 glyph texture atlas ready for GPU texture upload.
30#[derive(Clone, Debug, Getter, New)]
31pub struct PackedGlyphAtlas {
32    /// Texture width in pixels.
33    #[get(type(copy))]
34    pub(crate) width: u32,
35    /// Texture height in pixels.
36    #[get(type(copy))]
37    pub(crate) height: u32,
38    /// Packed RGBA8 pixel data (white RGB with glyph alpha coverage).
39    pub(crate) pixels: Vec<u8>,
40    /// Atlas UV rectangles `(x, y, w, h)` per cached `GlyphKey`.
41    pub(crate) regions: HashMap<GlyphKey, (u32, u32, u32, u32)>,
42    /// Revision number matching the source `GlyphAtlas`.
43    #[get(type(copy))]
44    pub(crate) revision: u64,
45}
46
47#[cfg(any(target_os = "macos", target_os = "ios"))]
48#[repr(C)]
49#[derive(Clone, Copy)]
50pub(crate) struct CtRange {
51    pub(crate) location: isize,
52    pub(crate) length: isize,
53}
54
55#[cfg(any(target_os = "macos", target_os = "ios"))]
56#[repr(C)]
57#[derive(Clone, Copy)]
58pub(crate) struct CtPoint {
59    pub(crate) x: f64,
60    pub(crate) y: f64,
61}
62
63#[cfg(any(target_os = "macos", target_os = "ios"))]
64#[repr(C)]
65#[derive(Clone, Copy)]
66pub(crate) struct CtSize {
67    pub(crate) width: f64,
68    pub(crate) height: f64,
69}
70
71/// Cached Apple CoreText `CTFontRef` handle paired with pre-queried vertical metrics and lazy cascaded fallbacks.
72#[cfg(any(target_os = "macos", target_os = "ios"))]
73#[derive(Clone, Copy)]
74pub(crate) struct CoreTextFontCacheEntry {
75    /// Packed `(size_tenths | bold<<16 | monospace<<17 | italic<<18)` style signature.
76    pub(crate) style_sig: u32,
77    /// Retained CoreText `CTFontRef` handle released on eviction or thread exit.
78    pub(crate) font: *mut c_void,
79    /// Pre-queried clamped typographic ascent in points.
80    pub(crate) ascent: f64,
81    /// Pre-queried clamped typographic descent in points.
82    pub(crate) descent: f64,
83    /// Lazily populated cascaded fallback `CTFontRef` slots (e.g. CJK / symbol fonts), null until a glyph misses `font`.
84    pub(crate) fallback_fonts: [*mut c_void; CORETEXT_FALLBACK_SLOTS],
85}
86
87/// Thread-local cache of Apple CoreText `CTFontRef` handles, singleton grayscale `CGColorSpaceRef`, and reusable bitmap context.
88#[cfg(any(target_os = "macos", target_os = "ios"))]
89pub(crate) struct CoreTextFontCache {
90    /// Singleton device-gray `CGColorSpaceRef` reused across all glyph rasterizations on this thread.
91    pub(crate) gray_space: *mut c_void,
92    /// Persistent scratch `CGBitmapContextRef` backed by `scratch_buf`, eliminating per-glyph context creation FFI calls.
93    pub(crate) scratch_ctx: *mut c_void,
94    /// Backing alpha8 pixel storage for `scratch_ctx`.
95    pub(crate) scratch_buf: Vec<u8>,
96    /// Current pixel width of `scratch_ctx`.
97    pub(crate) scratch_w: usize,
98    /// Current pixel height of `scratch_ctx`.
99    pub(crate) scratch_h: usize,
100    /// Bounded table of cached base `CTFontRef` handles (`<= CORETEXT_FONT_CACHE_MAX`).
101    pub(crate) fonts: Vec<CoreTextFontCacheEntry>,
102}
103
104/// Cached Windows Win32 `HFONT` handle paired with pre-queried font metrics.
105#[cfg(target_os = "windows")]
106#[derive(Clone, Copy)]
107pub(crate) struct Win32FontCacheEntry {
108    /// Packed `(size_px | bold<<16 | italic<<17 | face_kind<<18)` style signature.
109    pub(crate) style_sig: u32,
110    /// Retained Win32 `HFONT` handle deleted on eviction or thread exit.
111    pub(crate) hfont: *mut c_void,
112    /// Pre-queried clamped typographic ascent in pixels.
113    pub(crate) ascent: i32,
114    /// Pre-queried clamped typographic descent in pixels.
115    pub(crate) descent: i32,
116}
117
118/// Thread-local cache of Windows Win32 memory `HDC`, `HFONT` handles, and outline scratch buffer.
119#[cfg(target_os = "windows")]
120pub(crate) struct Win32FontCache {
121    /// Persistent compatible memory device context reused across glyph misses.
122    pub(crate) hdc: *mut c_void,
123    /// Bounded table of cached `HFONT` handles (`<= WIN32_FONT_CACHE_MAX`).
124    pub(crate) fonts: Vec<Win32FontCacheEntry>,
125    /// Reusable `GetGlyphOutlineW` byte scratch buffer eliminating per-glyph allocations.
126    pub(crate) raw_buf: Vec<u8>,
127}
128
129#[cfg(target_os = "windows")]
130#[repr(C)]
131#[derive(Clone, Copy, Default)]
132pub(crate) struct WinFixed {
133    pub(crate) fract: u16,
134    pub(crate) value: i16,
135}
136
137#[cfg(target_os = "windows")]
138#[repr(C)]
139#[derive(Clone, Copy, Default)]
140pub(crate) struct WinMat2 {
141    pub(crate) em11: WinFixed,
142    pub(crate) em12: WinFixed,
143    pub(crate) em21: WinFixed,
144    pub(crate) em22: WinFixed,
145}
146
147#[cfg(target_os = "windows")]
148#[repr(C)]
149#[derive(Clone, Copy, Default)]
150pub(crate) struct WinGlyphMetrics {
151    pub(crate) black_box_x: u32,
152    pub(crate) black_box_y: u32,
153    pub(crate) glyph_origin_x: i32,
154    pub(crate) glyph_origin_y: i32,
155    pub(crate) cell_inc_x: i16,
156    pub(crate) cell_inc_y: i16,
157}
158
159#[cfg(target_os = "windows")]
160#[repr(C)]
161#[derive(Clone, Copy, Default)]
162pub(crate) struct WinTextMetricW {
163    pub(crate) tm_height: i32,
164    pub(crate) tm_ascent: i32,
165    pub(crate) tm_descent: i32,
166    pub(crate) tm_internal_leading: i32,
167    pub(crate) tm_external_leading: i32,
168    pub(crate) tm_ave_char_width: i32,
169    pub(crate) tm_max_char_width: i32,
170    pub(crate) tm_weight: i32,
171    pub(crate) tm_overhang: i32,
172    pub(crate) tm_digitized_aspect_x: i32,
173    pub(crate) tm_digitized_aspect_y: i32,
174    pub(crate) tm_first_char: u16,
175    pub(crate) tm_last_char: u16,
176    pub(crate) tm_default_char: u16,
177    pub(crate) tm_break_char: u16,
178    pub(crate) tm_italic: u8,
179    pub(crate) tm_underlined: u8,
180    pub(crate) tm_struck_out: u8,
181    pub(crate) tm_pitch_and_family: u8,
182    pub(crate) tm_char_set: u8,
183}
184
185#[cfg(any(target_os = "linux", target_os = "android"))]
186#[repr(C)]
187#[derive(Clone, Copy)]
188pub(crate) struct FtBitmap {
189    pub(crate) rows: u32,
190    pub(crate) width: u32,
191    pub(crate) pitch: i32,
192    pub(crate) buffer: *const u8,
193    pub(crate) num_grays: u16,
194    pub(crate) pixel_mode: u8,
195    pub(crate) palette_mode: u8,
196    pub(crate) palette: *mut c_void,
197}
198
199#[cfg(any(target_os = "linux", target_os = "android"))]
200#[repr(C)]
201#[derive(Clone, Copy)]
202pub(crate) struct FtVector {
203    pub(crate) x: isize,
204    pub(crate) y: isize,
205}
206
207#[cfg(any(target_os = "linux", target_os = "android"))]
208#[repr(C)]
209#[derive(Clone, Copy)]
210pub(crate) struct FtGeneric {
211    pub(crate) data: *mut c_void,
212    pub(crate) finalizer: *mut c_void,
213}
214
215#[cfg(any(target_os = "linux", target_os = "android"))]
216#[repr(C)]
217#[derive(Clone, Copy)]
218pub(crate) struct FtBBox {
219    pub(crate) x_min: isize,
220    pub(crate) y_min: isize,
221    pub(crate) x_max: isize,
222    pub(crate) y_max: isize,
223}
224
225#[cfg(any(target_os = "linux", target_os = "android"))]
226#[repr(C)]
227#[derive(Clone, Copy)]
228pub(crate) struct FtGlyphMetrics {
229    pub(crate) width: isize,
230    pub(crate) height: isize,
231    pub(crate) hori_bearing_x: isize,
232    pub(crate) hori_bearing_y: isize,
233    pub(crate) hori_advance: isize,
234    pub(crate) vert_bearing_x: isize,
235    pub(crate) vert_bearing_y: isize,
236    pub(crate) vert_advance: isize,
237}
238
239#[cfg(any(target_os = "linux", target_os = "android"))]
240#[repr(C)]
241#[derive(Clone, Copy)]
242pub(crate) struct FtSizeMetrics {
243    pub(crate) x_ppem: u16,
244    pub(crate) y_ppem: u16,
245    pub(crate) x_scale: isize,
246    pub(crate) y_scale: isize,
247    pub(crate) ascender: isize,
248    pub(crate) descender: isize,
249    pub(crate) height: isize,
250    pub(crate) max_advance: isize,
251}
252
253#[cfg(any(target_os = "linux", target_os = "android"))]
254#[repr(C)]
255#[derive(Clone, Copy)]
256pub(crate) struct FtSizeRec {
257    pub(crate) face: *mut c_void,
258    pub(crate) generic: FtGeneric,
259    pub(crate) metrics: FtSizeMetrics,
260    pub(crate) internal: *mut c_void,
261}
262
263#[cfg(any(target_os = "linux", target_os = "android"))]
264#[repr(C)]
265#[derive(Clone, Copy)]
266pub(crate) struct FtGlyphSlotRec {
267    pub(crate) library: *mut c_void,
268    pub(crate) face: *mut c_void,
269    pub(crate) next: *mut c_void,
270    pub(crate) glyph_index: u32,
271    pub(crate) generic: FtGeneric,
272    pub(crate) metrics: FtGlyphMetrics,
273    pub(crate) linear_hori_advance: isize,
274    pub(crate) linear_vert_advance: isize,
275    pub(crate) advance: FtVector,
276    pub(crate) format: u32,
277    pub(crate) bitmap: FtBitmap,
278    pub(crate) bitmap_left: i32,
279    pub(crate) bitmap_top: i32,
280}
281
282#[cfg(any(target_os = "linux", target_os = "android"))]
283#[repr(C)]
284#[derive(Clone, Copy)]
285pub(crate) struct FtFaceRec {
286    pub(crate) num_faces: isize,
287    pub(crate) face_index: isize,
288    pub(crate) face_flags: isize,
289    pub(crate) style_flags: isize,
290    pub(crate) num_glyphs: isize,
291    pub(crate) family_name: *const c_char,
292    pub(crate) style_name: *const c_char,
293    pub(crate) num_fixed_sizes: i32,
294    pub(crate) available_sizes: *mut c_void,
295    pub(crate) num_charmaps: i32,
296    pub(crate) charmaps: *mut c_void,
297    pub(crate) generic: FtGeneric,
298    pub(crate) bbox: FtBBox,
299    pub(crate) units_per_em: u16,
300    pub(crate) ascender: i16,
301    pub(crate) descender: i16,
302    pub(crate) height: i16,
303    pub(crate) max_advance_width: i16,
304    pub(crate) max_advance_height: i16,
305    pub(crate) underline_position: i16,
306    pub(crate) underline_thickness: i16,
307    pub(crate) glyph: *const FtGlyphSlotRec,
308    pub(crate) size: *const FtSizeRec,
309}
310
311/// Lazily loaded FreeType face slot tracking probe state and most recently applied pixel size.
312#[cfg(any(target_os = "linux", target_os = "android"))]
313#[derive(Clone, Copy)]
314pub(crate) struct FtLazyFace {
315    /// Whether candidate font path probing has already been attempted for this slot.
316    pub(crate) probed: bool,
317    /// Opened FreeType face pointer, or null when unprobed or unavailable.
318    pub(crate) face: *mut FtFaceRec,
319    /// Most recently applied pixel size on `face` to skip redundant `FT_Set_Pixel_Sizes` calls.
320    pub(crate) last_size_px: u32,
321}
322
323/// Dynamically loaded FreeType 2 runtime holding the primary face and lazily resolved style/CJK fallback faces.
324#[cfg(any(target_os = "linux", target_os = "android"))]
325pub(crate) struct FreeTypeRuntime {
326    /// Initialized `FT_Library` handle retained for on-demand `FT_New_Face` calls.
327    pub(crate) library: *mut c_void,
328    /// Resolved `FT_New_Face` function pointer for lazy style and CJK fallback face opening.
329    pub(crate) new_face: FtNewFaceFn,
330    /// Primary regular Latin face opened on cold initialization.
331    pub(crate) regular: FtLazyFace,
332    /// Lazily opened bold sans face (opened only on first bold glyph).
333    pub(crate) bold: FtLazyFace,
334    /// Lazily opened monospace face (opened only on first monospace glyph).
335    pub(crate) mono: FtLazyFace,
336    /// Lazily opened bold monospace face (opened only on first bold monospace glyph).
337    pub(crate) mono_bold: FtLazyFace,
338    /// Lazily opened CJK / Unicode fallback face (opened only when primary face misses a codepoint).
339    pub(crate) cjk: FtLazyFace,
340    /// Resolved `FT_Set_Pixel_Sizes` function pointer.
341    pub(crate) set_pixel_sizes: FtSetPixelSizesFn,
342    /// Resolved `FT_Get_Char_Index` function pointer.
343    pub(crate) get_char_index: FtGetCharIndexFn,
344    /// Resolved `FT_Load_Glyph` function pointer.
345    pub(crate) load_glyph: FtLoadGlyphFn,
346}
347
348/// Thread-local reusable offscreen `<canvas>` and 2D context for Web glyph rasterization.
349#[cfg(target_arch = "wasm32")]
350pub(crate) struct WebFontScratch {
351    /// Lazily created offscreen canvas element.
352    pub(crate) canvas: Option<HtmlCanvasElement>,
353    /// Cached 2D rendering context bound to `canvas`.
354    pub(crate) ctx2d: Option<CanvasRenderingContext2d>,
355    /// Current allocated canvas pixel width.
356    pub(crate) width: u32,
357    /// Current allocated canvas pixel height.
358    pub(crate) height: u32,
359    /// Last CSS `font` string applied to `ctx2d`.
360    pub(crate) last_font: String,
361}