1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! This crate is a pure Rust reimplementation of the font subsystem of [U8g2](https://github.com/olikraus/u8g2).
//!
//! It is intended for the [embedded-graphics](https://crates.io/crates/embedded-graphics) ecosystem.
//!
//!
//! # Licensing
//!
//! While this crate is MIT / Apache-2.0 licensed, note that the fonts themselves *are not*.
//!
//! For more information about the font licenses, read the [license agreement](https://github.com/olikraus/u8g2/blob/master/LICENSE) of U8g2.
//! # Crate features
//! Additional features can be enabled by adding the following features to your Cargo.toml.
//!
//! - `embedded_graphics_textstyle`:
//! - enable [`U8g2TextStyle`] struct for drawing text with [`embedded_graphics::text::Text`].
//!
//! # Renderers
//!
//! This crate supports two text renderers:
//!
//! - [`FontRenderer`] — our own renderer
//! - optimized for the U8g2 fonts
//! - supports rendering [`format_args!()`](format_args)
//! - can render everything that can be passed
//! to `format!()`, [`write!()`](write) or `println!()`
//! - does not allocate an intermediate string buffer
//! - supports multi-line vertical alignment
//! - [`U8g2TextStyle`] — a compatibility layer for [`embedded_graphics::text`]
//! - exposes all fonts of this crate to [`embedded_graphics::text::Text`] rendering functions
//! - supports [`draw_whitespace`](embedded_graphics::text::renderer::TextRenderer::draw_whitespace) for monospace whitespace drawing with a background color
//!
//! Everything below this will be about [`FontRenderer`]. For more information about text rendering through [`embedded_graphics`], read the
//! [embedded-graphics font rendering documentation](embedded_graphics::text).
//! The intention of [`U8g2TextStyle`] is to replace `MonoTextStyle`.
//!
//! # Usage
//!
//! The central struct in this crate is the [`FontRenderer`]. It can render one specific font.
//!
//! A [`FontRenderer`] can be constructed through [`FontRenderer::new()`](FontRenderer::new). Its generic argument [`Font`] specifies which font it will render.
//!
//! Note that [`FontRenderer::new()`] is `const`, so it can be crated as a global variable at compile time for optimal performance.
//!
//! ## Fonts
//!
//! The fonts are directly imported from [U8g2](https://github.com/olikraus/u8g2/wiki).
//!
//! More information about all the available fonts can be found in the [U8g2 wiki](https://github.com/olikraus/u8g2/wiki/fntlistall).
//!
//! The fonts can also be seen in [this list](crate::fonts).
//!
//! ## Content Types
//!
//! Once constructed, the [`FontRenderer`] can render [the following objects](Content):
//!
//! - Characters: `'a'`
//! - Strings: `"Hello world!"`
//! - Format Strings: `format_args!("Nice: {}", 69)`
//!
//! ## Positioning and Alignment
//!
//! The [`FontRenderer::render()`](FontRenderer::render) allows for basic vertical positioning. Horizontally, it renders exactly like specified in the font.
//!
//! For more advanced usecases, use the [`FontRenderer::render_aligned()`](FontRenderer::render_aligned) method.
//! It further allows for horizontal alignment through an additional parameter.
//!
//! ## Bounding Box Calculation
//!
//! Additional to the [`render()`](FontRenderer::render) and [`render_aligned()`](FontRenderer::render_aligned) methods,
//! there is also [`get_rendered_dimensions()`](FontRenderer::get_rendered_dimensions) and
//! [`get_rendered_dimensions_aligned()`](FontRenderer::get_rendered_dimensions_aligned).
//!
//! Those functions behave almost identical to their `render` counterparts, but don't actually perform any rendering. This
//! can be very useful if the dimensions of the text are required for other drawing operations prior to the actual text rendering.
//!
//! ## Colors and Backgrounds
//!
//! While a foreground color must always be specified for rendering a font, there is also the option to set a background color.
//! This is mainly for monospace fonts.
//!
//! Note that many fonts do not actually support rendering with a background color (due to occlusions).
//! Supplying a background color to a font that doesn't support it causes a [runtime error](crate::Error::BackgroundColorNotSupported).
//!
//! # Example
//!
//! ```rust
//! # use u8g2_fonts::types::*;
//! # use u8g2_fonts::FontRenderer;
//! # use u8g2_fonts::fonts;
//! # use embedded_graphics_core::prelude::*;
//! # use embedded_graphics_core::pixelcolor::BinaryColor;
//! # pub fn render<Display>(mut display: Display)
//! # where
//! # Display: DrawTarget<Color = BinaryColor>,
//! # Display::Error: core::fmt::Debug
//! # {
//! let font = FontRenderer::new::<fonts::u8g2_font_haxrcorp4089_t_cyrillic>();
//!
//! font.render_aligned(
//! format_args!("Answer: {}", 42),
//! display.bounding_box().center() + Point::new(0, 16),
//! VerticalPosition::Baseline,
//! HorizontalAlignment::Center,
//! FontColor::Transparent(BinaryColor::On),
//! &mut display,
//! )
//! .unwrap();
//! # }
//! ```
/// A collection of [U8g2 fonts](https://github.com/olikraus/u8g2/wiki/fntlistall).
///
/// Note that every font has a different license. For more information, read the [U8g2 License Agreement](https://github.com/olikraus/u8g2/blob/master/LICENSE).
/// Data types used in common API functions.
pub use Content;
pub use Error;
pub use LookupError;
pub use Font;
pub use FontRenderer;
pub use U8g2TextStyle;