Add paragraph indentation and hfill

This commit is contained in:
gloamxun committed 2026-10-19 12:43:30 +08:00
1 parent bad477fbee
commit e44d4c8669
11 files changed
+362 -42

No files matched your search

+102
View File
@@ -177,3 +177,105 @@ Second document.
"different sources produced the same page hash"
);
}
fn text_x(svg: &str, needle: &str) -> f64 {
let closing = format!("{needle}</text>");
let index = svg
.find(&closing)
.unwrap_or_else(|| panic!("missing text element containing `{needle}`"));
let start = svg[..index]
.rfind("<text ")
.unwrap_or_else(|| panic!("no text element before `{needle}`"));
let tag = &svg[start..index];
tag.split("x=\"")
.nth(1)
.and_then(|rest| rest.split('"').next())
.and_then(|value| value.parse::<f64>().ok())
.unwrap_or_else(|| panic!("text element for `{needle}` has no x coordinate"))
}
#[test]
fn paragraph_indent_tracks_setlength_groups_and_noindent() {
let source = r#"\documentclass{article}
\begin{document}
\parindent=0pt
First paragraph.
\setlength{\parindent}{10mm}
Second paragraph.
{\setlength{\parindent}{0pt}
Third paragraph.
}
Fourth paragraph.
\addtolength{\parindent}{5mm}
Fifth paragraph.
\noindent
Noindent paragraph.
\end{document}
"#;
let value = fresh_preview(source, "parindent.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
let first = text_x(&svg, "First");
let second = text_x(&svg, "Second");
let third = text_x(&svg, "Third");
let fourth = text_x(&svg, "Fourth");
let fifth = text_x(&svg, "Fifth");
let noindent = text_x(&svg, "Noindent");
assert!(
first.abs() < 0.1,
"first paragraph should not be indented: {first}"
);
assert!((second - 10.0).abs() < 0.25, "second indent: {second}");
assert!(third.abs() < 0.1, "grouped zero indent leaked: {third}");
assert!(
(fourth - 10.0).abs() < 0.25,
"group restore failed: {fourth}"
);
assert!((fifth - 15.0).abs() < 0.25, "addtolength failed: {fifth}");
assert!(noindent.abs() < 0.1, "noindent ignored: {noindent}");
}
#[test]
fn default_parindent_is_seventeen_points() {
let source = r#"\documentclass{article}
\begin{document}
Default indent.
\end{document}
"#;
let value = fresh_preview(source, "default-parindent.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
let x = text_x(&svg, "Default");
// 17pt is 5.98mm.
assert!((x - 5.98).abs() < 0.15, "default parindent: {x}");
}
#[test]
fn hfill_fills_the_remaining_line() {
let source = r#"\documentclass{article}
\begin{document}
\setlength{\parindent}{0pt}
A\hfill B
C\hfill D\hfill E
\end{document}
"#;
let value = fresh_preview(source, "hfill.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
let a = text_x(&svg, "A");
let b = text_x(&svg, "B");
let c = text_x(&svg, "C");
let d = text_x(&svg, "D");
let e = text_x(&svg, "E");
assert!(a.abs() < 0.1, "A should stay at the left margin: {a}");
assert!(b > 150.0, "single hfill did not reach the right edge: {b}");
assert!(c.abs() < 0.1, "C should stay at the left margin: {c}");
assert!(d > 70.0 && d < 100.0, "middle hfill is not centered: {d}");
assert!(e > 150.0, "last hfill did not reach the right edge: {e}");
}