use std::error::Error;
use std::fmt;
use super::FontError;
use crate::renderer::{GlyphError, GlyphRunBudget, ImageBudget, ImageError};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextAtlasBudget {
width: u32,
height: u32,
max_cached_glyphs: usize,
run: GlyphRunBudget,
}
impl TextAtlasBudget {
pub fn new(
width: u32,
height: u32,
max_cached_glyphs: usize,
run: GlyphRunBudget,
) -> Result<Self, TextError> {
if width < 5 || height < 5 || !(1..=65535).contains(&max_cached_glyphs) {
return Err(TextError::InvalidBudget);
}
let budget = Self {
width,
height,
max_cached_glyphs,
run,
};
budget.image_budget()?;
Ok(budget)
}
pub const fn width(self) -> u32 {
self.width
}
pub const fn height(self) -> u32 {
self.height
}
pub const fn max_cached_glyphs(self) -> usize {
self.max_cached_glyphs
}
pub const fn run_budget(self) -> GlyphRunBudget {
self.run
}
pub(super) fn image_budget(self) -> Result<ImageBudget, TextError> {
let bytes = (self.width as usize)
.checked_mul(self.height as usize)
.and_then(|area| area.checked_mul(4))
.ok_or(TextError::InvalidBudget)?;
Ok(ImageBudget::new(self.width, self.height, bytes)?)
}
}
impl Default for TextAtlasBudget {
fn default() -> Self {
Self {
width: 1024,
height: 1024,
max_cached_glyphs: 4096,
run: GlyphRunBudget::default(),
}
}
}
#[derive(Debug)]
pub enum TextError {
Font(FontError),
Glyph(GlyphError),
InvalidBudget,
AtlasFull,
GlyphCacheFull,
AtlasMismatch,
InvalidPlacement,
AllocationFailed,
}
impl fmt::Display for TextError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Font(error) => write!(formatter, "text font: {error}"),
Self::Glyph(error) => write!(formatter, "text glyph resource: {error}"),
Self::InvalidBudget => formatter.write_str("invalid text atlas budget"),
Self::AtlasFull => formatter.write_str("fixed text atlas is full"),
Self::GlyphCacheFull => formatter.write_str("text glyph cache count limit reached"),
Self::AtlasMismatch => formatter.write_str("text run belongs to another atlas"),
Self::InvalidPlacement => {
formatter.write_str("text placement is not finite or representable")
}
Self::AllocationFailed => formatter.write_str("text storage allocation failed"),
}
}
}
impl Error for TextError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Font(error) => Some(error),
Self::Glyph(error) => Some(error),
_ => None,
}
}
}
impl From<FontError> for TextError {
fn from(error: FontError) -> Self {
Self::Font(error)
}
}
impl From<GlyphError> for TextError {
fn from(error: GlyphError) -> Self {
Self::Glyph(error)
}
}
impl From<ImageError> for TextError {
fn from(error: ImageError) -> Self {
Self::Glyph(error.into())
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct TextUpdateReport {
changed: bool,
instance_uploaded_bytes: usize,
cached_glyphs_added: usize,
}
impl TextUpdateReport {
pub(super) const fn new(instance_uploaded_bytes: usize, cached_glyphs_added: usize) -> Self {
Self {
changed: true,
instance_uploaded_bytes,
cached_glyphs_added,
}
}
pub const fn changed(self) -> bool {
self.changed
}
pub const fn instance_uploaded_bytes(self) -> usize {
self.instance_uploaded_bytes
}
pub const fn cached_glyphs_added(self) -> usize {
self.cached_glyphs_added
}
}