use crate::document::Paragraph;
use crate::graphics::{Contour, Point, Rect, Segment};
use crate::style::Style;
use crate::style::{EmphasisShape, EmphasisSkip};
use crate::text::line_boxes::BoxMetrics;
use crate::text::shaping::MissingGlyphs;
use crate::text::{GlyphRun, Line, MarkShape, TextContext, TextError};
use icu_properties::{
CodePointMapData,
props::{EastAsianWidth, GeneralCategory, GeneralCategoryGroup},
};
use unicode_segmentation::UnicodeSegmentation;
const MARK_SCALE: f32 = 0.5;
const PUNCTUATION_SYMBOLS: [char; 15] = [
'#', '%', '\u{2030}', '\u{2031}', '\u{066A}', '\u{0609}', '\u{060A}', '&', '\u{204A}', '@',
'\u{00A7}', '\u{00B6}', '\u{204B}', '\u{2053}', '\u{303D}',
];
pub(super) fn marked(cluster: &str, skip: EmphasisSkip) -> bool {
let categories = CodePointMapData::<GeneralCategory>::new();
let mut chars = cluster.chars();
let Some(first) = chars.next() else {
return false;
};
let category = categories.get(first);
if [
GeneralCategory::Control,
GeneralCategory::Format,
GeneralCategory::Unassigned,
]
.contains(&category)
|| (skip.narrow
&& !matches!(
CodePointMapData::<EastAsianWidth>::new().get(first),
EastAsianWidth::Wide | EastAsianWidth::Fullwidth
))
{
return false;
}
if GeneralCategoryGroup::Separator.contains(category) {
return !skip.spaces
|| chars.any(|ch| GeneralCategoryGroup::Mark.contains(categories.get(ch)));
}
let punctuation = GeneralCategoryGroup::Punctuation.contains(category);
let symbol = GeneralCategoryGroup::Symbol.contains(category)
|| (punctuation && {
let normalizer = icu_normalizer::DecomposingNormalizerBorrowed::new_nfkd();
let mut buffer = [0; 4];
let decomposed = normalizer.normalize(first.encode_utf8(&mut buffer));
let mut decomposed = decomposed.chars();
decomposed
.next()
.is_some_and(|symbol| PUNCTUATION_SYMBOLS.contains(&symbol))
&& decomposed.next().is_none()
});
!(symbol && skip.symbols || punctuation && !symbol && skip.punctuation)
}
fn mark_style(style: &Style) -> Style {
let mut mark = style.clone();
mark.size *= MARK_SCALE;
let mut east_asian = mark.font_features.east_asian.to_vec();
east_asian.push(crate::style::RUBY_FEATURE);
mark.font_features.east_asian = east_asian.into();
mark.letter_spacing = 0.0;
mark.word_spacing = crate::style::LengthPercentage::ZERO;
mark.text_paint.fill = mark.text_paint.emphasis_color.clone();
mark.text_paint.stroke_width = 0.0;
mark.text_paint.shadows = std::sync::Arc::from([]);
mark
}
fn has_marks(run: &GlyphRun, skip: EmphasisSkip) -> bool {
if run.combined {
let mut buffer = [0; 4];
return run.width != 0.0
&& marked(
crate::document::OBJECT_REPLACEMENT_CHARACTER.encode_utf8(&mut buffer),
skip,
);
}
run.text
.graphemes(true)
.any(|cluster| marked(cluster, skip))
}
pub(super) fn leading(
context: &TextContext<'_>,
style: &Style,
run: &GlyphRun,
) -> Result<Option<(f32, bool)>, TextError> {
if style.text_paint.emphasis.mark().is_none() || !has_marks(run, style.text_paint.emphasis_skip)
{
return Ok(None);
}
let base = BoxMetrics::of(context, style)?;
let mark = BoxMetrics::of(context, &mark_style(style))?;
let extra = mark.ascent + mark.descent + base.ascent + base.descent - base.line_height();
let under = style.text_paint.emphasis_position.under(&style.language);
Ok((extra > 0.0).then_some((extra, under)))
}
pub(super) fn place(
context: &TextContext<'_>,
paragraph: &Paragraph,
line: &mut Line,
) -> Result<(), TextError> {
for run in &mut line.runs {
let style = ¶graph.spans()[run.source_span].style;
let Some(text) = style.text_paint.emphasis.mark() else {
continue;
};
if !has_marks(run, style.text_paint.emphasis_skip) {
continue;
}
let mark_style = mark_style(style);
let shape = style.text_paint.emphasis.shape();
let shaped = match context.shape(
&text,
0..text.len(),
false,
&mark_style,
if shape.is_some() {
MissingGlyphs::Reject
} else {
MissingGlyphs::Substitute
},
) {
Ok(shaped) => Some(shaped),
Err(TextError::MissingGlyph { .. }) => None,
Err(error) => return Err(error),
};
let base = BoxMetrics::of(context, style)?;
let metrics = BoxMetrics::of(context, &mark_style)?;
let baseline = run.baseline_shift
+ if style.text_paint.emphasis_position.under(&style.language) {
base.descent + metrics.ascent
} else {
-(base.ascent + metrics.descent)
};
let mut marks = Vec::new();
let mut shapes = Vec::new();
let clusters = if run.combined {
vec![(0, run.text.as_str())]
} else {
run.text.grapheme_indices(true).collect()
};
for (offset, cluster) in clusters {
if !run.combined && !marked(cluster, style.text_paint.emphasis_skip) {
continue;
}
let start = run.source_range.start + offset;
let centre = run.x + f32::midpoint(run.x_at(start), run.x_at(start + cluster.len()));
match (&shaped, shape) {
(Some(shaped), _) => {
let width: f32 = shaped.iter().map(|mark| mark.width).sum();
let mut x = centre - width / 2.0;
for mark in shaped {
marks.push(GlyphRun {
x,
baseline_shift: baseline,
source_range: start..start + cluster.len(),
source_span: run.source_span,
..mark.clone()
});
x += mark.width;
}
}
(None, Some((shape, filled))) => shapes.push(MarkShape {
contours: synthesized(
shape,
filled,
[
centre,
baseline - f32::midpoint(metrics.ascent, -metrics.descent),
],
mark_style.size,
),
color: mark_style.text_paint.fill.used,
}),
(None, None) => unreachable!("strings substitute missing glyphs"),
}
}
run.marks = marks;
run.mark_shapes = shapes;
}
Ok(())
}
const DOT_RADIUS_EM: f32 = 0.15;
const CIRCLE_RADIUS_EM: f32 = 0.4;
const INNER_CIRCLE_RADIUS_EM: f32 = 0.2;
const TRIANGLE_SIDE_EM: f32 = 0.8;
const SESAME_LENGTH_EM: f32 = 0.5;
const SESAME_WIDTH_EM: f32 = 0.24;
const SESAME_LEAN: f32 = std::f32::consts::FRAC_PI_6;
const OPEN_LINE_EM: f32 = 0.08;
fn circle([x, y]: Point, radius: f32) -> Contour {
crate::paint::rounded_rect(
Rect {
x: x - radius,
y: y - radius,
width: 2.0 * radius,
height: 2.0 * radius,
},
[[radius; 2]; 4],
)
}
fn transformed(contour: &Contour, [cx, cy]: Point, factor: f32, angle: f32) -> Contour {
let (sin, cos) = angle.sin_cos();
let map = |[x, y]: Point| {
let [dx, dy] = [(x - cx) * factor, (y - cy) * factor];
[cx + dx * cos - dy * sin, cy + dx * sin + dy * cos]
};
Contour {
start: map(contour.start),
segments: contour
.segments
.iter()
.map(|segment| match *segment {
Segment::Line(point) => Segment::Line(map(point)),
Segment::Cubic(points) => Segment::Cubic(points.map(map)),
})
.collect(),
closed: contour.closed,
}
}
fn synthesized(shape: EmphasisShape, filled: bool, centre: Point, em: f32) -> Vec<Contour> {
let [x, y] = centre;
let outline = match shape {
EmphasisShape::Dot => circle(centre, DOT_RADIUS_EM * em),
EmphasisShape::Circle | EmphasisShape::DoubleCircle => {
circle(centre, CIRCLE_RADIUS_EM * em)
}
EmphasisShape::Triangle => {
let side = TRIANGLE_SIDE_EM * em;
let height = side * 3.0_f32.sqrt() / 2.0;
Contour::polygon(&[
[x, y - height / 2.0],
[x + side / 2.0, y + height / 2.0],
[x - side / 2.0, y + height / 2.0],
])
}
EmphasisShape::Sesame => {
let [length, width] = [SESAME_LENGTH_EM * em, SESAME_WIDTH_EM * em];
let upright = crate::paint::rounded_rect(
Rect {
x: x - width / 2.0,
y: y - length / 2.0,
width,
height: length,
},
[[width / 2.0; 2]; 4],
);
transformed(&upright, centre, 1.0, SESAME_LEAN)
}
};
let radius = match shape {
EmphasisShape::Dot => DOT_RADIUS_EM,
EmphasisShape::Circle | EmphasisShape::DoubleCircle => CIRCLE_RADIUS_EM,
EmphasisShape::Triangle => TRIANGLE_SIDE_EM / 3.0_f32.sqrt(),
EmphasisShape::Sesame => SESAME_WIDTH_EM / 2.0,
};
let hollow = |contour: &Contour, radius: f32| {
transformed(contour, centre, 1.0 - OPEN_LINE_EM / radius, 0.0)
};
let mut contours = vec![outline.clone()];
if !filled || shape == EmphasisShape::DoubleCircle {
contours.push(hollow(&outline, radius));
}
if shape == EmphasisShape::DoubleCircle {
let inner = circle(centre, INNER_CIRCLE_RADIUS_EM * em);
if !filled {
contours.push(hollow(&inner, INNER_CIRCLE_RADIUS_EM));
}
contours.push(inner);
}
contours
}