pub struct Font { /* private fields */ }Expand description
One face of an OpenType/TrueType font file or collection. Cheap to clone (the bytes are
shared); clones keep the same FontId, so they share cached glyphs.
Implementations§
Source§impl Font
impl Font
Sourcepub fn new(data: impl Into<Arc<[u8]>>) -> Result<Self, FontError>
pub fn new(data: impl Into<Arc<[u8]>>) -> Result<Self, FontError>
Loads a font from its file bytes (face 0 of a collection).
§Errors
FontError::Parse if the bytes are not a readable font.
Face index of the font file or collection in data, sharing the bytes.
Each call creates a new FontId: load a face once and clone the Font.
§Errors
FontError::Parse if the bytes are not a readable font or have no face index.
Sourcepub fn bundled_mono() -> Result<Self, FontError>
pub fn bundled_mono() -> Result<Self, FontError>
The bundled JetBrains Mono (BUNDLED_MONO). Every call returns the same font
(same FontId), so all users share its cached glyphs.
§Errors
Only if the bundled file were corrupt; tests guard against that.
Sourcepub fn index(&self) -> u32
pub fn index(&self) -> u32
The face’s index within its file (0 unless the file is a collection).
The shared file bytes, for handing to another library without copying.
Sourcepub fn glyph_id(&self, ch: char) -> Option<GlyphId>
pub fn glyph_id(&self, ch: char) -> Option<GlyphId>
The glyph the font draws for ch, or None if it has no glyph for it.
Sourcepub fn advance(&self, glyph: GlyphId, px: f32) -> f32
pub fn advance(&self, glyph: GlyphId, px: f32) -> f32
Horizontal advance of glyph at px pixels per em; 0 if unknown.
Sourcepub fn glyph_extent(&self, glyph: GlyphId, px: f32) -> Option<[f32; 2]>
pub fn glyph_extent(&self, glyph: GlyphId, px: f32) -> Option<[f32; 2]>
Width and height in pixels of glyph’s outline bounding box at px pixels per em,
without rasterizing it; None if the font cannot say (then the caller must assume the
worst). Some([0.0, 0.0]) for glyphs without an outline.
Sourcepub fn line_metrics(&self, px: f32) -> LineMetrics
pub fn line_metrics(&self, px: f32) -> LineMetrics
Ascent, descent and line gap at px pixels per em.