Expand maketitle into the article title block

This commit is contained in:
gloamxun committed 2026-10-24 20:17:55 +08:00
1 parent 925c916ea5
commit 569bec31b6
4 files changed
+140 -4

No files matched your search

+136
View File
@@ -145,6 +145,12 @@ struct PageGeometry {
base_font_size: Option<i64>,
/// Captured `tabular` environments: (column spec, halign source).
tabular: Vec<(String, String)>,
/// `\title{...}` captured from the preamble.
title: Option<String>,
/// `\author{...}` captured from the preamble.
author: Option<String>,
/// `\date{...}` captured from the preamble.
date: Option<String>,
}
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight`/`\vsize`/
@@ -246,6 +252,13 @@ fn apply_geometry_package(geometry: &mut PageGeometry, options: &str) {
}
}
/// Read the braced argument of a command that occupies a whole line.
fn braced_value(line: &str, command: &str) -> Option<String> {
let rest = line.strip_prefix(command)?.trim_start();
let (value, _) = braced(rest)?;
Some(value.trim().to_string())
}
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
let mut geometry = PageGeometry::default();
let mut body = String::new();
@@ -274,6 +287,18 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
}
continue;
}
if let Some(value) = braced_value(trimmed, "\\title") {
geometry.title = Some(value);
continue;
}
if let Some(value) = braced_value(trimmed, "\\author") {
geometry.author = Some(value);
continue;
}
if let Some(value) = braced_value(trimmed, "\\date") {
geometry.date = Some(value);
continue;
}
if let Some(options) = geometry_package_options(trimmed) {
apply_geometry_package(&mut geometry, &options);
continue;
@@ -1801,7 +1826,90 @@ fn aligned_cell(source: &str) -> MathAtom {
MathAtom::Row(atoms)
}
/// The article class title size for a base size.
fn title_size(base_font_size: i64) -> f64 {
match base_font_size / SCALE {
12 => 24.88,
11 => 20.74,
_ => 17.28,
}
}
/// The article class `\large` size for a base size.
fn title_large_size(base_font_size: i64) -> f64 {
match base_font_size / SCALE {
12 => 14.4,
_ => 12.0,
}
}
fn title_block(text: String, size: f64, space_before: i64) -> SourceParagraph {
SourceParagraph {
text,
font: ParagraphFont::Roman,
line_height: 12,
math: false,
left_skip: 0,
centered: true,
right_aligned: false,
space_before,
space_stretch: 0,
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
font_size: Some((size * SCALE as f64) as i64),
}
}
/// Expand `\maketitle` into the centered title block that article.cls sets.
fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<SourceParagraph> {
if !blocks
.iter()
.any(|block| block.text.trim() == "\\maketitle")
{
return blocks.to_vec();
}
let base = geometry.base_font_size.unwrap_or(10 * SCALE);
let mut result = Vec::new();
for block in blocks {
if block.text.trim() != "\\maketitle" {
result.push(block.clone());
continue;
}
if let Some(title) = geometry.title.as_deref().filter(|title| !title.is_empty()) {
let mut paragraph =
title_block(title.to_string(), title_size(base), block.space_before);
paragraph.page_break = block.page_break;
result.push(paragraph);
}
let author_space = block.space_before + 12 * SCALE;
if let Some(author) = geometry
.author
.as_deref()
.filter(|author| !author.is_empty())
{
result.push(title_block(
author.to_string(),
title_large_size(base),
author_space,
));
}
if let Some(date) = geometry.date.as_deref().filter(|date| !date.is_empty()) {
result.push(title_block(
date.to_string(),
title_large_size(base),
12 * SCALE,
));
}
}
result
}
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
let paragraphs = expand_maketitle(paragraphs, geometry);
let line_width = geometry.line_width.unwrap_or(480 * SCALE);
let units = paragraphs
.iter()
@@ -2428,6 +2536,34 @@ mod source_tests {
assert_eq!(glue.par_skip_shrink, Some(SCALE));
}
#[test]
fn maketitle_expands_title_author_and_date() {
let source = "\\title{Paper}\n\\author{Ada}\n\\date{2024}\n\n\\maketitle\n\nBody";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.title.as_deref(), Some("Paper"));
assert_eq!(geometry.author.as_deref(), Some("Ada"));
let blocks = paragraphs(&body).expect("blocks");
let expanded = expand_maketitle(&blocks, &geometry);
assert_eq!(expanded.len(), 4);
assert_eq!(expanded[0].text, "Paper");
assert!(expanded[0].centered);
assert_eq!(expanded[0].font_size, Some((17.28 * SCALE as f64) as i64));
assert_eq!(expanded[1].text, "Ada");
assert_eq!(expanded[1].font_size, Some((12.0 * SCALE as f64) as i64));
assert_eq!(expanded[2].text, "2024");
assert_eq!(expanded[3].text, "Body");
assert!(!expanded[3].centered);
}
#[test]
fn maketitle_paints_its_title_block() {
let source = "\\title{Paper}\n\\author{Ada}\n\n\\maketitle\n\nBody";
let json = preview_json(source).expect("preview");
assert!(json.contains("font-size=\\\"17.280\\\""), "{json}");
assert!(json.contains("font-size=\\\"12.000\\\""), "{json}");
assert!(json.contains(">B</text>"), "{json}");
}
#[test]
fn alignment_environments_set_the_block_alignment() {
let blocks = paragraphs("\\begin{center}\nCentered\n\\end{center}").expect("blocks");