use crate::sugarloaf::primitives::SugarCursor;
use crate::{DrawableChar, Graphic};
use swash::{Attributes, Setting};
pub type FontSettingKey = u32;
#[derive(Default, Clone, Debug)]
pub struct FontSettingCache<T: Copy + PartialOrd + PartialEq + std::fmt::Debug> {
settings: Vec<Setting<T>>,
lists: Vec<FontSettingList>,
tmp: Vec<Setting<T>>,
}
impl<T: Copy + PartialOrd + PartialEq + std::fmt::Debug> FontSettingCache<T> {
pub fn get(&self, key: u32) -> &[Setting<T>] {
if key == !0 {
&[]
} else {
self.lists
.get(key as usize)
.map(|list| list.get(&self.settings))
.unwrap_or(&[])
}
}
pub fn clear(&mut self) {
self.settings.clear();
self.lists.clear();
self.tmp.clear();
}
}
pub const EMPTY_FONT_SETTINGS: FontSettingKey = !0;
#[derive(Copy, Clone, Debug)]
struct FontSettingList {
pub start: u32,
pub end: u32,
}
impl FontSettingList {
pub fn get<T>(self, elements: &[T]) -> &[T] {
elements
.get(self.start as usize..self.end as usize)
.unwrap_or(&[])
}
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug, Default)]
pub enum UnderlineShape {
#[default]
Regular = 0,
Dotted = 1,
Dashed = 2,
Curly = 3,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct UnderlineInfo {
pub is_doubled: bool,
pub shape: UnderlineShape,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum SpanStyleDecoration {
Underline(UnderlineInfo),
Strikethrough,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct SpanStyle {
pub font_id: usize,
pub width: f32,
pub font_attrs: Attributes,
pub color: [f32; 4],
pub background_color: Option<[f32; 4]>,
pub font_vars: FontSettingKey,
pub decoration: Option<SpanStyleDecoration>,
pub decoration_color: Option<[f32; 4]>,
pub cursor: Option<SugarCursor>,
pub media: Option<Graphic>,
pub drawable_char: Option<DrawableChar>,
pub pua_constraint: Option<f32>,
pub nerd_font_constraint: Option<crate::font::nerd_font_attributes::Constraint>,
}
impl Default for SpanStyle {
fn default() -> Self {
Self {
font_id: 0,
width: 1.0,
font_attrs: Attributes::default(),
font_vars: EMPTY_FONT_SETTINGS,
color: [1.0, 1.0, 1.0, 1.0],
background_color: None,
cursor: None,
decoration: None,
decoration_color: None,
media: None,
drawable_char: None,
pua_constraint: None,
nerd_font_constraint: None,
}
}
}