Apply geometry package margins to the page
This commit is contained in:
1 parent
6ae609b60e
commit
2783dfb284
4 files changed
+94
-4
No files matched your search
@@ -179,6 +179,73 @@ fn class_baselineskip(font_size: i64) -> i64 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the option string of `\usepackage[opts]{geometry}` or `\geometry{opts}`.
|
||||
fn geometry_package_options(line: &str) -> Option<String> {
|
||||
if let Some(rest) = line.strip_prefix("\\geometry") {
|
||||
let (options, _) = braced(rest.trim_start())?;
|
||||
return Some(options.to_string());
|
||||
}
|
||||
let rest = line.strip_prefix("\\usepackage")?;
|
||||
let rest = rest.trim_start().strip_prefix('[')?;
|
||||
let (options, rest) = rest.split_once(']')?;
|
||||
let rest = rest.trim_start().strip_prefix('{')?;
|
||||
let (packages, _) = rest.split_once('}')?;
|
||||
if packages.split(',').any(|name| name.trim() == "geometry") {
|
||||
Some(options.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply the subset of the `geometry` package that route-D can honour.
|
||||
fn apply_geometry_package(geometry: &mut PageGeometry, options: &str) {
|
||||
let paper_width = geometry.paper_width.unwrap_or(612 * SCALE);
|
||||
let paper_height = geometry.paper_height.unwrap_or(792 * SCALE);
|
||||
let mut left = None;
|
||||
let mut right = None;
|
||||
let mut top = None;
|
||||
let mut bottom = None;
|
||||
for option in options.split(',') {
|
||||
let Some((key, value)) = option.split_once('=') else {
|
||||
continue;
|
||||
};
|
||||
let Some(value) = parse_dimension(value.trim()) else {
|
||||
continue;
|
||||
};
|
||||
match key.trim() {
|
||||
"margin" => {
|
||||
left = Some(value);
|
||||
right = Some(value);
|
||||
top = Some(value);
|
||||
bottom = Some(value);
|
||||
}
|
||||
"hmargin" => {
|
||||
left = Some(value);
|
||||
right = Some(value);
|
||||
}
|
||||
"vmargin" => {
|
||||
top = Some(value);
|
||||
bottom = Some(value);
|
||||
}
|
||||
"left" => left = Some(value),
|
||||
"right" => right = Some(value),
|
||||
"top" => top = Some(value),
|
||||
"bottom" => bottom = Some(value),
|
||||
"textwidth" => geometry.line_width = Some(value),
|
||||
"textheight" => geometry.textheight = Some(value),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if left.is_some() || right.is_some() || top.is_some() || bottom.is_some() {
|
||||
let left = left.unwrap_or(0);
|
||||
let right = right.unwrap_or(0);
|
||||
let top = top.unwrap_or(0);
|
||||
let bottom = bottom.unwrap_or(0);
|
||||
geometry.line_width = Some(paper_width - left - right);
|
||||
geometry.textheight = Some(paper_height - top - bottom);
|
||||
}
|
||||
}
|
||||
|
||||
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
let mut geometry = PageGeometry::default();
|
||||
let mut body = String::new();
|
||||
@@ -207,6 +274,10 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(options) = geometry_package_options(trimmed) {
|
||||
apply_geometry_package(&mut geometry, &options);
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with("\\documentclass") {
|
||||
geometry.base_font_size = document_class_size(trimmed);
|
||||
continue;
|
||||
@@ -2335,6 +2406,25 @@ mod source_tests {
|
||||
assert_eq!(glue.par_skip_shrink, Some(SCALE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn geometry_package_sets_margins() {
|
||||
let (_, geometry) = geometry_and_body("\\usepackage[margin=1in]{geometry}\n\nA paragraph.");
|
||||
let width = geometry.line_width.expect("width");
|
||||
let height = geometry.textheight.expect("height");
|
||||
assert!((width - 468 * SCALE).abs() <= 2 * SCALE, "width {width}");
|
||||
assert!((height - 648 * SCALE).abs() <= 2 * SCALE, "height {height}");
|
||||
let (_, command) = geometry_and_body("\\geometry{left=1in,right=2in}\n\nA paragraph.");
|
||||
let left_right = command.line_width.expect("left/right width");
|
||||
assert!(
|
||||
(left_right - 396 * SCALE).abs() <= 2 * SCALE,
|
||||
"left/right width {left_right}"
|
||||
);
|
||||
let (_, explicit) = geometry_and_body(
|
||||
"\\usepackage[margin=1in]{geometry}\n\\textwidth=300pt\n\nA paragraph.",
|
||||
);
|
||||
assert_eq!(explicit.line_width, Some(300 * SCALE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newpage_starts_a_new_page() {
|
||||
let source = "Alpha beta gamma delta.\n\n\\newpage\n\nEpsilon zeta eta theta.";
|
||||
|
||||
Reference in new issue
Block a user