use text_typeset::{FontFaceId, TextFontService};
pub(crate) fn shared_static(bytes: &'static [u8]) -> text_typeset::SharedFontData {
std::sync::Arc::new(bytes)
}
#[derive(Clone)]
pub struct FontFaceSpec {
pub data: text_typeset::SharedFontData,
pub is_default: bool,
pub default_size_px: f32,
}
impl std::fmt::Debug for FontFaceSpec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FontFaceSpec")
.field("data_len", &self.data.as_ref().as_ref().len())
.field("is_default", &self.is_default)
.field("default_size_px", &self.default_size_px)
.finish()
}
}
pub trait FontRegistrar {
fn register_on_service(&self, service: &mut TextFontService) -> Option<FontFaceId>;
}
pub struct EmbeddedInterRegistrar {
data: &'static [u8],
data_italic: &'static [u8],
mono_data: &'static [u8],
mono_data_italic: &'static [u8],
default_size_px: f32,
}
impl EmbeddedInterRegistrar {
pub const fn new() -> Self {
Self {
data: crate::embedded_fonts::INTER,
data_italic: crate::embedded_fonts::INTER_ITALIC,
mono_data: crate::embedded_fonts::JETBRAINS_MONO,
mono_data_italic: crate::embedded_fonts::JETBRAINS_MONO_ITALIC,
default_size_px: 14.0,
}
}
pub const fn with_size(size_px: f32) -> Self {
Self {
data: crate::embedded_fonts::INTER,
data_italic: crate::embedded_fonts::INTER_ITALIC,
mono_data: crate::embedded_fonts::JETBRAINS_MONO,
mono_data_italic: crate::embedded_fonts::JETBRAINS_MONO_ITALIC,
default_size_px: size_px,
}
}
}
impl Default for EmbeddedInterRegistrar {
fn default() -> Self {
Self::new()
}
}
impl FontRegistrar for EmbeddedInterRegistrar {
fn register_on_service(&self, service: &mut TextFontService) -> Option<FontFaceId> {
let face = service.register_font_shared(shared_static(self.data));
service.set_default_font(face, self.default_size_px);
let _ = service.register_font_shared(shared_static(self.data_italic));
service.set_generic_family("Inter", "Inter Variable");
let _ = service.register_font_shared(shared_static(self.mono_data));
let _ = service.register_font_shared(shared_static(self.mono_data_italic));
Some(face)
}
}
pub struct VecFontRegistrar {
faces: Vec<FontFaceSpec>,
}
impl VecFontRegistrar {
pub fn new(faces: Vec<FontFaceSpec>) -> Self {
Self { faces }
}
}
impl FontRegistrar for VecFontRegistrar {
fn register_on_service(&self, service: &mut TextFontService) -> Option<FontFaceId> {
let mut default = None;
for spec in &self.faces {
let face = service.register_font_shared(spec.data.clone());
if spec.is_default && default.is_none() {
service.set_default_font(face, spec.default_size_px);
default = Some(face);
}
}
default
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_static_face_is_shared_rather_than_copied() {
static FACE: &[u8] = b"not really a font, but the bytes are the point";
let spec = FontFaceSpec {
data: std::sync::Arc::new(FACE),
is_default: false,
default_size_px: 14.0,
};
let held: &[u8] = spec.data.as_ref().as_ref();
assert_eq!(
held.as_ptr(),
FACE.as_ptr(),
"the spec must point at the original bytes, not a copy of them"
);
assert_eq!(held.len(), FACE.len());
}
#[test]
fn an_owned_face_still_describes_itself() {
let bytes = vec![1u8, 2, 3, 4];
let spec = FontFaceSpec {
data: std::sync::Arc::new(bytes),
is_default: true,
default_size_px: 18.0,
};
assert_eq!(spec.data.as_ref().as_ref(), &[1u8, 2, 3, 4]);
assert!(spec.is_default);
}
}