Skip to main content

Crate spleen_font

Crate spleen_font 

Source
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.

FeatureEnables constantSize (KiB)
s5x8FONT_5X82 KiB
s6x12FONT_6X126 KiB
s8x16FONT_8X164 KiB
s12x24FONT_12X2412 KiB
s16x32FONT_16X3232 KiB
s32x64FONT_32X64128 KiB
allall of the above184 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

variantsize (px)index in [FONTS]
S5x85 × 80
S6x126 × 121
S8x168 × 162
S12x2412 × 243
S16x3216 × 324
S32x6432 × 645

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§

glyph
psf

Enums§

Size
Logical name for each embedded Spleen size.