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
use embedded_graphics_core::{
    prelude::{DrawTarget, Point, Size},
    primitives::Rectangle,
};
use crate::{
    content::{
        vertical_offset::compute_vertical_offset_from_static_newlines, LineDimensionsIterator,
    },
    font_reader::FontReader,
    types::{FontColor, HorizontalAlignment, RenderedDimensions, VerticalPosition},
    utils::{combine_bounding_boxes, HorizontalRenderedDimensions},
    Content, Error, Font, LookupError,
};
use self::render_actions::{compute_glyph_dimensions, compute_horizontal_offset, render_glyph};
pub mod render_actions;
/// Renders text of a specific [`Font`] to a [`DrawTarget`].
#[derive(Debug, Clone)]
pub struct FontRenderer {
    font: FontReader,
}
impl FontRenderer {
    /// Creates a new instance of a font renderer.
    ///
    /// # Generics
    ///
    /// * `FONT` - the font to render. See [fonts](crate::fonts) for a list of available fonts
    ///   and refer to [U8g2](https://github.com/olikraus/u8g2/wiki/fntlistall) for a more detailed description of each font.
    pub const fn new<FONT: Font>() -> Self {
        Self {
            font: FontReader::new::<FONT>(),
        }
    }
    /// Switches the font rendering mode to ignore all unrenderable characters
    /// instead of raising an error.
    ///
    /// By default, unknown chars will return an error.
    ///
    /// # Arguments
    ///
    /// * `ignore` - Whether unknown characters should be ignored.
    pub const fn with_ignore_unknown_chars(mut self, ignore: bool) -> Self {
        self.font = self.font.with_ignore_unknown_glyphs(ignore);
        self
    }
    /// Sets the line height.
    ///
    /// The line height is defined as the vertical distance between the baseline of two adjacent lines in pixels.
    ///
    /// If the line height is not set explicitly, it will default to the value returned by [`get_default_line_height()`](FontRenderer::get_default_line_height).
    ///
    /// # Arguments
    ///
    /// * `line_height` - The desired line height, in pixels.
    pub const fn with_line_height(mut self, line_height: u32) -> Self {
        self.font = self.font.with_line_height(line_height);
        self
    }
    /// Renders text to a display.
    ///
    /// Note that the background color is optional. Omitting it will render
    /// the string with a transparent background.
    ///
    /// Not every font supports a background color, some fonts require a transparent background.
    ///
    /// # Arguments
    ///
    /// * `content` - The text/character to render.
    /// * `position` - The position to render to.
    /// * `color` - The font color.
    /// * `vertical_pos` - The vertical positioning.
    /// * `display` - The display to render to.
    ///
    /// # Return
    ///
    /// The dimensions of the rendered text.
    /// The advance might be two-dimensional, as newlines change the y position.
    ///
    pub fn render<Display>(
        &self,
        content: impl Content,
        mut position: Point,
        vertical_pos: VerticalPosition,
        color: FontColor<Display::Color>,
        display: &mut Display,
    ) -> Result<RenderedDimensions, Error<Display::Error>>
    where
        Display: DrawTarget,
    {
        let font = &self.font;
        if color.has_background() && !font.supports_background_color {
            return Err(Error::BackgroundColorNotSupported);
        }
        let mut advance = Point::new(0, 0);
        let mut bounding_box = None;
        position.y += content.compute_vertical_offset(font, vertical_pos);
        content.for_each_char(|ch| -> Result<(), Error<Display::Error>> {
            if ch == '\n' {
                advance.x = 0;
                advance.y += i32::try_from(font.line_height).unwrap();
            } else {
                let dimensions = render_glyph(ch, position + advance, color, font, display)?;
                advance += dimensions.advance;
                bounding_box = combine_bounding_boxes(bounding_box, dimensions.bounding_box);
            }
            Ok(())
        })?;
        Ok(RenderedDimensions {
            advance,
            bounding_box,
        })
    }
    /// Renders text to a display with horizontal alignment.
    ///
    /// The `Left` alignment is identical to [`render()`](crate::FontRenderer::render).
    ///
    /// # Arguments
    ///
    /// * `content` - The text/character to render.
    /// * `position` - The position to render to.
    /// * `color` - The font color.
    /// * `vertical_pos` - The vertical positioning.
    /// * `horizontal_align` - The horizontal positioning.
    /// * `display` - The display to render to.
    ///
    /// # Return
    ///
    /// The bounding box of the rendered text.
    ///
    /// Does not return an advance value like [`render()`](crate::FontRenderer::render),
    /// as due to the alignment it would be meaningless.
    ///
    ///
    pub fn render_aligned<Display>(
        &self,
        content: impl Content,
        mut position: Point,
        vertical_pos: VerticalPosition,
        horizontal_align: HorizontalAlignment,
        color: FontColor<Display::Color>,
        display: &mut Display,
    ) -> Result<Option<Rectangle>, Error<Display::Error>>
    where
        Display: DrawTarget,
    {
        // If `horizontal_align` is `Left`, it is identical to
        // `render()`. As `render()` is quite a bit faster,
        // forward this call.
        if let HorizontalAlignment::Left = horizontal_align {
            position.x += compute_horizontal_offset(
                HorizontalAlignment::Left,
                HorizontalRenderedDimensions::empty(),
            );
            return self
                .render(content, position, vertical_pos, color, display)
                .map(|dims| dims.bounding_box);
        }
        // This function is a little more complicated.
        // To properly align horizontally, we need to iterate over every line twice.
        // This is really hard with format_args.
        // Therefore we introduce a line_dimensions_iterator that is almost no overhead for
        // glyphs/lines, but makes it possible to implement the format_args case.
        let font = &self.font;
        if color.has_background() && !font.supports_background_color {
            return Err(Error::BackgroundColorNotSupported);
        }
        position.y += content.compute_vertical_offset(font, vertical_pos);
        let mut bounding_box = None;
        let mut line_dimensions = content.line_dimensions_iterator();
        let mut advance = Point::new(
            compute_horizontal_offset(horizontal_align, line_dimensions.next(font)?),
            0,
        );
        content.for_each_char(|ch| -> Result<(), Error<Display::Error>> {
            if ch == '\n' {
                advance.x =
                    compute_horizontal_offset(horizontal_align, line_dimensions.next(font)?);
                advance.y += i32::try_from(font.line_height).unwrap();
            } else {
                let dimensions = render_glyph(ch, position + advance, color, font, display)?;
                advance += dimensions.advance;
                bounding_box = combine_bounding_boxes(bounding_box, dimensions.bounding_box);
            }
            Ok(())
        })?;
        Ok(bounding_box)
    }
    /// Calculates the dimensions that rendering text with [`render()`](crate::FontRenderer::render) would produce.
    ///
    /// # Arguments
    ///
    /// * `content` - The text/character to render.
    /// * `position` - The position to render to.
    /// * `vertical_pos` - The vertical positioning.
    ///
    /// # Return
    ///
    /// The dimensions of the rendered text.
    ///
    pub fn get_rendered_dimensions(
        &self,
        content: impl Content,
        mut position: Point,
        vertical_pos: VerticalPosition,
    ) -> Result<RenderedDimensions, LookupError> {
        let font = &self.font;
        let mut advance = Point::new(0, 0);
        let mut bounding_box = None;
        position.y += content.compute_vertical_offset(font, vertical_pos);
        content.for_each_char(|ch| -> Result<(), LookupError> {
            if ch == '\n' {
                advance.x = 0;
                advance.y += i32::try_from(font.line_height).unwrap();
            } else {
                let dimensions = compute_glyph_dimensions(ch, position + advance, font)?;
                advance += dimensions.advance;
                bounding_box = combine_bounding_boxes(bounding_box, dimensions.bounding_box);
            }
            Ok(())
        })?;
        Ok(RenderedDimensions {
            advance,
            bounding_box,
        })
    }
    /// Calculates the dimensions that rendering text with
    /// [`render_aligned()`](crate::FontRenderer::render_aligned) would produce.
    ///
    /// # Arguments
    ///
    /// * `content` - The text/character to render.
    /// * `position` - The position to render to.
    /// * `vertical_pos` - The vertical positioning.
    /// * `horizontal_align` - The horizontal alignment.
    ///
    /// # Return
    ///
    /// The bounding box of the rendered text.
    ///
    pub fn get_rendered_dimensions_aligned(
        &self,
        content: impl Content,
        mut position: Point,
        vertical_pos: VerticalPosition,
        horizontal_align: HorizontalAlignment,
    ) -> Result<Option<Rectangle>, LookupError> {
        let font = &self.font;
        position.y += content.compute_vertical_offset(font, vertical_pos);
        let mut bounding_box = None;
        let mut line_advance = 0;
        let mut line_bounding_box: Option<Rectangle> = None;
        content.for_each_char(|ch| -> Result<(), LookupError> {
            if ch == '\n' {
                let horizontal_offset = compute_horizontal_offset(
                    horizontal_align,
                    HorizontalRenderedDimensions {
                        advance: line_advance,
                        bounding_box_width: line_bounding_box.map_or(0, |b| b.size.width),
                        bounding_box_offset: line_bounding_box.map_or(0, |b| b.top_left.x),
                    },
                );
                // 'render' by moving the already known bounding box to the correct position
                if let Some(mut line_bounding_box) = line_bounding_box {
                    line_bounding_box.top_left.x += horizontal_offset;
                    line_bounding_box.top_left += position;
                    bounding_box = combine_bounding_boxes(bounding_box, Some(line_bounding_box));
                }
                line_advance = 0;
                line_bounding_box = None;
                position.y += i32::try_from(font.line_height).unwrap();
            } else {
                let dimensions = compute_glyph_dimensions(ch, Point::new(line_advance, 0), font)?;
                line_bounding_box =
                    combine_bounding_boxes(line_bounding_box, dimensions.bounding_box);
                line_advance += dimensions.advance.x;
            }
            Ok(())
        })?;
        // One last pass, if the string didn't end with a newline
        let horizontal_offset = compute_horizontal_offset(
            horizontal_align,
            HorizontalRenderedDimensions {
                advance: line_advance,
                bounding_box_width: line_bounding_box.map_or(0, |b| b.size.width),
                bounding_box_offset: line_bounding_box.map_or(0, |b| b.top_left.x),
            },
        );
        if let Some(mut line_bounding_box) = line_bounding_box {
            line_bounding_box.top_left.x += horizontal_offset;
            line_bounding_box.top_left += position;
            bounding_box = combine_bounding_boxes(bounding_box, Some(line_bounding_box));
        }
        Ok(bounding_box)
    }
    /// The ascent of the font.
    ///
    /// Usually a positive number.
    pub const fn get_ascent(&self) -> i8 {
        self.font.ascent
    }
    /// The descent of the font.
    ///
    /// *IMPORTANT*: This is usually a *negative* number.
    pub const fn get_descent(&self) -> i8 {
        self.font.descent
    }
    /// The maximum possible bounding box of all glyphs if they were rendered with
    /// [`render()`](crate::FontRenderer::render) at position `(0,0)`.
    pub const fn get_font_bounding_box(&self, vertical_pos: VerticalPosition) -> Rectangle {
        let y_offset = compute_vertical_offset_from_static_newlines(&self.font, vertical_pos, 0);
        Rectangle {
            top_left: Point::new(
                self.font.font_bounding_box_x_offset as i32,
                y_offset
                    - (self.font.font_bounding_box_height as i32
                        + self.font.font_bounding_box_y_offset as i32),
            ),
            size: Size::new(
                self.font.font_bounding_box_width as u32,
                self.font.font_bounding_box_height as u32,
            ),
        }
    }
    /// The default line height.
    ///
    /// The line height is defined as the vertical distance between the baseline of two adjacent lines in pixels.
    pub const fn get_default_line_height(&self) -> u32 {
        self.font.get_default_line_height() as u32
    }
}
#[cfg(test)]
mod tests {
    extern crate std;
    use std::println;
    use super::*;
    #[test]
    fn implements_debug() {
        println!(
            "{:?}",
            FontRenderer::new::<crate::fonts::u8g2_font_u8glib_4_tf>()
        );
    }
}