2021-01-03 20:39:11 +00:00
|
|
|
use ab_glyph::{GlyphId, Point};
|
2020-06-14 01:53:31 +00:00
|
|
|
use bevy_asset::{Assets, Handle};
|
2020-07-17 00:23:50 +00:00
|
|
|
use bevy_math::Vec2;
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_render::{
|
2024-01-03 03:31:04 +00:00
|
|
|
render_asset::RenderAssetPersistencePolicy,
|
2021-12-14 03:58:23 +00:00
|
|
|
render_resource::{Extent3d, TextureDimension, TextureFormat},
|
|
|
|
texture::Image,
|
|
|
|
};
|
2020-06-14 01:53:31 +00:00
|
|
|
use bevy_sprite::{DynamicTextureAtlasBuilder, TextureAtlas};
|
2020-08-29 00:08:51 +00:00
|
|
|
use bevy_utils::HashMap;
|
2020-06-14 01:53:31 +00:00
|
|
|
|
2021-01-03 20:39:11 +00:00
|
|
|
#[cfg(feature = "subpixel_glyph_atlas")]
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
pub struct SubpixelOffset {
|
|
|
|
x: u16,
|
|
|
|
y: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "subpixel_glyph_atlas")]
|
|
|
|
impl From<Point> for SubpixelOffset {
|
|
|
|
fn from(p: Point) -> Self {
|
|
|
|
fn f(v: f32) -> u16 {
|
|
|
|
((v % 1.) * (u16::MAX as f32)) as u16
|
|
|
|
}
|
|
|
|
Self {
|
|
|
|
x: f(p.x),
|
|
|
|
y: f(p.y),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "subpixel_glyph_atlas"))]
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
pub struct SubpixelOffset;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "subpixel_glyph_atlas"))]
|
|
|
|
impl From<Point> for SubpixelOffset {
|
|
|
|
fn from(_: Point) -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 01:53:31 +00:00
|
|
|
pub struct FontAtlas {
|
|
|
|
pub dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder,
|
2021-12-14 03:58:23 +00:00
|
|
|
pub glyph_to_atlas_index: HashMap<(GlyphId, SubpixelOffset), usize>,
|
2020-06-14 01:53:31 +00:00
|
|
|
pub texture_atlas: Handle<TextureAtlas>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FontAtlas {
|
|
|
|
pub fn new(
|
2021-12-14 03:58:23 +00:00
|
|
|
textures: &mut Assets<Image>,
|
2020-06-14 01:53:31 +00:00
|
|
|
texture_atlases: &mut Assets<TextureAtlas>,
|
|
|
|
size: Vec2,
|
|
|
|
) -> FontAtlas {
|
2021-12-14 03:58:23 +00:00
|
|
|
let atlas_texture = textures.add(Image::new_fill(
|
|
|
|
Extent3d {
|
|
|
|
width: size.x as u32,
|
|
|
|
height: size.y as u32,
|
|
|
|
depth_or_array_layers: 1,
|
|
|
|
},
|
2020-11-22 20:04:47 +00:00
|
|
|
TextureDimension::D2,
|
2020-07-26 19:08:41 +00:00
|
|
|
&[0, 0, 0, 0],
|
|
|
|
TextureFormat::Rgba8UnormSrgb,
|
2024-01-03 03:31:04 +00:00
|
|
|
// Need to keep this image CPU persistent in order to add additional glyphs later on
|
|
|
|
RenderAssetPersistencePolicy::Keep,
|
2020-07-26 19:08:41 +00:00
|
|
|
));
|
2020-06-14 01:53:31 +00:00
|
|
|
let texture_atlas = TextureAtlas::new_empty(atlas_texture, size);
|
|
|
|
Self {
|
|
|
|
texture_atlas: texture_atlases.add(texture_atlas),
|
2020-11-13 00:21:48 +00:00
|
|
|
glyph_to_atlas_index: HashMap::default(),
|
2023-11-14 13:44:25 +00:00
|
|
|
dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder::new(size, 0),
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 20:39:11 +00:00
|
|
|
pub fn get_glyph_index(
|
|
|
|
&self,
|
|
|
|
glyph_id: GlyphId,
|
|
|
|
subpixel_offset: SubpixelOffset,
|
2021-12-14 03:58:23 +00:00
|
|
|
) -> Option<usize> {
|
2021-01-03 20:39:11 +00:00
|
|
|
self.glyph_to_atlas_index
|
|
|
|
.get(&(glyph_id, subpixel_offset))
|
|
|
|
.copied()
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 20:39:11 +00:00
|
|
|
pub fn has_glyph(&self, glyph_id: GlyphId, subpixel_offset: SubpixelOffset) -> bool {
|
|
|
|
self.glyph_to_atlas_index
|
|
|
|
.contains_key(&(glyph_id, subpixel_offset))
|
2020-11-13 00:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_glyph(
|
2020-06-14 01:53:31 +00:00
|
|
|
&mut self,
|
2021-12-14 03:58:23 +00:00
|
|
|
textures: &mut Assets<Image>,
|
2020-06-14 01:53:31 +00:00
|
|
|
texture_atlases: &mut Assets<TextureAtlas>,
|
2020-11-13 00:21:48 +00:00
|
|
|
glyph_id: GlyphId,
|
2021-01-03 20:39:11 +00:00
|
|
|
subpixel_offset: SubpixelOffset,
|
2021-12-14 03:58:23 +00:00
|
|
|
texture: &Image,
|
2020-09-16 01:06:10 +00:00
|
|
|
) -> bool {
|
2020-06-14 01:53:31 +00:00
|
|
|
let texture_atlas = texture_atlases.get_mut(&self.texture_atlas).unwrap();
|
|
|
|
if let Some(index) =
|
|
|
|
self.dynamic_texture_atlas_builder
|
|
|
|
.add_texture(texture_atlas, textures, texture)
|
|
|
|
{
|
2021-01-03 20:39:11 +00:00
|
|
|
self.glyph_to_atlas_index
|
|
|
|
.insert((glyph_id, subpixel_offset), index);
|
2020-09-16 01:06:10 +00:00
|
|
|
true
|
2020-06-14 01:53:31 +00:00
|
|
|
} else {
|
2020-09-16 01:06:10 +00:00
|
|
|
false
|
2020-06-14 01:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|