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

+101 -18
View File
@@ -4114,9 +4114,9 @@ fn inline_math_segments(
for part in split_hrefs(text) {
match part {
HrefPart::Plain(piece) => {
let runs = inline_sized_runs(&piece, base)
.unwrap_or_else(|| vec![(base, style.font_size, piece.clone())]);
for (font, size, run_text) in runs {
let runs = inline_styled_runs(&piece, base)
.unwrap_or_else(|| vec![(base, style.font_size, 0, piece.clone())]);
for (font, size, raise, run_text) in runs {
for (is_math, piece) in split_inline_math(&run_text) {
if is_math {
found_math = true;
@@ -4124,7 +4124,12 @@ fn inline_math_segments(
parse_math(&piece),
));
} else if !piece.is_empty() {
if size == 655_360 {
if raise != 0 {
found_size = true;
segments.push(texrra_tex_core::paragraph::InlineSegment::Script(
font, size, raise, piece,
));
} else if size == 655_360 {
segments.push(texrra_tex_core::paragraph::InlineSegment::Text(
font, piece,
));
@@ -4165,33 +4170,47 @@ fn inline_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, St
)
}
fn inline_sized_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, i64, String)>> {
fn inline_styled_runs(
text: &str,
base: ParagraphFont,
) -> Option<Vec<(ParagraphFont, i64, i64, String)>> {
let mut runs = Vec::new();
let mut found = false;
parse_runs(text, base, 655_360, &mut runs, &mut found);
parse_runs(text, base, 655_360, 0, &mut runs, &mut found);
if !found {
return None;
}
Some(runs)
}
fn inline_sized_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, i64, String)>> {
Some(
inline_styled_runs(text, base)?
.into_iter()
.map(|(font, size, _, text)| (font, size, text))
.collect(),
)
}
fn push_run(
runs: &mut Vec<(ParagraphFont, i64, String)>,
runs: &mut Vec<(ParagraphFont, i64, i64, String)>,
font: ParagraphFont,
size: i64,
raise: i64,
text: &str,
) {
if text.is_empty() {
return;
}
if let Some((last_font, last_size, last_text)) = runs.last_mut()
if let Some((last_font, last_size, last_raise, last_text)) = runs.last_mut()
&& *last_font == font
&& *last_size == size
&& *last_raise == raise
{
last_text.push_str(text);
return;
}
runs.push((font, size, text.to_string()));
runs.push((font, size, raise, text.to_string()));
}
/// Match a balanced `{...}` at the start of `rest`, returning its contents and
@@ -4402,11 +4421,26 @@ fn accent_run(rest: &str) -> Option<(String, usize)> {
Some((text, after_start + extra))
}
/// Recognize `\textsuperscript{...}` and `\textsubscript{...}`.
fn script_run(rest: &str) -> Option<(bool, &str, usize)> {
for (command, superscript) in [("\\textsuperscript", true), ("\\textsubscript", false)] {
let Some(after) = rest.strip_prefix(command) else {
continue;
};
let skipped = after.len() - after.trim_start().len();
let after = after.trim_start();
let (inner, length) = braced_group(after)?;
return Some((superscript, inner, command.len() + skipped + length));
}
None
}
fn parse_runs(
text: &str,
font: ParagraphFont,
size: i64,
runs: &mut Vec<(ParagraphFont, i64, String)>,
raise: i64,
runs: &mut Vec<(ParagraphFont, i64, i64, String)>,
found: &mut bool,
) {
let mut index = 0;
@@ -4414,13 +4448,33 @@ fn parse_runs(
let rest = &text[index..];
if let Some((body, length)) = verb_run(rest) {
*found = true;
push_run(runs, ParagraphFont::Mono, size, &body);
push_run(runs, ParagraphFont::Mono, size, raise, &body);
index += length;
continue;
}
if let Some((superscript, inner, length)) = script_run(rest) {
*found = true;
let script_size = (size * 7 / 10).max(1);
let shift = size * 2 / 5;
let script_raise = if superscript {
raise + shift
} else {
raise - shift
};
parse_runs(inner, font, script_size, script_raise, runs, found);
index += length;
continue;
}
if let Some((declared, declared_size, body)) = nfss_font_with_size(rest, font) {
*found = true;
parse_runs(&body, declared, declared_size.unwrap_or(size), runs, found);
parse_runs(
&body,
declared,
declared_size.unwrap_or(size),
raise,
runs,
found,
);
return;
}
if let Some((inner, length)) = braced_group(rest) {
@@ -4433,25 +4487,25 @@ fn parse_runs(
if nfss_font_with_size(inner, font).is_some() {
*found = true;
}
parse_runs(&inner_text, inner_font, inner_size, runs, found);
parse_runs(&inner_text, inner_font, inner_size, raise, runs, found);
index += length;
continue;
}
if let Some((wrapper_font, inner, length)) = inline_wrapper(rest, font) {
*found = true;
parse_runs(inner, wrapper_font, size, runs, found);
parse_runs(inner, wrapper_font, size, raise, runs, found);
index += length;
continue;
}
if let Some((accent, length)) = accent_run(rest) {
*found = true;
push_run(runs, font, size, &accent);
push_run(runs, font, size, raise, &accent);
index += length;
continue;
}
if let Some((symbol, length)) = text_symbol(rest) {
*found = true;
push_run(runs, font, size, symbol);
push_run(runs, font, size, raise, symbol);
index += length;
continue;
}
@@ -4462,7 +4516,7 @@ fn parse_runs(
.find(|(_, character)| !character.is_ascii_alphabetic())
.map(|(offset, _)| offset.max(1))
.unwrap_or(rest.len());
push_run(runs, font, size, &rest[..step]);
push_run(runs, font, size, raise, &rest[..step]);
index += step;
continue;
}
@@ -4472,7 +4526,7 @@ fn parse_runs(
.find(|(_, character)| *character == '{' || *character == '\\')
.map(|(offset, _)| offset)
.unwrap_or(rest.len());
push_run(runs, font, size, &rest[..step]);
push_run(runs, font, size, raise, &rest[..step]);
index += step;
}
}
@@ -9141,6 +9195,17 @@ mod inline_tests {
assert!(inline_runs("\\LaTeX{}", ParagraphFont::Roman).is_some());
}
#[test]
fn text_superscript_paints_smaller_and_higher() {
let svg = preview_json("E = mc\\textsuperscript{2}").expect("preview");
assert!(
svg.contains("font-size=\\\"7.000\\\""),
"missing script size: {svg}"
);
assert!(svg.contains(">2</text>"), "missing script glyph: {svg}");
assert!(!svg.contains("textsuperscript"), "command leaked: {svg}");
}
#[test]
fn accent_commands_paint_precomposed_glyphs() {
let svg = preview_json("Caf\\'e au lait.").expect("preview");
@@ -9156,6 +9221,24 @@ mod inline_tests {
assert!(!json.contains("LaTeX{}"));
}
#[test]
fn text_superscripts_and_subscripts_split_sized_runs() {
assert_eq!(
inline_styled_runs("x\\textsuperscript{2}", ParagraphFont::Roman),
Some(vec![
(ParagraphFont::Roman, 655_360, 0, "x".to_string()),
(ParagraphFont::Roman, 458_752, 262_144, "2".to_string()),
])
);
assert_eq!(
inline_styled_runs("x\\textsubscript{i}", ParagraphFont::Roman),
Some(vec![
(ParagraphFont::Roman, 655_360, 0, "x".to_string()),
(ParagraphFont::Roman, 458_752, -262_144, "i".to_string()),
])
);
}
#[test]
fn accent_commands_compose_characters() {
assert_eq!(