Expand description
§spleen-font — tiny no_std PSF-2 bitmap‐font reader
This crate ships the six Spleen fixed-width fonts and gives you a panic-free, heap-free API to render them on any framebuffer.
glyph lookup row iterator bit iterator
┌──────────────┐ ┌────────────────┐ ┌──────────────────┐
UTF-8 ──► `PSF2Font` ──────► `Glyph<'_>` ───────► `GlyphRow<'_>` ─────► bool
│ (cache) │ │ one scan-line │ │ one pixel (fg/bg)│
└──────────────┘ └────────────────┘ └──────────────────┘§Features
no_std, zero allocations – suitable for kernels and bootloaders.- Constant-time glyph lookup for ASCII and cached Unicode.
- < 2 KiB RAM: 64-entry ring cache + iterator state.
- Exposes only data; pixel drawing/framebuffer manipulation is left to the user.
§Quick start
This example assumes that you have a framebuffer and a function to set pixels.
fn main() {
// only compiled & run when `s8x16` is enabled
#[cfg(feature = "s8x16")] {
use spleen_font::{PSF2Font, FONT_8X16};
fn set_pixel(_: &mut [u8], _: usize, _: usize, _: bool) { }
let mut font = PSF2Font::new(FONT_8X16).unwrap();
let mut framebuffer = [0u8; 1024];
if let Some(glyph) = font.glyph_for_utf8("é".as_bytes()) {
for (row_y, row) in glyph.enumerate() {
for (col_x, on) in row.enumerate() {
set_pixel(&mut framebuffer, col_x, row_y, on);
}
}
}
}
}To test this example, run:
cargo test --doc --features s8x16§Feature gates
To reduce the overall footprint, each of the six fonts is gated behind a feature. Note that while this does not reduce the size of the crate on crates.io, it does reduce the size of the compiled binary - only the blob for the selected font you enable will be included in the final binary.
| Feature | Enables constant | Size (KiB) |
|---|---|---|
| s5x8 | FONT_5X8 | 2 KiB |
| s6x12 | FONT_6X12 | 6 KiB |
| s8x16 | FONT_8X16 | 4 KiB |
| s12x24 | FONT_12X24 | 12 KiB |
| s16x32 | FONT_16X32 | 32 KiB |
| s32x64 | FONT_32X64 | 128 KiB |
| all | all of the above | 184 KiB |
Each entry is a raw byte slice &[u8] where the slice is the raw PSF-2 file embedded via include_bytes!.
By default, no font is enabled. Enabling a font in your Cargo.toml should look like this:
[dependencies]
spleen-font = { version = "0.1", features = ["s8x16"] }§Re-exports
§Bundled fonts
| variant | size (px) | index in [FONTS] |
|---|---|---|
S5x8 | 5 × 8 | 0 |
S6x12 | 6 × 12 | 1 |
S8x16 | 8 × 16 | 2 |
S12x24 | 12 × 24 | 3 |
S16x32 | 16 × 32 | 4 |
S32x64 | 32 × 64 | 5 |
Each entry is a tuple (&[u8], Size) where the slice is the raw PSF-2
file embedded via include_bytes!.
Spleen font Copyright (c) 2018-2024, Frederic Cambus, BSD2 License
*.psfu files obtained from https://github.com/fcambus/spleen
Re-exports§
pub use glyph::Glyph;pub use glyph::GlyphRow;pub use psf::PSF2Font;pub use psf::PSF2Header;pub use psf::PSF2_MAGIC;
Modules§
Enums§
- Size
- Logical name for each embedded Spleen size.