1use embedded_graphics_core::pixelcolor::Rgb565;
2#[cfg(feature = "embedded-graphics")]
3use embedded_graphics_core::pixelcolor::RgbColor;
4
5use crate::font::FontId;
6
7pub const CHAR_WIDTH: u32 = 4;
8pub const CHAR_HEIGHT: u32 = 6;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum TextAlign {
12 Left,
13 Center,
14 Right,
15}
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum VerticalAlign {
19 Top,
20 Middle,
21 Bottom,
22}
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25pub enum TextWrap {
26 None,
27 Character,
28 Word,
29}
30
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum TextOverflow {
33 Clip,
34 Ellipsis,
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub enum EllipsisMode {
39 ThreeDots,
40 SingleGlyph,
41}
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub enum TextOverflowPolicy {
45 Global(TextOverflow),
46 WrapThenEllipsis { max_lines: u8 },
47}
48
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub struct TextStyle {
51 pub color: Rgb565,
52 pub font: FontId,
53 pub opacity: u8,
54 pub align: TextAlign,
55 pub vertical_align: VerticalAlign,
56 pub wrap: TextWrap,
57 pub overflow: TextOverflow,
58 pub overflow_policy: TextOverflowPolicy,
59 pub kerning: bool,
60 pub max_lines: Option<u8>,
61 pub ellipsis: EllipsisMode,
62 pub line_spacing: u8,
63}
64
65impl TextStyle {
66 pub const fn new(color: Rgb565) -> Self {
67 Self {
68 color,
69 font: FontId::Tiny3x5,
70 opacity: 255,
71 align: TextAlign::Left,
72 vertical_align: VerticalAlign::Top,
73 wrap: TextWrap::None,
74 overflow: TextOverflow::Clip,
75 overflow_policy: TextOverflowPolicy::Global(TextOverflow::Clip),
76 kerning: false,
77 max_lines: None,
78 ellipsis: EllipsisMode::ThreeDots,
79 line_spacing: 1,
80 }
81 }
82
83 pub const fn centered(mut self) -> Self {
84 self.align = TextAlign::Center;
85 self.vertical_align = VerticalAlign::Middle;
86 self
87 }
88
89 pub const fn with_align(mut self, align: TextAlign) -> Self {
90 self.align = align;
91 self
92 }
93
94 pub const fn with_vertical_align(mut self, align: VerticalAlign) -> Self {
95 self.vertical_align = align;
96 self
97 }
98
99 pub const fn with_wrap(mut self, wrap: TextWrap) -> Self {
100 self.wrap = wrap;
101 self
102 }
103
104 pub const fn with_line_spacing(mut self, spacing: u8) -> Self {
105 self.line_spacing = spacing;
106 self
107 }
108
109 pub const fn with_overflow(mut self, overflow: TextOverflow) -> Self {
110 self.overflow = overflow;
111 self.overflow_policy = TextOverflowPolicy::Global(overflow);
112 self
113 }
114
115 pub const fn with_kerning(mut self, kerning: bool) -> Self {
116 self.kerning = kerning;
117 self
118 }
119
120 pub const fn with_max_lines(mut self, max_lines: Option<u8>) -> Self {
121 self.max_lines = max_lines;
122 self
123 }
124
125 pub const fn with_ellipsis_mode(mut self, ellipsis: EllipsisMode) -> Self {
126 self.ellipsis = ellipsis;
127 self
128 }
129
130 pub const fn with_overflow_policy(mut self, policy: TextOverflowPolicy) -> Self {
131 self.overflow_policy = policy;
132 self
133 }
134
135 pub const fn with_opacity(mut self, opacity: u8) -> Self {
136 self.opacity = opacity;
137 self
138 }
139
140 pub const fn with_font_id(mut self, font: FontId) -> Self {
141 self.font = font;
142 self
143 }
144
145 pub fn with_font(mut self, font: impl Into<FontId>) -> Self {
146 self.font = font.into();
147 self
148 }
149}
150
151#[cfg(feature = "embedded-graphics")]
152impl From<&embedded_graphics::mono_font::MonoTextStyle<'static, Rgb565>> for TextStyle {
153 fn from(mono_style: &embedded_graphics::mono_font::MonoTextStyle<'static, Rgb565>) -> Self {
154 let mut style = TextStyle::new(mono_style.text_color.unwrap_or(Rgb565::WHITE));
155 style.font = FontId::MonoFont(mono_style.font);
156 style
157 }
158}
159
160#[cfg(feature = "embedded-graphics")]
161impl From<embedded_graphics::mono_font::MonoTextStyle<'static, Rgb565>> for TextStyle {
162 fn from(mono_style: embedded_graphics::mono_font::MonoTextStyle<'static, Rgb565>) -> Self {
163 Self::from(&mono_style)
164 }
165}
166
167#[derive(Clone, Copy, Debug, PartialEq, Eq)]
168pub struct TextMetrics {
169 pub width: u32,
170 pub height: u32,
171}