embedded-ttf 0.2.2

Font rendering (ttf and otf) with embedded-graphics
Documentation
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Font rendering (ttf and otf) with embedded-graphics.
//!
//! Embedded graphics provides static mono font rendering directly from the code.
//! But it can render any font if the proper trait is implemented.
//!
//! This is an implementation that uses the [rusttype](https://gitlab.redox-os.org/redox-os/rusttype)
//! crate to parse ttf and otf fonts before rendering them on a `DrawTarget`
//!
//! # Usage
//!
//! Use [`FontTextStyleBuilder`] to easily create a [`FontTextStyle`] object.
//!
//! This style can then be directly used with embedded graphics' [`Text`] struct.
//!
//! ```
//! let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(350, 200));
//!
//! let style = FontTextStyleBuilder::new(
//!     Font::try_from_bytes(include_bytes!("../assets/Roboto-Regular.ttf")).unwrap())
//!     .font_size(16)
//!     .text_color(Rgb565::WHITE)
//!     .build();
//!
//! Text::new("Hello World!", Point::new(15, 30), style).draw(&mut display)?;
//! ```
//!
//! # Antialiasing
//!
//! TrueType fonts are much nicer with antialiasing. However, embedded_graphics does not support
//! retrieving current pixel while drawing, which prevents alpha blending and antialiasing.
//!
//! If you have a background color, the color is known and antialiasing is applied.
//! Otherwise, you can use the [`AntiAliasing`] enum either to disable antialiasing or to define
//! an antialiasing background color.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use num_traits::float::FloatCore;

#[cfg(feature = "std")]
use std as stdlib;

#[cfg(not(feature = "std"))]
mod stdlib {
    pub use ::alloc::vec;
    pub use core::*;
}

use stdlib::{f32, vec::Vec};

use embedded_graphics::{
    draw_target::DrawTarget,
    pixelcolor::Rgb888,
    prelude::*,
    primitives::Rectangle,
    text::{
        renderer::{CharacterStyle, TextMetrics, TextRenderer},
        Baseline, DecorationColor,
    },
};

use rusttype::Font;

/// Antialiasing can be challenging with embedded graphics since the background pixel is not known
/// during the drawing process.
#[derive(Debug, Clone)]
pub enum AntiAliasing<C> {
    /// Use the font background color (default), choose this if you defined a background color.
    /// This is equivalent to SolidColor if the background color is defined,
    /// This is equivalent to None if the background color is not defined
    BackgroundColor,
    /// Use given color as a "known" background color.
    SolidColor(C),
    /// Replace the alpha channel with a simple transparency (cutoff at 50%), choose this if you don't know the background at all.
    None,
}

/// Style properties for text using a ttf and otf font.
///
/// A `FontTextStyle` can be applied to a [`Text`] object to define how the text is drawn.
///
#[derive(Debug, Clone)]
pub struct FontTextStyle<C> {
    /// Text color.
    pub text_color: Option<C>,

    /// Background color.
    pub background_color: Option<C>,

    /// How to apply antialiasing.
    pub anti_aliasing: AntiAliasing<C>,

    /// Underline color.
    pub underline_color: DecorationColor<C>,

    /// Strikethrough color.
    pub strikethrough_color: DecorationColor<C>,

    /// Font size.
    pub font_size: u32,

    /// Font from rusttype.
    font: Font<'static>,
}

impl<C: PixelColor> FontTextStyle<C> {
    /// Creates a text style with a transparent background.
    pub fn new(font: Font<'static>, text_color: C, font_size: u32) -> Self {
        FontTextStyleBuilder::new(font)
            .text_color(text_color)
            .font_size(font_size)
            .build()
    }

    /// Resolves a decoration color.
    fn resolve_decoration_color(&self, color: DecorationColor<C>) -> Option<C> {
        match color {
            DecorationColor::None => None,
            DecorationColor::TextColor => self.text_color,
            DecorationColor::Custom(c) => Some(c),
        }
    }

    fn draw_background<D>(
        &self,
        width: u32,
        position: Point,
        target: &mut D,
    ) -> Result<(), D::Error>
    where
        D: DrawTarget<Color = C>,
    {
        if width == 0 {
            return Ok(());
        }

        if let Some(background_color) = self.background_color {
            target.fill_solid(
                &Rectangle::new(position, Size::new(width, self.font_size)),
                background_color,
            )?;
        }

        Ok(())
    }

