From a7982218179f9b663c0aaad915e96a3de7a25fd2 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Mon, 19 Oct 2026 14:23:53 +0800 Subject: [PATCH] Add chapter headings and chapter-prefixed numbering --- README.md | 2 +- crates/texrra-core/src/document_parser.rs | 24 ++++++++++++++++----- docs/ROADMAP.md | 3 ++- docs/STATUS.md | 11 ++++++---- src/rust_typeset/pdf.rs | 2 ++ src/rust_typeset/texlang_adapter.rs | 11 ++++++++++ tests/typeset.rs | 26 +++++++++++++++++++++++ 7 files changed, 68 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9120f4d..15abf1b 100644 --- a/README.md +++ b/README.md @@ -490,7 +490,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`, | Notes/floats | footnotes with page-bottom insertion area, title/author `\thanks`, figure/table `[htbp]` placement (top/bottom/page/here approximations) | deferred float queue, `\textfraction` | | Contents | `\tableofcontents`, `\listoffigures`, `\listoftables` generated from numbered headings and captions, dotted leaders, page numbers resolved after pagination | per-chapter book/report contents | | Pages | A4/Letter/... page sizes, margins/geometry, automatic pagination, `\newpage`, `\pagebreak`, plain/fancy page styles, `\thispagestyle`, fancyhdr L/C/R headers and footers | — | -| Classes/packages | built-in + local classes, option protocol, local package options, base font sizes 10/11/12pt | class-specific section formatting | +| Classes/packages | built-in + local classes, option protocol, local package options, base font sizes 10/11/12pt, chapter headings and numbering | class-specific formatting (automatic chapter page breaks) | | Live preview | incremental paragraph cache, page-hash diffing, SVG refresh | — | The compatibility layer also covers spacing macros, text symbols/letters, style diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index 9c89c76..0ea5a0e 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -460,7 +460,8 @@ struct Parser { text_style_stack: Vec<(expanded::TextStyle, bool, bool, bool, bool, bool)>, style_declarations: Vec<(usize, expanded::TextStyle)>, group_depth: usize, - section_numbers: [u32; 3], + section_numbers: [u32; 4], + has_chapters: bool, equation_number: u32, figure_number: u32, table_number: u32, @@ -518,7 +519,8 @@ impl Parser { text_style_stack: Vec::new(), style_declarations: Vec::new(), group_depth: 0, - section_numbers: [0; 3], + section_numbers: [0; 4], + has_chapters: false, equation_number: 0, figure_number: 0, table_number: 0, @@ -1257,7 +1259,10 @@ impl Parser { fn start_heading(&mut self, level: u8, numbered: bool) { self.flush_paragraph(); self.finish_heading(); - let level = level.clamp(1, 3); + let level = level.min(3); + if level == 0 { + self.has_chapters = true; + } let runs = if numbered { let number = self.next_section_number(level); self.current_reference = number.clone(); @@ -1941,10 +1946,19 @@ impl Parser { } fn next_section_number(&mut self, level: u8) -> String { - let index = usize::from(level.saturating_sub(1).min(2)); + let index = usize::from(level.min(3)); self.section_numbers[index] += 1; self.section_numbers[index + 1..].fill(0); - self.section_numbers[..=index] + if level == 0 { + self.has_chapters = true; + return self.section_numbers[0].to_string(); + } + let start = if self.has_chapters && self.section_numbers[0] > 0 { + 0 + } else { + 1 + }; + self.section_numbers[start..=index] .iter() .map(u32::to_string) .collect::>() diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 41a17a0..b75cae4 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -10,7 +10,8 @@ footnotes/`\thanks`; figure/table `[htbp]` placement approximations; page styles (plain/fancy, `\thispagestyle`, fancyhdr headers/footers, `\thepage`); geometry-aware `\textwidth`/`\textheight` named length registers; table of contents and figure/table lists with dotted leaders; native PDF rectangles; unified pagination; -`\centering`/`\raggedright`/`\raggedleft` declarations and environments +`\centering`/`\raggedright`/`\raggedleft` declarations and environments; +chapter headings with chapter-prefixed section numbering (`build_positioned_pages` is the only page builder and no separate break planner remains). diff --git a/docs/STATUS.md b/docs/STATUS.md index 6d0406f..7957dca 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -34,9 +34,11 @@ the source changes; the SVG preview is refreshed per page. - Notes: `\footnote{...}` page-bottom insertion area with separator rule, superscript markers, and title/author `\thanks`. - Contents: `\tableofcontents`, `\listoffigures`, and `\listoftables` - generate headings plus entries from numbered sections and captioned + generate headings plus entries from numbered chapters/sections and captioned figures/tables, with dotted leaders and page numbers resolved after pagination. +- Chapters: `\chapter` is a level-0 heading; chapter numbers prefix section + numbers (`1.1`, `1.1.1`) and reset lower counters. - Class sizes: 10pt/11pt/12pt set the body size and propagate through headings, title material, lists, tables, block boxes, footnotes, and the default paragraph indent (10/12pt use 15pt; 11pt uses 17pt). @@ -63,8 +65,9 @@ See `docs/ROADMAP.md` for concrete implementation plans. - Notes: footnotes inside captions, floats, or arbitrary boxes are not collected; the insertion area currently follows the page where the paragraph ends rather than the exact reference line. -- Contents: only numbered headings plus captions are listed; per-chapter - contents in book/report are not distinguished. +- Contents: chapter headings are listed with chapter-prefixed section + numbers, but contents are not split per chapter and `\chapter` does not + force a page break. - Miscellaneous: a document-level `\renewcommand{\maketitle}` is ignored in favor of the engine title block. @@ -72,7 +75,7 @@ See `docs/ROADMAP.md` for concrete implementation plans. - `examples/hello.tex`, `examples/boxes.tex`, `examples/compat.tex` compile to one A4 page each; the arXiv template project compiles to 3 Letter pages. -- `cargo fmt --check` passes; `cargo test --offline` (36 tests: 32 compiler +- `cargo fmt --check` passes; `cargo test --offline` (37 tests: 33 compiler integration tests + 4 server tests) covers the three examples, the arXiv fixture, floats/newpage, page styles and `\thispagestyle`, SVG/PDF output, project API, static frontend assets, path traversal, the legacy diff --git a/src/rust_typeset/pdf.rs b/src/rust_typeset/pdf.rs index ac5d8a8..6695b54 100644 --- a/src/rust_typeset/pdf.rs +++ b/src/rust_typeset/pdf.rs @@ -37,6 +37,8 @@ use self::layout::{ // from the article class's 14.4pt setting to 14pt. fn article_heading_font_size(level: u8, base: u8) -> u8 { match (base, level) { + (12, 0) => 24, + (_, 0) => 20, (12, 1) => 17, (_, 1) => 14, (12, 2) => 14, diff --git a/src/rust_typeset/texlang_adapter.rs b/src/rust_typeset/texlang_adapter.rs index 269b03c..ec07cd5 100644 --- a/src/rust_typeset/texlang_adapter.rs +++ b/src/rust_typeset/texlang_adapter.rs @@ -5832,6 +5832,13 @@ fn heading_start_primitive( Ok(()) } +fn chapter_primitive( + token: token::Token, + input: &mut vm::ExecutionInput, +) -> texlang::prelude::Result<()> { + heading_primitive(token, input, 0) +} + fn section_primitive( token: token::Token, input: &mut vm::ExecutionInput, @@ -7203,6 +7210,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn