From 1b0ce27ed3aa07d204c40ddd452ccd431c3a482b Mon Sep 17 00:00:00 2001 From: GloamXun Date: Mon, 19 Oct 2026 14:18:42 +0800 Subject: [PATCH] Honour the centering paragraph declaration --- README.md | 11 ++++++----- crates/texrra-core/src/document.rs | 1 + docs/ROADMAP.md | 2 +- docs/STATUS.md | 4 ++-- src/rust_typeset/frontend.rs | 1 - src/rust_typeset/pdf/layout.rs | 3 ++- src/rust_typeset/texlang_adapter.rs | 16 ++++++++++++++++ tests/typeset.rs | 27 +++++++++++++++++++++++++++ 8 files changed, 55 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9a00f40..e4eb8f9 100644 --- a/README.md +++ b/README.md @@ -298,10 +298,11 @@ block. The `center` environment is a block: its content wraps at the text width and every line is centered without justification (natural spaces), so `\begin{center}\url{...}\end{center}` and centered short text match LaTeX. -`\centering` inside a paragraph is still a no-op. `\raggedright` and -`\raggedleft` are paragraph-level declarations: the affected runs use natural -space widths, the declaration is scoped by groups and the `raggedright`/ -`raggedleft` environments, and the final line is not stretched to the margin. +`\centering`, `\raggedright`, and `\raggedleft` are paragraph-level +declarations: the affected runs use natural space widths, the declaration is +scoped by groups and the matching environments, centered lines are offset by +half the leftover width, and ragged-left/right lines are not stretched to the +margin. `\rule[raise]{width}{height}` is rendered as a filled rectangle in both the PDF and the SVG preview, including raised and dropped rules; a zero-thickness @@ -485,7 +486,7 @@ advisory breaks, starred `\vspace`/`\hspace`, `center`, `\hrule`, `\fbox`, | --- | --- | --- | | Boxes | `\rule`, inline framed `\fbox`, block `\parbox`/`\minipage`, `\vbox`/`\vtop` (as full-width block), inline `\mbox`/`\makebox`, `\raisebox`, `center`, `\hrule` | true nested VBox material | | Fonts | serif/sans/mono faces, bold/italic/mono/sans declarations, group scoping, true scaled small caps | — | -| Paragraphs | Knuth-Plass breaking, TeX hyphenation, justification, centering, `\raggedright`/`\raggedleft`, `\parindent` (17pt default), `\hfill`/`\hfil` | `\centering` in running text | +| Paragraphs | Knuth-Plass breaking, TeX hyphenation, justification, `\centering`/`\raggedright`/`\raggedleft`, `\parindent` (17pt default), `\hfill`/`\hfil` | — | | 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, page numbers resolved after pagination | dotted leaders, 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 | — | diff --git a/crates/texrra-core/src/document.rs b/crates/texrra-core/src/document.rs index be3b8c8..735e863 100644 --- a/crates/texrra-core/src/document.rs +++ b/crates/texrra-core/src/document.rs @@ -321,6 +321,7 @@ pub enum ParagraphAlignment { Justified, RaggedRight, RaggedLeft, + Centered, } #[derive(Clone, PartialEq, Eq, Hash)] diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index cca9088..70e9cf2 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -10,7 +10,7 @@ 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; native PDF rectangles; unified pagination; -`\raggedright`/`\raggedleft` declarations and environments +`\centering`/`\raggedright`/`\raggedleft` declarations and environments (`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 dc1c255..2d1f44d 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -20,7 +20,7 @@ the source changes; the SVG preview is refreshed per page. declarations with group scoping, grouped text-style commands, true small caps (lowercase uppercase glyphs at 80% size), and sans/serif/mono small caps. - Paragraphs: Knuth-Plass breaking, TeX hyphenation patterns, justification, - centering, `\raggedright`/`\raggedleft` declarations and environments, + `\centering`/`\raggedright`/`\raggedleft` declarations and environments, first-line `\parindent` (17pt default, group-scoped, `\setlength`/ `\addtolength`/`\noindent` aware), and `\hfill`/`\hfil` infinite glue. - Pages: A4/A5/B5/Letter/Legal/Executive, landscape, geometry margins and @@ -72,7 +72,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` (35 tests: 31 compiler +- `cargo fmt --check` passes; `cargo test --offline` (36 tests: 32 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/frontend.rs b/src/rust_typeset/frontend.rs index 1ca12e0..a447e94 100644 --- a/src/rust_typeset/frontend.rs +++ b/src/rust_typeset/frontend.rs @@ -31,7 +31,6 @@ const PRELUDE: &str = r#" \def\hfil{\hskip0pt plus 1fil\relax} \def\allowbreak{\penalty0} \def\nobreak{\penalty10000} -\def\centering{} \def\bibliographystyle#1{} \def\textbf#1{\texrrastylestart{bold}#1\texrrastyleend{bold}} \def\emph#1{\texrrastylestart{italic}#1\texrrastyleend{italic}} diff --git a/src/rust_typeset/pdf/layout.rs b/src/rust_typeset/pdf/layout.rs index 91c3d8e..bb05500 100644 --- a/src/rust_typeset/pdf/layout.rs +++ b/src/rust_typeset/pdf/layout.rs @@ -1392,6 +1392,7 @@ pub(super) fn build_positioned_pages( let paragraph_alignment = runs.iter().find_map(|run| run.alignment); let ragged = paragraph_alignment.is_some(); + let centered = paragraph_alignment == Some(ParagraphAlignment::Centered); let x = match (paragraph_alignment, &line.content) { ( Some(ParagraphAlignment::RaggedLeft), @@ -1420,7 +1421,7 @@ pub(super) fn build_positioned_pages( content: line.content, y: cursor, x, - centered: false, + centered, ragged, })); cursor = cursor.saturating_add(line_height); diff --git a/src/rust_typeset/texlang_adapter.rs b/src/rust_typeset/texlang_adapter.rs index f793f0a..269b03c 100644 --- a/src/rust_typeset/texlang_adapter.rs +++ b/src/rust_typeset/texlang_adapter.rs @@ -1738,6 +1738,18 @@ fn ragged_left_primitive( Ok(()) } +fn centering_primitive( + token: token::Token, + input: &mut vm::ExecutionInput, +) -> texlang::prelude::Result<()> { + write_event( + input, + token, + FrontendEventKind::ParagraphAlignment(texrra_core::document::ParagraphAlignment::Centered), + ); + Ok(()) +} + fn table_of_contents_primitive( token: token::Token, input: &mut vm::ExecutionInput, @@ -6801,6 +6813,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn left_alpha + 10.0, + "centering did not move the line: centered={centered_alpha}, left={left_alpha}\n{centered_svg}" + ); + let plain_alpha = text_x(¢ered_svg, "plain"); + let left_plain = text_x(&left_svg, "plain"); + assert!( + (plain_alpha - left_plain).abs() < 0.5, + "centering leaked into the following paragraph: centered={plain_alpha}, left={left_plain}" + ); +}