pub struct Font { /* private fields */ }Expand description
A loaded font, ready for shaping and rasterization.
Constructed from raw TTF/OTF bytes via Font::from_bytes. The data is
reference-counted so fonts can be cheaply shared and saved across frames.
An optional fallback font can be chained via Font::with_fallback; when
a glyph is missing from the primary font (glyph_id == 0 after shaping),
the fallback is consulted for both the glyph outline and advance width.
Implementations§
Source§impl Font
impl Font
Sourcepub fn from_bytes(data: Vec<u8>) -> Result<Self, &'static str>
pub fn from_bytes(data: Vec<u8>) -> Result<Self, &'static str>
Parse a font from raw TTF/OTF bytes.
Returns Err if the data is not a valid font.
Sourcepub fn from_slice(data: &[u8]) -> Result<Self, &'static str>
pub fn from_slice(data: &[u8]) -> Result<Self, &'static str>
Parse a font from a borrowed byte slice (data is copied).
Sourcepub fn with_fallback(self, fallback: Arc<Font>) -> Self
pub fn with_fallback(self, fallback: Arc<Font>) -> Self
Chain a fallback font consulted when this font lacks a glyph.
Returns self so it can be used as a builder method:
let font = Font::from_slice(MAIN_BYTES)?.with_fallback(Arc::new(emoji_font));pub fn units_per_em(&self) -> u16
Sourcepub fn ascender_px(&self, size: f64) -> f64
pub fn ascender_px(&self, size: f64) -> f64
Ascender height in pixels at the given font size.
Sourcepub fn descender_px(&self, size: f64) -> f64
pub fn descender_px(&self, size: f64) -> f64
Descender depth in pixels at the given font size (positive value).
Sourcepub fn line_height_px(&self, size: f64) -> f64
pub fn line_height_px(&self, size: f64) -> f64
Recommended line height in pixels at the given font size.
Sourcepub fn glyph_visual_bounds(&self, glyph: char, size: f64) -> Option<(f64, f64)>
pub fn glyph_visual_bounds(&self, glyph: char, size: f64) -> Option<(f64, f64)>
Actual vertical extent of a single glyph in pixels (Y-up), relative
to the baseline. Returns (y_min, y_max) where y_min is how far
the glyph dips below the baseline (negative for descenders, near
zero for upright glyphs) and y_max is how far it rises above.
Use this for visually centring an icon glyph in a button —
ascender_px/descender_px describe the FONT’s worst-case
extents and are too generous for most icon fonts (Font Awesome
glyphs sit in a sub-rectangle of the design space, so centring
by the font metric leaves them noticeably high). Returns None
when the glyph has no outline (e.g. a space) or isn’t in the
font.
Sourcepub fn characters(&self) -> BTreeSet<char>
pub fn characters(&self) -> BTreeSet<char>
Enumerate every character this font can actually render.
Walks the cmap table’s Unicode subtables, keeping only code points
that resolve to a real (non-.notdef) glyph, then recurses into the
fallback chain so the set reflects everything the whole font stack can
draw — Font Awesome / emoji glyphs that live in a fallback are
included, matching how the text renderer resolves them at shape time.
Returns a sorted, de-duplicated BTreeSet<char>. This backs the Font
Book demo’s glyph grid and its “supports N characters” count, replacing
the previous hard-coded glyph list.
Auto Trait Implementations§
impl Freeze for Font
impl RefUnwindSafe for Font
impl Send for Font
impl Sync for Font
impl Unpin for Font
impl UnsafeUnpin for Font
impl UnwindSafe for Font
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more