Skip to main content

azul_layout/
font_traits.rs

1//! Font traits that are always available, regardless of text_layout feature.
2//!
3//! These traits define the interface between the layout solver and the font system.
4//! The actual implementations live in text3/cache.rs when text_layout feature is enabled.
5
6use azul_core::geom::LogicalSize;
7
8#[cfg(all(feature = "text_layout", feature = "font_loading"))]
9pub use crate::text3::script::Language;
10#[cfg(all(feature = "text_layout", feature = "font_loading"))]
11pub use crate::text3::{
12    cache::{
13        AvailableSpace, BidiDirection, ContentIndex, FontHash, FontManager, FontSelector,
14        FontStyle, Glyph, ImageSource, InlineContent, InlineImage, InlineShape, LayoutCache,
15        LayoutError, LayoutFontMetrics, LayoutFragment, ObjectFit, SegmentAlignment, ShapeBoundary,
16        ShapeDefinition, ShapedItem, Size, StyleProperties, StyledRun, UnifiedConstraints,
17        UnifiedLayout, VerticalMetrics,
18    },
19    script::Script,
20};
21
22#[cfg(all(feature = "text_layout", feature = "font_loading"))]
23pub type TextLayoutCache = LayoutCache;
24
25#[cfg(not(all(feature = "text_layout", feature = "font_loading")))]
26pub use stub::TextLayoutCache;
27
28/// Trait for types that support cheap, shallow cloning (e.g., reference-counted types).
29pub trait ShallowClone {
30    /// Create a shallow clone (increment reference count, don't copy data)
31    fn shallow_clone(&self) -> Self;
32}
33
34/// Core trait for parsed fonts that can be used for text shaping and layout.
35///
36/// This trait abstracts over the actual font parsing implementation, allowing
37/// the layout solver to work with different font backends.
38pub trait ParsedFontTrait: Send + Clone + ShallowClone {
39    fn shape_text(
40        &self,
41        text: &str,
42        script: Script,
43        language: Language,
44        direction: BidiDirection,
45        style: &StyleProperties,
46    ) -> Result<Vec<Glyph>, LayoutError>;
47
48    /// Hash of the font, necessary for breaking layouted glyphs into glyph runs
49    fn get_hash(&self) -> u64;
50
51    fn get_glyph_size(&self, glyph_id: u16, font_size: f32) -> Option<LogicalSize>;
52
53    fn get_hyphen_glyph_and_advance(&self, font_size: f32) -> Option<(u16, f32)>;
54
55    fn get_kashida_glyph_and_advance(&self, font_size: f32) -> Option<(u16, f32)>;
56
57    fn has_glyph(&self, codepoint: u32) -> bool;
58
59    fn get_vertical_metrics(&self, glyph_id: u16) -> Option<VerticalMetrics>;
60
61    fn get_font_metrics(&self) -> LayoutFontMetrics;
62
63    fn num_glyphs(&self) -> u16;
64}
65
66/// Trait for loading fonts from raw bytes.
67///
68/// This allows different font loading strategies (e.g., allsorts, freetype, mock)
69/// to be used with the layout engine.
70pub trait FontLoaderTrait<T>: Send + core::fmt::Debug {
71    fn load_font(&self, font_bytes: &[u8], font_index: usize) -> Result<T, LayoutError>;
72}
73
74/// Opaque font identifier - wraps the underlying font system's ID type
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
76pub struct FontId(pub u64);
77
78/// Trait for font system abstraction (font discovery, fallback chains).
79///
80/// This abstracts over the underlying font system (e.g., rust_fontconfig, fontdb)
81/// allowing text3 to work with or without system font discovery.
82pub trait FontSystemTrait: Send + Sync {
83    /// The type of font fallback chain this system uses
84    type FallbackChain: FontFallbackChainTrait;
85
86    /// Resolve a character to a font ID using the fallback chain
87    fn resolve_char(&self, chain: &Self::FallbackChain, c: char) -> Option<FontId>;
88
89    /// Get the font data (bytes) for a font ID
90    fn get_font_data(&self, font_id: FontId) -> Option<alloc::vec::Vec<u8>>;
91
92    /// Get the font index within the font file
93    fn get_font_index(&self, font_id: FontId) -> usize;
94}
95
96/// Trait for font fallback chains
97pub trait FontFallbackChainTrait: Clone + Send + Sync {
98    /// Check if the chain is empty
99    fn is_empty(&self) -> bool;
100
101    /// Get the primary font ID (if any)
102    fn primary_font_id(&self) -> Option<FontId>;
103}
104
105// When text_layout or font_loading is disabled, provide minimal stub types
106#[cfg(not(all(feature = "text_layout", feature = "font_loading")))]
107pub use stub::*;
108
109#[cfg(not(all(feature = "text_layout", feature = "font_loading")))]
110mod stub {
111    use super::*;
112
113    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
114    pub struct Script;
115
116    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
117    pub struct Language;
118
119    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
120    pub enum FontStyle {
121        Normal,
122        Italic,
123        Oblique,
124    }
125
126    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
127    pub enum TextDirection {
128        LeftToRight,
129        RightToLeft,
130    }
131
132    /// Stub for BidiDirection when text_layout is disabled
133    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
134    pub enum BidiDirection {
135        Ltr,
136        Rtl,
137    }
138
139    #[derive(Debug, Clone)]
140    pub struct StyleProperties;
141
142    #[derive(Debug, Clone)]
143    pub struct Glyph;
144
145    #[derive(Debug, Clone, Copy)]
146    pub struct VerticalMetrics {
147        pub ascent: f32,
148        pub descent: f32,
149        pub line_gap: f32,
150    }
151
152    #[derive(Debug, Clone, Copy)]
153    pub struct LayoutFontMetrics {
154        pub ascent: f32,
155        pub descent: f32,
156        pub line_gap: f32,
157        pub units_per_em: u16,
158    }
159
160    #[derive(Debug, Clone)]
161    pub struct LayoutError;
162
163    #[derive(Debug, Clone)]
164    pub struct FontSelector;
165
166    #[derive(Debug)]
167    pub struct FontManager;
168
169    #[derive(Debug)]
170    pub struct LayoutCache;
171
172    // Additional stub types needed by solver3
173    pub type ContentIndex = usize;
174    pub type FontHash = u64;
175
176    #[derive(Debug, Clone)]
177    pub struct InlineContent;
178
179    #[derive(Debug, Clone)]
180    pub struct StyledRun;
181
182    #[derive(Debug, Clone)]
183    pub struct LayoutFragment;
184
185    #[derive(Debug, Clone)]
186    pub struct UnifiedConstraints;
187
188    #[derive(Debug, Clone)]
189    pub struct InlineImage;
190
191    #[derive(Debug, Clone)]
192    pub struct InlineShape;
193
194    #[derive(Debug, Clone)]
195    pub struct ShapeDefinition;
196
197    #[derive(Debug, Clone)]
198    pub struct ShapeBoundary;
199
200    #[derive(Debug, Clone)]
201    pub struct ShapedItem;
202
203    #[derive(Debug, Clone)]
204    pub enum ImageSource {
205        Ref(azul_core::resources::ImageRef),
206        Url(String),
207        Data(std::sync::Arc<[u8]>),
208        Svg(std::sync::Arc<str>),
209        Placeholder(Size),
210    }
211
212    #[derive(Debug, Clone, Copy)]
213    pub enum ObjectFit {
214        Contain,
215        Cover,
216        Fill,
217        None,
218        ScaleDown,
219    }
220
221    #[derive(Debug, Clone, Copy)]
222    pub enum SegmentAlignment {
223        Start,
224        Center,
225        End,
226    }
227
228    #[derive(Debug, Clone)]
229    pub struct UnifiedLayout;
230
231    pub type TextLayoutCache = LayoutCache;
232
233    pub type Size = azul_core::geom::LogicalSize;
234}