Crate mplusfonts

Source
Expand description

M+ FONTS for use with embedded-graphics; this crate is compatible with no_std and brings a font family by Coji Morishita into the embedded Rust ecosystem, including the variable-width M+ 1, M+ 2, and the monospaced M+ Code typefaces.

§Example of a bitmap font

Font rasterization is achieved using mplusfonts-macros, providing access to over 5 700 kanji glyphs.1 By default, mplus! expands to an empty bitmap font, so even basic characters such as digits and letters in Latin-script alphabets need to be specified in order for them to end up as pixel information in the generated data structure. For example, to create a fixed-width bitmap font that has support for rendering string representations of values 0x00 through 0xFF:

let bitmap_font = mplus!(code(100), 500, 12, false, 1, 8, '0'..='9', 'A'..='F', ["x"]);
  • code(100) - Use the monospaced M+ Code typeface; this is a variable font, so set its width-axis position to 100 percent (corresponds to M+ Code Latin 50).
  • 500 - Set the font weight to 500 (equivalent to MEDIUM).
  • 12 - Set the font size to 12 pixels per em-size.
  • false - Disable font hinting. Enable to force-align the top, the bottom, and the segments of glyph outlines that are running in parallel with the x-axis to the pixel grid.
  • 1 - Set the quantization level for positions per pixel to 1 (value not used with M+ Code — no sub-pixel offsets required).
  • 8 - Set the quantization level for gray values to 256 (8 bits per pixel).
  • '0'..='9', 'A'..='F', ["x"] - Enroll the characters and strings that comprise the text to be rendered.
Character ranges are intended to be used for including digits and, in general, when all strings using a set of characters need to be made renderable; otherwise, you end up with a higher than optimal amount of data. See the example below for the recommended way to use this crate.

§Example of rendering static text

Which characters a given instance of BitmapFont is required to include for rendering static text is deterministic. To cover such cases, this crate provides the strings attribute and two helper attributes.

#[mplusfonts::strings]
pub fn main() -> Result<(), Infallible> {
    let mut display: SimulatorDisplay<Rgb888> = SimulatorDisplay::new(Size::new(120, 120));

    #[strings::emit]
    let bitmap_font = mplus!(2, 480, 16, true, 4, 8, /* will inject ["It works!"] here */);

    let character_style = BitmapFontStyle::new(&bitmap_font, Rgb888::new(0, 210, 255));

    Text::new("It works!", Point::new(0, 120), character_style).draw(&mut display)?;

    let output_settings = OutputSettingsBuilder::new().scale(6).pixel_spacing(2).build();

    #[strings::skip]
    Window::new("Simulator", &output_settings).show_static(&display);

    Ok(())
}

For more examples, see the repository on GitHub.

Modules§

color
Color math for bitmap fonts.
glyph
Glyphs and glyph clusters.
image
Images and image drawables.
style
Styles and style builders.

Macros§

mplus
Produces a struct expression for creating a BitmapFont.

Structs§

BitmapFont
Bitmap font.
BitmapFontMetrics
Metrics of a bitmap font.
CharmapEntry
Charmap entry.
DecorationDimensions
Decoration dimensions for mplusfonts.

Enums§

Charmap
Charmap, for looking up glyph data, matching as many characters as possible at a time.

Type Aliases§

CharmapEntryKey
Key that is unique to a charmap entry in a bitmap font.

Attribute Macros§

strings
Collects string literals for rewriting mplus! macro invocations prior to their expansion.