Crate raster_fonts

source ·
Expand description

De-/Serializable runtime representation of bitmap font metadata.

Usage

RON

// Requires Cargo feature `serde-deserialize` and the `ron` crate:
const FONT_METADATA: &'static str = include_str!("../font-metadata.ron");
let font: raster_fonts::BitmapFont = ron::from_str(FONT_METADATA)?;

JSON

// Requires Cargo feature `serde-deserialize` and the `serde_json` crate:
const FONT_METADATA: &'static str = include_str!("../font-metadata.json");
let font: raster_fonts::BitmapFont = serde_json::from_str(FONT_METADATA)?;

RKYV

// `rkyv` requires the data to be aligned for zero-copy deserialization,
// which in turn requires some trickery to achieve in a `const` context:
const FONT_METADATA: &'static [u8] = {
    #[repr(C)]
    struct Aligned<T: ?Sized> {
        _align: [usize; 0],
        bytes: T,
    }
 
    const ALIGNED: &'static Aligned<[u8]> = &Aligned {
        _align: [],
        bytes: *include_bytes!("../font-metadata.rkyv"),
    };
     
    &ALIGNED.bytes
};
 
// Using the unsafe API for maximum performance:
use raster_fonts::BitmapFont;
let archived_font = unsafe { rkyv::archived_root::<BitmapFont>(FONT_METADATA) };
// Optionally, unpack the archived metadata before use:
use rkyv::Deserialize;
let deserialized_font: BitmapFont = archived_font.deserialize(&mut rkyv::Infallible).unwrap();

Structs

Runtime representation of all metadata for a single bitmap font.
The resolver for an archived BitmapFont
SourceRect and horizontal metrics of a glyph required for text layout.
The resolver for an archived BitmapGlyph
Coordinates and size of a rendered glyph in a packed bitmap.
The resolver for an archived SourceRect