Add float placement and page headers/footers

This commit is contained in:
gloamxun committed 2026-10-19 13:41:29 +08:00
1 parent 1da46c61d6
commit ed1da06685
16 files changed
+974 -90

No files matched your search

+167
View File
@@ -280,6 +280,84 @@ C\hfill D\hfill E
assert!(e > 150.0, "last hfill did not reach the right edge: {e}");
}
#[test]
fn figure_placement_and_newpage_split_pages() {
let image = format!("{}/examples/plot.png", env!("CARGO_MANIFEST_DIR"));
let template = r#"\documentclass{article}
\begin{document}
Before text.
\begin{figure}[PLACE]
\includegraphics[width=3cm]{IMAGE}
\caption{Float caption.}
\end{figure}
After text.
\end{document}
"#;
let compile = |placement: &str| {
let source = template
.replace("PLACE", placement)
.replace("IMAGE", &image);
fresh_preview(&source, "float.tex")
};
let top = compile("t");
assert_eq!(top["status"], "ok", "{}", top["message"]);
assert_eq!(pages(&top).len(), 2, "[t] should move the figure to page 2");
assert!(
!page_svg(&top, 0).contains("<image"),
"[t] figure on page 1"
);
assert!(page_svg(&top, 1).contains("<image"), "[t] figure missing");
let here = compile("h");
assert_eq!(here["status"], "ok", "{}", here["message"]);
assert_eq!(pages(&here).len(), 1, "[h] should stay in flow");
assert!(page_svg(&here, 0).contains("<image"), "[h] figure missing");
let float_page = compile("p");
assert_eq!(float_page["status"], "ok", "{}", float_page["message"]);
assert_eq!(pages(&float_page).len(), 3, "[p] should use its own page");
assert!(
page_svg(&float_page, 1).contains("<image"),
"[p] figure page missing"
);
let bottom = compile("b");
assert_eq!(bottom["status"], "ok", "{}", bottom["message"]);
assert_eq!(pages(&bottom).len(), 2, "[b] should end the page");
let bottom_svg = page_svg(&bottom, 0);
assert!(bottom_svg.contains("<image"), "[b] figure missing");
let image_y = bottom_svg
.split("<image")
.nth(1)
.and_then(|rest| rest.split("y=\"").nth(1))
.and_then(|rest| rest.split('"').next())
.and_then(|value| value.parse::<f64>().ok())
.unwrap_or_else(|| panic!("[b] image has no y coordinate: {bottom_svg}"));
assert!(
image_y > 200.0,
"[b] figure was not pushed to the bottom: {image_y}"
);
assert!(page_svg(&bottom, 1).contains("After"), "[b] following text");
let newpage = r#"\documentclass{article}
\begin{document}
First page.
\newpage
Second page.
\end{document}
"#;
let value = fresh_preview(newpage, "newpage.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
assert_eq!(pages(&value).len(), 2, "\\newpage page count");
assert!(page_svg(&value, 0).contains("First"), "first page text");
assert!(page_svg(&value, 1).contains("Second"), "second page text");
}
#[test]
fn sans_family_selects_the_sans_face() {
let source = r#"\documentclass{article}
@@ -515,3 +593,92 @@ Body text.
"title footnote is not at the page bottom: {note}"
);
}
#[test]
fn page_style_decorates_headers_and_footers() {
let source = r#"\documentclass{article}
\pagestyle{fancy}
\fancyhead[L]{Left head}
\fancyhead[C]{Middle head}
\fancyhead[R]{Right head}
\fancyfoot[C]{Foot \thepage}
\begin{document}
Body text.
\end{document}
"#;
let value = fresh_preview(source, "pagestyle.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
for text in ["Left", "Middle", "Right", "Foot"] {
assert!(
svg.contains(&format!(">{text}</text>")),
"missing decoration `{text}`: {svg}"
);
}
assert!(
text_x(&svg, "Left") < 1.0,
"left header is not at the left margin"
);
assert!(
text_x(&svg, "Right") > 140.0,
"right header is not at the right edge"
);
assert!(
text_y(&svg, "Left") < 15.0,
"header is not near the top of the page"
);
assert!(
text_y(&svg, "Foot") > 275.0,
"footer is not near the bottom of the page"
);
}
#[test]
fn thispagestyle_empty_suppresses_one_page_decorations() {
let source = r#"\documentclass{article}
\pagestyle{fancy}
\fancyhead[L]{Left head}
\fancyfoot[C]{Foot \thepage}
\begin{document}
First page.\thispagestyle{empty}
\newpage
Second page.
\end{document}
"#;
let value = fresh_preview(source, "thispagestyle.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
assert_eq!(pages(&value).len(), 2, "page count");
let first = page_svg(&value, 0);
assert!(
!first.contains(">Left</text>"),
"page 1 header was not suppressed: {first}"
);
assert!(
!first.contains(">Foot</text>"),
"page 1 footer was not suppressed: {first}"
);
let second = page_svg(&value, 1);
assert!(
second.contains(">Left</text>"),
"page 2 header missing: {second}"
);
assert!(
second.contains(">Foot</text>"),
"page 2 footer missing: {second}"
);
assert!(
second.contains(">2</text>"),
"page 2 number missing: {second}"
);
}
#[test]
fn default_plain_style_prints_a_page_number() {
let value = fresh_preview(include_str!("../examples/hello.tex"), "hello.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
assert!(
text_y(&svg, "1") > 275.0,
"default page number is not in the footer"
);
}