use strata_primitives::geom::LayoutUnit;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FontMetrics {
pub ascent: LayoutUnit,
pub descent: LayoutUnit,
pub line_gap: LayoutUnit,
pub x_height: LayoutUnit,
pub cap_height: LayoutUnit,
pub units_per_em: u16,
pub font_size: f32,
}
impl FontMetrics {
pub fn default_sans(font_size: f32) -> Self {
let ascent = LayoutUnit::from_f32(font_size * 0.82);
let descent = LayoutUnit::from_f32(font_size * 0.22);
let line_gap = LayoutUnit::from_f32(font_size * 0.06);
let x_height = LayoutUnit::from_f32(font_size * 0.52);
let cap_height = LayoutUnit::from_f32(font_size * 0.72);
Self {
ascent,
descent,
line_gap,
x_height,
cap_height,
units_per_em: 1000,
font_size,
}
}
pub fn default_mono(font_size: f32) -> Self {
let ascent = LayoutUnit::from_f32(font_size * 0.85);
let descent = LayoutUnit::from_f32(font_size * 0.25);
let line_gap = LayoutUnit::from_f32(font_size * 0.0);
let x_height = LayoutUnit::from_f32(font_size * 0.54);
let cap_height = LayoutUnit::from_f32(font_size * 0.74);
Self {
ascent,
descent,
line_gap,
x_height,
cap_height,
units_per_em: 1000,
font_size,
}
}
#[inline(always)]
pub fn natural_height(&self) -> LayoutUnit {
self.ascent + self.descent
}
#[inline(always)]
pub fn default_line_height(&self) -> LayoutUnit {
self.natural_height() + self.line_gap
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct FontHeight {
pub ascent: LayoutUnit,
pub descent: LayoutUnit,
}
impl FontHeight {
pub const EMPTY: Self = Self {
ascent: LayoutUnit(i32::MIN),
descent: LayoutUnit(i32::MIN),
};
pub const ZERO: Self = Self {
ascent: LayoutUnit::ZERO,
descent: LayoutUnit::ZERO,
};
#[inline(always)]
pub fn new(ascent: LayoutUnit, descent: LayoutUnit) -> Self {
Self { ascent, descent }
}
#[inline(always)]
pub fn from_metrics(metrics: &FontMetrics) -> Self {
Self {
ascent: metrics.ascent,
descent: metrics.descent,
}
}
#[inline(always)]
pub fn is_empty(self) -> bool {
self.ascent.0 == i32::MIN && self.descent.0 == i32::MIN
}
#[inline(always)]
pub fn line_height(self) -> LayoutUnit {
if self.is_empty() {
LayoutUnit::ZERO
} else {
self.ascent + self.descent
}
}
#[inline(always)]
pub fn add_leading(&mut self, leading: FontHeight) {
if !self.is_empty() {
self.ascent = self.ascent + leading.ascent;
self.descent = self.descent + leading.descent;
}
}
#[inline(always)]
pub fn move_by(&mut self, delta: LayoutUnit) {
if !self.is_empty() {
self.ascent = self.ascent - delta;
self.descent = self.descent + delta;
}
}
#[inline(always)]
pub fn unite(&mut self, other: FontHeight) {
if other.is_empty() {
return;
}
if self.is_empty() {
*self = other;
return;
}
if other.ascent > self.ascent {
self.ascent = other.ascent;
}
if other.descent > self.descent {
self.descent = other.descent;
}
}
}
pub fn compute_half_leading(
line_height: LayoutUnit,
ascent: LayoutUnit,
descent: LayoutUnit,
) -> (LayoutUnit, LayoutUnit) {
let font_height = ascent + descent;
let leading = line_height - font_height;
let leading_above = LayoutUnit(leading.0 / 2);
let leading_below = leading - leading_above;
(leading_above, leading_below)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CssStrut {
pub metrics: FontHeight,
pub line_height: LayoutUnit,
}
impl CssStrut {
pub fn new(font_metrics: &FontMetrics, explicit_line_height: Option<f32>) -> Self {
let line_height = match explicit_line_height {
Some(lh) if lh > 0.0 => LayoutUnit::from_f32(lh),
_ => font_metrics.default_line_height(),
};
let (leading_above, leading_below) =
compute_half_leading(line_height, font_metrics.ascent, font_metrics.descent);
let metrics = FontHeight::new(
font_metrics.ascent + leading_above,
font_metrics.descent + leading_below,
);
Self {
metrics,
line_height,
}
}
}