Skip to main content

easyofd_core/model/
text_object.rs

1//! 文本对象。
2
3/// 带位置、字体和内容的文本对象。
4#[derive(Debug, Clone)]
5pub struct TextObject {
6    /// 距左边缘的 X 位置(mm)。
7    pub x: f64,
8    /// 距顶部的 Y 位置(mm)。
9    pub y: f64,
10    /// 字体族名称(如 "SimSun"、"SimHei")。
11    pub font: String,
12    /// 字号(pt)。
13    pub size: f64,
14    /// 字重: 400 = 正常, 700 = 粗体。
15    pub weight: u32,
16    /// 是否斜体。
17    pub italic: bool,
18    /// 文本颜色(RGB 十六进制,如 0x000000 为黑色)。
19    pub color: u32,
20    /// 实际文本内容。
21    pub text: String,
22    /// 可选的文本宽度覆盖(mm)。
23    /// 如果为 None,写入器将根据字符数估算。
24    pub width: Option<f64>,
25    /// 可选的文本高度覆盖(mm)。
26    /// 如果为 None,写入器将使用字号。
27    pub height: Option<f64>,
28}
29
30impl TextObject {
31    /// 使用默认样式创建新的文本对象。
32    #[must_use]
33    pub fn new(x: f64, y: f64, text: impl Into<String>) -> Self {
34        Self {
35            x,
36            y,
37            font: "SimSun".to_string(),
38            size: 12.0,
39            weight: 400,
40            italic: false,
41            color: 0x000_000,
42            text: text.into(),
43            width: None,
44            height: None,
45        }
46    }
47
48    /// 设置字体族。
49    #[must_use]
50    pub fn font(mut self, font: impl Into<String>) -> Self {
51        self.font = font.into();
52        self
53    }
54
55    /// 设置字号(pt)。
56    #[must_use]
57    pub fn size(mut self, size: f64) -> Self {
58        self.size = size;
59        self
60    }
61
62    /// 设置粗体。
63    #[must_use]
64    pub fn bold(mut self) -> Self {
65        self.weight = 700;
66        self
67    }
68
69    /// 设置斜体。
70    #[must_use]
71    pub fn italic(mut self) -> Self {
72        self.italic = true;
73        self
74    }
75
76    /// 设置文本颜色(RGB 十六进制)。
77    #[must_use]
78    pub fn color(mut self, color: u32) -> Self {
79        self.color = color;
80        self
81    }
82}