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
//! Whitespace rendering

use core::{
    fmt::{Debug, Formatter, Result},
    marker::PhantomData,
    mem::MaybeUninit,
};
use embedded_graphics::{prelude::*, style::TextStyle};

/// Pixel iterator to render font spacing
#[derive(Clone)]
pub struct EmptySpaceIterator<C, F>
where
    C: PixelColor,
    F: Font + Copy,
{
    _font: PhantomData<F>,
    color: MaybeUninit<C>,
    pos: Point,
    char_walk: Point,
    walk_max_x: i32,
}

impl<C, F> Debug for EmptySpaceIterator<C, F>
where
    C: PixelColor,
    F: Font + Copy,
{
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        // Manual Debug implementation is necessary because MaybeUninit is only Debug in >=1.41.0
        f.debug_struct(&core::any::type_name::<Self>())
            .field("_font", &self._font)
            .field("color", &core::any::type_name::<C>())
            .field("pos", &self.pos)
            .field("char_walk", &self.char_walk)
            .field("walk_max_x", &self.walk_max_x)
            .finish()
    }
}

impl<C, F> EmptySpaceIterator<C, F>
where
    C: PixelColor,
    F: Font + Copy,
{
    /// Creates a new pixel iterator to draw empty spaces.
    #[inline]
    #[must_use]
    pub fn new(width: u32, position: Point, style: TextStyle<C, F>) -> Self {
        if width == 0 || style.background_color.is_none() {
            Self {
                _font: PhantomData,
                color: MaybeUninit::uninit(),
                pos: Point::zero(),
                char_walk: Point::zero(),
                walk_max_x: 0,
            }
        } else {
            let walk_max_x = position.x + width as i32 - 1;
            let walk_max_y = position.y + F::CHARACTER_SIZE.height as i32;

            Self {
                _font: PhantomData,
                color: MaybeUninit::new(style.background_color.unwrap()),
                pos: Point::new(position.x, walk_max_y),
                char_walk: position,
                walk_max_x,
            }
        }
    }
}

impl<C, F> Iterator for EmptySpaceIterator<C, F>
where
    C: PixelColor,
    F: Font + Copy,
{
    type Item = Pixel<C>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let walk = self.char_walk;
        if walk.y < self.pos.y {
            if walk.x < self.walk_max_x {
                self.char_walk.x += 1;
            } else {
                self.char_walk.x = self.pos.x;
                self.char_walk.y += 1;
            }

            // Skip to next point if pixel is transparent
            Some(Pixel(walk, unsafe {
                // this is safe because if not initialized,
                // coordinates are set to never hit this line
                self.color.assume_init()
            }))
        } else {
            // Done with filling this space
            None
        }
    }
}

#[cfg(test)]
mod test {
    use super::EmptySpaceIterator;
    use embedded_graphics::{
        fonts::{Font6x6, Font6x8},
        pixelcolor::BinaryColor,
        prelude::*,
        style::TextStyleBuilder,
    };

    #[test]
    fn zero_width_does_not_render_anything() {
        let style = TextStyleBuilder::new(Font6x8)
            .background_color(BinaryColor::On)
            .build();

        assert_eq!(0, EmptySpaceIterator::new(0, Point::zero(), style).count());
    }

    #[test]
    fn transparent_background_does_not_render_anything() {
        let style = TextStyleBuilder::new(Font6x8)
            .text_color(BinaryColor::On)
            .build();

        assert_eq!(0, EmptySpaceIterator::new(10, Point::zero(), style).count());
    }

    #[test]
    fn first_point_in_position() {
        let style = TextStyleBuilder::new(Font6x8)
            .background_color(BinaryColor::On)
            .build();

        let pos = Point::new(8, 6);
        assert_eq!(
            pos,
            EmptySpaceIterator::new(10, pos, style).next().unwrap().0
        );
    }

    #[test]
    fn minimal_number_of_pixels_returned() {
        let style = TextStyleBuilder::new(Font6x8)
            .text_color(BinaryColor::On)
            .background_color(BinaryColor::Off)
            .build();

        assert_eq!(
            80,
            EmptySpaceIterator::new(10, Point::zero(), style).count()
        );

        let style = TextStyleBuilder::new(Font6x6)
            .text_color(BinaryColor::On)
            .background_color(BinaryColor::Off)
            .build();

        assert_eq!(
            60,
            EmptySpaceIterator::new(10, Point::zero(), style).count()
        );
    }
}