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
//! Font helper extensions.
//!
//! Extends font types with some helper methods.
use embedded_graphics::fonts::Font;

/// `Font` extensions
pub trait FontExt {
    /// Measures a sequence of characters in a line with a determinate maximum width.
    ///
    /// Returns the width of the characters that fit into the given space and whether or not all of
    /// the input fits into the given space.
    fn measure_line(line: &str, max_width: u32) -> LineMeasurement;

    /// Returns the total width of the character plus the character spacing.
    fn total_char_width(c: char) -> u32;

    /// Measure text width
    fn str_width(s: &str) -> u32;

    /// Measures a sequence of characters in a line with a determinate maximum width.
    ///
    /// Returns the width of the characters that fit into the given space and the processed string.
    fn max_str_width(s: &str, max_width: u32) -> (u32, &str);

    /// Measures a sequence of spaces in a line with a determinate maximum width.
    ///
    /// Returns the width of the spaces that fit into the given space and the number of spaces that
    /// fit.
    fn max_space_width(n: u32, max_width: u32) -> (u32, u32);
}

/// Result of a `measure_line` function call.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct LineMeasurement {
    /// The maximum width that still fits into the given width limit.
    pub width: u32,

    /// Whether or not the whole sequence fits into the given width limit.
    pub fits_line: bool,
}

impl LineMeasurement {
    /// Creates a new measurement result object.
    #[inline]
    #[must_use]
    pub const fn new(width: u32, fits_line: bool) -> Self {
        LineMeasurement { width, fits_line }
    }

    /// Creates a new measurement result object for an empty line.
    #[inline]
    #[must_use]
    pub const fn empty() -> Self {
        Self::new(0, true)
    }
}

impl<F> FontExt for F
where
    F: Font,
{
    #[inline]
    #[must_use]
    fn measure_line(line: &str, max_width: u32) -> LineMeasurement {
        let (width, processed) = Self::max_str_width(line, max_width);

        LineMeasurement::new(width, processed == line)
    }

    #[inline]
    fn total_char_width(c: char) -> u32 {
        F::char_width(c) + F::CHARACTER_SPACING
    }

    #[inline]
    fn str_width(s: &str) -> u32 {
        s.chars().map(F::total_char_width).sum::<u32>()
    }

    #[inline]
    #[must_use]
    fn max_str_width(s: &str, max_width: u32) -> (u32, &str) {
        let mut width = 0;
        for (idx, c) in s.char_indices() {
            let new_width = width + F::total_char_width(c);
            if new_width > max_width {
                return (width, unsafe { s.get_unchecked(0..idx) });
            } else {
                width = new_width;
            }
        }
        (width, s)
    }

    #[inline]
    #[must_use]
    fn max_space_width(n: u32, max_width: u32) -> (u32, u32) {
        let space_width = F::total_char_width(' ');
        let num_spaces = (max_width / space_width).min(n);

        (num_spaces * space_width, num_spaces)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use embedded_graphics::fonts::Font6x8;

    #[test]
    fn test_max_fitting_empty() {
        assert_eq!(Font6x8::measure_line("", 54), LineMeasurement::new(0, true));
        assert_eq!((0, ""), Font6x8::max_str_width("", 54));
    }

    #[test]
    fn test_max_fitting_exact() {
        let measurement = Font6x8::measure_line("somereall", 54);
        assert_eq!(measurement, LineMeasurement::new(54, true));
        assert_eq!((54, "somereall"), Font6x8::max_str_width("somereall", 54));
    }

    #[test]
    fn test_max_fitting_long_exact() {
        let measurement = Font6x8::measure_line("somereallylongword", 54);
        assert_eq!(measurement, LineMeasurement::new(54, false));
        assert_eq!(
            (54, "somereall"),
            Font6x8::max_str_width("somereallylongword", 54)
        );
    }

    #[test]
    fn test_max_fitting_long() {
        let measurement = Font6x8::measure_line("somereallylongword", 55);
        assert_eq!(measurement, LineMeasurement::new(54, false));
        assert_eq!(
            (54, "somereall"),
            Font6x8::max_str_width("somereallylongword", 55)
        );
    }

    #[test]
    fn test_max_space_width() {
        assert_eq!((0, 0), Font6x8::max_space_width(0, 36));
        assert_eq!((36, 6), Font6x8::max_space_width(6, 36));
        assert_eq!((36, 6), Font6x8::max_space_width(6, 36));
        assert_eq!((36, 6), Font6x8::max_space_width(6, 38));
        assert_eq!((36, 6), Font6x8::max_space_width(7, 36));
    }
}