use crate::document::Paragraph;
use crate::graphics::{DecorationLine, DecorationPaint, Rect};
use crate::style::{Style, TextDecoration, UnderlinePosition};
use crate::text::line_boxes::{BoxMetrics, Slot, slot_width};
use crate::text::{Line, TextContext, TextError};
const FALLBACK_THICKNESS_EM: f32 = 0.05;
const FALLBACK_UNDERLINE_EM: f32 = 0.1;
const JOIN_TOLERANCE_PT: f32 = 1.0e-3;
const LINES: [DecorationLine; 3] = [
DecorationLine::Underline,
DecorationLine::Overline,
DecorationLine::LineThrough,
];
struct DecoratingFont {
underline: [f32; 2],
strikeout: [f32; 2],
ascent: f32,
descent: f32,
}
impl DecoratingFont {
fn of(context: &TextContext<'_>, style: &Style) -> Result<Self, TextError> {
let instance = context
.face(context.primary(style)?, style)?
.ok_or(TextError::NoFontCandidates)?;
let face = &instance.face;
let size = instance.used_size(style);
let em = size / face.units_per_em() as f32;
let metrics = BoxMetrics::of(context, style)?;
let fallback = size * FALLBACK_THICKNESS_EM;
let line = |metrics: Option<ttf_parser::LineMetrics>, top: f32| {
metrics
.filter(|metrics| metrics.thickness > 0)
.map_or([top, fallback], |metrics| {
[
-f32::from(metrics.position) * em,
f32::from(metrics.thickness) * em,
]
})
};
Ok(Self {
underline: line(face.underline_metrics(), size * FALLBACK_UNDERLINE_EM),
strikeout: line(
face.strikeout_metrics(),
-(metrics.x_height + fallback) / 2.0,
),
ascent: metrics.ascent,
descent: metrics.descent,
})
}
fn place(&self, line: DecorationLine, decoration: &TextDecoration, style: &Style) -> [f32; 2] {
let [font_top, font_thickness] = match line {
DecorationLine::LineThrough => self.strikeout,
_ => self.underline,
};
let thickness = decoration.thickness.unwrap_or(font_thickness);
let offset = style
.text_decorations
.underline_offset
.as_ref()
.map(|offset| offset.resolve(style.size));
let top = match line {
DecorationLine::Underline => match style.text_decorations.underline_position {
UnderlinePosition::Font => offset.unwrap_or(font_top),
UnderlinePosition::Under => self.descent + offset.unwrap_or(0.0),
},
DecorationLine::Overline => -self.ascent - thickness,
DecorationLine::LineThrough => font_top + (font_thickness - thickness) / 2.0,
};
[top, thickness]
}
}
fn draws(decoration: &TextDecoration, line: DecorationLine) -> bool {
match line {
DecorationLine::Underline => decoration.lines.underline,
DecorationLine::Overline => decoration.lines.overline,
DecorationLine::LineThrough => decoration.lines.line_through,
}
}
struct Decorating<'a> {
owner: Option<usize>,
style: &'a Style,
baseline: f32,
decorations: Vec<&'a TextDecoration>,
}
pub(crate) fn paint(
context: &TextContext<'_>,
paragraph: &Paragraph,
line: &Line,
) -> Result<Vec<DecorationPaint>, TextError> {
let mut decorating = vec![Decorating {
owner: None,
style: paragraph.style(),
baseline: 0.0,
decorations: paragraph.style().text_decorations.applied().collect(),
}];
for fragment in &line.boxes {
let style = ¶graph.boxes()[fragment.index].style;
let declared = &style.text_decorations.declared;
if declared.lines.is_empty()
|| decorating
.last()
.is_some_and(|last| last.owner == Some(fragment.index))
{
continue;
}
decorating.push(Decorating {
owner: Some(fragment.index),
style,
baseline: fragment.baseline,
decorations: vec![declared],
});
}
decorating.retain(|decorating| !decorating.decorations.is_empty());
let mut placed = Vec::with_capacity(decorating.len());
for decorating in &decorating {
placed.push((
DecoratingFont::of(context, decorating.style)?,
segments(paragraph, line, decorating.owner),
));
}
let mut paints = Vec::new();
for kind in LINES {
for (decorating, (font, segments)) in decorating.iter().zip(&placed) {
for decoration in decorating
.decorations
.iter()
.filter(|decoration| draws(decoration, kind))
{
let [top, thickness] = font.place(kind, decoration, decorating.style);
paints.extend(segments.iter().map(|&([left, right], positioned)| {
DecorationPaint {
line: kind,
style: decoration.style,
color: decoration.color,
rect: Rect {
x: left,
y: decorating.baseline + top,
width: right - left,
height: thickness,
},
owner: decorating.owner,
positioned,
}
}));
}
}
}
Ok(paints)
}
fn segments(
paragraph: &Paragraph,
line: &Line,
owner: Option<usize>,
) -> Vec<([f32; 2], Option<usize>)> {
let content = line.ellipsis.unwrap_or(line.runs.len());
let inside = |inner: Option<usize>| owner.is_none_or(|owner| paragraph.is_within(inner, owner));
let mut extents: Vec<([f32; 2], Option<usize>)> = Vec::new();
let mut x = 0.0;
for &slot in &line.order {
let width = slot_width(&line.runs, &line.atomics, slot);
let decorated = match slot {
Slot::Run(index) => {
let inline_box = paragraph.spans()[line.runs[index].source_span].inline_box;
((owner.is_none() || index < content) && inside(inline_box)).then_some(inline_box)
}
Slot::Space { owner: inner, .. } => inside(inner).then_some(inner),
Slot::Atomic(_) | Slot::Edge { .. } => None,
};
if let Some(inline_box) = decorated
&& width > 0.0
{
extents.push((
[x, x + width],
paragraph.positioned_within(inline_box, owner),
));
}
x += width;
}
extents.sort_by(|a, b| a.0[0].total_cmp(&b.0[0]));
let mut joined: Vec<([f32; 2], Option<usize>)> = Vec::with_capacity(extents.len());
for ([left, right], positioned) in extents {
match joined.last_mut() {
Some((last, last_positioned))
if *last_positioned == positioned && left <= last[1] + JOIN_TOLERANCE_PT =>
{
last[1] = last[1].max(right);
}
_ => joined.push(([left, right], positioned)),
}
}
joined
}