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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Textbox styling.
use embedded_graphics::{prelude::*, style::TextStyle};

pub mod builder;

use crate::{
    alignment::{HorizontalTextAlignment, VerticalTextAlignment},
    parser::{Parser, Token},
    rendering::{StateFactory, StyledTextBoxIterator},
    utils::{font_ext::FontExt, rect_ext::RectExt},
    TextBox,
};
pub use builder::TextBoxStyleBuilder;

/// Styling options of a [`TextBox`].
///
/// `TextBoxStyle` contains the `Font`, foreground and background `PixelColor`,
/// [`HorizontalTextAlignment`] and [`VerticalTextAlignment`] information necessary to draw a
/// [`TextBox`].
///
/// To construct a new `TextBoxStyle` object, use the [`new`] or [`from_text_style`] methods or
/// the [`TextBoxStyleBuilder`] object.
///
/// [`TextBox`]: ../struct.TextBox.html
/// [`HorizontalTextAlignment`]: ../alignment/trait.HorizontalTextAlignment.html
/// [`VerticalTextAlignment`]: ../alignment/trait.VerticalTextAlignment.html
/// [`TextBoxStyleBuilder`]: builder/struct.TextBoxStyleBuilder.html
/// [`new`]: #method.new
/// [`from_text_style`]: #method.from_text_style
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct TextBoxStyle<C, F, A, V>
where
    C: PixelColor,
    F: Font + Copy,
    A: HorizontalTextAlignment,
    V: VerticalTextAlignment,
{
    /// Style properties for text.
    pub text_style: TextStyle<C, F>,

    /// Horizontal text alignment.
    pub alignment: A,

    /// Vertical text alignment.
    pub vertical_alignment: V,
}

