Skip to main content

multi_mono_font/
mono_image.rs

1use crate::draw_target::MultiMonoFontDrawTarget;
2use embedded_graphics::{
3    geometry::OriginDimensions,
4    image::{ImageDrawable, ImageRaw},
5    pixelcolor::BinaryColor,
6    prelude::{DrawTarget, PixelColor, Size},
7    primitives::Rectangle,
8};
9
10/// A static binary image drawable.
11///
12/// The image is drawn with the foreground color where the pixel is set to `BinaryColor::On`,
13/// and with the background color where the pixel is set to `BinaryColor::Off`.
14#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
15#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
16pub struct MonoImage<'a, C> {
17    image: &'a ImageRaw<'a, BinaryColor>,
18    foreground_color: C,
19    background_color: Option<C>,
20}
21
22impl<'a, C> MonoImage<'a, C> {
23    /// Creates a text drawable with the default text style.
24    pub const fn new(image: &'a ImageRaw<'a, BinaryColor>, foreground_color: C) -> Self {
25        Self {
26            image,
27            foreground_color,
28            background_color: None,
29        }
30    }
31
32    /// Sets the background color.
33    pub fn with_background_color(mut self, background_color: C) -> Self {
34        self.background_color = Some(background_color);
35        self
36    }
37}
38
39impl<C> OriginDimensions for MonoImage<'_, C>
40where
41    C: PixelColor + From<<C as PixelColor>::Raw>,
42{
43    fn size(&self) -> Size {
44        self.image.size()
45    }
46}
47
48impl<'a, C> ImageDrawable for MonoImage<'a, C>
49where
50    C: PixelColor + From<<C as PixelColor>::Raw>,
51{
52    type Color = C;
53
54    fn draw<D>(&self, target: &mut D) -> Result<(), D::Error>
55    where
56        D: DrawTarget<Color = C>,
57    {
58        let mut bin_target =
59            MultiMonoFontDrawTarget::new(target, self.foreground_color, self.background_color);
60
61        self.image.draw(&mut bin_target)
62    }
63
64    fn draw_sub_image<D>(&self, target: &mut D, area: &Rectangle) -> Result<(), D::Error>
65    where
66        D: DrawTarget<Color = Self::Color>,
67    {
68        let mut bin_target =
69            MultiMonoFontDrawTarget::new(target, self.foreground_color, self.background_color);
70
71        self.image.draw_sub_image(&mut bin_target, area)
72    }
73}