Do not assume font handle is present in assets. (#490)

This commit is contained in:
Boutillier 2020-09-19 21:57:52 +02:00 committed by GitHub
parent 4b83cfc729
commit b9f18efd86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 62 deletions

View File

@ -52,57 +52,58 @@ impl FontAtlasSet {
font_size: f32, font_size: f32,
text: &str, text: &str,
) -> f32 { ) -> 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<Glyph> = None;
let mut width = 0.0; let mut width = 0.0;
for character in text.chars() { if let Some(font) = fonts.get(&self.font) {
if character.is_control() { let scaled_font = ab_glyph::Font::as_scaled(&font.font, font_size);
continue; let font_atlases = self
} .font_atlases
let glyph = scaled_font.scaled_glyph(character); .entry(FloatOrd(font_size))
if let Some(last_glyph) = last_glyph.take() { .or_insert_with(|| {
width += scaled_font.kern(last_glyph.id, glyph.id); vec![FontAtlas::new(
} textures,
if !font_atlases texture_atlases,
.iter() Vec2::new(512.0, 512.0),
.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 mut last_glyph: Option<Glyph> = None;
let add_char_to_font_atlas = |atlas: &mut FontAtlas| -> bool { for character in text.chars() {
atlas.add_char(textures, texture_atlases, character, &glyph_texture) if character.is_control() {
}; continue;
if !font_atlases.iter_mut().any(add_char_to_font_atlas) { }
font_atlases.push(FontAtlas::new( let glyph = scaled_font.scaled_glyph(character);
textures, if let Some(last_glyph) = last_glyph.take() {
texture_atlases, width += scaled_font.kern(last_glyph.id, glyph.id);
Vec2::new(512.0, 512.0), }
)); if !font_atlases
if !font_atlases.last_mut().unwrap().add_char( .iter()
textures, .any(|atlas| atlas.get_char_index(character).is_some())
texture_atlases, {
character, if let Some(outlined_glyph) = scaled_font.outline_glyph(glyph.clone()) {
&glyph_texture, let glyph_texture = Font::get_outlined_glyph_texture(outlined_glyph);
) { let add_char_to_font_atlas = |atlas: &mut FontAtlas| -> bool {
panic!("could not add character to newly created FontAtlas"); 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 width

View File

@ -61,21 +61,23 @@ pub fn draw_text_system(
mut query: Query<(&mut Draw, &Text, &Node, &GlobalTransform)>, mut query: Query<(&mut Draw, &Text, &Node, &GlobalTransform)>,
) { ) {
for (mut draw, text, node, global_transform) in &mut query.iter() { for (mut draw, text, node, global_transform) in &mut query.iter() {
let position = global_transform.translation() - (node.size / 2.0).extend(0.0); if let Some(font) = fonts.get(&text.font) {
let mut drawable_text = DrawableText { let position = global_transform.translation() - (node.size / 2.0).extend(0.0);
font: fonts.get(&text.font).unwrap(), let mut drawable_text = DrawableText {
font_atlas_set: font_atlas_sets font,
.get(&text.font.as_handle::<FontAtlasSet>()) font_atlas_set: font_atlas_sets
.unwrap(), .get(&text.font.as_handle::<FontAtlasSet>())
texture_atlases: &texture_atlases, .unwrap(),
render_resource_bindings: &mut render_resource_bindings, texture_atlases: &texture_atlases,
asset_render_resource_bindings: &mut asset_render_resource_bindings, render_resource_bindings: &mut render_resource_bindings,
position, asset_render_resource_bindings: &mut asset_render_resource_bindings,
msaa: &msaa, position,
style: &text.style, msaa: &msaa,
text: &text.value, style: &text.style,
container_size: node.size, text: &text.value,
}; container_size: node.size,
drawable_text.draw(&mut draw, &mut draw_context).unwrap(); };
drawable_text.draw(&mut draw, &mut draw_context).unwrap();
}
} }
} }