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
//! Options for drawing paths.

/// Options for drawing stroked lines.
/// Most of these are self explanatory, but some aren't.
///
/// `dash` has two parts. The Vec<f64> pattern array and an offset. The array
/// represents alternating lengths to be drawn and undrawn repeatedly. The offset
/// specifes how far into the pattern it should start. On platforms that do not
/// support an odd number of lengths in the array, the implementation may
/// concatenate two copies of the array to reach an even count.
///
/// `miter_limit` controls how corners are drawn when `line_join` is set to
/// Miter. Will draw corners as `Bevel` instead of `Miter` if the limit is
/// reached. See the reference below on how `miter_limit` is calculated.
///
/// See
/// https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/psrefman.pdf
/// for more information and examples
#[derive(Clone, PartialEq, Debug)]
pub struct StrokeStyle {
    pub line_join: Option<LineJoin>,
    pub line_cap: Option<LineCap>,
    pub dash: Option<(Vec<f64>, f64)>,
    pub miter_limit: Option<f64>,
}

/// Options for angled joins in strokes.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum LineJoin {
    Miter,
    Round,
    Bevel,
}

/// Options for the cap of stroked lines.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum LineCap {
    Butt,
    Round,
    Square,
}

impl StrokeStyle {
    #[allow(clippy::new_without_default)]
    pub fn new() -> StrokeStyle {
        StrokeStyle {
            line_join: None,
            line_cap: None,
            dash: None,
            miter_limit: None,
        }
    }

    /// Builder-style method to set the [`LineJoin`].
    ///
    /// [`LineJoin`]: enum.LineJoin.html
    pub fn line_join(mut self, line_join: LineJoin) -> Self {
        self.line_join = Some(line_join);
        self
    }

    /// Builder-style method to set the [`LineCap`].
    ///
    /// [`LineCap`]: enum.LineCap.html
    pub fn line_cap(mut self, line_cap: LineCap) -> Self {
        self.line_cap = Some(line_cap);
        self
    }

    /// Builder-style method to set the line dash.
    ///
    /// Dash style is represented as a vector of alternating on-off lengths,
    /// and an offset length.
    pub fn dash(mut self, dashes: Vec<f64>, offest: f64) -> Self {
        self.dash = Some((dashes, offest));
        self
    }

    /// Builder-style method to set the miter limit.
    pub fn miter_limit(mut self, miter_limit: f64) -> Self {
        self.miter_limit = Some(miter_limit);
        self
    }

    pub fn set_line_join(&mut self, line_join: LineJoin) {
        self.line_join = Some(line_join);
    }

    pub fn set_line_cap(&mut self, line_cap: LineCap) {
        self.line_cap = Some(line_cap);
    }

    pub fn set_dash(&mut self, dashes: Vec<f64>, offset: f64) {
        self.dash = Some((dashes, offset));
    }

    pub fn set_miter_limit(&mut self, miter_limit: f64) {
        self.miter_limit = Some(miter_limit);
    }
}