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
use core::ops::Range;

use crate::{draw_target::DrawTarget, primitives::common::Scanline};

/// Scanline with stroke and fill regions.
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub struct StyledScanline {
    y: i32,
    stroke_range: Range<i32>,
    fill_range: Range<i32>,
}

impl StyledScanline {
    /// Creates a new styled scanline.
    pub fn new(y: i32, stroke_range: Range<i32>, fill_range: Option<Range<i32>>) -> Self {
        let fill_range = fill_range.unwrap_or_else(|| stroke_range.end..stroke_range.end);

        Self {
            y,
            stroke_range,
            fill_range,
        }
    }

    /// Returns the stroke region on the left side.
    ///
    /// If the scanline contains no fill region the entire scanline will be returned.
    pub const fn stroke_left(&self) -> Scanline {
        Scanline::new(self.y, self.stroke_range.start..self.fill_range.start)
    }

    /// Returns the stroke region on the right side.
    ///
    /// If the scanline contains no fill region an empty scanline will be returned.
    pub const fn stroke_right(&self) -> Scanline {
        Scanline::new(self.y, self.fill_range.end..self.stroke_range.end)
    }

    /// Returns the fill region.
    pub fn fill(&self) -> Scanline {
        Scanline::new(self.y, self.fill_range.clone())
    }

    /// Draws the stroke regions.
    pub fn draw_stroke<T: DrawTarget>(
        &self,
        target: &mut T,
        stroke_color: T::Color,
    ) -> Result<(), T::Error> {
        self.stroke_left().draw(target, stroke_color)?;
        self.stroke_right().draw(target, stroke_color)
    }

    /// Draws the stroke and fill regions.
    pub fn draw_stroke_and_fill<T: DrawTarget>(
        &self,
        target: &mut T,
        stroke_color: T::Color,
        fill_color: T::Color,
    ) -> Result<(), T::Error> {
        self.stroke_left().draw(target, stroke_color)?;
        self.fill().draw(target, fill_color)?;
        self.stroke_right().draw(target, stroke_color)
    }
}