From 2d02286de7c98de813bb3178de94cb362c76cac3 Mon Sep 17 00:00:00 2001 From: Boris Boutillier Date: Mon, 14 Sep 2020 14:10:37 +0200 Subject: [PATCH] Do not assume font handle is present in assets. --- crates/bevy_text/src/font_atlas_set.rs | 93 +++++++++++++------------- crates/bevy_ui/src/widget/text.rs | 34 +++++----- 2 files changed, 65 insertions(+), 62 deletions(-) diff --git a/crates/bevy_text/src/font_atlas_set.rs b/crates/bevy_text/src/font_atlas_set.rs index a38e3aaacf58f..56289aa0bf506 100644 --- a/crates/bevy_text/src/font_atlas_set.rs +++ b/crates/bevy_text/src/font_atlas_set.rs @@ -52,57 +52,58 @@ impl FontAtlasSet { font_size: f32, text: &str, ) -> f32 { - let font = fonts.get(&self.font).unwrap(); - let scaled_font = ab_glyph::Font::as_scaled(&font.font, font_size); - let font_atlases = self - .font_atlases - .entry(FloatOrd(font_size)) - .or_insert_with(|| { - vec![FontAtlas::new( - textures, - texture_atlases, - Vec2::new(512.0, 512.0), - )] - }); - - let mut last_glyph: Option = None; let mut width = 0.0; - for character in text.chars() { - if character.is_control() { - continue; - } - let glyph = scaled_font.scaled_glyph(character); - if let Some(last_glyph) = last_glyph.take() { - width += scaled_font.kern(last_glyph.id, glyph.id); - } - if !font_atlases - .iter() - .any(|atlas| atlas.get_char_index(character).is_some()) - { - if let Some(outlined_glyph) = scaled_font.outline_glyph(glyph.clone()) { - let glyph_texture = Font::get_outlined_glyph_texture(outlined_glyph); - let add_char_to_font_atlas = |atlas: &mut FontAtlas| -> bool { - atlas.add_char(textures, texture_atlases, character, &glyph_texture) - }; - if !font_atlases.iter_mut().any(add_char_to_font_atlas) { - font_atlases.push(FontAtlas::new( - textures, - texture_atlases, - Vec2::new(512.0, 512.0), - )); - if !font_atlases.last_mut().unwrap().add_char( - textures, - texture_atlases, - character, - &glyph_texture, - ) { - panic!("could not add character to newly created FontAtlas"); + if let Some(font) = fonts.get(&self.font) { + let scaled_font = ab_glyph::Font::as_scaled(&font.font, font_size); + let font_atlases = self + .font_atlases + .entry(FloatOrd(font_size)) + .or_insert_with(|| { + vec![FontAtlas::new( + textures, + texture_atlases, + Vec2::new(512.0, 512.0), + )] + }); + + let mut last_glyph: Option = None; + for character in text.chars() { + if character.is_control() { + continue; + } + let glyph = scaled_font.scaled_glyph(character); + if let Some(last_glyph) = last_glyph.take() { + width += scaled_font.kern(last_glyph.id, glyph.id); + } + if !font_atlases + .iter() + .any(|atlas| atlas.get_char_index(character).is_some()) + { + if let Some(outlined_glyph) = scaled_font.outline_glyph(glyph.clone()) { + let glyph_texture = Font::get_outlined_glyph_texture(outlined_glyph); + let add_char_to_font_atlas = |atlas: &mut FontAtlas| -> bool { + atlas.add_char(textures, texture_atlases, character, &glyph_texture) + }; + if !font_atlases.iter_mut().any(add_char_to_font_atlas) { + font_atlases.push(FontAtlas::new( + textures, + texture_atlases, + Vec2::new(512.0, 512.0), + )); + if !font_atlases.last_mut().unwrap().add_char( + textures, + texture_atlases, + character, + &glyph_texture, + ) { + panic!("could not add character to newly created FontAtlas"); + } } } + width += scaled_font.h_advance(glyph.id); + last_glyph = Some(glyph); } } - width += scaled_font.h_advance(glyph.id); - last_glyph = Some(glyph); } width diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index bac361589e32e..6032b38a13d62 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -61,21 +61,23 @@ pub fn draw_text_system( mut query: Query<(&mut Draw, &Text, &Node, &GlobalTransform)>, ) { for (mut draw, text, node, global_transform) in &mut query.iter() { - let position = global_transform.translation() - (node.size / 2.0).extend(0.0); - let mut drawable_text = DrawableText { - font: fonts.get(&text.font).unwrap(), - font_atlas_set: font_atlas_sets - .get(&text.font.as_handle::()) - .unwrap(), - texture_atlases: &texture_atlases, - render_resource_bindings: &mut render_resource_bindings, - asset_render_resource_bindings: &mut asset_render_resource_bindings, - position, - msaa: &msaa, - style: &text.style, - text: &text.value, - container_size: node.size, - }; - drawable_text.draw(&mut draw, &mut draw_context).unwrap(); + if let Some(font) = fonts.get(&text.font) { + let position = global_transform.translation() - (node.size / 2.0).extend(0.0); + let mut drawable_text = DrawableText { + font, + font_atlas_set: font_atlas_sets + .get(&text.font.as_handle::()) + .unwrap(), + texture_atlases: &texture_atlases, + render_resource_bindings: &mut render_resource_bindings, + asset_render_resource_bindings: &mut asset_render_resource_bindings, + position, + msaa: &msaa, + style: &text.style, + text: &text.value, + container_size: node.size, + }; + drawable_text.draw(&mut draw, &mut draw_context).unwrap(); + } } }