Raise and lower superscript and subscript runs

This commit is contained in:
gloamxun committed 2026-10-25 02:14:51 +08:00
1 parent 9dea19d5aa
commit 0b2265580f
6 files changed
+190 -24

No files matched your search

+82
View File
@@ -23,6 +23,9 @@ pub struct HorizontalBox {
/// Explicit glyph size in scaled points for a sized run.
#[serde(default)]
pub size: Option<i64>,
/// Vertical offset in scaled points; positive raises the glyphs.
#[serde(default)]
pub raise: i64,
}
/// A run of paragraph material: text in one face or an inline formula.
@@ -31,6 +34,8 @@ pub enum InlineSegment {
Text(ParagraphFont, String),
/// A run carrying its own type size, such as `{\small ...}`.
SizedText(ParagraphFont, i64, String),
/// A run raised or lowered like `\textsuperscript`.
Script(ParagraphFont, i64, i64, String),
Math(crate::math::MathAtom),
Link {
font: ParagraphFont,
@@ -634,6 +639,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
italic: 0,
font: None,
math: None,
raise: 0,
size: None,
link: None,
}));
@@ -1103,6 +1109,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
italic: 0,
font: None,
math: None,
raise: 0,
size: None,
link: None,
}));
@@ -1115,6 +1122,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
italic: 0,
font: None,
math: None,
raise: 0,
size: None,
link: None,
}));
@@ -1461,6 +1469,7 @@ pub fn text_items_with_font(
italic: font.italic(character),
font: None,
math: None,
raise: 0,
size: None,
link: None,
}));
@@ -1668,6 +1677,46 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
previous = Some((*font, text.clone()));
seen_content = true;
}
InlineSegment::Script(font, size, raise, text) => {
if text.is_empty() {
continue;
}
let starts_with_space = text.chars().next().is_some_and(char::is_whitespace);
let boundary_space = match &previous {
Some((_previous_font, previous_text)) => {
previous_text
.chars()
.next_back()
.is_some_and(char::is_whitespace)
|| starts_with_space
}
None => seen_content && starts_with_space,
};
if boundary_space {
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(
previous.as_ref().map_or(*font, |(font, _)| *font),
);
items.push(HorizontalItem::Glue {
width: scale_font_size(metrics.space(), *size),
stretch: scale_font_size(metrics.stretch(), *size),
shrink: scale_font_size(metrics.shrink(), *size),
order: 0,
});
}
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*font);
let mut run = text_items_with_font(text, style, &metrics);
scale_items_for_size(&mut run, *size);
for item in &mut run {
if let HorizontalItem::Box(box_) = item {
box_.font = Some(*font);
box_.size = Some(*size);
box_.raise = *raise;
}
}
items.extend(run);
previous = Some((*font, text.clone()));
seen_content = true;
}
InlineSegment::Link { font, url, text } => {
if text.is_empty() {
continue;
@@ -1731,6 +1780,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
font: None,
math: Some(Box::new(atom.clone())),
size: None,
raise: 0,
link: None,
}));
previous = None;
@@ -1743,6 +1793,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
paragraph.font = match segments.first() {
Some(InlineSegment::Text(font, _))
| Some(InlineSegment::SizedText(font, _, _))
| Some(InlineSegment::Script(font, _, _, _))
| Some(InlineSegment::Link { font, .. }) => *font,
_ => ParagraphFont::Roman,
};
@@ -2029,6 +2080,36 @@ mod tests {
assert!(linked.iter().all(|box_| box_.character.is_some()));
}
#[test]
fn raised_segments_carry_their_size_and_shift() {
let style = ParagraphStyle {
line_width: 480 * 65_536,
par_indent: 0,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_segments(
&[
InlineSegment::Text(ParagraphFont::Roman, "x".into()),
InlineSegment::Script(ParagraphFont::Roman, 458_752, 262_144, "2".into()),
],
&style,
);
let boxes: Vec<_> = paragraph
.lines
.iter()
.flatten()
.filter_map(|item| match item {
HorizontalItem::Box(box_) if box_.character.is_some() => Some(box_),
_ => None,
})
.collect();
assert_eq!(boxes.len(), 2);
assert_eq!(boxes[0].raise, 0);
assert_eq!(boxes[1].size, Some(458_752));
assert_eq!(boxes[1].raise, 262_144);
assert!(boxes[1].width < boxes[0].width);
}
#[test]
fn sized_segments_keep_their_own_glyph_size() {
let style = ParagraphStyle {
@@ -2108,6 +2189,7 @@ mod tests {
italic: 0,
font: None,
math: None,
raise: 0,
size: None,
link: None,
})
+3 -2
View File
@@ -578,13 +578,14 @@ impl Page {
flush_link(&mut link_run, &mut list.items);
}
let glyph_size = box_.size.unwrap_or(size);
let glyph_y = baseline - box_.raise;
if let Some(math) = &box_.math {
let math_font = Cmr10Metrics::default();
Self::paint_math(
math,
&math_font,
x,
baseline,
glyph_y,
glyph_size,
&mut list.items,
);
@@ -593,7 +594,7 @@ impl Page {
list.push(DisplayItem::Text {
font: box_.font.map_or(face, paragraph_face),
x,
y: baseline,
y: glyph_y,
size: glyph_size,
text: character_text(character),
});