embedded_vintage_fonts/
lib.rs

1#![no_std]
2
3//! The monospace fonts from
4//! [embedded-graphics](https://github.com/embedded-graphics/embedded-graphics/tree/embedded-graphics-v0.6.2)
5//! back the overhauled font handling form 0.7. The fronts provided here use
6//! the same bitmaps with new
7//! [`MonoFont`](embedded_graphics::mono_font::MonoFont) structs.
8//!
9//! # Example
10//!
11//! Draw the text "Hello!" to a mock display using the 6 x 8 pixel font
12//! [`FONT_6X8`].
13//!
14//! ```rust
15//! use embedded_graphics::{
16//!     mock_display::MockDisplay,
17//!     mono_font::MonoTextStyle,
18//!     pixelcolor::BinaryColor,
19//!     prelude::*,
20//!     text::Text,
21//! };
22//! use embedded_vintage_fonts::FONT_6X8;
23//!
24//! # fn main() -> Result<(), core::convert::Infallible> {
25//! let mut display = MockDisplay::new();
26//! let style = MonoTextStyle::new(&FONT_6X8, BinaryColor::On);
27//!
28//! Text::new("Hello!", Point::new(0, FONT_6X8.baseline as i32), style)
29//!     .draw(&mut display)?;
30//!
31//! assert_eq!(
32//!     display,
33//!     MockDisplay::from_pattern(&[
34//!         "#   #        ##    ##           #",
35//!         "#   #         #     #           #",
36//!         "#   #  ###    #     #    ###    #",
37//!         "##### #   #   #     #   #   #   #",
38//!         "#   # #####   #     #   #   #   #",
39//!         "#   # #       #     #   #   #    ",
40//!         "#   #  ###   ###   ###   ###    #",
41//!     ])
42//! );
43//! # Ok(())
44//! # }
45//! ```
46//!
47//! # Glyph Coverage
48//!
49//! Most fonts from this crate provide support for [ISO/IEC
50//! 8859-1](https://en.wikipedia.org/wiki/ISO/IEC_8859-1) (Latin-1).
51//! [`FONT_6X12`] supports only [ASCII](https://en.wikipedia.org/wiki/ASCII)
52//! characters.
53
54use embedded_graphics::{
55    geometry::Size,
56    image::ImageRaw,
57    mono_font::{mapping::StrGlyphMapping, DecorationDimensions, MonoFont},
58};
59
60const GLYPH_MAPPING: StrGlyphMapping =
61    StrGlyphMapping::new("\0\u{20}\u{7e}\0\u{a1}\u{ff}", '?' as usize - ' ' as usize);
62const GLYPH_MAPPING_6X12: StrGlyphMapping =
63    StrGlyphMapping::new("\0\u{20}\u{7e}", '?' as usize - ' ' as usize);
64
65/// An upscaled version of [`FONT_12X16`] previously known as `Font24x32`.
66pub const FONT_24X32: MonoFont = MonoFont {
67    image: ImageRaw::new(include_bytes!("../data/font24x32_1bpp.raw"), 960),
68    character_size: Size::new(24, 32),
69    character_spacing: 0,
70    baseline: 27,
71    strikethrough: DecorationDimensions::new(14, 4),
72    underline: DecorationDimensions::new(29, 4),
73    glyph_mapping: &GLYPH_MAPPING,
74};
75
76/// The 12 x 16 pixel font formerly known `Font12x16`.
77///
78/// ![Source
79/// image](https://raw.githubusercontent.com/sirhcel/embedded-vintage-fonts/master/data/font12x16.png)
80pub const FONT_12X16: MonoFont = MonoFont {
81    image: ImageRaw::new(include_bytes!("../data/font12x16_1bpp.raw"), 480),
82    character_size: Size::new(12, 16),
83    character_spacing: 0,
84    baseline: 13,
85    strikethrough: DecorationDimensions::new(7, 2),
86    underline: DecorationDimensions::new(15, 2),
87    glyph_mapping: &GLYPH_MAPPING,
88};
89
90/// The 8 x 16 pixel font formerly known `Font8x16`.
91///
92/// ![Source
93/// image](https://raw.githubusercontent.com/sirhcel/embedded-vintage-fonts/master/data/font8x16.png)
94pub const FONT_8X16: MonoFont = MonoFont {
95    image: ImageRaw::new(include_bytes!("../data/font8x16_1bpp.raw"), 240),
96    character_size: Size::new(8, 16),
97    character_spacing: 0,
98    baseline: 11,
99    strikethrough: DecorationDimensions::new(6, 2),
100    underline: DecorationDimensions::new(13, 2),
101    glyph_mapping: &GLYPH_MAPPING,
102};
103
104/// The 6 x 12 pixel font formerly known `Font6x12`.
105///
106/// ![Source
107/// image](https://raw.githubusercontent.com/sirhcel/embedded-vintage-fonts/master/data/font6x12.png)
108pub const FONT_6X12: MonoFont = MonoFont {
109    image: ImageRaw::new(include_bytes!("../data/font6x12_1bpp.raw"), 96),
110    character_size: Size::new(6, 12),
111    character_spacing: 0,
112    baseline: 9,
113    strikethrough: DecorationDimensions::new(5, 1),
114    underline: DecorationDimensions::new(11, 1),
115    glyph_mapping: &GLYPH_MAPPING_6X12,
116};
117
118/// The 6 x 8 pixel font formerly known `Font6x8`.
119///
120/// ![Source
121/// image](https://raw.githubusercontent.com/sirhcel/embedded-vintage-fonts/master/data/font6x8.png)
122pub const FONT_6X8: MonoFont = MonoFont {
123    image: ImageRaw::new(include_bytes!("../data/font6x8_1bpp.raw"), 240),
124    character_size: Size::new(6, 8),
125    character_spacing: 0,
126    baseline: 6,
127    strikethrough: DecorationDimensions::new(3, 1),
128    underline: DecorationDimensions::new(8, 1),
129    glyph_mapping: &GLYPH_MAPPING,
130};