impl<C, F, A, V> TextBoxStyle<C, F, A, V>
where
    C: PixelColor,
    F: Font + Copy,
    A: HorizontalTextAlignment,
    V: VerticalTextAlignment,
{
    /// Creates a `TextBoxStyle` object with transparent background.
    #[inline]
    pub fn new(font: F, text_color: C, alignment: A, vertical_alignment: V) -> Self {
        Self {
            text_style: TextStyle::new(font, text_color),
            alignment,
            vertical_alignment,
        }
    }

    /// Creates a `TextBoxStyle` object from the given text style and alignment.
    #[inline]
    pub fn from_text_style(
        text_style: TextStyle<C, F>,
        alignment: A,
        vertical_alignment: V,
    ) -> Self {
        Self {
            text_style,
            alignment,
            vertical_alignment,
        }
    }

    /// Returns the size of a token if it fits the line, or the max size that fits and the remaining
    /// unprocessed part.
    fn measure_word(w: &str, max_width: u32) -> (u32, Option<&str>) {
        let (width, consumed) = F::max_str_width_nocr(w, max_width);
        let carried = if consumed == w {
            None
        } else {
            Some(unsafe {
                // consumed is the first part of w, so it's length must be
                // on char boundary
                w.get_unchecked(consumed.len()..)
            })
        };

        (width, carried)
    }

    /// Counts the number of printed whitespaces in a word
    fn count_printed_spaces(w: &str) -> u32 {
        w.chars().filter(|&c| c == '\u{A0}').count() as u32
    }

    /// Measure the width and count spaces in a single line of text.
    ///
    /// Returns (width, rendered space count, carried token)
    ///
    /// Instead of peeking ahead when processing tokens, this function advances the parser before
    /// processing a token. If a token opens a new line, it will be returned as the carried token.
    /// If the carried token is `None`, the parser has finished processing the text.
    #[inline]
    #[must_use]
    pub fn measure_line<'a>(
        &self,
        parser: &mut Parser<'a>,
        carried_token: Option<Token<'a>>,
        max_line_width: u32,
    ) -> (u32, u32, Option<Token<'a>>) {
        let mut current_width = 0;
        let mut last_spaces = 0;
        let mut last_space_width = 0;
        let mut is_first_word = true;

        if let Some(t) = carried_token {
            match t {
                Token::Word(w) => {
                    let (width, carried) = Self::measure_word(w, max_line_width);

                    if let Some(w) = carried {
                        let spaces = Self::count_printed_spaces(w);
                        return (width, spaces, Some(Token::Word(w)));
                    }

                    is_first_word = false;
                    current_width = width;
                }

                Token::Whitespace(n) => {
                    if A::STARTING_SPACES {
                        let (width, consumed) = F::max_space_width(n, max_line_width);
                        let carried = n - consumed;

                        if carried != 0 {
                            let token = Some(Token::Whitespace(carried - 1));
                            return if A::ENDING_SPACES {
                                (width, consumed, token)
                            } else {
                                (0, 0, token)
                            };
                        }

                        last_spaces = n;
                        last_space_width = width;
                    }
                }

                Token::NewLine => {
                    // eat the newline
                }

                Token::CarriageReturn => {
                    // eat the \r since it's meaningless in the beginning of a line
                }
            }
        }

        let mut total_spaces = 0;
        let mut carried_token = None;
        for token in parser {
            let available_space = max_line_width - current_width - last_space_width;
            match token {
                Token::Word(w) => {
                    let (width, carried) = Self::measure_word(w, available_space);

                    if let Some(carried_w) = carried {
                        let carried_word = if is_first_word {
                            if width != 0 {
                                let spaces =
                                    Self::count_printed_spaces(&w[0..w.len() - carried_w.len()]);
                                current_width += last_space_width + width;
                                total_spaces += last_spaces + spaces;
                            }
                            // first word; break word into parts
                            carried_w
                        } else {
                            // carry the whole word
                            w
                        };
                        carried_token.replace(Token::Word(carried_word));
                        break;
                    }

                    let spaces = Self::count_printed_spaces(w);
                    // If there's no carried token, width != 0 assuming there are no empty words
                    current_width += last_space_width + width;
                    total_spaces += last_spaces + spaces;

                    is_first_word = false;
                    last_space_width = 0;
                    last_spaces = 0;
                }

                Token::Whitespace(n) => {
                    if A::STARTING_SPACES || !is_first_word {
                        let (width, mut consumed) = F::max_space_width(n, available_space);

                        // update before breaking, so that ENDING_SPACES can use data
                        last_spaces += n;
                        last_space_width += width;

                        if n != consumed {
                            if A::ENDING_SPACES {
                                consumed += 1;
                            }
                            carried_token.replace(Token::Whitespace(n - consumed));
                            break;
                        }
                    }
                }

                Token::NewLine | Token::CarriageReturn => {
                    carried_token.replace(token);
                    break;
                }
            }
        }
        if A::ENDING_SPACES {
            total_spaces += last_spaces;
            current_width += last_space_width;
        }
        (current_width, total_spaces, carried_token)
    }

    /// Measures text height when rendered using a given width.
    ///
    /// # Example: measure height of text when rendered using a 6x8 font and 72px width.
    ///
    /// ```rust
    /// # use embedded_text::style::builder::TextBoxStyleBuilder;
    /// # use embedded_graphics::fonts::Font6x8;
    /// # use embedded_graphics::pixelcolor::BinaryColor;
    /// #
    /// let style = TextBoxStyleBuilder::new(Font6x8)
    ///     .text_color(BinaryColor::On)
    ///     .build();
    ///
    /// let height = style.measure_text_height(
    ///     "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
    ///     72,
    /// );
    ///
    /// // Expect 7 lines of text, wrapped in something like the following:
    ///
    /// // |Lorem Ipsum |
    /// // |is simply   |
    /// // |dummy text  |
    /// // |of the      |
    /// // |printing and|
    /// // |typesetting |
    /// // |industry.   |
    ///
    /// assert_eq!(7 * 8, height);
    /// ```
    #[inline]
    #[must_use]
    pub fn measure_text_height(&self, text: &str, max_width: u32) -> u32 {
        let mut current_height = 0;
        let mut parser = Parser::parse(text);
        let mut carry = None;
        let mut bytes = parser.remaining();

        loop {
            let (w, _, t) = self.measure_line(&mut parser, carry.clone(), max_width);
            let remaining = parser.remaining();
            // exit condition:
            // - no more tokens to process
            // - the same token is encountered twice
            if t.is_none() || (t == carry && bytes == remaining) {
                if w != 0 {
                    current_height += F::CHARACTER_SIZE.height;
                }
                return current_height;
            }
            if t != Some(Token::CarriageReturn) {
                current_height += F::CHARACTER_SIZE.height;
            }
            bytes = remaining;
            carry = t;
        }
    }
}

/// A styled [`TextBox`] struct.
///
/// This structure is constructed by calling the [`into_styled`] method of a [`TextBox`] object.
/// Use the [`draw`] method to draw the textbox on a display.
///
/// [`TextBox`]: ../struct.TextBox.html
/// [`into_styled`]: ../struct.TextBox.html#method.into_styled
/// [`draw`]: #method.draw
pub struct StyledTextBox<'a, C, F, A, V>
where
    C: PixelColor,
    F: Font + Copy,
    A: HorizontalTextAlignment,
    V: VerticalTextAlignment,
{
    /// A [`TextBox`] that has an associated [`TextBoxStyle`].
    ///
    /// [`TextBox`]: ../struct.TextBox.html
    /// [`TextBoxStyle`]: struct.TextBoxStyle.html
    pub text_box: TextBox<'a>,

    /// The style of the [`TextBox`].
    ///
    /// [`TextBox`]: ../struct.TextBox.html
    pub style: TextBoxStyle<C, F, A, V>,
}

impl<'a, C, F, A, V> Drawable<C> for &'a StyledTextBox<'a, C, F, A, V>
where
    C: PixelColor,
    F: Font + Copy,
    A: HorizontalTextAlignment,
    V: VerticalTextAlignment,
    StyledTextBoxIterator<'a, C, F, A, V>: Iterator<Item = Pixel<C>>,
    StyledTextBox<'a, C, F, A, V>: StateFactory<'a, F>,
{
    #[inline]
    fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
        display.draw_iter(StyledTextBoxIterator::new(self))
    }
}

