Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_text/src/font_atlas_set.rs
9412 views
1
use crate::{FontAtlas, FontHinting, FontSmoothing, GlyphCacheKey};
2
use bevy_asset::Assets;
3
use bevy_derive::{Deref, DerefMut};
4
use bevy_ecs::resource::Resource;
5
use bevy_image::Image;
6
use bevy_platform::collections::HashMap;
7
8
/// Identifies the font atlases for a particular font in [`FontAtlasSet`]
9
///
10
/// Allows an `f32` font size to be used as a key in a `HashMap`, by its binary representation.
11
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
12
pub struct FontAtlasKey {
13
/// Font data id
14
pub id: u32,
15
/// Font data index
16
pub index: u32,
17
/// Font size via `f32::to_bits`
18
pub font_size_bits: u32,
19
/// Hash of normalized variation coords for this run.
20
pub variations_hash: u64,
21
/// Hinting
22
pub hinting: FontHinting,
23
/// Antialiasing method
24
pub font_smoothing: FontSmoothing,
25
}
26
27
/// Set of rasterized fonts stored in [`FontAtlas`]es.
28
#[derive(Debug, Default, Resource, Deref, DerefMut)]
29
pub struct FontAtlasSet(HashMap<FontAtlasKey, Vec<FontAtlas>>);
30
31
impl FontAtlasSet {
32
/// Checks whether the given subpixel-offset glyph is contained in any of the [`FontAtlas`]es for the font identified by the given [`FontAtlasKey`].
33
pub fn has_glyph(&self, cache_key: GlyphCacheKey, font_key: &FontAtlasKey) -> bool {
34
self.get(font_key)
35
.is_some_and(|font_atlas| font_atlas.iter().any(|atlas| atlas.has_glyph(cache_key)))
36
}
37
38
/// Returns the total size in bytes of the image data for all fonts.
39
pub fn total_bytes(&self, images: &Assets<Image>) -> u64 {
40
self.values()
41
.flat_map(|font_atlases| font_atlases.iter())
42
.map(|font_atlas| {
43
images
44
.get(&font_atlas.texture)
45
.and_then(|image| image.data.as_ref())
46
.map_or(0, |data| data.len() as u64)
47
})
48
.sum()
49
}
50
}
51
52