Read documentclass size options as the base font size

This commit is contained in:
gloamxun committed 2026-10-24 19:47:45 +08:00
1 parent 9876895a84
commit c494ad04ae
4 files changed
+83 -8

No files matched your search

+78 -3
View File
@@ -139,6 +139,8 @@ struct PageGeometry {
line_penalty: Option<i64>,
adj_demerits: Option<i64>,
space_factor_codes: Vec<(char, i64)>,
/// Base font size from `\documentclass[11pt]`-style options.
base_font_size: Option<i64>,
/// Captured `tabular` environments: (column spec, halign source).
tabular: Vec<(String, String)>,
}
@@ -147,6 +149,34 @@ struct PageGeometry {
/// `\textheight` assignments out of the source and return the remaining body.
/// Each command takes a TeX dimension. LaTeX resets `\vsize` from
/// `\textheight` at every page, so `\textheight` wins when both appear.
/// Read the base font size from a class option list such as
/// `\documentclass[11pt,a4paper]{article}`.
fn document_class_size(line: &str) -> Option<i64> {
let rest = line.strip_prefix("\\documentclass")?.trim_start();
let rest = rest.strip_prefix('[')?;
let end = rest.find(']')?;
for option in rest[..end].split(',') {
match option.trim() {
"10pt" => return Some(10 * SCALE),
"11pt" => return Some(11 * SCALE),
"12pt" => return Some(12 * SCALE),
_ => {}
}
}
None
}
/// LaTeX's article class sets `\baselineskip` from the base size.
fn class_baselineskip(font_size: i64) -> i64 {
if font_size >= 12 * 65_536 {
14 * 65_536 + 32_768
} else if font_size >= 11 * 65_536 {
13 * 65_536 + 39_322
} else {
12 * 65_536
}
}
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
let mut geometry = PageGeometry::default();
let mut body = String::new();
@@ -175,6 +205,10 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
}
continue;
}
if trimmed.starts_with("\\documentclass") {
geometry.base_font_size = document_class_size(trimmed);
continue;
}
if let Some(value) = dimension_assignment(trimmed, "hoffset") {
geometry.origin.h_offset = value;
} else if let Some(value) = dimension_assignment(trimmed, "voffset") {
@@ -1668,6 +1702,13 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
unit.space_shrink = paragraph.space_shrink;
return unit;
}
let base_font_size = paragraph
.font_size
.or(geometry.base_font_size)
.unwrap_or(655_360);
let baselineskip = geometry
.baseline_skip
.unwrap_or_else(|| class_baselineskip(base_font_size));
let style = ParagraphStyle {
line_width: 480 * SCALE,
left_skip: paragraph.left_skip * SCALE + geometry.left_skip.unwrap_or(0),
@@ -1690,7 +1731,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
par_fillskip_fil: geometry.par_fillskip.map(|(_, _, fil)| fil).unwrap_or(true),
frenchspacing: geometry.french_spacing.unwrap_or(false),
space_factor_codes: geometry.space_factor_codes.clone(),
baselineskip: geometry.baseline_skip.unwrap_or(12 * 65536),
baselineskip,
lineskip: geometry.lineskip.unwrap_or(65536),
lineskiplimit: geometry.lineskiplimit.unwrap_or(0),
par_indent: geometry.par_indent.unwrap_or(0),
@@ -1705,11 +1746,11 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
adj_demerits: geometry.adj_demerits.unwrap_or(10_000),
hyphenation_exceptions: geometry.hyphenation_exceptions.clone(),
line_height: if paragraph.line_height == 12 {
geometry.baseline_skip.unwrap_or(12 * 65536)
baselineskip
} else {
paragraph.line_height * SCALE
},
font_size: paragraph.font_size.unwrap_or(655_360),
font_size: base_font_size,
char_width: 10 * SCALE,
space_width: 4 * SCALE,
space_stretch: 6 * SCALE,
@@ -1848,6 +1889,40 @@ mod tests {
assert_eq!(unit.source, "Huge text");
}
#[test]
fn documentclass_size_options_set_the_base_size() {
let source =
"\\documentclass[12pt]{article}\n\\begin{document}\n\nPlain text\n\n\\end{document}";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.base_font_size, Some(12 * SCALE));
let paragraphs = paragraphs(&body).unwrap();
let document = build_document(&paragraphs, &geometry);
let style = document.units[0].paragraph_style.as_ref().unwrap();
assert_eq!(style.font_size, 12 * SCALE);
assert_eq!(style.baselineskip, 14 * 65_536 + 32_768);
}
#[test]
fn explicit_size_commands_override_the_class_size() {
let source = "\\documentclass[12pt]{article}\n\\begin{document}\n\n\\small Small text\n\n\\end{document}";
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs(&body).unwrap();
let document = build_document(&paragraphs, &geometry);
let style = document.units[0].paragraph_style.as_ref().unwrap();
assert_eq!(style.font_size, 9 * SCALE);
}
#[test]
fn documentclass_defaults_to_ten_point() {
let source = "\\documentclass{article}\n\\begin{document}\n\nPlain text\n\n\\end{document}";
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs(&body).unwrap();
let document = build_document(&paragraphs, &geometry);
let style = document.units[0].paragraph_style.as_ref().unwrap();
assert_eq!(style.font_size, 10 * SCALE);
assert_eq!(style.baselineskip, 12 * 65_536);
}
#[test]
fn engine_template_keeps_paragraph_columns() {
assert_eq!(engine_template("{|l|c|p{3cm}|}"), "lcp{3cm}");