use std::collections::{HashMap, HashSet};
use sdl3::{pixels::Color, render::{Texture, TextureValueError, UpdateTextureError}, Error};
use ab_glyph::Font;
pub trait ThreadSafeFont: Font + Send + Sync {}
impl<T: Font + Send + Sync> ThreadSafeFont for T {}
pub struct TextCache<'a, Font: ThreadSafeFont> {
pub(crate) map_regular: HashMap<(char, Color), (Texture<'a>, u32, u32, f32, f32)>,
pub(crate) set_regular: HashSet<(char, Color)>,
pub(crate) map_subpixel: HashMap<(char, u32, Color, Color), (Texture<'a>, u32, u32, f32, f32)>,
pub(crate) set_subpixel: HashSet<(char, u32, Color, Color)>,
pub(crate) font: &'a Font,
}
impl<'a, Font: ThreadSafeFont> TextCache<'a, Font> {
#[inline]
pub fn new(font: &'a Font) -> Self {
Self {
map_regular: HashMap::new(),
set_regular: HashSet::new(),
map_subpixel: HashMap::new(),
set_subpixel: HashSet::new(),
font,
}
}
pub fn clear(&mut self) {
self.map_regular.clear();
self.set_regular.clear();
self.map_subpixel.clear();
self.set_subpixel.clear();
}
}
#[derive(Debug)]
pub enum RenderTextError {
SdlError (Error),
SdlTextureValueError (TextureValueError),
SdlUpdateTextureError (UpdateTextureError),
}
impl From<Error> for RenderTextError {
fn from(value: Error) -> Self {
Self::SdlError (value)
}
}
impl From<TextureValueError> for RenderTextError {
fn from(value: TextureValueError) -> Self {
Self::SdlTextureValueError (value)
}
}
impl From<UpdateTextureError> for RenderTextError {
fn from(value: UpdateTextureError) -> Self {
Self::SdlUpdateTextureError (value)
}
}
impl std::fmt::Display for RenderTextError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SdlError (err) => write!(f, "Render Text Error: {err}"),
Self::SdlTextureValueError (err) => write!(f, "Render Text Error: {err}"),
Self::SdlUpdateTextureError (err) => write!(f, "Render Text Error: {err}"),
}
}
}
impl std::error::Error for RenderTextError {}