Paint inline size changes at their own size

This commit is contained in:
gloamxun committed 2026-10-24 23:22:29 +08:00
1 parent 00f95fdd09
commit ae69eba64d
6 files changed
+158 -28

No files matched your search

+71 -20
View File
@@ -3059,11 +3059,13 @@ fn inline_math_segments(
let mut segments = Vec::new();
let mut found_math = false;
let mut found_link = false;
let mut found_size = false;
for part in split_hrefs(text) {
match part {
HrefPart::Plain(piece) => {
let runs = inline_runs(&piece, base).unwrap_or_else(|| vec![(base, piece.clone())]);
for (font, run_text) in runs {
let runs = inline_sized_runs(&piece, base)
.unwrap_or_else(|| vec![(base, style.font_size, piece.clone())]);
for (font, size, run_text) in runs {
for (is_math, piece) in split_inline_math(&run_text) {
if is_math {
found_math = true;
@@ -3071,8 +3073,18 @@ fn inline_math_segments(
parse_math(&piece),
));
} else if !piece.is_empty() {
segments
.push(texrra_tex_core::paragraph::InlineSegment::Text(font, piece));
if size == 655_360 {
segments.push(texrra_tex_core::paragraph::InlineSegment::Text(
font, piece,
));
} else {
found_size = true;
segments.push(
texrra_tex_core::paragraph::InlineSegment::SizedText(
font, size, piece,
),
);
}
}
}
}
@@ -3090,30 +3102,45 @@ fn inline_math_segments(
}
}
}
(found_math || found_link).then_some(segments)
(found_math || found_link || found_size).then_some(segments)
}
fn inline_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, String)>> {
Some(
inline_sized_runs(text, base)?
.into_iter()
.map(|(font, _, text)| (font, text))
.collect(),
)
}
fn inline_sized_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, i64, String)>> {
let mut runs = Vec::new();
let mut found = false;
parse_runs(text, base, &mut runs, &mut found);
parse_runs(text, base, 655_360, &mut runs, &mut found);
if !found {
return None;
}
Some(runs)
}
fn push_run(runs: &mut Vec<(ParagraphFont, String)>, font: ParagraphFont, text: &str) {
fn push_run(
runs: &mut Vec<(ParagraphFont, i64, String)>,
font: ParagraphFont,
size: i64,
text: &str,
) {
if text.is_empty() {
return;
}
if let Some((last_font, last_text)) = runs.last_mut()
if let Some((last_font, last_size, last_text)) = runs.last_mut()
&& *last_font == font
&& *last_size == size
{
last_text.push_str(text);
return;
}
runs.push((font, text.to_string()));
runs.push((font, size, text.to_string()));
}
/// Match a balanced `{...}` at the start of `rest`, returning its contents and
@@ -3182,32 +3209,35 @@ fn inline_wrapper(rest: &str, base: ParagraphFont) -> Option<(ParagraphFont, &st
fn parse_runs(
text: &str,
font: ParagraphFont,
runs: &mut Vec<(ParagraphFont, String)>,
size: i64,
runs: &mut Vec<(ParagraphFont, i64, String)>,
found: &mut bool,
) {
let mut index = 0;
while index < text.len() {
let rest = &text[index..];
if let Some((declared, body)) = nfss_font(rest, font) {
if let Some((declared, declared_size, body)) = nfss_font_with_size(rest, font) {
*found = true;
parse_runs(&body, declared, runs, found);
parse_runs(&body, declared, declared_size.unwrap_or(size), runs, found);
return;
}
if let Some((inner, length)) = braced_group(rest) {
let (inner_font, inner_text) = match nfss_font(inner, font) {
Some((declared, body)) => (declared, body),
None => (font, inner.to_string()),
let (inner_font, inner_size, inner_text) = match nfss_font_with_size(inner, font) {
Some((declared, declared_size, body)) => {
(declared, declared_size.unwrap_or(size), body)
}
None => (font, size, inner.to_string()),
};
if nfss_font(inner, font).is_some() {
if nfss_font_with_size(inner, font).is_some() {
*found = true;
}
parse_runs(&inner_text, inner_font, runs, found);
parse_runs(&inner_text, inner_font, inner_size, runs, found);
index += length;
continue;
}
if let Some((wrapper_font, inner, length)) = inline_wrapper(rest, font) {
*found = true;
parse_runs(inner, wrapper_font, runs, found);
parse_runs(inner, wrapper_font, size, runs, found);
index += length;
continue;
}
@@ -3218,7 +3248,7 @@ fn parse_runs(
.find(|(_, character)| !character.is_ascii_alphabetic())
.map(|(offset, _)| offset.max(1))
.unwrap_or(rest.len());
push_run(runs, font, &rest[..step]);
push_run(runs, font, size, &rest[..step]);
index += step;
continue;
}
@@ -3228,7 +3258,7 @@ fn parse_runs(
.find(|(_, character)| *character == '{' || *character == '\\')
.map(|(offset, _)| offset)
.unwrap_or(rest.len());
push_run(runs, font, &rest[..step]);
push_run(runs, font, size, &rest[..step]);
index += step;
}
}
@@ -5629,6 +5659,27 @@ A definition.
);
}
#[test]
fn size_commands_split_sized_runs() {
let runs = inline_sized_runs("plain \\large large text", ParagraphFont::Roman)
.expect("sized runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Roman, 655_360, "plain ".to_string()),
(ParagraphFont::Roman, 786_432, "large text".to_string()),
]
);
}
#[test]
fn inline_size_commands_paint_bigger_glyphs() {
let json = preview_json("Body \\large Big.").expect("preview");
assert!(json.contains("12.000"), "{json}");
assert!(json.contains("10.000"), "{json}");
assert!(!json.contains("\\large"), "{json}");
}
#[test]
fn href_creates_link_boxes() {
let source = "Visit \\href{https://example.com}{the site} now.";