    fn draw_strikethrough<D>(
        &self,
        width: u32,
        position: Point,
        target: &mut D,
    ) -> Result<(), D::Error>
    where
        D: DrawTarget<Color = C>,
    {
        if let Some(strikethrough_color) = self.resolve_decoration_color(self.strikethrough_color) {
            let top_left = position + Point::new(0, self.font_size as i32 / 2);
            // small strikethrough width
            let size = Size::new(width, self.font_size / 30 + 1);

            target.fill_solid(&Rectangle::new(top_left, size), strikethrough_color)?;
        }

        Ok(())
    }

    fn draw_underline<D>(&self, width: u32, position: Point, target: &mut D) -> Result<(), D::Error>
    where
        D: DrawTarget<Color = C>,
    {
        if let Some(underline_color) = self.resolve_decoration_color(self.underline_color) {
            let top_left = position + Point::new(0, self.font_size as i32);
            // small underline width
            let size = Size::new(width, self.font_size / 30 + 1);

            target.fill_solid(&Rectangle::new(top_left, size), underline_color)?;
        }

        Ok(())
    }
}

impl<C: PixelColor> CharacterStyle for FontTextStyle<C> {
    type Color = C;

    fn set_text_color(&mut self, text_color: Option<Self::Color>) {
        self.text_color = text_color;
    }

    fn set_background_color(&mut self, background_color: Option<Self::Color>) {
        self.background_color = background_color;
        if background_color.is_some() {
            // best antialiasing in this case
            self.anti_aliasing = AntiAliasing::BackgroundColor;
        }
    }

    fn set_underline_color(&mut self, underline_color: DecorationColor<Self::Color>) {
        self.underline_color = underline_color;
    }

    fn set_strikethrough_color(&mut self, strikethrough_color: DecorationColor<Self::Color>) {
        self.strikethrough_color = strikethrough_color;
    }
}

impl<C> TextRenderer for FontTextStyle<C>
where
    C: PixelColor + Into<Rgb888> + From<Rgb888> + stdlib::fmt::Debug,
{
    type Color = C;

    fn draw_string<D>(
        &self,
        text: &str,
        position: Point,
        _baseline: Baseline,
        target: &mut D,
    ) -> Result<Point, D::Error>
    where
        D: DrawTarget<Color = Self::Color>,
    {
        let scale = rusttype::Scale::uniform(self.font_size as f32);

        let v_metrics = self.font.v_metrics(scale);
        let offset = rusttype::point(0.0, v_metrics.ascent);

        let glyphs: Vec<rusttype::PositionedGlyph> =
            self.font.layout(text, scale, offset).collect();

        let width = glyphs
            .iter()
            .rev()
            .filter_map(|g| {
                g.pixel_bounding_box()
                    .map(|b| b.min.x as f32 + g.unpositioned().h_metrics().advance_width)
            })
            .next()
            .unwrap_or(0.0)
            .ceil() as i32;

        let height = self.font_size as i32;

        let mut pixels = Vec::new();

        if let Some(text_color) = self.text_color {
            for g in glyphs.iter() {
                if let Some(bb) = g.pixel_bounding_box() {
                    g.draw(|off_x, off_y, v| {
                        let off_x = off_x as i32 + bb.min.x;
                        let off_y = off_y as i32 + bb.min.y;
                        // There's still a possibility that the glyph clips the boundaries of the bitmap
                        if off_x >= 0 && off_x < width as i32 && off_y >= 0 && off_y < height as i32
                        {
                            let text_a = (v * 255.0) as u32;

                            let bg_color = match self.anti_aliasing {
                                AntiAliasing::BackgroundColor => self.background_color,
                                AntiAliasing::SolidColor(c) => Some(c),
                                AntiAliasing::None => None,
                            };
                            match bg_color {
                                None => if text_a > 127 {
                                    pixels.push(Pixel(
                                        Point::new(position.x + off_x, position.y + off_y),
                                        text_color
                                    ));
                                }
                                Some(color) => {
                                    let a = text_a as u16;
                                    let fg = text_color.into();
                                    let bg = color.into();
                                    // blend with background color
                                    let new_r = (a * fg.r() as u16 + (255-a) * bg.r() as u16) / 255;
                                    let new_g = (a * fg.g() as u16 + (255-a) * bg.g() as u16) / 255;
                                    let new_b = (a * fg.b() as u16 + (255-a) * bg.b() as u16) / 255;

                                    pixels.push(Pixel(
                                        Point::new(position.x + off_x, position.y + off_y),
                                        Rgb888::new(new_r as u8, new_g as u8, new_b as u8).into(),
                                    ));
                                }
                            }
                        }
                    });
                }
            }
        }

        self.draw_background(width as u32, position, target)?;
        target.draw_iter(pixels)?;
        self.draw_strikethrough(width as u32, position, target)?;
        self.draw_underline(width as u32, position, target)?;

        Ok(position + Point::new(width, 0))
    }

    fn draw_whitespace<D>(
        &self,
        width: u32,
        position: Point,
        _baseline: Baseline,
        target: &mut D,
    ) -> Result<Point, D::Error>
    where
        D: DrawTarget<Color = Self::Color>,
    {
        self.draw_background(width, position, target)?;
        self.draw_strikethrough(width, position, target)?;
        self.draw_underline(width, position, target)?;

        Ok(position + Size::new(width, 0))
    }

    fn measure_string(&self, text: &str, position: Point, _baseline: Baseline) -> TextMetrics {
        let scale = rusttype::Scale::uniform(self.font_size as f32);
        let v_metrics = self.font.v_metrics(scale);
        let offset = rusttype::point(0.0, v_metrics.ascent);

        let glyphs: Vec<rusttype::PositionedGlyph> =
            self.font.layout(text, scale, offset).collect();

        let width = glyphs
            .iter()
            .rev()
            .map(|g| g.position().x as f32 + g.unpositioned().h_metrics().advance_width)
            .next()
            .unwrap_or(0.0)
            .ceil() as f64;

        let size = Size::new(width as u32, self.font_size);

        TextMetrics {
            bounding_box: Rectangle::new(position, size),
            next_position: position + size.x_axis(),
        }
    }

    fn line_height(&self) -> u32 {
        self.font_size
    }
}

