pub(crate) const UNDERLINE_BASELINE_RATIO: f64 = 0.17;
pub(crate) fn underline_multi_lines(shape: u8) -> Option<&'static [(f64, f64)]> {
match shape {
7 => Some(&[(0.160, 0.032), (0.250, 0.032)]),
8 => Some(&[(0.175, 0.048), (0.300, 0.100)]),
9 => Some(&[(0.200, 0.100), (0.325, 0.048)]),
10 => Some(&[(0.165, 0.032), (0.250, 0.080), (0.335, 0.032)]),
_ => None,
}
}
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DecorationStroke {
pub offset: f64,
pub width: f64,
pub dash: Vec<f64>,
}
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DecorationDots {
pub offset: f64,
pub radius: f64,
pub step: f64,
}
#[derive(Debug, Clone, serde::Serialize)]
pub(crate) struct TopUnderlineGeometry {
pub phase: f64,
pub strokes: Vec<DecorationStroke>,
pub dots: Option<DecorationDots>,
}
impl TopUnderlineGeometry {
pub fn vertical_bounds(&self) -> (f64, f64) {
self.strokes.iter().fold(
self.dots
.as_ref()
.map_or((f64::INFINITY, f64::NEG_INFINITY), |dot| {
(dot.offset - dot.radius, dot.offset + dot.radius)
}),
|(top, bottom), stroke| {
(
top.min(stroke.offset - stroke.width / 2.0),
bottom.max(stroke.offset + stroke.width / 2.0),
)
},
)
}
}
pub(crate) fn top_underline_geometry(
style: &super::TextStyle,
font_size: f64,
) -> Option<TopUnderlineGeometry> {
use crate::model::style::UnderlineType;
if style.underline != UnderlineType::Top || style.underline_shape > 10 {
return None;
}
let size = style
.top_underline_font_size
.filter(|size| size.is_finite() && *size > 0.0)
.unwrap_or(font_size);
if !(size * 2.0).is_finite() || size <= 0.0 {
return None;
}
let shape = style.underline_shape;
let phase = style
.top_underline_phase
.filter(|phase| phase.is_finite())
.unwrap_or(0.0);
if shape == 6 {
return Some(TopUnderlineGeometry {
phase,
strokes: Vec::new(),
dots: Some(DecorationDots {
offset: -0.875 * size,
radius: 0.03 * size,
step: size / 7.0,
}),
});
}
let bands: &[(f64, f64)] = match shape {
7 => &[(0.955, 0.030), (0.865, 0.030)],
8 => &[(1.019, 0.048), (0.900, 0.100)],
9 => &[(0.993, 0.100), (0.874, 0.048)],
10 => &[(1.035, 0.030), (0.950, 0.080), (0.865, 0.030)],
_ => &[(0.870, 0.040)],
};
let pattern: &[f64] = match shape {
1 => &[2.0, 3.0],
2 => &[10.0, 6.0],
3 => &[20.0, 6.0, 2.0, 6.0],
4 => &[20.0, 6.0, 2.0, 6.0, 2.0, 6.0],
5 => &[20.0, 6.0],
_ => &[],
};
Some(TopUnderlineGeometry {
phase,
strokes: bands
.iter()
.map(|&(offset, width)| DecorationStroke {
offset: -offset * size,
width: width * size,
dash: pattern.iter().map(|value| value * size / 35.0).collect(),
})
.collect(),
dots: None,
})
}
pub(crate) fn underline_geometry(
style: &super::TextStyle,
font_size: f64,
) -> Option<TopUnderlineGeometry> {
use crate::model::style::UnderlineType;
if style.underline != UnderlineType::Bottom || style.underline_shape != 3 {
return top_underline_geometry(style, font_size);
}
if !(font_size * 2.0).is_finite() || font_size <= 0.0 {
return None;
}
let scale = font_size / 35.0;
let dot = (scale * 2.0 / 0.16).round().max(1.0) * 0.16;
let gap = (scale * 6.0 / 0.32).round().max(1.0) * 0.32;
Some(TopUnderlineGeometry {
phase: style
.top_underline_phase
.filter(|phase| phase.is_finite())
.unwrap_or(0.0),
strokes: vec![DecorationStroke {
offset: font_size * UNDERLINE_BASELINE_RATIO,
width: font_size * 0.04,
dash: vec![scale * 20.0, gap, dot, gap],
}],
dots: None,
})
}
pub(crate) fn resolve_page_phases(root: &mut super::render_tree::RenderNode) {
use super::render_tree::{RenderNodeType, TextRunNode};
use super::text_direction::VisualTextPositions;
use crate::model::style::UnderlineType;
for child in &mut root.children {
resolve_page_phases(child);
}
if !matches!(root.node_type, RenderNodeType::TextLine(_)) {
return;
}
type Key = (u32, u8, u32, bool);
struct Group {
key: Key,
baseline: f64,
end: f64,
origin: f64,
runs: Vec<usize>,
}
let mut groups: Vec<Group> = Vec::new();
let mut active = false;
for (index, node) in root.children.iter().enumerate() {
let RenderNodeType::TextRun(run) = &node.node_type else {
active = false;
continue;
};
let TextRunNode { style, .. } = run;
let Some(id) = run.char_shape_id.filter(|_| {
(style.underline == UnderlineType::Top
|| (style.underline == UnderlineType::Bottom && style.underline_shape == 3))
&& run.rotation == 0.0
&& !run.is_vertical
&& run.char_overlap.is_none()
&& !run.display_or_text().contains('\u{fffc}')
}) else {
active = false;
continue;
};
let text = run.display_or_text();
let positions = run.replay_positions_for(text);
let (Some(&start), Some(&end)) = (positions.first(), positions.last()) else {
active = false;
continue;
};
let (_, baseline) =
style.script_draw_metrics(style.effective_font_size(), node.bbox.y + run.baseline);
let start = node.bbox.x + start;
let end = node.bbox.x + end;
let origin = VisualTextPositions::for_style(text, &positions, style)
.map(|v| {
v.segments(text.chars().count())
.into_iter()
.map(|(left, _)| node.bbox.x + left)
.fold(f64::INFINITY, f64::min)
})
.unwrap_or(start);
if ![start, end, origin, baseline].iter().all(|x| x.is_finite()) || end <= start {
active = false;
continue;
}
let key = (
id,
style.underline_shape,
style.underline_color,
style.underline == UnderlineType::Top,
);
if active
&& groups.last().is_some_and(|g| {
g.key == key && (g.baseline - baseline).abs() < 1e-6 && (g.end - start).abs() < 1e-6
})
{
let g = groups.last_mut().unwrap();
g.end = end;
g.origin = g.origin.min(origin);
g.runs.push(index);
} else {
groups.push(Group {
key,
baseline,
end,
origin,
runs: vec![index],
});
}
active = true;
}
for group in groups {
for index in group.runs {
let node = &mut root.children[index];
if let RenderNodeType::TextRun(run) = &mut node.node_type {
run.style.top_underline_phase = Some(node.bbox.x - group.origin);
}
}
}
}
pub(crate) fn raised_decoration_ink_bbox(
bbox: super::render_tree::BoundingBox,
run: &super::render_tree::TextRunNode,
) -> Option<super::render_tree::BoundingBox> {
use crate::model::style::UnderlineType;
let style = &run.style;
let top_underline = style.underline == UnderlineType::Top && style.underline_shape <= 10;
let emphasis = (1..=6).contains(&style.emphasis_dot);
if !(top_underline || emphasis)
|| run.char_overlap.is_some()
|| run.rotation != 0.0
|| run.is_vertical
|| !run.display_or_text().chars().any(|ch| ch != '\u{FFFC}')
{
return None;
}
let base_size = if style.font_size > 0.0 {
style.font_size
} else {
12.0
};
let (font_size, baseline) = style.script_draw_metrics(base_size, bbox.y + run.baseline);
let mut top = f64::INFINITY;
let mut bottom = f64::NEG_INFINITY;
let mut left = bbox.x;
let mut right = bbox.x + bbox.width;
let geometry = top_underline_geometry(style, font_size);
if let Some(geometry) = &geometry {
let (ink_top, ink_bottom) = geometry.vertical_bounds();
top = top.min(baseline + ink_top);
bottom = bottom.max(baseline + ink_bottom);
}
if emphasis {
let text = run.display_or_text();
let positions = run.replay_positions_for(text);
if let Some(ink) = super::emphasis::geometry(text, &positions, style, font_size)
.and_then(|g| g.ink_bounds())
{
top = top.min(baseline + ink.y);
bottom = bottom.max(baseline + ink.y + ink.height);
left = left.min(bbox.x + ink.x);
right = right.max(bbox.x + ink.x + ink.width);
}
}
let end_pad = geometry
.as_ref()
.and_then(|g| g.dots.as_ref())
.map_or(0.0, |d| d.radius);
let ink = super::render_tree::BoundingBox::new(
left - end_pad,
top,
right - left + 2.0 * end_pad,
bottom - top,
);
[ink.x, ink.y, ink.width, ink.height]
.iter()
.all(|v| v.is_finite())
.then_some(ink)
}