Align centered and flushed blocks within the measure

This commit is contained in:
gloamxun committed 2026-10-24 20:15:31 +08:00
1 parent 2783dfb284
commit 925c916ea5
5 files changed
+104 -5

No files matched your search

+54
View File
@@ -1026,6 +1026,28 @@ fn take_labels(text: &mut String) -> Vec<String> {
}
fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
for (environment, centered, right_aligned) in [
("center", true, false),
("flushleft", false, false),
("flushright", false, true),
] {
let begin = format!("\\begin{{{environment}}}");
let finish = format!("\\end{{{environment}}}");
let Some(body) = raw
.strip_prefix(&begin)
.and_then(|text| text.strip_suffix(&finish))
else {
continue;
};
let body = body.trim();
if body.is_empty() {
continue;
}
let mut block = parse_paragraph(body);
block.centered = centered;
block.right_aligned = right_aligned;
return vec![block];
}
for (environment, numbered) in [("itemize", false), ("enumerate", true)] {
let begin = format!("\\begin{{{environment}}}");
let finish = format!("\\end{{{environment}}}");
@@ -2406,6 +2428,38 @@ mod source_tests {
assert_eq!(glue.par_skip_shrink, Some(SCALE));
}
#[test]
fn alignment_environments_set_the_block_alignment() {
let blocks = paragraphs("\\begin{center}\nCentered\n\\end{center}").expect("blocks");
assert!(blocks[0].centered);
assert!(!blocks[0].right_aligned);
let blocks = paragraphs("\\begin{flushright}\nRight\n\\end{flushright}").expect("blocks");
assert!(blocks[0].right_aligned);
assert!(!blocks[0].centered);
let blocks = paragraphs("\\begin{flushleft}\nLeft\n\\end{flushleft}").expect("blocks");
assert!(!blocks[0].centered);
assert!(!blocks[0].right_aligned);
}
#[test]
fn centered_blocks_paint_centred_lines() {
let wide = "\\begin{center}\nshort\n\\end{center}";
let blocks = paragraphs(wide).expect("blocks");
let document = build_document(&blocks, &PageGeometry::default());
let engine = texrra_tex_core::Engine::layout_paged(&document).expect("layout");
let page = &engine.pages()[0];
let x = page
.display_list()
.items
.into_iter()
.find_map(|item| match item {
texrra_tex_core::DisplayItem::Text { x, text, .. } if text == "s" => Some(x),
_ => None,
})
.expect("text");
assert!(x > 0, "centred text starts at {x}");
}
#[test]
fn geometry_package_sets_margins() {
let (_, geometry) = geometry_and_body("\\usepackage[margin=1in]{geometry}\n\nA paragraph.");