/// Text style builder for ttf and otf fonts.
///
/// Use this builder to create [`FontTextStyle`]s for [`Text`].
pub struct FontTextStyleBuilder<C: PixelColor> {
    style: FontTextStyle<C>,
}

impl<C: PixelColor> FontTextStyleBuilder<C> {
    /// Create a new text style builder.
    pub fn new(font: Font<'static>) -> Self {
        Self {
            style: FontTextStyle {
                font,
                background_color: None,
                anti_aliasing: AntiAliasing::None,
                font_size: 12,
                text_color: None,
                underline_color: DecorationColor::None,
                strikethrough_color: DecorationColor::None,
            },
        }
    }

    /// Set the font size of the style in pixels.
    pub fn font_size(mut self, font_size: u32) -> Self {
        self.style.font_size = font_size;
        self
    }

    /// Enable underline using the text color.
    pub fn underline(mut self) -> Self {
        self.style.underline_color = DecorationColor::TextColor;
        self
    }

    /// Enable strikethrough using the text color.
    pub fn strikethrough(mut self) -> Self {
        self.style.strikethrough_color = DecorationColor::TextColor;
        self
    }

    /// Set the text color.
    pub fn text_color(mut self, text_color: C) -> Self {
        self.style.text_color = Some(text_color);
        self.style.anti_aliasing = AntiAliasing::BackgroundColor;
        self
    }

    /// Set the background color.
    pub fn background_color(mut self, background_color: C) -> Self {
        self.style.background_color = Some(background_color);
        self
    }

    /// Apply antialiasing over a known color.
    pub fn anti_aliasing_color(mut self, background_color: C) -> Self {
        self.style.anti_aliasing = AntiAliasing::SolidColor(background_color);
        self
    }

    /// Enable underline with a custom color.
    pub fn underline_with_color(mut self, underline_color: C) -> Self {
        self.style.underline_color = DecorationColor::Custom(underline_color);
        self
    }

    /// Enable strikethrough with a custom color.
    pub fn strikethrough_with_color(mut self, strikethrough_color: C) -> Self {
        self.style.strikethrough_color = DecorationColor::Custom(strikethrough_color);

        self
    }

    /// Build the text style.
    pub fn build(self) -> FontTextStyle<C> {
        self.style
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use embedded_graphics::pixelcolor::Rgb888;

    //TODO
}