use crate::introspection::Tagged;
use ttf_parser::Tag;
use crate::foundations::{Content, Smart, elem};
use crate::layout::{Em, Length};
use crate::text::{FontMetrics, ScriptMetrics, TextSize};
#[elem(title = "Subscript", Tagged)]
pub struct SubElem {
#[default(true)]
pub typographic: bool,
pub baseline: Smart<Length>,
pub size: Smart<TextSize>,
#[required]
pub body: Content,
}
#[elem(title = "Superscript", Tagged)]
pub struct SuperElem {
#[default(true)]
pub typographic: bool,
pub baseline: Smart<Length>,
pub size: Smart<TextSize>,
#[required]
pub body: Content,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ShiftSettings {
pub typographic: bool,
pub shift: Smart<Em>,
pub size: Smart<Em>,
pub kind: ScriptKind,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ScriptKind {
Sub,
Super,
}
impl ScriptKind {
pub fn default_metrics(self) -> &'static ScriptMetrics {
match self {
Self::Sub => &DEFAULT_SUBSCRIPT_METRICS,
Self::Super => &DEFAULT_SUPERSCRIPT_METRICS,
}
}
pub fn read_metrics(self, font_metrics: &FontMetrics) -> &ScriptMetrics {
match self {
Self::Sub => font_metrics.subscript.as_ref(),
Self::Super => font_metrics.superscript.as_ref(),
}
.unwrap_or(self.default_metrics())
}
pub const fn feature(self) -> Tag {
match self {
Self::Sub => Tag::from_bytes(b"subs"),
Self::Super => Tag::from_bytes(b"sups"),
}
}
}
pub static DEFAULT_SUBSCRIPT_METRICS: ScriptMetrics = ScriptMetrics {
width: Em::new(0.6),
height: Em::new(0.6),
horizontal_offset: Em::zero(),
vertical_offset: Em::new(-0.2),
};
pub static DEFAULT_SUPERSCRIPT_METRICS: ScriptMetrics = ScriptMetrics {
width: Em::new(0.6),
height: Em::new(0.6),
horizontal_offset: Em::zero(),
vertical_offset: Em::new(0.5),
};