Honour raggedright and raggedleft paragraph alignment

This commit is contained in:
gloamxun committed 2026-10-19 14:16:50 +08:00
1 parent c7b7660049
commit 2c8d5e12d8
13 files changed
+238 -40

No files matched your search

+12
View File
@@ -314,6 +314,15 @@ pub struct RuleBox {
pub height: common::Scaled,
}
/// Paragraph-level alignment requested by `\raggedright` or `\raggedleft`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ParagraphAlignment {
#[default]
Justified,
RaggedRight,
RaggedLeft,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Run {
pub text: String,
@@ -333,6 +342,8 @@ pub struct Run {
pub small_caps: bool,
/// True for the synthetic kern that carries paragraph indentation.
pub paragraph_indent: bool,
/// Alignment declaration active when this run started its paragraph.
pub alignment: Option<ParagraphAlignment>,
}
impl Run {
@@ -354,6 +365,7 @@ impl Run {
sans: false,
small_caps: false,
paragraph_indent: false,
alignment: None,
}
}
}
+62 -3
View File
@@ -4,8 +4,8 @@ use crate::{
TeXGlue, TeXGlueOrder,
document::{
Block, ColumnAlignment, DocumentModel, EquationNumber, InlineBox, InlineBoxAlign,
ListBlock, ListItem, PageMargins, PageSize, PageStyleOverride, ParsedDocument, RuleBox,
Run, UnresolvedReference,
ListBlock, ListItem, PageMargins, PageSize, PageStyleOverride, ParagraphAlignment,
ParsedDocument, RuleBox, Run, UnresolvedReference,
},
expanded,
math_expr::*,
@@ -494,6 +494,8 @@ struct Parser {
paragraph_source: Option<SourceLocation>,
paragraph_indent: Option<common::Scaled>,
suppress_indent: bool,
paragraph_alignment: ParagraphAlignment,
alignment_saves: Vec<ParagraphAlignment>,
}
impl Parser {
@@ -540,6 +542,8 @@ impl Parser {
paragraph_source: None,
paragraph_indent: None,
suppress_indent: false,
paragraph_alignment: ParagraphAlignment::Justified,
alignment_saves: Vec::new(),
}
}
@@ -816,6 +820,7 @@ impl Parser {
sans,
small_caps,
paragraph_indent: false,
alignment: None,
};
self.target_runs().push(marker);
}
@@ -857,6 +862,7 @@ impl Parser {
monospace: false,
paragraph_indent: false,
alignment: None,
sans: false,
small_caps: false,
@@ -1001,7 +1007,13 @@ impl Parser {
expanded::FrontendEventKind::ListOfTables => {
self.push_contents_placeholder(CONTENTS_TABLES_MARKER);
}
expanded::FrontendEventKind::GroupStart => self.group_depth += 1,
expanded::FrontendEventKind::ParagraphAlignment(alignment) => {
self.paragraph_alignment = *alignment;
}
expanded::FrontendEventKind::GroupStart => {
self.group_depth += 1;
self.alignment_saves.push(self.paragraph_alignment);
}
expanded::FrontendEventKind::GroupEnd => {
let depth = self.group_depth;
while self
@@ -1014,6 +1026,10 @@ impl Parser {
}
}
self.group_depth = self.group_depth.saturating_sub(1);
self.paragraph_alignment = self
.alignment_saves
.pop()
.unwrap_or(ParagraphAlignment::Justified);
}
expanded::FrontendEventKind::LiteralAmpersand
| expanded::FrontendEventKind::MathCell => self.append_literal_ampersand(),
@@ -1224,6 +1240,7 @@ impl Parser {
monospace: false,
paragraph_indent: false,
alignment: None,
sans: false,
small_caps: false,
@@ -1330,6 +1347,12 @@ impl Parser {
small_caps,
);
}
let alignment = self.paragraph_alignment;
if alignment != ParagraphAlignment::Justified
&& let Some(run) = self.target_runs().last_mut()
{
run.alignment = Some(alignment);
}
}
fn append_kern(&mut self, width: common::Scaled) {
@@ -1366,6 +1389,7 @@ impl Parser {
monospace,
paragraph_indent: false,
alignment: None,
sans: false,
small_caps: false,
@@ -1403,6 +1427,7 @@ impl Parser {
monospace,
paragraph_indent: false,
alignment: None,
sans: false,
small_caps: false,
@@ -1445,6 +1470,7 @@ impl Parser {
monospace,
paragraph_indent: false,
alignment: None,
sans: false,
small_caps: false,
@@ -1497,6 +1523,7 @@ impl Parser {
monospace,
paragraph_indent: false,
alignment: None,
sans: false,
small_caps: false,
@@ -1636,6 +1663,7 @@ impl Parser {
monospace,
paragraph_indent: false,
alignment: None,
sans: false,
small_caps: false,
@@ -1752,6 +1780,11 @@ impl Parser {
let indent = self.paragraph_indent.take().unwrap_or(common::Scaled::ZERO);
if !self.paragraph.is_empty() {
let mut runs = std::mem::take(&mut self.paragraph);
if self.paragraph_alignment != ParagraphAlignment::Justified
&& let Some(first) = runs.first_mut()
{
first.alignment = Some(self.paragraph_alignment);
}
prepend_paragraph_indent(&mut runs, indent);
let source = self.paragraph_source.take();
self.push_block_at(Block::Paragraph(runs), source);
@@ -1851,6 +1884,30 @@ impl Parser {
});
}
(false, "abstract") => self.flush_paragraph(),
(true, "raggedright") => {
self.flush_paragraph();
self.alignment_saves.push(self.paragraph_alignment);
self.paragraph_alignment = ParagraphAlignment::RaggedRight;
}
(false, "raggedright") => {
self.flush_paragraph();
self.paragraph_alignment = self
.alignment_saves
.pop()
.unwrap_or(ParagraphAlignment::Justified);
}
(true, "raggedleft") => {
self.flush_paragraph();
self.alignment_saves.push(self.paragraph_alignment);
self.paragraph_alignment = ParagraphAlignment::RaggedLeft;
}
(false, "raggedleft") => {
self.flush_paragraph();
self.paragraph_alignment = self
.alignment_saves
.pop()
.unwrap_or(ParagraphAlignment::Justified);
}
(true, "center") => {
self.flush_paragraph();
if self.centered.is_none() {
@@ -2108,6 +2165,7 @@ fn indent_run(indent: common::Scaled) -> Run {
y_offset: None,
footnote_number: None,
paragraph_indent: true,
alignment: None,
}
}
@@ -2193,6 +2251,7 @@ fn append_run(
sans,
small_caps,
paragraph_indent: false,
alignment: None,
y_offset: None,
+1
View File
@@ -245,6 +245,7 @@ pub enum FrontendEventKind {
header: bool,
},
PageNumber,
ParagraphAlignment(crate::document::ParagraphAlignment),
TableOfContents,
ListOfFigures,
ListOfTables,