Add paragraph indentation and hfill
This commit is contained in:
1 parent
bad477fbee
commit
e44d4c8669
11 files changed
+362
-42
No files matched your search
@@ -55,12 +55,13 @@ impl Default for PageSize {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
#[derive(Clone)]
|
||||
pub struct DocumentModel {
|
||||
pub title: String,
|
||||
pub author: String,
|
||||
pub date: String,
|
||||
pub page: PageSize,
|
||||
pub parindent: common::Scaled,
|
||||
pub margins: PageMargins,
|
||||
pub show_title: bool,
|
||||
pub blocks: Vec<Block>,
|
||||
@@ -69,6 +70,25 @@ pub struct DocumentModel {
|
||||
pub citations: HashMap<String, usize>,
|
||||
}
|
||||
|
||||
impl Default for DocumentModel {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
title: String::new(),
|
||||
author: String::new(),
|
||||
date: String::new(),
|
||||
page: PageSize::default(),
|
||||
// article's default \parindent for 10/11pt text.
|
||||
parindent: common::Scaled(17 * 65_536),
|
||||
margins: PageMargins::default(),
|
||||
show_title: false,
|
||||
blocks: Vec::new(),
|
||||
block_sources: Vec::new(),
|
||||
labels: HashMap::new(),
|
||||
citations: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ParsedDocument(pub DocumentModel);
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -199,6 +219,8 @@ pub struct Run {
|
||||
pub bold: bool,
|
||||
pub italic: bool,
|
||||
pub monospace: bool,
|
||||
/// True for the synthetic kern that carries paragraph indentation.
|
||||
pub paragraph_indent: bool,
|
||||
}
|
||||
|
||||
impl Run {
|
||||
@@ -215,6 +237,7 @@ impl Run {
|
||||
bold: false,
|
||||
italic: false,
|
||||
monospace: false,
|
||||
paragraph_indent: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -407,6 +407,8 @@ struct Parser {
|
||||
centered: Option<Vec<Run>>,
|
||||
current_source: Option<SourceLocation>,
|
||||
paragraph_source: Option<SourceLocation>,
|
||||
paragraph_indent: Option<common::Scaled>,
|
||||
suppress_indent: bool,
|
||||
}
|
||||
|
||||
impl Parser {
|
||||
@@ -444,6 +446,8 @@ impl Parser {
|
||||
centered: None,
|
||||
current_source: None,
|
||||
paragraph_source: None,
|
||||
paragraph_indent: None,
|
||||
suppress_indent: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,9 +471,12 @@ impl Parser {
|
||||
if let Some(candidate) =
|
||||
rich_paragraph_candidate(expanded, item_index, self.document.blocks.len())
|
||||
{
|
||||
if let Some(runs) =
|
||||
if let Some(mut runs) =
|
||||
paragraph_cache.get_rich(&candidate.key, &candidate.math_expressions)
|
||||
{
|
||||
self.begin_paragraph_if_needed();
|
||||
let indent = self.paragraph_indent.take().unwrap_or(common::Scaled::ZERO);
|
||||
prepend_paragraph_indent(&mut runs, indent);
|
||||
self.push_block_at(Block::Paragraph(runs), Some(candidate.source));
|
||||
self.current_source = Some(candidate.end_source);
|
||||
item_index = candidate.end_index;
|
||||
@@ -494,8 +501,12 @@ impl Parser {
|
||||
if !pending_text.is_empty() {
|
||||
self.current_source = pending_source.take();
|
||||
if paragraph_break && self.can_reuse_plain_paragraph() {
|
||||
let runs = paragraph_cache.parse(&pending_text);
|
||||
let mut runs = paragraph_cache.parse(&pending_text);
|
||||
if !runs.is_empty() {
|
||||
self.begin_paragraph_if_needed();
|
||||
let indent =
|
||||
self.paragraph_indent.take().unwrap_or(common::Scaled::ZERO);
|
||||
prepend_paragraph_indent(&mut runs, indent);
|
||||
self.push_block_at(
|
||||
Block::Paragraph(runs),
|
||||
self.current_source.clone(),
|
||||
@@ -535,7 +546,9 @@ impl Parser {
|
||||
if let Some(Block::Paragraph(runs)) =
|
||||
self.document.blocks.get(candidate.block_index)
|
||||
{
|
||||
paragraph_cache.insert_rich(candidate.key, runs.clone());
|
||||
let mut cached = runs.clone();
|
||||
strip_paragraph_indent(&mut cached);
|
||||
paragraph_cache.insert_rich(candidate.key, cached);
|
||||
}
|
||||
}
|
||||
cache_scan_pending = true;
|
||||
@@ -555,7 +568,9 @@ impl Parser {
|
||||
&& candidate.end_index == expanded.items.len()
|
||||
&& let Some(Block::Paragraph(runs)) = self.document.blocks.get(candidate.block_index)
|
||||
{
|
||||
paragraph_cache.insert_rich(candidate.key, runs.clone());
|
||||
let mut cached = runs.clone();
|
||||
strip_paragraph_indent(&mut cached);
|
||||
paragraph_cache.insert_rich(candidate.key, cached);
|
||||
}
|
||||
while !self.lists.is_empty() {
|
||||
self.finish_list();
|
||||
@@ -656,6 +671,7 @@ impl Parser {
|
||||
expanded::FrontendEventKind::MakeTitle => {
|
||||
self.flush_paragraph();
|
||||
self.document.show_title = true;
|
||||
self.suppress_indent = true;
|
||||
}
|
||||
expanded::FrontendEventKind::InlineBoxStart => {
|
||||
self.inline_boxes.push(Vec::new());
|
||||
@@ -675,6 +691,8 @@ impl Parser {
|
||||
bold: false,
|
||||
italic: false,
|
||||
monospace: false,
|
||||
|
||||
paragraph_indent: false,
|
||||
};
|
||||
self.target_runs().push(run);
|
||||
}
|
||||
@@ -872,6 +890,14 @@ impl Parser {
|
||||
self.start_heading(*level, *numbered)
|
||||
}
|
||||
expanded::FrontendEventKind::HeadingEnd => self.finish_heading(),
|
||||
expanded::FrontendEventKind::Parindent(width) => {
|
||||
self.document.parindent = *width;
|
||||
}
|
||||
expanded::FrontendEventKind::NoIndent => {
|
||||
if self.paragraph.is_empty() {
|
||||
self.suppress_indent = true;
|
||||
}
|
||||
}
|
||||
expanded::FrontendEventKind::CaptionStart => {
|
||||
self.flush_paragraph();
|
||||
self.caption_capture = Some(Vec::new());
|
||||
@@ -931,6 +957,8 @@ impl Parser {
|
||||
bold,
|
||||
italic: true,
|
||||
monospace: false,
|
||||
|
||||
paragraph_indent: false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -963,10 +991,14 @@ impl Parser {
|
||||
},
|
||||
heading.source,
|
||||
);
|
||||
self.suppress_indent = true;
|
||||
}
|
||||
}
|
||||
|
||||
fn append_text(&mut self, text: &str) {
|
||||
if normalize_whitespace(text).is_empty() {
|
||||
return;
|
||||
}
|
||||
if !text.is_empty()
|
||||
&& self.paragraph.is_empty()
|
||||
&& self.heading.is_none()
|
||||
@@ -1043,6 +1075,8 @@ impl Parser {
|
||||
bold,
|
||||
italic,
|
||||
monospace,
|
||||
|
||||
paragraph_indent: false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1071,6 +1105,8 @@ impl Parser {
|
||||
bold,
|
||||
italic,
|
||||
monospace,
|
||||
|
||||
paragraph_indent: false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1104,6 +1140,8 @@ impl Parser {
|
||||
bold,
|
||||
italic,
|
||||
monospace,
|
||||
|
||||
paragraph_indent: false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1147,10 +1185,27 @@ impl Parser {
|
||||
bold,
|
||||
italic,
|
||||
monospace,
|
||||
|
||||
paragraph_indent: false,
|
||||
});
|
||||
}
|
||||
|
||||
fn target_runs(&mut self) -> &mut Vec<Run> {
|
||||
let paragraph_target = self.inline_boxes.is_empty()
|
||||
&& self.centered.is_none()
|
||||
&& self.framed_box.is_none()
|
||||
&& self.metadata_capture.is_none()
|
||||
&& self.caption_capture.is_none()
|
||||
&& self.heading.is_none()
|
||||
&& self.table.is_none()
|
||||
&& self.lists.is_empty()
|
||||
&& !self
|
||||
.bibliography
|
||||
.as_ref()
|
||||
.is_some_and(|bibliography| bibliography.current.is_some());
|
||||
if paragraph_target {
|
||||
self.begin_paragraph_if_needed();
|
||||
}
|
||||
if let Some(runs) = self.inline_boxes.last_mut() {
|
||||
runs
|
||||
} else if let Some(runs) = &mut self.centered {
|
||||
@@ -1178,6 +1233,23 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
|
||||
fn begin_paragraph_if_needed(&mut self) {
|
||||
if !self.paragraph.is_empty() {
|
||||
return;
|
||||
}
|
||||
// Empty appends (for example whitespace-only tokens or macro-definition
|
||||
// text in the prelude) must not freeze the indentation of the paragraph
|
||||
// that eventually receives the first real run. Re-evaluate until a run
|
||||
// actually lands in the paragraph.
|
||||
self.paragraph_indent = Some(if self.suppress_indent {
|
||||
common::Scaled::ZERO
|
||||
} else {
|
||||
self.document.parindent
|
||||
});
|
||||
self.suppress_indent = false;
|
||||
self.paragraph_source = self.current_source.clone();
|
||||
}
|
||||
|
||||
fn append_literal_ampersand(&mut self) {
|
||||
let bold = self.bold;
|
||||
let italic = self.italic;
|
||||
@@ -1214,6 +1286,8 @@ impl Parser {
|
||||
bold,
|
||||
italic,
|
||||
monospace,
|
||||
|
||||
paragraph_indent: false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1227,8 +1301,10 @@ impl Parser {
|
||||
|
||||
fn flush_paragraph(&mut self) {
|
||||
trim_run_end(&mut self.paragraph);
|
||||
let indent = self.paragraph_indent.take().unwrap_or(common::Scaled::ZERO);
|
||||
if !self.paragraph.is_empty() {
|
||||
let runs = std::mem::take(&mut self.paragraph);
|
||||
let mut runs = std::mem::take(&mut self.paragraph);
|
||||
prepend_paragraph_indent(&mut runs, indent);
|
||||
let source = self.paragraph_source.take();
|
||||
self.push_block_at(Block::Paragraph(runs), source);
|
||||
} else {
|
||||
@@ -1523,6 +1599,35 @@ pub fn parse_document(
|
||||
Parser::new().parse(source, paragraph_cache)
|
||||
}
|
||||
|
||||
fn indent_run(indent: common::Scaled) -> Run {
|
||||
Run {
|
||||
text: String::new(),
|
||||
math: None,
|
||||
kern: Some(indent),
|
||||
glue: None,
|
||||
penalty: None,
|
||||
rule: None,
|
||||
box_run: None,
|
||||
unresolved_reference: None,
|
||||
bold: false,
|
||||
italic: false,
|
||||
monospace: false,
|
||||
paragraph_indent: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn prepend_paragraph_indent(runs: &mut Vec<Run>, indent: common::Scaled) {
|
||||
if indent.0 > 0 {
|
||||
runs.insert(0, indent_run(indent));
|
||||
}
|
||||
}
|
||||
|
||||
fn strip_paragraph_indent(runs: &mut Vec<Run>) {
|
||||
if runs.first().is_some_and(|run| run.paragraph_indent) {
|
||||
runs.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
fn append_run(runs: &mut Vec<Run>, mut text: String, bold: bool, italic: bool, monospace: bool) {
|
||||
if text.is_empty() {
|
||||
return;
|
||||
@@ -1560,6 +1665,8 @@ fn append_run(runs: &mut Vec<Run>, mut text: String, bold: bool, italic: bool, m
|
||||
bold,
|
||||
italic,
|
||||
monospace,
|
||||
|
||||
paragraph_indent: false,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -213,6 +213,8 @@ pub enum FrontendEventKind {
|
||||
level: u8,
|
||||
numbered: bool,
|
||||
},
|
||||
Parindent(common::Scaled),
|
||||
NoIndent,
|
||||
HeadingEnd,
|
||||
CaptionStart,
|
||||
CaptionEnd,
|
||||
|
||||
Reference in new issue
Block a user