use crate::text::style::{
LineHeightAlignment, LineHeightMode, LineHeightStyle, LineHeightTrim, TextStyle,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LineBox {
pub height: f32,
pub baseline: f32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FontExtent {
pub ascent: f32,
pub descent: f32,
pub line_gap: f32,
}
impl FontExtent {
pub fn new(ascent: f32, descent: f32, line_gap: f32) -> Self {
Self {
ascent,
descent,
line_gap,
}
}
pub fn natural(self) -> f32 {
self.ascent + self.descent
}
}
pub fn line_box(style: &TextStyle, extent: FontExtent, asked: f32, grid: f32) -> LineBox {
let grid = if grid.is_finite() && grid > 0.0 {
grid
} else {
1.0
};
match style.paragraph_style.line_height_style {
None => unstyled_line_box(extent, asked, grid),
Some(line_height_style) => {
let padding = font_padding(style, extent);
aosp_line_box(line_height_style, extent, asked, padding, grid)
}
}
}
fn unstyled_line_box(extent: FontExtent, asked: f32, grid: f32) -> LineBox {
let natural = (extent.natural() * grid).ceil() / grid;
LineBox {
height: asked,
baseline: extent.ascent + (asked - natural) * 0.5,
}
}
fn font_padding(style: &TextStyle, extent: FontExtent) -> f32 {
let asked = style
.paragraph_style
.platform_style
.and_then(|platform| platform.include_font_padding)
.unwrap_or(false);
if asked && extent.line_gap.is_finite() && extent.line_gap > 0.0 {
extent.line_gap
} else {
0.0
}
}
fn aosp_line_box(
style: LineHeightStyle,
extent: FontExtent,
asked: f32,
padding: f32,
grid: f32,
) -> LineBox {
let up = |value: f32| (value * grid).ceil() / grid;
let down = |value: f32| (value * grid).floor() / grid;
let round = |value: f32| ((value * grid) + 0.5).floor() / grid;
let ascent = -round(-extent.ascent.max(0.0));
let descent = round(extent.descent.max(0.0));
let above_padding = down(padding * 0.5);
let below_padding = padding - above_padding;
let natural = up(ascent + descent + padding);
let asked = if asked.is_finite() {
up(asked)
} else {
natural
};
let height = match style.mode {
LineHeightMode::Fixed => asked.max(1.0),
LineHeightMode::Minimum => asked.max(natural).max(1.0),
LineHeightMode::Tight => natural.max(1.0),
};
let leading = height - (ascent + descent + padding);
let (mut above, mut below) = match style.alignment {
LineHeightAlignment::Top => (0.0, leading),
LineHeightAlignment::Bottom => (leading, 0.0),
LineHeightAlignment::Center => {
let below = up(leading * 0.5);
(leading - below, below)
}
LineHeightAlignment::Proportional => {
let total = ascent + descent;
if total > 0.0 {
let above = leading * (ascent / total);
(above, leading - above)
} else {
(leading * 0.5, leading * 0.5)
}
}
};
above += above_padding;
below += below_padding;
let (trim_above, trim_below) = match style.trim {
LineHeightTrim::None => (false, false),
LineHeightTrim::FirstLineTop => (true, false),
LineHeightTrim::LastLineBottom => (false, true),
LineHeightTrim::Both => (true, true),
};
let mut height = height;
if trim_above {
height -= above;
above = 0.0;
}
if trim_below {
height -= below;
}
LineBox {
height: height.max(1.0),
baseline: above + ascent,
}
}
#[cfg(test)]
#[path = "tests/line_box_tests.rs"]
mod tests;