-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix font atlas overflow #495
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ type FontSizeKey = FloatOrd; | |
#[derive(Default)] | ||
pub struct FontAtlasSet { | ||
font: Handle<Font>, | ||
font_atlases: HashMap<FontSizeKey, FontAtlas>, | ||
font_atlases: HashMap<FontSizeKey, Vec<FontAtlas>>, | ||
} | ||
|
||
#[derive(Debug)] | ||
|
@@ -30,15 +30,17 @@ impl FontAtlasSet { | |
} | ||
} | ||
|
||
pub fn iter(&self) -> impl Iterator<Item = (&FontSizeKey, &FontAtlas)> { | ||
pub fn iter(&self) -> impl Iterator<Item = (&FontSizeKey, &Vec<FontAtlas>)> { | ||
self.font_atlases.iter() | ||
} | ||
|
||
pub fn has_char(&self, character: char, font_size: f32) -> bool { | ||
self.font_atlases | ||
.get(&FloatOrd(font_size)) | ||
.map_or(false, |font_atlas| { | ||
font_atlas.get_char_index(character).is_some() | ||
font_atlas | ||
.iter() | ||
.any(|atlas| atlas.get_char_index(character).is_some()) | ||
}) | ||
} | ||
|
||
|
@@ -55,7 +57,13 @@ impl FontAtlasSet { | |
let font_atlas = self | ||
.font_atlases | ||
.entry(FloatOrd(font_size)) | ||
.or_insert_with(|| FontAtlas::new(textures, texture_atlases, Vec2::new(512.0, 512.0))); | ||
.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; | ||
|
@@ -67,10 +75,30 @@ impl FontAtlasSet { | |
if let Some(last_glyph) = last_glyph.take() { | ||
width += scaled_font.kern(last_glyph.id, glyph.id); | ||
} | ||
if font_atlas.get_char_index(character).is_none() { | ||
if !font_atlas | ||
.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); | ||
font_atlas.add_char(textures, texture_atlases, character, &glyph_texture); | ||
let add_char_to_fontatlas = |atlas: &mut FontAtlas| -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: as long as it is, naming conventions dictate that this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed - although I hate putting it into a Fn ... but clippy complained :-( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bawhaha yeah sometimes clippy is overly opinionated imo. |
||
atlas.add_char(textures, texture_atlases, character, &glyph_texture) | ||
}; | ||
if !font_atlas.iter_mut().any(add_char_to_fontatlas) { | ||
font_atlas.push(FontAtlas::new( | ||
textures, | ||
texture_atlases, | ||
Vec2::new(512.0, 512.0), | ||
)); | ||
if !font_atlas.last_mut().unwrap().add_char( | ||
textures, | ||
texture_atlases, | ||
character, | ||
&glyph_texture, | ||
) { | ||
panic!("could not add character to newly created fontatlas"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: lets use type name casing here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure thing |
||
} | ||
} | ||
} | ||
} | ||
width += scaled_font.h_advance(glyph.id); | ||
|
@@ -84,12 +112,18 @@ impl FontAtlasSet { | |
self.font_atlases | ||
.get(&FloatOrd(font_size)) | ||
verzuz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.and_then(|font_atlas| { | ||
font_atlas | ||
.get_char_index(character) | ||
.map(|char_index| GlyphAtlasInfo { | ||
texture_atlas: font_atlas.texture_atlas, | ||
if let Some(atlas) = font_atlas | ||
.iter() | ||
.find(|atlas| atlas.get_char_index(character).is_some()) | ||
{ | ||
let char_index = atlas.get_char_index(character).unwrap(); | ||
Some(GlyphAtlasInfo { | ||
texture_atlas: atlas.texture_atlas, | ||
char_index, | ||
}) | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: lets rename this
font_atlases
now that its a vecThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure - makes sense