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
use std::fmt::Write;

/// Holds parameters for the SuperTitle
pub struct SuperTitleParams {
    /// The x location of the text in figure coordinates (default = 0.5)
    x: Option<f64>,

    /// The y location of the text in figure coordinates (default = 0.98)
    y: Option<f64>,

    /// The horizontal alignment of the text relative to (x, y) (default = "center")
    ///
    /// Options: "center", "left", "right"
    align_horizontal: String,

    /// The vertical alignment of the text relative to (x, y) (default = "top")
    ///
    /// Options: "top", "center", "bottom", "baseline"
    align_vertical: String,

    /// The font size of the text
    fontsize: f64,

    /// The font weight of the text
    fontweight: f64,
}

impl SuperTitleParams {
    /// Allocates a new instance
    pub fn new() -> Self {
        SuperTitleParams {
            x: None,
            y: None,
            align_horizontal: String::new(),
            align_vertical: String::new(),
            fontsize: 0.0,
            fontweight: 0.0,
        }
    }

    /// Sets the x location of the text in figure coordinates (default = 0.5)
    pub fn set_x(&mut self, value: f64) -> &mut Self {
        self.x = Some(value);
        self
    }

    /// Sets the y location of the text in figure coordinates (default = 0.98)
    pub fn set_y(&mut self, value: f64) -> &mut Self {
        self.y = Some(value);
        self
    }

    /// Sets the horizontal alignment of the text relative to (x, y) (default = "center")
    ///
    /// Options: "center", "left", "right"
    pub fn set_align_horizontal(&mut self, value: &str) -> &mut Self {
        self.align_horizontal = String::from(value);
        self
    }

    /// Sets the vertical alignment of the text relative to (x, y) (default = "top")
    ///
    /// Options: "top", "center", "bottom", "baseline"
    pub fn set_align_vertical(&mut self, value: &str) -> &mut Self {
        self.align_vertical = String::from(value);
        self
    }

    /// Sets the font size of the text
    pub fn set_fontsize(&mut self, value: f64) -> &mut Self {
        self.fontsize = value;
        self
    }

    /// Sets the font weight of the text
    pub fn set_fontweight(&mut self, value: f64) -> &mut Self {
        self.fontweight = value;
        self
    }

    /// Returns options for SuperTitle
    pub(crate) fn options(&self) -> String {
        let mut opt = String::new();
        if let Some(v) = self.x {
            write!(&mut opt, ",x={}", v).unwrap()
        }
        if let Some(v) = self.y {
            write!(&mut opt, ",y={}", v).unwrap()
        }
        if self.align_horizontal != "" {
            write!(&mut opt, ",ha='{}'", self.align_horizontal).unwrap()
        }
        if self.align_vertical != "" {
            write!(&mut opt, ",va='{}'", self.align_vertical).unwrap()
        }
        if self.fontsize > 0.0 {
            write!(&mut opt, ",fontsize={}", self.fontsize).unwrap()
        }
        if self.fontweight > 0.0 {
            write!(&mut opt, ",fontweight={}", self.fontweight).unwrap()
        }
        opt
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::SuperTitleParams;

    #[test]
    fn new_works() {
        let params = SuperTitleParams::new();
        assert_eq!(params.x, None);
        assert_eq!(params.y, None);
        assert_eq!(params.align_horizontal.len(), 0);
        assert_eq!(params.align_vertical.len(), 0);
        assert_eq!(params.fontsize, 0.0);
        assert_eq!(params.fontweight, 0.0);
    }

    #[test]
    fn options_works() {
        let mut params = SuperTitleParams::new();
        params
            .set_x(0.6)
            .set_y(0.8)
            .set_align_horizontal("center")
            .set_align_vertical("center")
            .set_fontsize(8.0)
            .set_fontweight(10.0);
        let opt = params.options();
        assert_eq!(
            opt,
            ",x=0.6\
             ,y=0.8\
             ,ha='center'\
             ,va='center'\
             ,fontsize=8\
             ,fontweight=10"
        );
    }
}