use std::{
fmt,
hash::{Hash, Hasher},
sync::Arc,
};
use sim_engine::FontFace;
use crate::identity::ApplicationId;
use super::{TextError, TextLimits, TextPreparationSession, TextSettings};
#[derive(Clone)]
pub struct TextFont {
application: ApplicationId,
slot: usize,
data: Arc<FontData>,
}
#[derive(Debug)]
struct FontData {
face: FontFace,
face_slot: usize,
settings: TextSettings,
}
impl TextFont {
pub fn shares_face_with(&self, other: &Self) -> bool {
self.application == other.application && self.data.face_slot == other.data.face_slot
}
pub fn settings(&self) -> TextSettings {
self.data.settings
}
pub fn font_bytes(&self) -> usize {
self.data.face.allocation_bytes()
}
pub fn shaping_session(&self) -> Result<TextPreparationSession<'_>, TextError> {
TextPreparationSession::new(self)
}
pub(crate) fn face(&self) -> &FontFace {
&self.data.face
}
#[cfg(any(all(feature = "desktop", feature = "text"), test))]
pub(crate) const fn slot(&self) -> usize {
self.slot
}
}
impl fmt::Debug for TextFont {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("TextFont")
.field("application", &self.application)
.field("slot", &self.slot)
.finish_non_exhaustive()
}
}
impl PartialEq for TextFont {
fn eq(&self, other: &Self) -> bool {
self.application == other.application && self.slot == other.slot
}
}
impl Eq for TextFont {}
impl Hash for TextFont {
fn hash<H: Hasher>(&self, state: &mut H) {
self.application.hash(state);
self.slot.hash(state);
}
}
#[derive(Debug)]
pub(crate) struct TextRegistry {
application: ApplicationId,
limits: TextLimits,
fonts: Vec<TextFont>,
font_bytes: usize,
}
impl TextRegistry {
pub(crate) const fn new(application: ApplicationId, limits: TextLimits) -> Self {
Self {
application,
limits,
fonts: Vec::new(),
font_bytes: 0,
}
}
pub(crate) fn register(
&mut self,
bytes: Vec<u8>,
settings: TextSettings,
) -> Result<TextFont, TextError> {
settings.style(1.0)?;
if self.fonts.len() >= self.limits.max_fonts() {
return Err(TextError::FontLimitExceeded {
limit: self.limits.max_fonts(),
});
}
let incoming = bytes.capacity();
if incoming > self.limits.max_font_bytes().saturating_sub(self.font_bytes) {
return Err(TextError::FontByteLimitExceeded {
limit: self.limits.max_font_bytes(),
retained: self.font_bytes,
incoming,
});
}
let face = FontFace::from_bytes(bytes, settings.font_budget())?;
self.fonts
.try_reserve_exact(1)
.map_err(|source| TextError::AllocationFailed { source })?;
let font = TextFont {
application: self.application,
slot: self.fonts.len(),
data: Arc::new(FontData {
face,
settings,
face_slot: self.fonts.len(),
}),
};
self.font_bytes += incoming;
self.fonts.push(font.clone());
Ok(font)
}
pub(crate) fn register_style(
&mut self,
source: &TextFont,
settings: TextSettings,
) -> Result<TextFont, TextError> {
if !self.contains(source) {
return Err(TextError::ForeignFont);
}
settings.style(1.0)?;
if self.fonts.len() >= self.limits.max_fonts() {
return Err(TextError::FontLimitExceeded {
limit: self.limits.max_fonts(),
});
}
let face = source.face();
for (resource, requested, limit) in [
(
sim_engine::FontBudgetResource::FontBytes,
face.allocation_bytes(),
settings.font_budget().max_font_bytes(),
),
(
sim_engine::FontBudgetResource::FontGlyphs,
face.glyph_count(),
settings.font_budget().max_font_glyphs(),
),
] {
if requested > limit {
return Err(sim_engine::FontError::BudgetExceeded {
resource,
required: requested,
limit,
}
.into());
}
}
self.fonts
.try_reserve_exact(1)
.map_err(|source| TextError::AllocationFailed { source })?;
let font = TextFont {
application: self.application,
slot: self.fonts.len(),
data: Arc::new(FontData {
face: face.clone(),
settings,
face_slot: source.data.face_slot,
}),
};
self.fonts.push(font.clone());
Ok(font)
}
pub(crate) fn contains(&self, font: &TextFont) -> bool {
font.application == self.application
&& self
.fonts
.get(font.slot)
.is_some_and(|stored| stored == font && Arc::ptr_eq(&stored.data, &font.data))
}
pub(crate) fn len(&self) -> usize {
self.fonts.len()
}
pub(crate) const fn font_bytes(&self) -> usize {
self.font_bytes
}
}