Skip to content

Commit

Permalink
Do not assume font handle is present in assets. (bevyengine#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisBoutillier authored and mrk-its committed Oct 6, 2020
1 parent 39c77ff commit 0f8a579
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 62 deletions.
93 changes: 47 additions & 46 deletions crates/bevy_text/src/font_atlas_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Glyph> = 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<Glyph> = 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
Expand Down
34 changes: 18 additions & 16 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<FontAtlasSet>())
.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::<FontAtlasSet>())
.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();
}
}
}

0 comments on commit 0f8a579

Please sign in to comment.