1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::align::AlignX;
use crate::color::Color;
use crate::renderer::FontAsset;
use crate::shaders::{ShaderAsset, ShaderBuilder, ShaderConfig};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum WrapMode {
/// Wraps on whitespaces not breaking words
#[default]
Words,
/// Only wraps on new line characters
Newline,
/// Never wraps, can overflow of parent layout
None,
}
/// Configuration settings for rendering text elements.
#[derive(Debug, Clone)]
pub struct TextConfig {
/// Internal engine user data.
pub(crate) user_data: usize,
/// The color of the text.
pub color: Color,
/// The font asset to use. `None` means use the default font.
pub font_asset: Option<&'static FontAsset>,
/// The font size of the text.
pub font_size: u16,
/// The spacing between letters.
pub letter_spacing: u16,
/// The height of each line of text.
pub line_height: u16,
/// Defines the text wrapping behavior.
pub wrap_mode: WrapMode,
/// The alignment of the text.
pub alignment: AlignX,
/// Per-element shader effects applied to this text.
pub(crate) effects: Vec<ShaderConfig>,
/// When true, the text content is exposed to screen readers as static text.
pub(crate) accessible: bool,
}
impl TextConfig {
/// Creates a new `TextConfig` instance with default values.
pub(crate) fn new() -> Self {
Self::default()
}
/// Sets the text color.
#[inline]
pub fn color(&mut self, color: impl Into<Color>) -> &mut Self {
self.color = color.into();
self
}
/// Sets the font to use for this text element.
#[inline]
pub fn font(&mut self, asset: &'static FontAsset) -> &mut Self {
self.font_asset = Some(asset);
self
}
/// Sets the font size.
#[inline]
pub fn font_size(&mut self, size: u16) -> &mut Self {
self.font_size = size;
self
}
/// Sets the letter spacing.
#[inline]
pub fn letter_spacing(&mut self, spacing: u16) -> &mut Self {
self.letter_spacing = spacing;
self
}
/// Sets the line height.
#[inline]
pub fn line_height(&mut self, height: u16) -> &mut Self {
self.line_height = height;
self
}
/// Sets the text wrapping mode.
#[inline]
pub fn wrap_mode(&mut self, mode: WrapMode) -> &mut Self {
self.wrap_mode = mode;
self
}
/// Sets the text alignment.
#[inline]
pub fn alignment(&mut self, alignment: AlignX) -> &mut Self {
self.alignment = alignment;
self
}
/// Adds a per-element shader effect to this text.
#[inline]
pub fn effect(&mut self, asset: &ShaderAsset, f: impl FnOnce(&mut ShaderBuilder<'_>)) -> &mut Self {
let mut builder = ShaderBuilder::new(asset);
f(&mut builder);
self.effects.push(builder.into_config());
self
}
/// Makes this text element visible to screen readers.
///
/// ```ignore
/// ui.text("Hello world", |t| t.font_size(16).accessible());
/// ```
#[inline]
pub fn accessible(&mut self) -> &mut Self {
self.accessible = true;
self
}
}
impl Default for TextConfig {
fn default() -> Self {
Self {
user_data: 0,
color: Color::rgba(0., 0., 0., 0.),
font_size: 0,
letter_spacing: 0,
line_height: 0,
wrap_mode: WrapMode::Words,
alignment: AlignX::Left,
effects: Vec::new(),
font_asset: None,
accessible: false,
}
}
}