use crate::{LogicalPixels, PhysicalPerLogical};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FontBudgetResource {
FontBytes,
FontGlyphs,
TextBytes,
ShapedGlyphs,
GlyphPixels,
OutlineSegments,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FontError {
InvalidFont,
BudgetExceeded {
resource: FontBudgetResource,
required: usize,
limit: usize,
},
InvalidScale,
UnsupportedText {
byte_index: usize,
},
MissingGlyph {
byte_index: usize,
},
InvalidGlyph {
glyph_id: u16,
},
UnsupportedGlyph {
glyph_id: u16,
},
InvalidGeometry,
AllocationFailed {
requested_bytes: usize,
},
}
impl std::fmt::Display for FontError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidFont => formatter.write_str("invalid or unsupported outline font"),
Self::BudgetExceeded {
resource,
required,
limit,
} => write!(
formatter,
"font {resource:?} requires {required}, limit is {limit}"
),
Self::InvalidScale => formatter.write_str("invalid physical font scale"),
Self::UnsupportedText { byte_index } => write!(
formatter,
"unsupported single-line character at UTF-8 byte {byte_index}"
),
Self::MissingGlyph { byte_index } => write!(
formatter,
"font has no glyph for UTF-8 cluster at byte {byte_index}"
),
Self::InvalidGlyph { glyph_id } => {
write!(formatter, "invalid font glyph id {glyph_id}")
}
Self::UnsupportedGlyph { glyph_id } => write!(
formatter,
"font glyph {glyph_id} requires unsupported color or bitmap presentation"
),
Self::InvalidGeometry => {
formatter.write_str("font geometry is not finite or representable")
}
Self::AllocationFailed { requested_bytes } => write!(
formatter,
"font allocation failed for {requested_bytes} bytes"
),
}
}
}
impl std::error::Error for FontError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FontBudget {
max_font_bytes: usize,
max_font_glyphs: usize,
}
impl FontBudget {
pub const fn new(max_font_bytes: usize, max_font_glyphs: usize) -> Self {
Self {
max_font_bytes,
max_font_glyphs,
}
}
pub const fn max_font_bytes(self) -> usize {
self.max_font_bytes
}
pub const fn max_font_glyphs(self) -> usize {
self.max_font_glyphs
}
}
impl Default for FontBudget {
fn default() -> Self {
Self::new(8 * 1024 * 1024, 65_535)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextLayoutBudget {
max_text_bytes: usize,
max_shaped_glyphs: usize,
max_glyph_pixels: usize,
max_outline_segments: usize,
}
impl TextLayoutBudget {
pub const fn new(
text_bytes: usize,
shaped_glyphs: usize,
glyph_pixels: usize,
outline_segments: usize,
) -> Self {
Self {
max_text_bytes: text_bytes,
max_shaped_glyphs: shaped_glyphs,
max_glyph_pixels: glyph_pixels,
max_outline_segments: outline_segments,
}
}
pub const fn max_text_bytes(self) -> usize {
self.max_text_bytes
}
pub const fn max_shaped_glyphs(self) -> usize {
self.max_shaped_glyphs
}
pub const fn max_glyph_pixels(self) -> usize {
self.max_glyph_pixels
}
pub const fn max_outline_segments(self) -> usize {
self.max_outline_segments
}
}
impl Default for TextLayoutBudget {
fn default() -> Self {
Self::new(16 * 1024, 16 * 1024, 1024 * 1024, 16 * 1024)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TextDirection {
#[default]
Auto,
Ltr,
Rtl,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextStyle {
logical_em_size: LogicalPixels,
scale: PhysicalPerLogical,
direction: TextDirection,
}
impl TextStyle {
pub fn new(
logical_em_size: LogicalPixels,
scale: PhysicalPerLogical,
) -> Result<Self, FontError> {
let physical = logical_em_size.get() * scale.get();
if !physical.is_normal() || physical <= 0.0 {
return Err(FontError::InvalidScale);
}
Ok(Self {
logical_em_size,
scale,
direction: TextDirection::Auto,
})
}
pub const fn with_direction(mut self, direction: TextDirection) -> Self {
self.direction = direction;
self
}
pub const fn logical_em_size(self) -> LogicalPixels {
self.logical_em_size
}
pub const fn scale(self) -> PhysicalPerLogical {
self.scale
}
pub const fn direction(self) -> TextDirection {
self.direction
}
pub fn physical_em_size(self) -> f32 {
self.logical_em_size.get() * self.scale.get()
}
}
pub(super) fn check(
resource: FontBudgetResource,
required: usize,
limit: usize,
) -> Result<(), FontError> {
if required > limit {
Err(FontError::BudgetExceeded {
resource,
required,
limit,
})
} else {
Ok(())
}
}
pub(super) fn reserve<T>(values: &mut Vec<T>, count: usize) -> Result<(), FontError> {
let requested_bytes = count
.checked_mul(std::mem::size_of::<T>())
.ok_or(FontError::InvalidGeometry)?;
values
.try_reserve_exact(count)
.map_err(|_| FontError::AllocationFailed { requested_bytes })
}