Skip to main content

widgetkit_render/
style.rs

1#[derive(Clone, Copy, Debug, PartialEq)]
2pub struct Stroke {
3    pub width: f32,
4}
5
6impl Stroke {
7    pub const fn new(width: f32) -> Self {
8        Self { width }
9    }
10}
11
12impl Default for Stroke {
13    fn default() -> Self {
14        Self { width: 1.0 }
15    }
16}
17
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub enum TextAlign {
20    Left,
21    Center,
22    Right,
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub enum TextBaseline {
27    Top,
28    Middle,
29    Alphabetic,
30    Bottom,
31}
32
33#[derive(Clone, Copy, Debug, PartialEq)]
34pub struct TextMetrics {
35    pub width: f32,
36    pub height: f32,
37    pub line_height: f32,
38    pub baseline: f32,
39    pub line_count: usize,
40}
41
42#[derive(Clone, Debug, PartialEq)]
43pub struct TextStyle {
44    size: f32,
45    line_height: Option<f32>,
46    align: TextAlign,
47    baseline: TextBaseline,
48}
49
50impl TextStyle {
51    pub fn new() -> Self {
52        Self {
53            size: 14.0,
54            line_height: None,
55            align: TextAlign::Left,
56            baseline: TextBaseline::Top,
57        }
58    }
59
60    pub fn size(mut self, size: f32) -> Self {
61        self.size = size.max(1.0);
62        self
63    }
64
65    pub fn line_height(mut self, line_height: f32) -> Self {
66        self.line_height = Some(line_height.max(1.0));
67        self
68    }
69
70    pub fn align(mut self, align: TextAlign) -> Self {
71        self.align = align;
72        self
73    }
74
75    pub fn baseline(mut self, baseline: TextBaseline) -> Self {
76        self.baseline = baseline;
77        self
78    }
79
80    pub fn pixel_size(&self) -> f32 {
81        self.size
82    }
83
84    pub(crate) fn line_height_override(&self) -> Option<f32> {
85        self.line_height
86    }
87
88    pub(crate) fn align_mode(&self) -> TextAlign {
89        self.align
90    }
91
92    pub(crate) fn baseline_mode(&self) -> TextBaseline {
93        self.baseline
94    }
95}
96
97impl Default for TextStyle {
98    fn default() -> Self {
99        Self::new()
100    }
101}