pub trait FontRef: Send + Sync {
// Required methods
fn data(&self) -> &[u8] ⓘ;
fn units_per_em(&self) -> u16;
fn glyph_id(&self, ch: char) -> Option<GlyphId>;
fn advance_width(&self, glyph_id: GlyphId) -> f32;
// Provided methods
fn data_shared(&self) -> Option<Arc<dyn AsRef<[u8]> + Send + Sync>> { ... }
fn metrics(&self) -> Option<FontMetrics> { ... }
fn glyph_count(&self) -> Option<u32> { ... }
fn variation_axes(&self) -> Option<Vec<VariationAxis>> { ... }
fn is_variable(&self) -> bool { ... }
}Expand description
Your key to unlocking font secrets
Every font format speaks the same language through this trait. TTF, OTF, WOFF - they all expose their data and metrics the same way.
struct MyFont {
data: Vec<u8>,
// ... your internal state
}
impl FontRef for MyFont {
fn data(&self) -> &[u8] {
&self.data
}
fn units_per_em(&self) -> u16 {
1000 // Common for Type 1 fonts
}
fn glyph_id(&self, ch: char) -> Option<GlyphId> {
// Turn Unicode into font-specific glyph IDs
Some(42)
}
fn advance_width(&self, glyph_id: GlyphId) -> f32 {
// How far to move after this glyph
500.0
}
}Required Methods§
Sourcefn units_per_em(&self) -> u16
fn units_per_em(&self) -> u16
The font’s internal coordinate system scale
Used to convert between font units and rendered pixels. Type 1 fonts use 1000, TrueType often uses 2048.
Sourcefn glyph_id(&self, ch: char) -> Option<GlyphId>
fn glyph_id(&self, ch: char) -> Option<GlyphId>
Find the glyph that represents this character
Returns None when the font doesn’t contain this character.
Sourcefn advance_width(&self, glyph_id: GlyphId) -> f32
fn advance_width(&self, glyph_id: GlyphId) -> f32
How wide this glyph stands in font units
This spacing determines how glyphs sit next to each other.
Provided Methods§
Optional shared font bytes for zero-copy downstream consumption.
Implementations that store their bytes in an Arc SHOULD override this to avoid per-call
allocations/copies in downstream libraries that require shared ownership.
Sourcefn metrics(&self) -> Option<FontMetrics>
fn metrics(&self) -> Option<FontMetrics>
Optional font-wide metrics in font units (ascent/descent/line gap).
Backends that can parse OpenType tables SHOULD provide this to allow consumers to make baseline/layout decisions without depending on a specific font parser.
Sourcefn glyph_count(&self) -> Option<u32>
fn glyph_count(&self) -> Option<u32>
How many glyphs this font contains
Useful for validation when shapers return glyph IDs.
Sourcefn variation_axes(&self) -> Option<Vec<VariationAxis>>
fn variation_axes(&self) -> Option<Vec<VariationAxis>>
Variable font axes from the fvar table.
Returns None for non-variable fonts or if axes cannot be parsed. Returns Some(empty vec) if the font has an fvar table with no axes.
Sourcefn is_variable(&self) -> bool
fn is_variable(&self) -> bool
Whether this font is a variable font (has fvar table).