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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//! Monospaced bitmap fonts.
//!
//! This module contains support for drawing monospaced bitmap fonts and provides
//! several [built-in fonts].
//!
//! Additional custom fonts can be added by the application or other crates. This
//! is demonstrated in the `text-custom-font` example in the simulator crate.
//!
//! # Examples
//!
//! The examples below use the [`Font6x8`] font, however any of the [built-in fonts]
//! in this module or custom fonts can be substituted.
//!
//! ## Draw text without using `egtext` macro
//!
//! Text can be drawn to a display by creating a [`Text`] object and attaching a
//! text style to it by using a [`Styled`] object. By creating the text style manually,
//! without using the [`egtext`] macro, it can be reused to style multiple text objects.
//!
//! ```rust
//! use embedded_graphics::{
//!     fonts::{Font6x8, Text},
//!     pixelcolor::Rgb565,
//!     prelude::*,
//!     style::{TextStyle, TextStyleBuilder},
//! };
//! # use embedded_graphics::mock_display::MockDisplay;
//! # let mut display: MockDisplay<Rgb565> = MockDisplay::default();
//!
//! // Create a new text style
//! let style = TextStyleBuilder::new(Font6x8)
//!     .text_color(Rgb565::YELLOW)
//!     .background_color(Rgb565::BLUE)
//!     .build();
//!
//! // Create a text at position (20, 30) and draw it using the previously defined style
//! Text::new("Hello Rust!", Point::new(20, 30))
//!     .into_styled(style)
//!     .draw(&mut display)?;
//! # Ok::<(), core::convert::Infallible>(())
//! ```
//!
//! ## Draw text using the `egtext` macro
//!
//! Creating styled text can be simplified by using the [`egtext`] and [`text_style`] macros.
//! All style properties in [`TextStyle`] can be set by using assignments inside
//! the [`text_style`] macro call.
//!
//! The following example draws the same text as the previous example but uses
//! the [`egtext`] macro to build the necessary styled text objects, and the [`text_style`] macro to style it.
//!
//! ```rust
//! use embedded_graphics::{egtext, fonts::Font6x8, pixelcolor::Rgb565, prelude::*, text_style};
//! # use embedded_graphics::mock_display::MockDisplay;
//! # let mut display: MockDisplay<Rgb565> = MockDisplay::default();
//!
//! egtext!(
//!     text = "Hello Rust!",
//!     top_left = (20, 30),
//!     style = text_style!(
//!         font = Font6x8,
//!         text_color = Rgb565::YELLOW,
//!         background_color = Rgb565::BLUE,
//!     )
//! )
//! .draw(&mut display)?;
//! # Ok::<(), core::convert::Infallible>(())
//! ```
//!
//! It is also possible to provide a style created without using the [`text_style`] macro. In this example, [`TextStyleBuilder`] is used.
//!
//! ```rust
//! use embedded_graphics::{
//!     egtext,
//!     fonts::Font6x8,
//!     pixelcolor::Rgb565,
//!     prelude::*,
//!     style::{TextStyle, TextStyleBuilder},
//! };
//! # use embedded_graphics::mock_display::MockDisplay;
//! # let mut display: MockDisplay<Rgb565> = MockDisplay::default();
//!
//! egtext!(
//!     text = "Hello Rust!",
//!     top_left = (20, 30),
//!     style = TextStyleBuilder::new(Font6x8)
//!         .text_color(Rgb565::YELLOW)
//!         .background_color(Rgb565::BLUE)
//!         .build()
//! )
//! .draw(&mut display)?;
//! # Ok::<(), core::convert::Infallible>(())
//! ```
//!
//! ## Translate text by (20px, 30px)
//!
//! ```rust
//! use embedded_graphics::{
//!     fonts::{Font6x8, Text},
//!     pixelcolor::BinaryColor,
//!     prelude::*,
//!     style::TextStyle,
//! };
//! # use embedded_graphics::mock_display::MockDisplay;
//! # let mut display: MockDisplay<BinaryColor> = MockDisplay::default();
//!
//! Text::new("Hello Rust!", Point::zero())
//!     .into_styled(TextStyle::new(Font6x8, BinaryColor::On))
//!     .translate(Point::new(20, 30))
//!     .draw(&mut display)?;
//!
//! // this is equivalent to:
//!
//! Text::new("Hello Rust!", Point::new(20, 30))
//!     .into_styled(TextStyle::new(Font6x8, BinaryColor::On))
//!     .draw(&mut display)?;
//! # Ok::<(), core::convert::Infallible>(())
//! ```
//!
//! ## Use `write!()` and arrayvec to render a formatted string
//!
//! This example uses arrayvec's [`ArrayString`] to render a floating point value using the
//! [`write!()`] macro. These strings have a fixed maximum length, but allow the use of
//! Rust's builtin string formatting.
//!
//! ```rust
//! use arrayvec::ArrayString;
//! use core::fmt::Write;
//! use embedded_graphics::{egtext, fonts::Font6x8, pixelcolor::Rgb565, prelude::*, text_style};
//! # use embedded_graphics::mock_display::MockDisplay;
//! # let mut display = MockDisplay::default();
//!
//! let value = 12.34567;
//!
//! // Create a fixed buffer of length 12
//! let mut buf = ArrayString::<[_; 12]>::new();
//!
//! // Output `Value: 12.35`
//! write!(&mut buf, "Value: {:.2}", value).expect("Failed to write to buffer");
//!
//! egtext!(
//!     text = &buf,
//!     top_left = Point::zero(),
//!     style = text_style!(
//!         font = Font6x8,
//!         text_color = Rgb565::YELLOW,
//!         background_color = Rgb565::BLUE,
//!     )
//! )
//! .draw(&mut display)?;
//! # Ok::<(), core::convert::Infallible>(())
//! ```
//!
//! # Built-in fonts
//!
//! | Type | Screenshot |
//! |------|------------|
//! | [`Font6x6`] | ![6x6 font spritemap screenshot](https://raw.githubusercontent.com/jamwaffles/embedded-graphics/master/embedded-graphics/data/font6x6.png) |
//! | [`Font6x8`] | ![6x8 font spritemap screenshot](https://raw.githubusercontent.com/jamwaffles/embedded-graphics/master/embedded-graphics/data/font6x8.png) |
//! | [`Font6x12`] | ![6x12 font spritemap screenshot](https://raw.githubusercontent.com/jamwaffles/embedded-graphics/master/embedded-graphics/data/font6x12.png) |
//! | [`Font8x16`] | ![8x16 font spritemap screenshot](https://raw.githubusercontent.com/jamwaffles/embedded-graphics/master/embedded-graphics/data/font8x16.png) |
//! | [`Font12x16`] | ![12x16 font spritemap screenshot](https://raw.githubusercontent.com/jamwaffles/embedded-graphics/master/embedded-graphics/data/font12x16.png) |
//! | [`Font24x32`] | The 24x32 font is a pixel doubled version of the 12x16 font. |
//!
//! [built-in fonts]: #built-in-fonts
//! [`egtext`]: ../macro.egtext.html
//! [`text_style`]: ../macro.text_style.html
//! [`Font6x6`]: struct.Font6x6.html
//! [`Font6x8`]: struct.Font6x8.html
//! [`Font6x12`]: struct.Font6x12.html
//! [`Font8x16`]: struct.Font8x16.html
//! [`Font12x16`]: struct.Font12x16.html
//! [`Font24x32`]: struct.Font24x32.html
//! [`Text`]: struct.Text.html
//! [`Styled`]: ../style/struct.Styled.html
//! [`TextStyle`]: ../style/struct.TextStyle.html
//! [`ArrayString`]: https://docs.rs/arrayvec/0.4.11/arrayvec/struct.ArrayString.html
//! [`write!()`]: https://doc.rust-lang.org/nightly/std/macro.write.html

