Track textwidth and textheight from page geometry

This commit is contained in:
gloamxun committed 2026-10-19 13:46:25 +08:00
1 parent ed1da06685
commit 269433abdb
7 files changed
+153 -28

No files matched your search

+77
View File
@@ -194,6 +194,43 @@ fn text_x(svg: &str, needle: &str) -> f64 {
.unwrap_or_else(|| panic!("text element for `{needle}` has no x coordinate"))
}
fn first_black_rect_width(svg: &str) -> f64 {
for fragment in svg.split("<rect ").skip(1) {
let tag = fragment.split('>').next().unwrap_or(fragment);
if !tag.contains("fill=\"#000000\"") {
continue;
}
let value = tag
.split("width=\"")
.nth(1)
.and_then(|rest| rest.split('"').next())
.and_then(|value| value.parse::<f64>().ok());
if let Some(width) = value {
return width;
}
}
panic!("no filled rectangle in SVG: {svg}");
}
fn max_black_rect_height(svg: &str) -> f64 {
let mut maximum: Option<f64> = None;
for fragment in svg.split("<rect ").skip(1) {
let tag = fragment.split('>').next().unwrap_or(fragment);
if !tag.contains("fill=\"#000000\"") {
continue;
}
let value = tag
.split("height=\"")
.nth(1)
.and_then(|rest| rest.split('"').next())
.and_then(|value| value.parse::<f64>().ok());
if let Some(height) = value {
maximum = Some(maximum.map_or(height, |current| current.max(height)));
}
}
maximum.unwrap_or_else(|| panic!("no filled rectangle in SVG: {svg}"))
}
#[test]
fn paragraph_indent_tracks_setlength_groups_and_noindent() {
let source = r#"\documentclass{article}
@@ -682,3 +719,43 @@ fn default_plain_style_prints_a_page_number() {
"default page number is not in the footer"
);
}
#[test]
fn geometry_updates_text_width_and_text_height() {
let source = r#"\documentclass{article}
\geometry{textwidth=100mm,textheight=200mm}
\begin{document}
\noindent\rule{\textwidth}{2pt}
\noindent\rule{2pt}{\textheight}
\end{document}
"#;
let value = fresh_preview(source, "geometry-lengths.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let svg = page_svg(&value, 0);
let width = first_black_rect_width(&svg);
assert!(
(width - 100.0).abs() < 1.0,
"textwidth did not follow geometry: {width}"
);
let height = max_black_rect_height(&svg);
assert!(
(height - 200.0).abs() < 1.0,
"textheight did not follow geometry: {height}"
);
}
#[test]
fn letter_paper_updates_text_width_length() {
let source = r#"\documentclass[letterpaper]{article}
\begin{document}
\noindent\rule{\textwidth}{2pt}
\end{document}
"#;
let value = fresh_preview(source, "letter-lengths.tex");
assert_eq!(value["status"], "ok", "{}", value["message"]);
let width = first_black_rect_width(&page_svg(&value, 0));
assert!(
(width - 165.9).abs() < 1.0,
"letter textwidth is wrong: {width}"
);
}