#[derive(Debug, Clone)]
pub struct OfdGraphics2DDrawParam {
pub stroke_color: Option<u32>,
pub fill_color: Option<u32>,
pub line_width: f64,
pub opacity: f64,
pub font_size: f64,
pub font_name: Option<String>,
}
impl OfdGraphics2DDrawParam {
#[must_use]
pub fn new() -> Self {
Self {
stroke_color: None,
fill_color: None,
line_width: 1.0,
opacity: 1.0,
font_size: 12.0,
font_name: None,
}
}
#[must_use]
pub fn stroke_color(mut self, color: u32) -> Self {
self.stroke_color = Some(color);
self
}
#[must_use]
pub fn fill_color(mut self, color: u32) -> Self {
self.fill_color = Some(color);
self
}
#[must_use]
pub fn line_width(mut self, width: f64) -> Self {
self.line_width = width;
self
}
#[must_use]
pub fn opacity(mut self, opacity: f64) -> Self {
self.opacity = opacity.clamp(0.0, 1.0);
self
}
#[must_use]
pub fn font_size(mut self, size: f64) -> Self {
self.font_size = size;
self
}
#[must_use]
pub fn font_name(mut self, name: impl Into<String>) -> Self {
self.font_name = Some(name.into());
self
}
}
impl Default for OfdGraphics2DDrawParam {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_defaults() {
let p = OfdGraphics2DDrawParam::new();
assert!(p.stroke_color.is_none());
assert!(p.fill_color.is_none());
assert!((p.line_width - 1.0).abs() < f64::EPSILON);
assert!((p.opacity - 1.0).abs() < f64::EPSILON);
assert!((p.font_size - 12.0).abs() < f64::EPSILON);
assert!(p.font_name.is_none());
}
#[test]
fn test_builder() {
let p = OfdGraphics2DDrawParam::new()
.stroke_color(0xFF_0000)
.fill_color(0x00_FF00)
.line_width(2.5)
.opacity(0.8)
.font_size(14.0)
.font_name("SimSun");
assert_eq!(p.stroke_color, Some(0xFF_0000));
assert_eq!(p.fill_color, Some(0x00_FF00));
assert!((p.line_width - 2.5).abs() < f64::EPSILON);
assert!((p.opacity - 0.8).abs() < f64::EPSILON);
assert_eq!(p.font_name.as_deref(), Some("SimSun"));
}
#[test]
fn test_opacity_clamp() {
let p = OfdGraphics2DDrawParam::new().opacity(1.5);
assert!((p.opacity - 1.0).abs() < f64::EPSILON);
let p2 = OfdGraphics2DDrawParam::new().opacity(-0.5);
assert!((p2.opacity - 0.0).abs() < f64::EPSILON);
}
#[test]
fn test_default() {
let p = OfdGraphics2DDrawParam::default();
assert!((p.line_width - 1.0).abs() < f64::EPSILON);
}
}