Honour raggedright and raggedleft paragraph alignment
This commit is contained in:
1 parent
c7b7660049
commit
2c8d5e12d8
13 files changed
+238
-40
No files matched your search
@@ -32,8 +32,6 @@ const PRELUDE: &str = r#"
|
||||
\def\allowbreak{\penalty0}
|
||||
\def\nobreak{\penalty10000}
|
||||
\def\centering{}
|
||||
\def\raggedright{}
|
||||
\def\raggedleft{}
|
||||
\def\bibliographystyle#1{}
|
||||
\def\textbf#1{\texrrastylestart{bold}#1\texrrastyleend{bold}}
|
||||
\def\emph#1{\texrrastylestart{italic}#1\texrrastyleend{italic}}
|
||||
|
||||
@@ -86,6 +86,7 @@ struct PositionedLine {
|
||||
y: i64,
|
||||
x: i64,
|
||||
centered: bool,
|
||||
ragged: bool,
|
||||
}
|
||||
|
||||
struct PositionedTableLine {
|
||||
@@ -495,6 +496,9 @@ impl Element for PositionedPages {
|
||||
} else {
|
||||
text_line
|
||||
};
|
||||
if planned_line.ragged {
|
||||
text_line = text_line.ragged();
|
||||
}
|
||||
text_line.render(context, line_area, inherited_style)?
|
||||
}
|
||||
PositionedLineContent::InlineMath(element) => {
|
||||
@@ -2346,6 +2350,7 @@ pub(super) fn render_pdf(
|
||||
monospace: false,
|
||||
|
||||
paragraph_indent: false,
|
||||
alignment: None,
|
||||
|
||||
sans: false,
|
||||
small_caps: false,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::*;
|
||||
use texrra_core::document::UnresolvedReference;
|
||||
use texrra_core::document::{ParagraphAlignment, UnresolvedReference};
|
||||
|
||||
const ARTICLE_11PT_LIST_TOPSEP_PT: f64 = 9.0;
|
||||
const ARTICLE_11PT_LIST_TOPSEP_STRETCH_PT: f64 = 3.0;
|
||||
@@ -1172,6 +1172,7 @@ fn append_page_footnotes(
|
||||
y,
|
||||
x: 0,
|
||||
centered: false,
|
||||
ragged: false,
|
||||
}));
|
||||
y = y.saturating_add(separator_gap);
|
||||
for line in note_lines {
|
||||
@@ -1185,6 +1186,7 @@ fn append_page_footnotes(
|
||||
y,
|
||||
x: millimeters_to_scaled(8.0),
|
||||
centered: false,
|
||||
ragged: false,
|
||||
}));
|
||||
y = y.saturating_add(height);
|
||||
}
|
||||
@@ -1315,6 +1317,7 @@ pub(super) fn build_positioned_pages(
|
||||
y: cursor,
|
||||
x: 0,
|
||||
centered: true,
|
||||
ragged: false,
|
||||
}));
|
||||
cursor = cursor.saturating_add(line.height);
|
||||
}
|
||||
@@ -1387,6 +1390,27 @@ pub(super) fn build_positioned_pages(
|
||||
max_depth,
|
||||
);
|
||||
|
||||
let paragraph_alignment = runs.iter().find_map(|run| run.alignment);
|
||||
let ragged = paragraph_alignment.is_some();
|
||||
let x = match (paragraph_alignment, &line.content) {
|
||||
(
|
||||
Some(ParagraphAlignment::RaggedLeft),
|
||||
PositionedLineContent::Text { line, font_size },
|
||||
) => {
|
||||
let text_line = text_layout::TextLine::new(
|
||||
line,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
*font_size,
|
||||
);
|
||||
millimeters_to_scaled(
|
||||
(content_width_mm - text_line.natural_width_mm(font_cache))
|
||||
.max(0.0),
|
||||
)
|
||||
}
|
||||
_ => 0,
|
||||
};
|
||||
pages
|
||||
.last_mut()
|
||||
.expect("the page list starts with one page")
|
||||
@@ -1395,8 +1419,9 @@ pub(super) fn build_positioned_pages(
|
||||
anchor,
|
||||
content: line.content,
|
||||
y: cursor,
|
||||
x: 0,
|
||||
x,
|
||||
centered: false,
|
||||
ragged,
|
||||
}));
|
||||
cursor = cursor.saturating_add(line_height);
|
||||
}
|
||||
@@ -1561,6 +1586,7 @@ pub(super) fn build_positioned_pages(
|
||||
y: cursor,
|
||||
x: 0,
|
||||
centered: false,
|
||||
ragged: false,
|
||||
}));
|
||||
cursor = cursor.saturating_add(rule_height);
|
||||
pending_gap = paragraph_gap;
|
||||
@@ -1726,6 +1752,7 @@ pub(super) fn build_positioned_pages(
|
||||
y: cursor,
|
||||
x: millimeters_to_scaled(label_x_mm),
|
||||
centered: false,
|
||||
ragged: false,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -1739,6 +1766,7 @@ pub(super) fn build_positioned_pages(
|
||||
y: cursor,
|
||||
x: millimeters_to_scaled(x_mm),
|
||||
centered: false,
|
||||
ragged: false,
|
||||
}));
|
||||
cursor = cursor.saturating_add(line.height);
|
||||
}
|
||||
@@ -1802,6 +1830,7 @@ pub(super) fn build_positioned_pages(
|
||||
y: cursor,
|
||||
x: 0,
|
||||
centered: true,
|
||||
ragged: false,
|
||||
}));
|
||||
cursor = cursor.saturating_add(line.height);
|
||||
line_index += 1;
|
||||
@@ -2034,6 +2063,7 @@ pub(super) fn build_positioned_pages(
|
||||
y: cursor,
|
||||
x: 0,
|
||||
centered: false,
|
||||
ragged: false,
|
||||
}));
|
||||
cursor = cursor.saturating_add(line.height);
|
||||
line_index += 1;
|
||||
@@ -2083,6 +2113,7 @@ pub(super) fn build_positioned_pages(
|
||||
y: cursor,
|
||||
x: 0,
|
||||
centered: false,
|
||||
ragged: false,
|
||||
}));
|
||||
cursor = cursor.saturating_add(line.height);
|
||||
line_index += 1;
|
||||
@@ -2343,6 +2374,7 @@ fn append_decoration(
|
||||
y,
|
||||
x,
|
||||
centered: false,
|
||||
ragged: false,
|
||||
}));
|
||||
y = y.saturating_add(line.height);
|
||||
}
|
||||
|
||||
@@ -91,6 +91,7 @@ fn page_svg(
|
||||
} else {
|
||||
Alignment::Left
|
||||
},
|
||||
line.ragged,
|
||||
content_width,
|
||||
fonts,
|
||||
cjk_family,
|
||||
@@ -180,6 +181,7 @@ fn write_table(
|
||||
x,
|
||||
y + Mm::from(cursor + cell_y),
|
||||
alignment,
|
||||
false,
|
||||
column_width,
|
||||
fonts,
|
||||
cjk_family,
|
||||
@@ -228,6 +230,7 @@ fn write_figure(
|
||||
Mm::from(0.0),
|
||||
y + Mm::from(cursor),
|
||||
Alignment::Center,
|
||||
false,
|
||||
content_width,
|
||||
fonts,
|
||||
cjk_family,
|
||||
@@ -245,6 +248,7 @@ fn write_line_content(
|
||||
x: Mm,
|
||||
y: Mm,
|
||||
alignment: Alignment,
|
||||
ragged: bool,
|
||||
area_width: Mm,
|
||||
fonts: &fonts::FontCache,
|
||||
cjk_family: Option<FontFamily<fonts::Font>>,
|
||||
@@ -253,18 +257,27 @@ fn write_line_content(
|
||||
) {
|
||||
match content {
|
||||
PositionedLineContent::Text { line, font_size } => {
|
||||
text_layout::TextLine::new(line, cjk_family, monospace_family, sans_family, *font_size)
|
||||
.aligned(alignment)
|
||||
.write_svg(
|
||||
fonts,
|
||||
cjk_family,
|
||||
monospace_family,
|
||||
sans_family,
|
||||
area_width,
|
||||
x,
|
||||
y,
|
||||
output,
|
||||
);
|
||||
let mut text_line = text_layout::TextLine::new(
|
||||
line,
|
||||
cjk_family,
|
||||
monospace_family,
|
||||
sans_family,
|
||||
*font_size,
|
||||
)
|
||||
.aligned(alignment);
|
||||
if ragged {
|
||||
text_line = text_line.ragged();
|
||||
}
|
||||
text_line.write_svg(
|
||||
fonts,
|
||||
cjk_family,
|
||||
monospace_family,
|
||||
sans_family,
|
||||
area_width,
|
||||
x,
|
||||
y,
|
||||
output,
|
||||
);
|
||||
}
|
||||
PositionedLineContent::InlineMath(element) => {
|
||||
element.write_svg(
|
||||
@@ -324,6 +337,7 @@ fn write_framed_box(
|
||||
Mm::from(element.inset_mm),
|
||||
line_y,
|
||||
Alignment::Left,
|
||||
false,
|
||||
Mm::from(width - 2.0 * element.inset_mm),
|
||||
fonts,
|
||||
cjk_family,
|
||||
@@ -359,6 +373,7 @@ fn write_centered(
|
||||
Mm::from(0.0),
|
||||
line_y,
|
||||
Alignment::Center,
|
||||
false,
|
||||
content_width,
|
||||
fonts,
|
||||
cjk_family,
|
||||
|
||||
@@ -1710,6 +1710,34 @@ fn page_style_primitive(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ragged_right_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::ParagraphAlignment(
|
||||
texrra_core::document::ParagraphAlignment::RaggedRight,
|
||||
),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ragged_left_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::ParagraphAlignment(
|
||||
texrra_core::document::ParagraphAlignment::RaggedLeft,
|
||||
),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn table_of_contents_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
@@ -6765,6 +6793,14 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
|
||||
texlang::command::BuiltIn::new_execution(page_break_primitive)
|
||||
.with_tag(layout_primitive_tag(LayoutPrimitive::PageBreak)),
|
||||
);
|
||||
built_ins.insert(
|
||||
"raggedright",
|
||||
texlang::command::BuiltIn::new_execution(ragged_right_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"raggedleft",
|
||||
texlang::command::BuiltIn::new_execution(ragged_left_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"pagestyle",
|
||||
texlang::command::BuiltIn::new_execution(page_style_primitive),
|
||||
|
||||
@@ -83,6 +83,8 @@ pub(super) struct TextLine {
|
||||
width: Scaled,
|
||||
items: Vec<TextLineItem>,
|
||||
alignment: Alignment,
|
||||
/// Keep natural inter-word spaces instead of the justified glue widths.
|
||||
ragged: bool,
|
||||
font_size: u8,
|
||||
}
|
||||
|
||||
@@ -270,6 +272,7 @@ impl TextLine {
|
||||
width: line.width,
|
||||
items,
|
||||
alignment: Alignment::Left,
|
||||
ragged: false,
|
||||
font_size,
|
||||
}
|
||||
}
|
||||
@@ -279,6 +282,11 @@ impl TextLine {
|
||||
self
|
||||
}
|
||||
|
||||
pub(super) fn ragged(mut self) -> Self {
|
||||
self.ragged = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub(super) fn aligned(mut self, alignment: Alignment) -> Self {
|
||||
self.alignment = alignment;
|
||||
self
|
||||
@@ -286,17 +294,23 @@ impl TextLine {
|
||||
|
||||
/// Natural (unjustified) width of the line in millimetres, for centering
|
||||
/// and right-aligning decorations whose box may be wider than the text.
|
||||
pub(super) fn natural_width_mm(&self, fonts: &fonts::FontCache) -> f64 {
|
||||
pub(crate) fn natural_width_mm(&self, fonts: &fonts::FontCache) -> f64 {
|
||||
let natural_space = Style::new()
|
||||
.with_font_size(self.font_size)
|
||||
.str_width(fonts, " ");
|
||||
let mut x = Mm::from(0.0);
|
||||
for item in &self.items {
|
||||
let last_content = self
|
||||
.items
|
||||
.iter()
|
||||
.rposition(|item| !matches!(item, TextLineItem::Glue(_)))
|
||||
.unwrap_or(0);
|
||||
for (index, item) in self.items.iter().enumerate() {
|
||||
match item {
|
||||
TextLineItem::Text(span) => {
|
||||
x += span.style.str_width(fonts, &span.text);
|
||||
}
|
||||
TextLineItem::Glue(_) => x += natural_space,
|
||||
TextLineItem::Glue(_) if index < last_content => x += natural_space,
|
||||
TextLineItem::Glue(_) => {}
|
||||
TextLineItem::Kern(width) => x += *width,
|
||||
TextLineItem::Rule { width, .. } => x += *width,
|
||||
TextLineItem::Frame { width, .. } => x += *width,
|
||||
@@ -373,7 +387,7 @@ impl TextLine {
|
||||
x += span.style.str_width(fonts, &span.text);
|
||||
}
|
||||
TextLineItem::Glue(width) => {
|
||||
x += if self.alignment == Alignment::Left {
|
||||
x += if self.alignment == Alignment::Left && !self.ragged {
|
||||
*width
|
||||
} else {
|
||||
natural_space
|
||||
|
||||
Reference in new issue
Block a user