2020-11-13 00:21:48 +00:00
|
|
|
use ab_glyph::{FontArc, FontVec, InvalidFont, OutlinedGlyph};
|
2020-11-28 00:39:59 +00:00
|
|
|
use bevy_reflect::TypeUuid;
|
2021-03-17 23:59:51 +00:00
|
|
|
use bevy_render::texture::{Extent3d, Texture, TextureDimension, TextureFormat};
|
2020-05-13 20:09:32 +00:00
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
#[derive(Debug, TypeUuid)]
|
|
|
|
#[uuid = "97059ac6-c9ba-4da9-95b6-bed82c3ce198"]
|
2020-05-13 20:09:32 +00:00
|
|
|
pub struct Font {
|
2020-11-13 00:21:48 +00:00
|
|
|
pub font: FontArc,
|
2020-05-13 20:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Font {
|
2020-05-31 22:59:11 +00:00
|
|
|
pub fn try_from_bytes(font_data: Vec<u8>) -> Result<Self, InvalidFont> {
|
|
|
|
let font = FontVec::try_from_vec(font_data)?;
|
2020-11-13 00:21:48 +00:00
|
|
|
let font = FontArc::new(font);
|
2020-05-31 22:59:11 +00:00
|
|
|
Ok(Font { font })
|
2020-05-13 20:09:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 01:53:31 +00:00
|
|
|
pub fn get_outlined_glyph_texture(outlined_glyph: OutlinedGlyph) -> Texture {
|
|
|
|
let bounds = outlined_glyph.px_bounds();
|
|
|
|
let width = bounds.width() as usize;
|
|
|
|
let height = bounds.height() as usize;
|
|
|
|
let mut alpha = vec![0.0; width * height];
|
|
|
|
outlined_glyph.draw(|x, y, v| {
|
|
|
|
alpha[y as usize * width + x as usize] = v;
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: make this texture grayscale
|
|
|
|
Texture::new(
|
2020-11-22 20:04:47 +00:00
|
|
|
Extent3d::new(width as u32, height as u32, 1),
|
|
|
|
TextureDimension::D2,
|
2020-06-14 01:53:31 +00:00
|
|
|
alpha
|
|
|
|
.iter()
|
2021-03-17 23:59:51 +00:00
|
|
|
.map(|a| vec![255, 255, 255, (*a * 255.0) as u8])
|
2020-06-14 01:53:31 +00:00
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<u8>>(),
|
2020-07-26 19:08:41 +00:00
|
|
|
TextureFormat::Rgba8UnormSrgb,
|
2020-06-14 01:53:31 +00:00
|
|
|
)
|
|
|
|
}
|
2020-05-13 20:09:32 +00:00
|
|
|
}
|