2020-11-13 00:21:48 +00:00
|
|
|
use crate::{error::TextError, Font, FontAtlas};
|
|
|
|
use ab_glyph::{GlyphId, OutlinedGlyph};
|
2020-06-14 01:53:31 +00:00
|
|
|
use bevy_asset::{Assets, Handle};
|
2020-07-17 01:26:21 +00:00
|
|
|
use bevy_core::FloatOrd;
|
2020-07-17 00:23:50 +00:00
|
|
|
use bevy_math::Vec2;
|
2020-06-14 01:53:31 +00:00
|
|
|
use bevy_render::texture::Texture;
|
|
|
|
use bevy_sprite::TextureAtlas;
|
2020-10-18 20:48:15 +00:00
|
|
|
use bevy_type_registry::TypeUuid;
|
2020-11-13 00:21:48 +00:00
|
|
|
use bevy_utils::{AHashExt, HashMap};
|
2020-06-14 01:53:31 +00:00
|
|
|
|
|
|
|
type FontSizeKey = FloatOrd;
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
#[derive(TypeUuid)]
|
2020-10-18 20:48:15 +00:00
|
|
|
#[uuid = "73ba778b-b6b5-4f45-982d-d21b6b86ace2"]
|
2020-06-14 01:53:31 +00:00
|
|
|
pub struct FontAtlasSet {
|
2020-09-16 01:06:10 +00:00
|
|
|
font_atlases: HashMap<FontSizeKey, Vec<FontAtlas>>,
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-06-14 01:53:31 +00:00
|
|
|
pub struct GlyphAtlasInfo {
|
|
|
|
pub texture_atlas: Handle<TextureAtlas>,
|
2020-11-13 00:21:48 +00:00
|
|
|
pub glyph_index: u32,
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
impl Default for FontAtlasSet {
|
|
|
|
fn default() -> Self {
|
|
|
|
FontAtlasSet {
|
|
|
|
font_atlases: HashMap::with_capacity(1),
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-13 00:21:48 +00:00
|
|
|
}
|
2020-06-14 01:53:31 +00:00
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
impl FontAtlasSet {
|
2020-09-16 01:06:10 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&FontSizeKey, &Vec<FontAtlas>)> {
|
2020-06-14 01:53:31 +00:00
|
|
|
self.font_atlases.iter()
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
pub fn has_glyph(&self, glyph_id: GlyphId, font_size: f32) -> bool {
|
2020-06-14 01:53:31 +00:00
|
|
|
self.font_atlases
|
|
|
|
.get(&FloatOrd(font_size))
|
|
|
|
.map_or(false, |font_atlas| {
|
2020-11-13 00:21:48 +00:00
|
|
|
font_atlas.iter().any(|atlas| atlas.has_glyph(glyph_id))
|
2020-06-14 01:53:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
pub fn add_glyph_to_atlas(
|
2020-06-14 01:53:31 +00:00
|
|
|
&mut self,
|
|
|
|
texture_atlases: &mut Assets<TextureAtlas>,
|
|
|
|
textures: &mut Assets<Texture>,
|
2020-11-13 00:21:48 +00:00
|
|
|
outlined_glyph: OutlinedGlyph,
|
|
|
|
) -> Result<GlyphAtlasInfo, TextError> {
|
|
|
|
let glyph = outlined_glyph.glyph();
|
|
|
|
let glyph_id = glyph.id;
|
|
|
|
let font_size = glyph.scale.y;
|
2020-09-19 21:22:01 +00:00
|
|
|
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),
|
|
|
|
)]
|
|
|
|
});
|
2020-11-13 00:21:48 +00:00
|
|
|
let glyph_texture = Font::get_outlined_glyph_texture(outlined_glyph);
|
|
|
|
let add_char_to_font_atlas = |atlas: &mut FontAtlas| -> bool {
|
|
|
|
atlas.add_glyph(textures, texture_atlases, glyph_id, &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_glyph(
|
|
|
|
textures,
|
|
|
|
texture_atlases,
|
|
|
|
glyph_id,
|
|
|
|
&glyph_texture,
|
|
|
|
) {
|
|
|
|
return Err(TextError::FailedToAddGlyph(glyph_id));
|
2020-09-19 21:22:01 +00:00
|
|
|
}
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
2020-07-28 04:04:04 +00:00
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
Ok(self.get_glyph_atlas_info(font_size, glyph_id).unwrap())
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
pub fn get_glyph_atlas_info(
|
|
|
|
&self,
|
|
|
|
font_size: f32,
|
|
|
|
glyph_id: GlyphId,
|
|
|
|
) -> Option<GlyphAtlasInfo> {
|
2020-06-14 01:53:31 +00:00
|
|
|
self.font_atlases
|
|
|
|
.get(&FloatOrd(font_size))
|
2020-11-13 00:21:48 +00:00
|
|
|
.and_then(|font_atlases| {
|
|
|
|
font_atlases
|
2020-09-16 01:06:10 +00:00
|
|
|
.iter()
|
|
|
|
.find_map(|atlas| {
|
|
|
|
atlas
|
2020-11-13 00:21:48 +00:00
|
|
|
.get_glyph_index(glyph_id)
|
|
|
|
.map(|glyph_index| (glyph_index, atlas.texture_atlas.clone_weak()))
|
2020-09-16 01:06:10 +00:00
|
|
|
})
|
2020-11-13 00:21:48 +00:00
|
|
|
.map(|(glyph_index, texture_atlas)| GlyphAtlasInfo {
|
2020-09-16 01:06:10 +00:00
|
|
|
texture_atlas,
|
2020-11-13 00:21:48 +00:00
|
|
|
glyph_index,
|
2020-06-14 01:53:31 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|