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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::Color;
/// Styling of texts.
///
/// Note that styles that are not enabled in [`crate::Features`] are ignored.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct TextStyle {
/// The text body's color.
pub color: GlyphColoring,
/// The outer stroke.
pub stroke_out: Outline,
/// The inner stroke.
pub stroke_in: Outline,
/// The outer glow. Rendered outside the outer stroke.
pub glow_out: Glow,
/// The outer glow. Rendered inside the inner stroke.
pub glow_in: Glow,
/// The text's shadow. Does not consider additional thickness from outer stroke or outer glow.
pub shadow: Shadow,
}
impl TextStyle {
/// Scale effect widths to fit within the MSDF atlas's representable range.
///
/// The MSDF atlas can only represent distances in the range `-0.5em` to `0.5em`. If the total
/// outward expansion (outer stroke + outer glow) or inward expansion (inner stroke + inner glow)
/// exceeds this range for the given `em_size`, effect widths are scaled down proportionally.
///
/// # Arguments
/// * `em_size` - The em size of the glyph in screen pixels.
///
/// # Returns
/// A new `TextStyle` with widths scaled to fit within the representable range.
pub fn clamped_to_msdf_range(self, em_size: f32) -> Self {
let max_expansion = 0.5 * em_size;
let total_out = self.stroke_out.width + self.glow_out.width;
let total_in = self.stroke_in.width + self.glow_in.width;
let scale_out = if total_out > max_expansion && total_out > 0.0 {
max_expansion / total_out
} else {
1.0
};
let scale_in = if total_in > max_expansion && total_in > 0.0 {
max_expansion / total_in
} else {
1.0
};
Self {
color: self.color,
stroke_out: Outline {
width: self.stroke_out.width * scale_out,
..self.stroke_out
},
stroke_in: Outline {
width: self.stroke_in.width * scale_in,
..self.stroke_in
},
glow_out: Glow {
width: self.glow_out.width * scale_out,
..self.glow_out
},
glow_in: Glow {
width: self.glow_in.width * scale_in,
..self.glow_in
},
shadow: Shadow {
additional_width: self.shadow.additional_width * scale_out,
..self.shadow
},
}
}
}
/// How a glyph is colored.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GlyphColoring {
/// A single solid color.
Solid(Color),
/// A linear gradient between two colors.
Gradient {
/// The color at gradient position 0.
primary: Color,
/// The color at gradient position 1.
secondary: Color,
/// The gradient direction vector (normalized).
direction: glam::Vec2,
},
}
impl Default for GlyphColoring {
fn default() -> Self {
GlyphColoring::Solid(Color::WHITE)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Outline {
/// The outline's color.
pub color: GlyphColoring,
/// The width of the outline, in pixels.
pub width: f32,
/// Roundness of the shape when extruded.
///
/// Implemented as blend factor between pseudo-sdf and true sdf. At 0, the pseudo-sdf is used
/// which preserves corner, at 1 the true sdf is used which is has rounder color. Only
/// points outside of the glyph body is relevant, since inside the glyph body, the true sdf
/// pseudo-sdf is identical.
pub roundness: f32,
}
impl Default for Outline {
fn default() -> Self {
Self {
color: GlyphColoring::Solid(Color::TRANSPARENT),
width: 0.0,
roundness: 0.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Glow {
/// The glow's color.
pub color: GlyphColoring,
/// The width of the glow, in pixels.
pub width: f32,
/// The spread of the glow, from 0.0 to 1.0.
///
/// This divides the glow into two, at the width*spread point, the inner region is drawn with
/// full alpha, and the outer region is drawn with alpha fading to 0.
pub spread: f32,
/// Roundness of the shape when extruded.
///
/// Implemented as blend factor between pseudo-sdf and true sdf. At 0, the pseudo-sdf is used
/// which preserves corner, at 1 the true sdf is used which is has rounder color. Only
/// points outside of the glyph body is relevant, since inside the glyph body, the true sdf
/// pseudo-sdf is identical.
pub roundness: f32,
}
impl Default for Glow {
fn default() -> Self {
Self {
color: GlyphColoring::Solid(Color::TRANSPARENT),
width: 0.0,
spread: 0.0,
roundness: 0.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Shadow {
/// The shadow's color.
pub color: GlyphColoring,
/// The direction of the shadow.
pub direction: glam::Vec2,
/// Expand the shadow by additional amount in pixels, compared to the main glyph body.
pub additional_width: f32,
/// The spread of the shadow, from 0.0 to 1.0, in the expanded region.
///
/// This divides the glow into two, at the width*spread point, the inner region is drawn with
/// full alpha, and the outer region is drawn with alpha fading to 0.
pub spread: f32,
/// Roundness of the shape when extruded.
///
/// Implemented as blend factor between pseudo-sdf and true sdf. At 0, the pseudo-sdf is used
/// which preserves corner, at 1 the true sdf is used which is has rounder color. Only
/// points outside of the glyph body is relevant, since inside the glyph body, the true sdf
/// pseudo-sdf is identical.
pub roundness: f32,
}
impl Default for Shadow {
fn default() -> Self {
Self {
color: GlyphColoring::Solid(Color::TRANSPARENT),
direction: glam::Vec2::ZERO,
additional_width: 0.0,
spread: 0.0,
roundness: 0.0,
}
}
}