mplusfonts/
lib.rs

1//! **M<sup>+</sup> FONTS** for use with [`embedded-graphics`](embedded_graphics); this crate is
2//! compatible with `no_std` and brings [a font family](https://mplusfonts.github.io/) by Coji
3//! Morishita into the embedded Rust ecosystem, including the variable-width **M<sup>+</sup> 1**,
4//! **M<sup>+</sup> 2**, and the monospaced **M<sup>+</sup> Code** typefaces.
5//!
6//! # Example of a bitmap font
7//!
8//! Font rasterization is achieved using [`mplusfonts-macros`](mplusfonts_macros), providing access
9//! to over 5 700 kanji glyphs.[^1] By default, [`mplus!`] expands to an empty bitmap font, so even
10//! basic characters such as digits and letters in Latin-script alphabets need to be specified in
11//! order for them to end up as pixel information in the generated data structure. For example, to
12//! create a fixed-width bitmap font that has support for rendering string representations of
13//! values `0x00` through `0xFF`:
14//!
15//! ```
16//! # use mplusfonts::mplus;
17//! #
18//! let bitmap_font = mplus!(code(100), 500, 12, false, 1, 8, '0'..='9', 'A'..='F', ["x"]);
19//! ```
20//!
21//! * `code(100)` - Use the monospaced **M<sup>+</sup> Code** typeface; this is a variable font, so
22//!   set its width-axis position to 100 percent (corresponds to **M<sup>+</sup> Code Latin 50**).
23//! * `500` - Set the font weight to 500 (equivalent to `MEDIUM`).
24//! * `12` - Set the font size to 12 pixels per _em_-size.
25//! * `false` - Disable font hinting. Enable to force-align the top, the bottom, and the segments
26//!   of glyph outlines that are running in parallel with the _x_-axis to the pixel grid.
27//! * `1` - Set the quantization level for positions per pixel to 1 (value not used with
28//!   **M<sup>+</sup> Code** --- no sub-pixel offsets required).
29//! * `8` - Set the quantization level for gray values to 256 (8 bits per pixel).
30//! * `'0'..='9'`, `'A'..='F'`, `["x"]` - Enroll the characters and strings that comprise the text
31//!   to be rendered.
32//!
33//! <div class="warning">
34//!   Character ranges are intended to be used for including digits and, in general, when all
35//!   strings using a set of characters need to be made renderable; otherwise, you end up with a
36//!   higher than optimal amount of data. See the example below for the recommended way to use this
37//!   crate.
38//! </div>
39//!
40//! [^1]: <https://mplusfonts.github.io/#variable>
41//!
42//! # Example of rendering static text
43//!
44//! Which characters a given instance of [`BitmapFont`] is required to include for rendering static
45//! text is deterministic. To cover such cases, this crate provides the [`strings`] attribute and
46//! two helper attributes.
47//!
48//! ```
49//! # use core::convert::Infallible;
50//! #
51//! # use embedded_graphics::pixelcolor::Rgb888;
52//! # use embedded_graphics::prelude::*;
53//! # use embedded_graphics::text::Text;
54//! # use embedded_graphics_simulator::{OutputSettingsBuilder, SimulatorDisplay, Window};
55//! # use mplusfonts::mplus;
56//! # use mplusfonts::style::BitmapFontStyle;
57//! #
58//! #[mplusfonts::strings]
59//! pub fn main() -> Result<(), Infallible> {
60//!     let mut display: SimulatorDisplay<Rgb888> = SimulatorDisplay::new(Size::new(120, 120));
61//!
62//!     #[strings::emit]
63//!     let bitmap_font = mplus!(2, 480, 16, true, 4, 8, /* will inject ["It works!"] here */);
64//!
65//!     let character_style = BitmapFontStyle::new(&bitmap_font, Rgb888::new(0, 210, 255));
66//!
67//!     Text::new("It works!", Point::new(0, 120), character_style).draw(&mut display)?;
68//!
69//!     let output_settings = OutputSettingsBuilder::new().scale(6).pixel_spacing(2).build();
70//!
71//!     #[strings::skip]
72//!     Window::new("Simulator", &output_settings).show_static(&display);
73//!
74//!     Ok(())
75//! }
76//! ```
77//!
78//! For more examples, see the [repository](https://github.com/immersum/mplusfonts) on GitHub.
79
80#![no_std]
81#![warn(missing_docs)]
82#![warn(missing_debug_implementations)]
83#![warn(missing_copy_implementations)]
84
85mod adapter;
86mod builder;
87mod charmap;
88mod font;
89mod metrics;
90mod rect;
91
92pub mod color;
93pub mod glyph;
94pub mod image;
95pub mod style;
96
97pub use font::BitmapFont;
98
99pub use charmap::*;
100pub use metrics::*;
101
102pub use mplusfonts_macros::mplus;
103pub use mplusfonts_macros::strings;