Skip to main content

Font

Struct Font 

Source
pub struct Font {
    pub lookup_table: &'static [GlyphEntry],
    pub data: &'static [u8],
    pub ascii_begin: u32,
    pub ascii_end: u32,
    pub max_height: u8,
    pub proportional_width: bool,
}
Expand description

A bitmap font definition.

Contains all data needed to render text: glyph lookup table, bitmap data, and metadata about the font.

§Fields

  • lookup_table: Array of glyph entries, one per character in the supported range
  • data: Raw bitmap data for all glyphs, packed as bits
  • ascii_begin: First character code in the supported range (e.g., 32 for space)
  • ascii_end: Last character code in the supported range (e.g., 126 for tilde)
  • max_height: Height of all glyphs in pixels
  • proportional_width: Whether glyphs have variable widths (vs fixed-width)

§Examples

let font = Font::new(
    &LOOKUP_TABLE,    // &[GlyphEntry]
    &FONT_DATA,       // &[u8]
    32,               // ASCII start
    126,              // ASCII end
    8,                // Height
    true,             // Proportional
);

Fields§

§lookup_table: &'static [GlyphEntry]

Lookup table mapping characters to glyph metadata.

§data: &'static [u8]

Packed bitmap data for all glyphs.

§ascii_begin: u32

First character code supported by this font.

§ascii_end: u32

Last character code supported by this font.

§max_height: u8

Maximum height of glyphs in this font (in pixels).

§proportional_width: bool

Whether this font uses proportional widths (vs monospace).

Implementations§

Source§

impl Font

Source

pub const fn new( lookup_table: &'static [GlyphEntry], data: &'static [u8], ascii_begin: u32, ascii_end: u32, max_height: u8, proportional_width: bool, ) -> Self

Create a new font definition.

§Arguments
  • lookup_table - Array of glyph entries, one per supported character
  • data - Raw bitmap data for all glyphs
  • ascii_begin - First supported character code
  • ascii_end - Last supported character code (inclusive)
  • max_height - Height of all glyphs in pixels
  • proportional_width - Whether glyphs have variable widths
§Examples
let font = Font::new(
    &GLYPHS,      // 95 entries for ASCII 32-126
    &FONT_DATA,   // Binary bitmap data
    32,           // Space
    126,          // Tilde
    8,            // 8-pixel height
    false,        // Monospace
);
Source

pub fn get_glyph(&self, c: char) -> Option<&GlyphEntry>

Get the glyph metadata for a character.

Returns None if the character is not supported by this font.

Source

pub fn glyph_data(&self, entry: &GlyphEntry) -> &[u8]

Get the bitmap data for a glyph.

Data is stored as packed bits: 8 bits per byte, where each set bit represents a pixel that should be drawn.

Source

pub fn measure_str(&self, text: &str, tracking: i32) -> u32

Measure the pixel width of a text string.

Returns the total width including any unsupported characters (which contribute 0 width) and the specified tracking value.

§Arguments
  • text - The string to measure
  • tracking - Extra spacing to add between characters in pixels
§Examples
let width = font.measure_str("Hello", 0);      // No extra spacing
let width = font.measure_str("Hello", 2);      // 2 pixels between chars

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.