mod font12x16;
mod font24x32;
mod font6x12;
mod font6x6;
mod font6x8;
mod font8x16;
mod text;

pub use text::{StyledTextIterator, Text};

pub use font12x16::Font12x16;
pub use font24x32::Font24x32;
pub use font6x12::Font6x12;
pub use font6x6::Font6x6;
pub use font6x8::Font6x8;
pub use font8x16::Font8x16;

use crate::geometry::Size;

/// Monospaced bitmap font.
pub trait Font {
    /// Raw image data containing the font.
    const FONT_IMAGE: &'static [u8];

    /// The width of the raw image data.
    ///
    /// The width must be divisible by `8` and `CHARACTER_SIZE.width`.
    const FONT_IMAGE_WIDTH: u32;

    /// Size of a single character in pixel.
    const CHARACTER_SIZE: Size;

    /// Spacing between characters.
    ///
    /// The spacing defines how many empty pixels are added horizontally between adjacent characters
    /// on a single line of text.
    const CHARACTER_SPACING: u32 = 0;

    /// Whether characters have a variable width or not.
    ///
    /// Variable width characters have a maximum width of CHARACTER_SIZE.x, but the empty columns at
    /// the right of each characters are ignored, allowing some characters to be smaller than others.
    const VARIABLE_WIDTH: bool = false;

    /// Returns the position a character in the font.
    fn char_offset(_: char) -> u32;

