pub struct BitmapFont {
pub glyph_w: u8,
pub glyph_h: u8,
pub first_char: u16,
pub glyph_count: u16,
pub bitmap: &'static [u8],
pub glyph_advances: Option<&'static [u8]>,
pub advance_x: u8,
pub line_height: u8,
pub bit_order: BitOrder,
}Expand description
A bitmap font as a static data descriptor.
All fields are compile-time constants so fonts can live in
.rodata and a BitmapFont value can be declared as a const
right next to its bitmap. The type stays size-agnostic – 6×8,
8×8, 8×16, 12×16 all work; the uploader reads glyph_w /
glyph_h and lays out the atlas from there.
Glyph i in the bitmap slice occupies
bitmap[i * row_bytes * glyph_h .. i * row_bytes * glyph_h + row_bytes * glyph_h], where row_bytes = ceil(glyph_w / 8).
For 8-wide glyphs that’s one byte per row. Wider glyphs (12,
16 px) use multiple bytes per row, MSB-first within the row
(i.e., byte 0 covers columns 0..=7, byte 1 covers columns
8..=15, regardless of BitOrder within each byte).
The first_char / glyph_count window is a codepoint range –
code c is looked up at offset c - first_char in the bitmap
as long as first_char <= c < first_char + glyph_count.
Anything outside falls back to the missing-glyph box.
Fields§
§glyph_w: u8Glyph cell width in pixels.
glyph_h: u8Glyph cell height in pixels.
first_char: u16First codepoint covered. For a basic-Latin font this is
usually 0x00 (null) or 0x20 (space).
glyph_count: u16Number of glyphs in this font. The range
first_char..first_char + glyph_count is the supported set.
bitmap: &'static [u8]Row-major bitmap bytes. glyph_count * glyph_h * ceil(glyph_w / 8) bytes total.
glyph_advances: Option<&'static [u8]>Optional per-glyph pixel advances. Proportional TTF imports fill this;
fixed-cell bitmap fonts leave it None and use advance_x.
advance_x: u8Pixel step between adjacent characters on a text line.
Usually == glyph_w for fixed-width fonts. Proportional fonts use this
as the fallback for missing/out-of-range glyphs.
line_height: u8Pixel step between text lines. Usually == glyph_h.
bit_order: BitOrderBit packing within each bitmap byte.
Implementations§
Source§impl BitmapFont
impl BitmapFont
Sourcepub const fn row_bytes(&self) -> usize
pub const fn row_bytes(&self) -> usize
Bytes per row of a single glyph in the source bitmap.
Derived from BitmapFont::glyph_w – 1 byte for ≤ 8-wide
fonts, 2 bytes for 9..16-wide, etc.
Sourcepub const fn glyph_stride(&self) -> usize
pub const fn glyph_stride(&self) -> usize
Total bitmap bytes per glyph.
Sourcepub fn glyph_index(&self, ch: char) -> Option<u16>
pub fn glyph_index(&self, ch: char) -> Option<u16>
Glyph index for ch if the font covers its codepoint.
Sourcepub fn glyph_advance(&self, ch: char) -> u8
pub fn glyph_advance(&self, ch: char) -> u8
Pixel advance for ch, falling back to BitmapFont::advance_x when
the font has no per-glyph table or ch is outside the covered range.
Sourcepub fn text_width(&self, text: &str) -> u16
pub fn text_width(&self, text: &str) -> u16
Pixel width of text with this font’s advance metrics.
Sourcepub fn text_width_with_spacing(&self, text: &str, letter_spacing: i8) -> u16
pub fn text_width_with_spacing(&self, text: &str, letter_spacing: i8) -> u16
Pixel width of text with an extra signed spacing value inserted
between adjacent characters. The spacing is in final screen pixels,
so callers that scale glyphs can decide whether or not to scale the
spacing separately.
Sourcepub fn glyph_row_packed(&self, i: u16, r: u8) -> u32
pub fn glyph_row_packed(&self, i: u16, r: u8) -> u32
Fetch row r of glyph i as 4 bytes LSB-packed little-
endian – the row comes out as a u32 with pixel 0 in bit
0, pixel 1 in bit 1, … up to pixel 31 (enough for any
realistic cell width). Handles BitOrder normalisation
in one place so callers don’t have to branch.
Trait Implementations§
Source§impl Clone for BitmapFont
impl Clone for BitmapFont
Source§fn clone(&self) -> BitmapFont
fn clone(&self) -> BitmapFont
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BitmapFont
impl Debug for BitmapFont
impl Copy for BitmapFont
Auto Trait Implementations§
impl Freeze for BitmapFont
impl RefUnwindSafe for BitmapFont
impl Send for BitmapFont
impl Sync for BitmapFont
impl Unpin for BitmapFont
impl UnsafeUnpin for BitmapFont
impl UnwindSafe for BitmapFont
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)