Support geometry text block dimensions

This commit is contained in:
gloamxun committed 2026-10-20 00:34:58 +08:00
1 parent 0864536d6d
commit 5862664c76
2 files changed
+59 -5

No files matched your search

+46 -5
View File
@@ -2556,10 +2556,29 @@ fn apply_class_font_size(document: &mut DocumentModel, settings: &str) {
/// This is shared by the document parser and by the frontend's live length
/// tracker, so `\textwidth`/`\textheight` expanded during macro processing
/// match the layout that the parser will produce.
fn split_geometry_settings(settings: &str) -> Vec<&str> {
let mut parts = Vec::new();
let mut depth = 0usize;
let mut start = 0usize;
for (index, character) in settings.char_indices() {
match character {
'{' => depth += 1,
'}' => depth = depth.saturating_sub(1),
',' if depth == 0 => {
parts.push(&settings[start..index]);
start = index + 1;
}
_ => {}
}
}
parts.push(&settings[start..]);
parts
}
pub fn apply_geometry_settings(page: &mut PageSize, margins: &mut PageMargins, settings: &str) {
// Paper size first: explicit margins in the same list must be measured
// against the final page dimensions.
for setting in settings.split(',') {
for setting in split_geometry_settings(settings) {
let setting = setting.trim();
if setting.is_empty() {
continue;
@@ -2590,7 +2609,7 @@ pub fn apply_geometry_settings(page: &mut PageSize, margins: &mut PageMargins, s
}
}
for setting in settings.split(',') {
for setting in split_geometry_settings(settings) {
let Some((key, value)) = setting.split_once('=') else {
continue;
};
@@ -2626,14 +2645,36 @@ pub fn apply_geometry_settings(page: &mut PageSize, margins: &mut PageMargins, s
// influences the computed bottom margin in the same settings list.
let page_width = page.width_mm;
let page_height = page.height_mm;
for setting in settings.split(',') {
for setting in split_geometry_settings(settings) {
let Some((key, value)) = setting.split_once('=') else {
continue;
};
let Some(value) = parse_dimension(value.trim()) else {
let key = key.trim();
let value = value.trim();
if key == "text" {
let inner = value
.strip_prefix('{')
.and_then(|value| value.strip_suffix('}'))
.unwrap_or(value);
let Some((width, height)) = inner.split_once(',') else {
continue;
};
let (Some(width), Some(height)) = (
parse_dimension(width.trim()),
parse_dimension(height.trim()),
) else {
continue;
};
let side = ((page_width - width) / 2.0).max(0.0);
margins.left = side;
margins.right = side;
margins.bottom = (page_height - margins.top - height).max(0.0);
continue;
}
let Some(value) = parse_dimension(value) else {
continue;
};
match key.trim() {
match key {
"textwidth" => {
let side = ((page_width - value) / 2.0).max(0.0);
margins.left = side;
+13
View File
@@ -1838,6 +1838,19 @@ See \eqref{eq:x}. $\bar{x}+\vec{v}$.
assert_eq!(value["status"], "ok");
}
#[test]
fn geometry_text_sets_the_text_block() {
let source = "\\documentclass{article}\n\\usepackage[text={100mm,150mm}]{geometry}\n\\begin{document}\nBody\n\\end{document}\n";
let value = fresh_preview(source, "geometry-text.tex");
assert_eq!(value["status"], "ok");
let svg = page_svg(&value, 0);
assert!(
svg.contains("translate(55.0000 25.0000)"),
"geometry text={{100mm,150mm}} should center a 100mm block: {}",
&svg[..svg.len().min(300)]
);
}
#[test]
fn sivp_project_exports_a_pdf() {
let source = std::fs::read_to_string("projects/_sivp__/sn-article.tex").unwrap();