    /// Returns the actual width of a character in the font.
    fn char_width(c: char) -> u32 {
        if Self::VARIABLE_WIDTH {
            let mut x_max = 0;
            for y in 0..Self::CHARACTER_SIZE.height {
                for x in (x_max..Self::CHARACTER_SIZE.width).rev() {
                    if Self::character_pixel(c, x, y) {
                        x_max = x;
                        break;
                    }
                }
            }
            x_max + 1
        } else {
            Self::CHARACTER_SIZE.width
        }
    }

    /// Returns the value of a pixel in a character in the font.
    fn character_pixel(c: char, x: u32, y: u32) -> bool {
        let char_per_row = Self::FONT_IMAGE_WIDTH / Self::CHARACTER_SIZE.width;

        // Char _code_ offset from first char, most often a space
        // E.g. first char = ' ' (32), target char = '!' (33), offset = 33 - 32 = 1
        let char_offset = Self::char_offset(c);
        let row = char_offset / char_per_row;

        // Top left corner of character, in pixels
        let char_x = (char_offset - (row * char_per_row)) * Self::CHARACTER_SIZE.width;
        let char_y = row * Self::CHARACTER_SIZE.height;

        // Bit index
        // = X pixel offset for char
        // + Character row offset (row 0 = 0, row 1 = (192 * 8) = 1536)
        // + X offset for the pixel block that comprises this char
        // + Y offset for pixel block
        let bitmap_bit_index = char_x + x + ((char_y + y) * Self::FONT_IMAGE_WIDTH);

        let bitmap_byte = bitmap_bit_index / 8;
        let bitmap_bit = 7 - (bitmap_bit_index % 8);

        Self::FONT_IMAGE[bitmap_byte as usize] & (1 << bitmap_bit) != 0
    }
}

/// Creates a styled text.
///
/// The `egtext` macro expects the text, the position and styling properties as arguments.
///
/// The `style` property accepts a [`TextStyle`] object. This can be an object literal, usage of the
/// [`text_style`] macro, or something else like a function call that returns [`TextStyle`].
///
/// # Examples
///
/// ```rust
/// use embedded_graphics::{egtext, fonts::Font6x8, pixelcolor::Rgb888, prelude::*, text_style};
///
/// let text = egtext!(
///     text = "text",
///     top_left = Point::zero(),
///     style = text_style!(
///         font = Font6x8, // Font must to be the first styling property
///         text_color = Rgb888::RED,
///     )
/// );
/// ```
///
/// [`TextStyle`]: ../style/struct.TextStyle.html
/// [`text_style`]: ../macro.text_style.html
#[macro_export]
macro_rules! egtext {
    (text = $text:expr, top_left = $position:expr,
        style = $style:expr $(,)?) => {{
        $crate::fonts::Text::new($text, $crate::geometry::Point::from($position))
            .into_styled($style)
    }};
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        geometry::Point,
        pixelcolor::{BinaryColor, Rgb565, RgbColor},
        style::{Styled, TextStyle},
        text_style,
    };

    #[test]
    fn font_macros() {
        let _text: Styled<Text<'_>, TextStyle<BinaryColor, Font6x6>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font6x6)
        );
        let _text: Styled<Text<'_>, TextStyle<BinaryColor, Font6x8>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font6x8)
        );
        let _text: Styled<Text<'_>, TextStyle<BinaryColor, Font6x12>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font6x12)
        );
        let _text: Styled<Text<'_>, TextStyle<BinaryColor, Font8x16>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font8x16)
        );
        let _text: Styled<Text<'_>, TextStyle<BinaryColor, Font12x16>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font12x16)
        );
        let _text: Styled<Text<'_>, TextStyle<BinaryColor, Font24x32>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font24x32)
        );
    }

    #[test]
    fn styled_text() {
        let _text: Styled<Text<'_>, TextStyle<Rgb565, Font6x6>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font6x6, text_color = Rgb565::RED)
        );
        let _text: Styled<Text<'_>, TextStyle<Rgb565, Font6x8>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font6x8, text_color = Rgb565::RED)
        );
        let _text: Styled<Text<'_>, TextStyle<Rgb565, Font6x12>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font6x12, text_color = Rgb565::GREEN)
        );
        let _text: Styled<Text<'_>, TextStyle<Rgb565, Font8x16>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font8x16, text_color = Rgb565::BLUE)
        );
        let _text: Styled<Text<'_>, TextStyle<Rgb565, Font12x16>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font12x16, text_color = Rgb565::YELLOW)
        );
        let _text: Styled<Text<'_>, TextStyle<Rgb565, Font24x32>> = egtext!(
            text = "Hello!",
            top_left = Point::zero(),
            style = text_style!(font = Font24x32, text_color = Rgb565::MAGENTA)
        );
    }
}