Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_text/src/parley_context.rs
30635 views
1
use crate::TextError;
2
use crate::{FontSmoothing, FontSource};
3
use bevy_derive::Deref;
4
use bevy_derive::DerefMut;
5
use bevy_ecs::resource::Resource;
6
use parley::LayoutContext;
7
use parley::{FontContext, GenericFamily};
8
use swash::scale::ScaleContext;
9
10
#[derive(Copy, Clone, PartialEq, Default, Debug)]
11
/// Per-section metadata attached to shaped text runs.
12
///
13
/// This brush is stored in Parley's [`Layout`](`parley::Layout`) so Bevy can identify which text section
14
/// a glyph run belongs to, and the antialiasing method that should be used during rendering.
15
pub struct TextBrush {
16
/// Index of the text section within its `ComputedTextBlock`.
17
pub section_index: u32,
18
/// Antialiasing method to use when rendering the text.
19
pub font_smoothing: FontSmoothing,
20
}
21
22
impl TextBrush {
23
/// Creates a new brush for glyphs from the given text section.
24
pub fn new(section_index: u32, font_smoothing: FontSmoothing) -> Self {
25
TextBrush {
26
section_index,
27
font_smoothing,
28
}
29
}
30
}
31
32
/// A font database and cache, used for font family resolution and text layout.
33
///
34
/// This resource is a wrapper around [`parley::FontContext`].
35
#[derive(Resource, Default, Deref, DerefMut)]
36
pub struct FontCx(pub FontContext);
37
38
impl FontCx {
39
/// Get the family name associated with a [`FontSource`].
40
///
41
/// If the `FontSource` is a `Handle`, returns `None`. The family name can be found by using the handle to look
42
/// up the `Font` asset instead.
43
pub fn get_family<'a>(&'a mut self, source: &'a FontSource) -> Option<&'a str> {
44
let generic_family = match source {
45
FontSource::Handle(_) => return None,
46
FontSource::Family(family) => return Some(family.as_str()),
47
FontSource::Serif => GenericFamily::Serif,
48
FontSource::SansSerif => GenericFamily::SansSerif,
49
FontSource::Cursive => GenericFamily::Cursive,
50
FontSource::Fantasy => GenericFamily::Fantasy,
51
FontSource::Monospace => GenericFamily::Monospace,
52
FontSource::SystemUi => GenericFamily::SystemUi,
53
FontSource::UiSerif => GenericFamily::UiSerif,
54
FontSource::UiSansSerif => GenericFamily::UiSansSerif,
55
FontSource::UiMonospace => GenericFamily::UiMonospace,
56
FontSource::UiRounded => GenericFamily::UiRounded,
57
FontSource::Emoji => GenericFamily::Emoji,
58
FontSource::Math => GenericFamily::Math,
59
FontSource::FangSong => GenericFamily::FangSong,
60
};
61
62
let family_id = self.0.collection.generic_families(generic_family).next();
63
family_id.and_then(|id| self.0.collection.family_name(id))
64
}
65
66
/// Sets the fallback font for a given generic family.
67
///
68
/// In most cases, these methods do not need to called manually,
69
/// as [`parley::fontique`] will automatically select appropriate default fonts based on available system fonts.
70
///
71
/// Note that the `parley/system` feature must be enabled to allow automatic system font discovery.
72
///
73
/// These methods will return an error if the provided family name does not already exist in the font collection.
74
pub fn set_generic_family(
75
&mut self,
76
generic: GenericFamily,
77
family_name: &str,
78
) -> Result<(), TextError> {
79
self.collection
80
.family_id(family_name)
81
.ok_or(TextError::NoSuchFontFamily(family_name.to_string()))
82
.map(|id| {
83
self.collection
84
.set_generic_families(generic, core::iter::once(id));
85
})
86
}
87
88
/// Sets the serif generic family mapping.
89
pub fn set_serif_family(&mut self, family_name: &str) -> Result<(), TextError> {
90
self.set_generic_family(GenericFamily::Serif, family_name)
91
}
92
93
/// Sets the sans-serif generic family mapping.
94
pub fn set_sans_serif_family(&mut self, family_name: &str) -> Result<(), TextError> {
95
self.set_generic_family(GenericFamily::SansSerif, family_name)
96
}
97
98
/// Sets the cursive generic family mapping.
99
pub fn set_cursive_family(&mut self, family_name: &str) -> Result<(), TextError> {
100
self.set_generic_family(GenericFamily::Cursive, family_name)
101
}
102
103
/// Sets the fantasy generic family mapping.
104
pub fn set_fantasy_family(&mut self, family_name: &str) -> Result<(), TextError> {
105
self.set_generic_family(GenericFamily::Fantasy, family_name)
106
}
107
108
/// Sets the monospace generic family mapping.
109
pub fn set_monospace_family(&mut self, family_name: &str) -> Result<(), TextError> {
110
self.set_generic_family(GenericFamily::Monospace, family_name)
111
}
112
113
/// Sets the system-ui generic family mapping.
114
pub fn set_system_ui_family(&mut self, family_name: &str) -> Result<(), TextError> {
115
self.set_generic_family(GenericFamily::SystemUi, family_name)
116
}
117
118
/// Sets the ui-serif generic family mapping.
119
pub fn set_ui_serif_family(&mut self, family_name: &str) -> Result<(), TextError> {
120
self.set_generic_family(GenericFamily::UiSerif, family_name)
121
}
122
123
/// Sets the ui-sans-serif generic family mapping.
124
pub fn set_ui_sans_serif_family(&mut self, family_name: &str) -> Result<(), TextError> {
125
self.set_generic_family(GenericFamily::UiSansSerif, family_name)
126
}
127
128
/// Sets the ui-monospace generic family mapping.
129
pub fn set_ui_monospace_family(&mut self, family_name: &str) -> Result<(), TextError> {
130
self.set_generic_family(GenericFamily::UiMonospace, family_name)
131
}
132
133
/// Sets the ui-rounded generic family mapping.
134
pub fn set_ui_rounded_family(&mut self, family_name: &str) -> Result<(), TextError> {
135
self.set_generic_family(GenericFamily::UiRounded, family_name)
136
}
137
138
/// Sets the emoji generic family mapping.
139
pub fn set_emoji_family(&mut self, family_name: &str) -> Result<(), TextError> {
140
self.set_generic_family(GenericFamily::Emoji, family_name)
141
}
142
143
/// Sets the math generic family mapping.
144
pub fn set_math_family(&mut self, family_name: &str) -> Result<(), TextError> {
145
self.set_generic_family(GenericFamily::Math, family_name)
146
}
147
148
/// Sets the fangsong generic family mapping.
149
pub fn set_fang_song_family(&mut self, family_name: &str) -> Result<(), TextError> {
150
self.set_generic_family(GenericFamily::FangSong, family_name)
151
}
152
}
153
154
/// Text layout context
155
#[derive(Resource, Default, Deref, DerefMut)]
156
pub struct LayoutCx(pub LayoutContext<TextBrush>);
157
158
/// Text scaler context
159
#[derive(Resource, Default, Deref, DerefMut)]
160
pub struct ScaleCx(pub ScaleContext);
161
162