bevy/crates/bevy_text/src/font.rs

31 lines
907 B
Rust
Raw Normal View History

2020-05-13 20:09:32 +00:00
use crate::render::render_text;
use bevy_render::{texture::Texture, Color};
use font_kit::{error::FontLoadingError, metrics::Metrics};
use skribo::{FontCollection, FontFamily};
use std::sync::Arc;
pub struct Font {
pub collection: FontCollection,
pub metrics: Metrics,
}
2020-05-16 02:46:09 +00:00
unsafe impl Send for Font {}
unsafe impl Sync for Font {}
2020-05-13 20:09:32 +00:00
impl Font {
pub fn try_from_bytes(font_data: Vec<u8>) -> Result<Self, FontLoadingError> {
let font = font_kit::font::Font::from_bytes(Arc::new(font_data), 0)?;
let metrics = font.metrics();
let mut collection = FontCollection::new();
collection.add_family(FontFamily::new_from_font(font));
Ok(Font {
collection,
metrics,
})
}
pub fn render_text(&self, text: &str, color: Color, width: usize, height: usize) -> Texture {
render_text(self, text, color, width, height)
}
}