Skip to content

Commit b9f18ef

Browse files
Do not assume font handle is present in assets. (#490)
1 parent 4b83cfc commit b9f18ef

File tree

2 files changed

+65
-62
lines changed

2 files changed

+65
-62
lines changed

crates/bevy_text/src/font_atlas_set.rs

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -52,57 +52,58 @@ impl FontAtlasSet {
5252
font_size: f32,
5353
text: &str,
5454
) -> f32 {
55-
let font = fonts.get(&self.font).unwrap();
56-
let scaled_font = ab_glyph::Font::as_scaled(&font.font, font_size);
57-
let font_atlases = self
58-
.font_atlases
59-
.entry(FloatOrd(font_size))
60-
.or_insert_with(|| {
61-
vec![FontAtlas::new(
62-
textures,
63-
texture_atlases,
64-
Vec2::new(512.0, 512.0),
65-
)]
66-
});
67-
68-
let mut last_glyph: Option<Glyph> = None;
6955
let mut width = 0.0;
70-
for character in text.chars() {
71-
if character.is_control() {
72-
continue;
73-
}
74-
let glyph = scaled_font.scaled_glyph(character);
75-
if let Some(last_glyph) = last_glyph.take() {
76-
width += scaled_font.kern(last_glyph.id, glyph.id);
77-
}
78-
if !font_atlases
79-
.iter()
80-
.any(|atlas| atlas.get_char_index(character).is_some())
81-
{
82-
if let Some(outlined_glyph) = scaled_font.outline_glyph(glyph.clone()) {
83-
let glyph_texture = Font::get_outlined_glyph_texture(outlined_glyph);
84-
let add_char_to_font_atlas = |atlas: &mut FontAtlas| -> bool {
85-
atlas.add_char(textures, texture_atlases, character, &glyph_texture)
86-
};
87-
if !font_atlases.iter_mut().any(add_char_to_font_atlas) {
88-
font_atlases.push(FontAtlas::new(
89-
textures,
90-
texture_atlases,
91-
Vec2::new(512.0, 512.0),
92-
));
93-
if !font_atlases.last_mut().unwrap().add_char(
94-
textures,
95-
texture_atlases,
96-
character,
97-
&glyph_texture,
98-
) {
99-
panic!("could not add character to newly created FontAtlas");
56+
if let Some(font) = fonts.get(&self.font) {
57+
let scaled_font = ab_glyph::Font::as_scaled(&font.font, font_size);
58+
let font_atlases = self
59+
.font_atlases
60+
.entry(FloatOrd(font_size))
61+
.or_insert_with(|| {
62+
vec![FontAtlas::new(
63+
textures,
64+
texture_atlases,
65+
Vec2::new(512.0, 512.0),
66+
)]
67+
});
68+
69+
let mut last_glyph: Option<Glyph> = None;
70+
for character in text.chars() {
71+
if character.is_control() {
72+
continue;
73+
}
74+
let glyph = scaled_font.scaled_glyph(character);
75+
if let Some(last_glyph) = last_glyph.take() {
76+
width += scaled_font.kern(last_glyph.id, glyph.id);
77+
}
78+
if !font_atlases
79+
.iter()
80+
.any(|atlas| atlas.get_char_index(character).is_some())
81+
{
82+
if let Some(outlined_glyph) = scaled_font.outline_glyph(glyph.clone()) {
83+
let glyph_texture = Font::get_outlined_glyph_texture(outlined_glyph);
84+
let add_char_to_font_atlas = |atlas: &mut FontAtlas| -> bool {
85+
atlas.add_char(textures, texture_atlases, character, &glyph_texture)
86+
};
87+
if !font_atlases.iter_mut().any(add_char_to_font_atlas) {
88+
font_atlases.push(FontAtlas::new(
89+
textures,
90+
texture_atlases,
91+
Vec2::new(512.0, 512.0),
92+
));
93+
if !font_atlases.last_mut().unwrap().add_char(
94+
textures,
95+
texture_atlases,
96+
character,
97+
&glyph_texture,
98+
) {
99+
panic!("could not add character to newly created FontAtlas");
100+
}
100101
}
101102
}
103+
width += scaled_font.h_advance(glyph.id);
104+
last_glyph = Some(glyph);
102105
}
103106
}
104-
width += scaled_font.h_advance(glyph.id);
105-
last_glyph = Some(glyph);
106107
}
107108

108109
width

crates/bevy_ui/src/widget/text.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,23 @@ pub fn draw_text_system(
6161
mut query: Query<(&mut Draw, &Text, &Node, &GlobalTransform)>,
6262
) {
6363
for (mut draw, text, node, global_transform) in &mut query.iter() {
64-
let position = global_transform.translation() - (node.size / 2.0).extend(0.0);
65-
let mut drawable_text = DrawableText {
66-
font: fonts.get(&text.font).unwrap(),
67-
font_atlas_set: font_atlas_sets
68-
.get(&text.font.as_handle::<FontAtlasSet>())
69-
.unwrap(),
70-
texture_atlases: &texture_atlases,
71-
render_resource_bindings: &mut render_resource_bindings,
72-
asset_render_resource_bindings: &mut asset_render_resource_bindings,
73-
position,
74-
msaa: &msaa,
75-
style: &text.style,
76-
text: &text.value,
77-
container_size: node.size,
78-
};
79-
drawable_text.draw(&mut draw, &mut draw_context).unwrap();
64+
if let Some(font) = fonts.get(&text.font) {
65+
let position = global_transform.translation() - (node.size / 2.0).extend(0.0);
66+
let mut drawable_text = DrawableText {
67+
font,
68+
font_atlas_set: font_atlas_sets
69+
.get(&text.font.as_handle::<FontAtlasSet>())
70+
.unwrap(),
71+
texture_atlases: &texture_atlases,
72+
render_resource_bindings: &mut render_resource_bindings,
73+
asset_render_resource_bindings: &mut asset_render_resource_bindings,
74+
position,
75+
msaa: &msaa,
76+
style: &text.style,
77+
text: &text.value,
78+
container_size: node.size,
79+
};
80+
drawable_text.draw(&mut draw, &mut draw_context).unwrap();
81+
}
8082
}
8183
}

0 commit comments

Comments
 (0)