profont/
lib.rs

1#![no_std]
2
3//! The [ProFont](https://web.archive.org/web/20180412214402/http://tobiasjung.name/profont/)
4//! monospace programming font for use with
5//! [embedded-graphics](https://github.com/jamwaffles/embedded-graphics). Font data taken from the
6//! [ProFont homepage](https://web.archive.org/web/20180412214402/http://tobiasjung.name/profont/).
7//!
8//! # Examples
9//!
10//! Draw the text "Hello world" to a mock display using the 7pt ProFont font.
11//!
12//! ```rust
13//! use embedded_graphics::{
14//!     mock_display::MockDisplay,
15//!     mono_font::MonoTextStyle,
16//!     pixelcolor::Rgb888,
17//!     prelude::*,
18//!     text::Text,
19//! };
20//! use profont::PROFONT_7_POINT;
21//!
22//! # fn main() -> Result<(), core::convert::Infallible> {
23//! let mut display = MockDisplay::new();
24//!
25//! let text_style = MonoTextStyle::new(&PROFONT_7_POINT, Rgb888::RED);
26//!
27//! Text::new("Hello world", Point::new(0, 7), text_style).draw(&mut display)?;
28//! # Ok(()) }
29//! ```
30//!
31//! For a more complete example see [the example in the ssd1675
32//! crate](https://github.com/wezm/ssd1675/blob/master/examples/raspberry_pi_inky_phat.rs).
33//!
34//! ### Glyph Coverage
35//!
36//! This crate provides support for [ISO/IEC 8859-1](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
37//! (latin1), although do note that the font is missing a few glyphs in this range.
38
39use embedded_graphics::{
40    geometry::Size,
41    image::ImageRaw,
42    mono_font::{mapping::StrGlyphMapping, DecorationDimensions, MonoFont},
43};
44
45const CHARS_PER_ROW: u32 = 32;
46
47/// Character ranges for all fonts.
48///
49/// This consists of two character ranges - ASCII from ' ' to '~', then ISO 8859-1 from `&nbsp;`
50/// (HTML notation) to `ÿ`. Unknown characters fall back to `?`.
51const GLYPH_MAPPING: StrGlyphMapping =
52    StrGlyphMapping::new("\0 ~\0\u{00A0}ÿ", '?' as usize - ' ' as usize);
53
54/// The 7 point size with a character size of 5x10 pixels.
55pub const PROFONT_7_POINT: MonoFont = MonoFont {
56    image: ImageRaw::new(
57        include_bytes!("../data/ProFont7Point.raw"),
58        CHARS_PER_ROW * 5,
59    ),
60
61    character_size: Size::new(5, 10),
62    character_spacing: 0,
63    baseline: 7,
64    underline: DecorationDimensions::new(8, 1),
65    strikethrough: DecorationDimensions::new(6, 1),
66    glyph_mapping: &GLYPH_MAPPING,
67};
68
69/// The 9 point size with a character size of 6x11 pixels.
70pub const PROFONT_9_POINT: MonoFont = MonoFont {
71    image: ImageRaw::new(
72        include_bytes!("../data/ProFont9Point.raw"),
73        CHARS_PER_ROW * 6,
74    ),
75
76    character_size: Size::new(6, 11),
77    character_spacing: 0,
78    baseline: 8,
79    underline: DecorationDimensions::new(10, 1),
80    strikethrough: DecorationDimensions::new(6, 1),
81    glyph_mapping: &GLYPH_MAPPING,
82};
83
84/// The 10 point size with a character size of 6x12 pixels.
85pub const PROFONT_10_POINT: MonoFont = MonoFont {
86    image: ImageRaw::new(
87        include_bytes!("../data/ProFont10Point.raw"),
88        CHARS_PER_ROW * 6,
89    ),
90
91    character_size: Size::new(6, 12),
92    character_spacing: 1,
93    baseline: 9,
94    underline: DecorationDimensions::new(10 + 1, 1),
95    strikethrough: DecorationDimensions::new(7, 1),
96    glyph_mapping: &GLYPH_MAPPING,
97};
98
99/// The 12 point size with a character size of 7x15 pixels.
100pub const PROFONT_12_POINT: MonoFont = MonoFont {
101    image: ImageRaw::new(
102        include_bytes!("../data/ProFont12Point.raw"),
103        CHARS_PER_ROW * 7,
104    ),
105
106    character_size: Size::new(7, 15),
107    character_spacing: 1,
108    baseline: 11,
109    underline: DecorationDimensions::new(13, 1),
110    strikethrough: DecorationDimensions::new(8, 1),
111    glyph_mapping: &GLYPH_MAPPING,
112};
113
114/// The 14 point size with a character size of 10x17 pixels.
115pub const PROFONT_14_POINT: MonoFont = MonoFont {
116    image: ImageRaw::new(
117        include_bytes!("../data/ProFont14Point.raw"),
118        CHARS_PER_ROW * 10,
119    ),
120
121    character_size: Size::new(10, 17),
122    character_spacing: 0,
123    baseline: 13,
124    underline: DecorationDimensions::new(15, 1),
125    strikethrough: DecorationDimensions::new(9, 2),
126    glyph_mapping: &GLYPH_MAPPING,
127};
128
129/// The 18 point size with a character size of 12x22 pixels.
130pub const PROFONT_18_POINT: MonoFont = MonoFont {
131    image: ImageRaw::new(
132        include_bytes!("../data/ProFont18Point.raw"),
133        CHARS_PER_ROW * 12,
134    ),
135
136    character_size: Size::new(12, 22),
137    character_spacing: 0,
138    baseline: 17,
139    underline: DecorationDimensions::new(19, 2),
140    strikethrough: DecorationDimensions::new(12, 2),
141    glyph_mapping: &GLYPH_MAPPING,
142};
143
144/// The 24 point size with a character size of 16x29 pixels.
145pub const PROFONT_24_POINT: MonoFont = MonoFont {
146    image: ImageRaw::new(
147        include_bytes!("../data/ProFont24Point.raw"),
148        CHARS_PER_ROW * 16,
149    ),
150
151    character_size: Size::new(16, 29),
152    character_spacing: 0,
153    baseline: 24,
154    underline: DecorationDimensions::new(26, 2),
155    strikethrough: DecorationDimensions::new(16, 2),
156    glyph_mapping: &GLYPH_MAPPING,
157};