use crate::{FontAtlas, FontHinting, FontSmoothing, GlyphCacheKey};
use bevy_asset::Assets;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::resource::Resource;
use bevy_image::Image;
use bevy_platform::collections::HashMap;
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub struct FontAtlasKey {
pub id: u32,
pub index: u32,
pub font_size_bits: u32,
pub variations_hash: u64,
pub hinting: FontHinting,
pub font_smoothing: FontSmoothing,
}
#[derive(Debug, Default, Resource, Deref, DerefMut)]
pub struct FontAtlasSet(HashMap<FontAtlasKey, Vec<FontAtlas>>);
impl FontAtlasSet {
pub fn has_glyph(&self, cache_key: GlyphCacheKey, font_key: &FontAtlasKey) -> bool {
self.get(font_key)
.is_some_and(|font_atlas| font_atlas.iter().any(|atlas| atlas.has_glyph(cache_key)))
}
pub fn total_bytes(&self, images: &Assets<Image>) -> u64 {
self.values()
.flat_map(|font_atlases| font_atlases.iter())
.map(|font_atlas| {
images
.get(&font_atlas.texture)
.and_then(|image| image.data.as_ref())
.map_or(0, |data| data.len() as u64)
})
.sum()
}
}