Honour the centering paragraph declaration

This commit is contained in:
gloamxun committed 2026-10-19 14:18:42 +08:00
1 parent 2c8d5e12d8
commit 1b0ce27ed3
8 files changed
+55 -10

No files matched your search

+6 -5
View File
@@ -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 | — |
+1
View File
@@ -321,6 +321,7 @@ pub enum ParagraphAlignment {
Justified,
RaggedRight,
RaggedLeft,
Centered,
}
#[derive(Clone, PartialEq, Eq, Hash)]
+1 -1
View File
@@ -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).
+2 -2
View File
@@ -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
-1
View File
@@ -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}}
+2 -1
View File
@@ -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);
+16
View File
@@ -1738,6 +1738,18 @@ fn ragged_left_primitive(
Ok(())
}
fn centering_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> 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<CompilerState>,
@@ -6801,6 +6813,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"raggedleft",
texlang::command::BuiltIn::new_execution(ragged_left_primitive),
);
built_ins.insert(
"centering",
texlang::command::BuiltIn::new_execution(centering_primitive),
);
built_ins.insert(
"pagestyle",
texlang::command::BuiltIn::new_execution(page_style_primitive),
+27
View File
@@ -924,3 +924,30 @@ fn ragged_alignment_declarations_are_honoured() {
"raggedright still justified the line: ragged={ragged_beta}, justified={justified_beta}\nRAGGED={ragged_svg}\nJUSTIFIED={justified_svg}"
);
}
#[test]
fn centering_declaration_centers_a_paragraph() {
let prefix = r"\documentclass{article}
\usepackage[margin=6.5cm]{geometry}
\begin{document}
";
let centered_source =
format!("{prefix}{{\\centering alpha beta}}\n\nplain paragraph\n\\end{{document}}");
let left_source = format!("{prefix}alpha beta\n\nplain paragraph\n\\end{{document}}");
let centered = fresh_preview(&centered_source, "centered.tex");
let left = fresh_preview(&left_source, "left.tex");
let centered_svg = page_svg(&centered, 0);
let left_svg = page_svg(&left, 0);
let centered_alpha = text_x(&centered_svg, "alpha");
let left_alpha = text_x(&left_svg, "alpha");
assert!(
centered_alpha > left_alpha + 10.0,
"centering did not move the line: centered={centered_alpha}, left={left_alpha}\n{centered_svg}"
);
let plain_alpha = text_x(&centered_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}"
);
}