impl<C, F, A, V> Transform for StyledTextBox<'_, C, F, A, V>
where
    C: PixelColor,
    F: Font + Copy,
    A: HorizontalTextAlignment,
    V: VerticalTextAlignment,
{
    #[inline]
    #[must_use]
    fn translate(&self, by: Point) -> Self {
        Self {
            text_box: self.text_box.translate(by),
            style: self.style,
        }
    }

    #[inline]
    fn translate_mut(&mut self, by: Point) -> &mut Self {
        self.text_box.bounds.translate_mut(by);

        self
    }
}

impl<C, F, A, V> Dimensions for StyledTextBox<'_, C, F, A, V>
where
    C: PixelColor,
    F: Font + Copy,
    A: HorizontalTextAlignment,
    V: VerticalTextAlignment,
{
    #[inline]
    #[must_use]
    fn top_left(&self) -> Point {
        self.text_box.bounds.top_left
    }

    #[inline]
    #[must_use]
    fn bottom_right(&self) -> Point {
        self.text_box.bounds.bottom_right
    }

    #[inline]
    #[must_use]
    fn size(&self) -> Size {
        RectExt::size(self.text_box.bounds)
    }
}

#[cfg(test)]
mod test {
    use crate::{alignment::*, parser::Parser, style::builder::TextBoxStyleBuilder};
    use embedded_graphics::{
        fonts::{Font, Font6x8},
        pixelcolor::BinaryColor,
    };

    #[test]
    fn test_measure_height() {
        let data = [
            ("", 0, 0),
            (" ", 0, 8),
            (" ", 5, 8),
            (" ", 6, 8),
            ("\n", 6, 8),
            ("\n ", 6, 16),
            ("word", 4 * 6, 8), // exact fit into 1 line
            ("word", 4 * 6 - 1, 16),
            ("word", 2 * 6, 16),      // exact fit into 2 lines
            ("word word", 4 * 6, 16), // exact fit into 2 lines
            ("word\n", 2 * 6, 16),
            ("word\nnext", 50, 16),
            ("word\n\nnext", 50, 24),
            ("word\n  \nnext", 50, 24),
            ("verylongword", 50, 16),
            ("some verylongword", 50, 24),
            ("1 23456 12345 61234 561", 36, 40),
            ("    Word      ", 36, 24),
            ("Longer\rnowrap", 36, 8),
        ];
        let textbox_style = TextBoxStyleBuilder::new(Font6x8)
            .text_color(BinaryColor::On)
            .build();
        for (i, (text, width, expected_height)) in data.iter().enumerate() {
            let height = textbox_style.measure_text_height(text, *width);
            assert_eq!(
                height, *expected_height,
                "#{}: Height of \"{}\" is {} but is expected to be {}",
                i, text, height, expected_height
            );
        }
    }

    #[test]
    fn test_measure_height_ignored_spaces() {
        let data = [
            ("", 0, 0),
            (" ", 0, 0),
            (" ", 6, 0),
            ("\n ", 6, 8),
            ("word\n", 2 * 6, 16),
            ("word\n  \nnext", 50, 24),
            ("    Word      ", 36, 8),
        ];
        let textbox_style = TextBoxStyleBuilder::new(Font6x8)
            .alignment(CenterAligned)
            .text_color(BinaryColor::On)
            .build();
        for (i, (text, width, expected_height)) in data.iter().enumerate() {
            let height = textbox_style.measure_text_height(text, *width);
            assert_eq!(
                height, *expected_height,
                "#{}: Height of \"{}\" is {} but is expected to be {}",
                i, text, height, expected_height
            );
        }
    }

    #[test]
    fn test_measure_line_counts_nbsp() {
        let textbox_style = TextBoxStyleBuilder::new(Font6x8)
            .alignment(CenterAligned)
            .text_color(BinaryColor::On)
            .build();

        let mut text = Parser::parse("123\u{A0}45");

        let (w, s, _) =
            textbox_style.measure_line(&mut text, None, 5 * Font6x8::CHARACTER_SIZE.width);
        assert_eq!(w, 5 * Font6x8::CHARACTER_SIZE.width);
        assert_eq!(s, 1);
    }

    #[test]
    fn test_measure_height_nbsp() {
        let textbox_style = TextBoxStyleBuilder::new(Font6x8)
            .alignment(CenterAligned)
            .text_color(BinaryColor::On)
            .build();

        let text = "123\u{A0}45 123";

        let height = textbox_style.measure_text_height(text, 5 * Font6x8::CHARACTER_SIZE.width);
        assert_eq!(height, 16);

        // bug discovered while using the interactive example
        let textbox_style = TextBoxStyleBuilder::new(Font6x8)
            .alignment(LeftAligned)
            .text_color(BinaryColor::On)
            .build();

        let text = "embedded-text also\u{A0}supports non-breaking spaces.";

        let height = textbox_style.measure_text_height(text, 79);
        assert_eq!(height, 4 * Font6x8::CHARACTER_SIZE.height);
    }
}