Space text with quad and punctuation commands

This commit is contained in:
gloamxun committed 2026-10-26 04:30:22 +08:00
1 parent c290b6f38d
commit 1e8b2367ca
3 files changed
+66 -3

No files matched your search

+63
View File
@@ -6952,6 +6952,36 @@ fn expand_ensuremath_plain(text: &str) -> String {
out
}
fn replace_spacing_commands(text: &str) -> String {
let mut out = text.to_string();
for (command, replacement) in [
("\\qquad", "\\hspace{2em}"),
("\\quad", "\\hspace{1em}"),
("\\;", "\\hspace{0.27778em}"),
("\\:", "\\hspace{0.22222em}"),
("\\,", "\\hspace{0.16667em}"),
] {
out = out.replace(command, replacement);
}
out
}
fn expand_math_spaces(text: &str) -> String {
let parts: Vec<&str> = text.split('$').collect();
let mut out = String::new();
for (index, part) in parts.iter().enumerate() {
if index > 0 {
out.push('$');
}
if index % 2 == 0 {
out.push_str(&replace_spacing_commands(part));
} else {
out.push_str(part);
}
}
out
}
fn expand_hspace_plain(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
@@ -9097,6 +9127,7 @@ fn build_document_with_labels(
}
text = strip_noop_commands(&text);
text = strip_box_commands(&text);
text = expand_math_spaces(&text);
text = expand_ensuremath(&text);
text = expand_hspace(&text);
if let Some((color, rest)) = color_switch(&text) {
@@ -13163,6 +13194,38 @@ A definition.
assert_eq!(runs[0].3, "Boxed");
}
#[test]
fn quad_commands_become_text_spaces() {
let blocks = paragraphs("A\\quad B").expect("paragraphs");
let document = build_document(&blocks, &PageGeometry::default());
let engine = Engine::layout(&document);
let mut positions = Vec::new();
for page in engine.pages() {
for item in page.display_list().items {
if let texrra_tex_core::render::DisplayItem::Text { text, x, .. } = item {
if text == "A" || text == "B" {
positions.push((text, x));
}
}
}
}
let a = positions.iter().find(|(text, _)| text == "A").expect("A").1;
let b = positions.iter().find(|(text, _)| text == "B").expect("B").1;
assert!(b - a > 700_000, "gap {}", b - a);
let leaked = document.units[0].items.iter().any(|item| match item {
texrra_tex_core::VerticalItem::Paragraph(paragraph) => {
paragraph.lines.iter().flatten().any(|item| {
matches!(
item,
texrra_tex_core::paragraph::HorizontalItem::Box(b) if b.character == Some('q')
)
})
}
_ => false,
});
assert!(!leaked);
}
#[test]
fn hspace_keeps_its_math_width() {
let blocks = paragraphs("A \\hspace{2em} B").expect("paragraphs");