use super::config::FontConfig;
use super::loader::FontLoader;
use super::validated::StbTrueTypeFontData;
#[derive(Clone, Debug)]
pub struct FontSource<'a> {
pub(super) kind: FontSourceKind<'a>,
pub(super) size_pixels: Option<f32>,
pub(super) config: Option<FontConfig>,
}
#[derive(Clone, Debug)]
pub(super) enum FontSourceKind<'a> {
Default,
DefaultVector,
DefaultBitmap,
StbTrueType(StbTrueTypeFontData),
TtfData(&'a [u8]),
CompressedTtfData(&'a [u8]),
CompressedTtfBase85(&'a str),
}
impl<'a> FontSource<'a> {
pub fn default_font() -> Self {
Self {
kind: FontSourceKind::Default,
size_pixels: None,
config: None,
}
}
pub fn default_font_with_size(size: f32) -> Self {
Self {
size_pixels: Some(size),
..Self::default_font()
}
}
pub fn default_vector() -> Self {
Self {
kind: FontSourceKind::DefaultVector,
size_pixels: None,
config: None,
}
}
pub fn default_vector_with_size(size: f32) -> Self {
Self {
size_pixels: Some(size),
..Self::default_vector()
}
}
pub fn default_bitmap() -> Self {
Self {
kind: FontSourceKind::DefaultBitmap,
size_pixels: None,
config: None,
}
}
pub fn default_bitmap_with_size(size: f32) -> Self {
Self {
size_pixels: Some(size),
..Self::default_bitmap()
}
}
pub fn stb_truetype(data: StbTrueTypeFontData) -> Self {
Self {
kind: FontSourceKind::StbTrueType(data),
size_pixels: None,
config: None,
}
}
pub fn stb_truetype_with_size(data: StbTrueTypeFontData, size: f32) -> Self {
Self {
size_pixels: Some(size),
..Self::stb_truetype(data)
}
}
pub unsafe fn ttf_data(data: &'a [u8]) -> Self {
Self {
kind: FontSourceKind::TtfData(data),
size_pixels: None,
config: None,
}
}
pub unsafe fn ttf_data_with_size(data: &'a [u8], size: f32) -> Self {
Self {
size_pixels: Some(size),
..unsafe { Self::ttf_data(data) }
}
}
pub unsafe fn compressed_ttf_data(data: &'a [u8]) -> Self {
Self {
kind: FontSourceKind::CompressedTtfData(data),
size_pixels: None,
config: None,
}
}
pub unsafe fn compressed_ttf_data_with_size(data: &'a [u8], size: f32) -> Self {
Self {
size_pixels: Some(size),
..unsafe { Self::compressed_ttf_data(data) }
}
}
pub unsafe fn compressed_ttf_base85(data: &'a str) -> Self {
Self {
kind: FontSourceKind::CompressedTtfBase85(data),
size_pixels: None,
config: None,
}
}
pub unsafe fn compressed_ttf_base85_with_size(data: &'a str, size: f32) -> Self {
Self {
size_pixels: Some(size),
..unsafe { Self::compressed_ttf_base85(data) }
}
}
pub fn with_config(mut self, config: FontConfig) -> Self {
if matches!(&self.kind, FontSourceKind::StbTrueType(_)) {
assert_stb_truetype_config(&config, "FontSource::with_config()");
}
self.config = Some(config);
self
}
}
pub(super) fn assert_stb_truetype_config(config: &FontConfig, caller: &str) {
let stb_loader = FontLoader::stb_truetype().as_ptr();
assert!(
config.raw.FontLoader.is_null() || config.raw.FontLoader == stb_loader,
"{caller} cannot override a validated stb_truetype source with another font loader"
);
assert_eq!(
config.raw.FontNo, 0,
"{caller} cannot override the standalone font index of a validated stb_truetype source"
);
}