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
use crate::{
    draw_target::DrawTarget, geometry::Dimensions, iterator::ContiguousIteratorExt,
    pixelcolor::BinaryColor, primitives::Rectangle, Pixel,
};

pub struct MonoFontDrawTarget<'a, T, C> {
    parent: &'a mut T,
    colors: C,
}

impl<'a, T: DrawTarget, C> MonoFontDrawTarget<'a, T, C> {
    pub fn new(parent: &'a mut T, colors: C) -> Self {
        Self { parent, colors }
    }
}

impl<T: DrawTarget> DrawTarget for MonoFontDrawTarget<'_, T, Foreground<T::Color>> {
    type Color = BinaryColor;
    type Error = T::Error;

    fn fill_contiguous<I>(&mut self, area: &Rectangle, colors: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Self::Color>,
    {
        let foreground_color = self.colors.0;

        self.parent.draw_iter(
            colors
                .into_iter()
                .into_pixels(area)
                .filter(|Pixel(_, color)| color.is_on())
                .map(|Pixel(pos, _)| Pixel(pos, foreground_color)),
        )
    }

    fn draw_iter<I>(&mut self, _pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        unreachable!()
    }

    fn fill_solid(&mut self, area: &Rectangle, color: Self::Color) -> Result<(), Self::Error> {
        match color {
            BinaryColor::On => self.parent.fill_solid(area, self.colors.0),
            BinaryColor::Off => Ok(()),
        }
    }

    fn clear(&mut self, _color: Self::Color) -> Result<(), Self::Error> {
        unreachable!()
    }
}

impl<T: DrawTarget> DrawTarget for MonoFontDrawTarget<'_, T, Background<T::Color>> {
    type Color = BinaryColor;
    type Error = T::Error;

    fn fill_contiguous<I>(&mut self, area: &Rectangle, colors: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Self::Color>,
    {
        let foreground_color = self.colors.0;

        self.parent.draw_iter(
            colors
                .into_iter()
                .into_pixels(area)
                .filter(|Pixel(_, color)| color.is_off())
                .map(|Pixel(pos, _)| Pixel(pos, foreground_color)),
        )
    }

    fn draw_iter<I>(&mut self, _pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        unreachable!()
    }

    fn fill_solid(&mut self, area: &Rectangle, color: Self::Color) -> Result<(), Self::Error> {
        match color {
            BinaryColor::On => Ok(()),
            BinaryColor::Off => self.parent.fill_solid(area, self.colors.0),
        }
    }

    fn clear(&mut self, _color: Self::Color) -> Result<(), Self::Error> {
        unreachable!()
    }
}

impl<T: DrawTarget> DrawTarget for MonoFontDrawTarget<'_, T, Both<T::Color>> {
    type Color = BinaryColor;
    type Error = T::Error;

    fn fill_contiguous<I>(&mut self, area: &Rectangle, colors: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Self::Color>,
    {
        let foreground_color = self.colors.0;
        let background_color = self.colors.1;

        self.parent.fill_contiguous(
            area,
            colors.into_iter().map(|color| match color {
                BinaryColor::Off => background_color,
                BinaryColor::On => foreground_color,
            }),
        )
    }

    fn draw_iter<I>(&mut self, _pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        unreachable!()
    }

    fn fill_solid(&mut self, area: &Rectangle, color: Self::Color) -> Result<(), Self::Error> {
        match color {
            BinaryColor::On => self.parent.fill_solid(area, self.colors.0),
            BinaryColor::Off => self.parent.fill_solid(area, self.colors.1),
        }
    }

    fn clear(&mut self, _color: Self::Color) -> Result<(), Self::Error> {
        unreachable!()
    }
}

impl<T: DrawTarget, C> Dimensions for MonoFontDrawTarget<'_, T, C> {
    fn bounding_box(&self) -> Rectangle {
        self.parent.bounding_box()
    }
}

pub struct Foreground<C>(pub C);
pub struct Background<C>(pub C);
pub struct Both<C>(pub C, pub C);