Skip to main content

easyofd_core/model/
path_object.rs

1//! 路径对象。
2
3/// 矢量路径对象(直线、矩形、曲线)。
4#[derive(Debug, Clone)]
5pub struct PathObject {
6    /// 距左边缘的 X 位置(mm)。
7    pub x: f64,
8    /// 距顶部的 Y 位置(mm)。
9    pub y: f64,
10    /// 描边颜色(RGB 十六进制)。
11    pub stroke_color: u32,
12    /// 描边宽度(mm)。
13    pub stroke_width: f64,
14    /// 填充颜色(RGB 十六进制,可选)。
15    pub fill_color: Option<u32>,
16    /// SVG 风格的路径数据字符串。
17    pub path_data: String,
18}
19
20impl PathObject {
21    /// 创建新的路径对象。
22    #[must_use]
23    pub fn new(x: f64, y: f64, path_data: impl Into<String>) -> Self {
24        Self {
25            x,
26            y,
27            stroke_color: 0x000_000,
28            stroke_width: 0.35,
29            fill_color: None,
30            path_data: path_data.into(),
31        }
32    }
33
34    /// 创建水平线。
35    #[must_use]
36    pub fn hline(x1: f64, y: f64, x2: f64) -> Self {
37        Self::new(x1, y, format!("M{x1} {y}L{x2} {y}"))
38    }
39
40    /// 创建垂直线。
41    #[must_use]
42    pub fn vline(x: f64, y1: f64, y2: f64) -> Self {
43        Self::new(x, y1, format!("M{x} {y1}L{x} {y2}"))
44    }
45
46    /// 创建矩形轮廓。
47    #[must_use]
48    #[allow(clippy::many_single_char_names)]
49    pub fn rect(x: f64, y: f64, w: f64, h: f64) -> Self {
50        let d = format!("M{x} {y}L{} {y}L{} {}L{x} {}Z", x + w, x + w, y + h, y + h);
51        Self::new(x, y, d)
52    }
53
54    /// 设置描边颜色。
55    #[must_use]
56    pub fn stroke_color(mut self, color: u32) -> Self {
57        self.stroke_color = color;
58        self
59    }
60
61    /// 设置描边宽度。
62    #[must_use]
63    pub fn stroke_width(mut self, width: f64) -> Self {
64        self.stroke_width = width;
65        self
66    }
67
68    /// 设置填充颜色。
69    #[must_use]
70    pub fn fill_color(mut self, color: u32) -> Self {
71        self.fill_color = Some(color);
72        self
73    }
74}