use sim_engine::{
FontBudget, FontError, LogicalPixels, PhysicalPerLogical, TextDirection, TextLayoutBudget,
TextStyle,
};
use super::TextAtlasLimits;
use super::TextError;
#[cfg(feature = "text")]
use sim_engine::TextAtlasBudget;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextLimits {
max_fonts: usize,
max_font_bytes: usize,
}
impl TextLimits {
pub const fn new(max_fonts: usize, max_font_bytes: usize) -> Self {
Self {
max_fonts,
max_font_bytes,
}
}
pub const fn max_fonts(self) -> usize {
self.max_fonts
}
pub const fn max_font_bytes(self) -> usize {
self.max_font_bytes
}
}
impl Default for TextLimits {
fn default() -> Self {
Self::new(8, 32 * 1024 * 1024)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextSettings {
logical_em_size: f32,
direction: TextDirection,
font_budget: FontBudget,
layout_budget: TextLayoutBudget,
atlas_limits: TextAtlasLimits,
#[cfg(feature = "text")]
atlas_budget: TextAtlasBudget,
}
impl TextSettings {
pub fn new(logical_em_size: f32) -> Result<Self, TextError> {
let settings = Self {
logical_em_size,
..Self::default()
};
settings.style(1.0)?;
Ok(settings)
}
pub const fn with_direction(mut self, direction: TextDirection) -> Self {
self.direction = direction;
self
}
pub const fn with_font_budget(mut self, budget: FontBudget) -> Self {
self.font_budget = budget;
self
}
pub const fn with_layout_budget(mut self, budget: TextLayoutBudget) -> Self {
self.layout_budget = budget;
self
}
#[cfg(feature = "text")]
pub const fn with_atlas_budget(mut self, budget: TextAtlasBudget) -> Self {
self.atlas_limits = TextAtlasLimits::from_engine(budget);
self.atlas_budget = budget;
self
}
pub const fn logical_em_size(self) -> f32 {
self.logical_em_size
}
pub const fn direction(self) -> TextDirection {
self.direction
}
pub const fn font_budget(self) -> FontBudget {
self.font_budget
}
pub const fn layout_budget(self) -> TextLayoutBudget {
self.layout_budget
}
#[cfg(feature = "text")]
pub const fn atlas_budget(self) -> TextAtlasBudget {
self.atlas_budget
}
pub fn with_atlas_limits(mut self, limits: TextAtlasLimits) -> Result<Self, TextError> {
#[cfg(feature = "text")]
{
self.atlas_budget = limits.engine_budget()?;
}
self.atlas_limits = limits;
Ok(self)
}
pub const fn atlas_limits(self) -> TextAtlasLimits {
self.atlas_limits
}
pub fn style(self, scale: f32) -> Result<TextStyle, TextError> {
let size = LogicalPixels::new(self.logical_em_size)
.map_err(|_| TextError::Font(FontError::InvalidScale))?;
let scale =
PhysicalPerLogical::new(scale).map_err(|_| TextError::Font(FontError::InvalidScale))?;
Ok(TextStyle::new(size, scale)?.with_direction(self.direction))
}
}
impl Default for TextSettings {
fn default() -> Self {
Self {
logical_em_size: 24.0,
direction: TextDirection::Auto,
font_budget: FontBudget::default(),
layout_budget: TextLayoutBudget::new(4096, 1024, 1024 * 1024, 16 * 1024),
atlas_limits: TextAtlasLimits::default(),
#[cfg(feature = "text")]
atlas_budget: TextAtlasBudget::default(),
}
}
}