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 rangedata: Raw bitmap data for all glyphs, packed as bitsascii_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 pixelsproportional_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: u32First character code supported by this font.
ascii_end: u32Last character code supported by this font.
max_height: u8Maximum height of glyphs in this font (in pixels).
proportional_width: boolWhether this font uses proportional widths (vs monospace).
Implementations§
Source§impl Font
impl Font
Sourcepub const fn new(
lookup_table: &'static [GlyphEntry],
data: &'static [u8],
ascii_begin: u32,
ascii_end: u32,
max_height: u8,
proportional_width: bool,
) -> Self
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 characterdata- Raw bitmap data for all glyphsascii_begin- First supported character codeascii_end- Last supported character code (inclusive)max_height- Height of all glyphs in pixelsproportional_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
);Sourcepub fn get_glyph(&self, c: char) -> Option<&GlyphEntry>
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.
Sourcepub fn glyph_data(&self, entry: &GlyphEntry) -> &[u8]
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.
Sourcepub fn measure_str(&self, text: &str, tracking: i32) -> u32
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 measuretracking- 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