Add footnote insertion area and thanks

This commit is contained in:
gloamxun committed 2026-10-19 13:11:17 +08:00
1 parent 5fa301fdcd
commit 1da46c61d6
11 files changed
+486 -28

No files matched your search

+90 -1
View File
@@ -420,6 +420,8 @@ struct Parser {
box_width: Option<common::Scaled>,
inline_boxes: Vec<InlineBoxSpec>,
minipage: Option<MinipageBuilder>,
footnote_capture: Option<Vec<Run>>,
footnote_counter: u32,
centered: Option<Vec<Run>>,
current_source: Option<SourceLocation>,
paragraph_source: Option<SourceLocation>,
@@ -461,6 +463,8 @@ impl Parser {
box_width: None,
inline_boxes: Vec::new(),
minipage: None,
footnote_capture: None,
footnote_counter: 0,
centered: None,
current_source: None,
paragraph_source: None,
@@ -613,6 +617,7 @@ impl Parser {
&& self.centered.is_none()
&& self.inline_boxes.is_empty()
&& self.minipage.is_none()
&& self.footnote_capture.is_none()
&& !self.bold
&& !self.italic
&& !self.monospace
@@ -699,6 +704,43 @@ impl Parser {
self.document.show_title = true;
self.suppress_indent = true;
}
expanded::FrontendEventKind::FootnoteStart => {
self.footnote_capture = Some(Vec::new());
}
expanded::FrontendEventKind::FootnoteEnd => {
if let Some(mut runs) = self.footnote_capture.take() {
trim_run_end(&mut runs);
self.footnote_counter += 1;
let number = self.footnote_counter;
self.document
.footnotes
.push(crate::document::Footnote { number, runs });
let bold = self.bold;
let italic = self.italic;
let monospace = self.monospace;
let sans = self.sans;
let small_caps = self.small_caps;
let marker = Run {
text: superscript_number(number),
math: None,
kern: None,
glue: None,
penalty: None,
rule: None,
box_run: None,
y_offset: None,
footnote_number: Some(number),
unresolved_reference: None,
bold,
italic,
monospace,
sans,
small_caps,
paragraph_indent: false,
};
self.target_runs().push(marker);
}
}
expanded::FrontendEventKind::InlineBoxStart {
width,
align,
@@ -741,6 +783,8 @@ impl Parser {
small_caps: false,
y_offset: None,
footnote_number: None,
};
self.target_runs().push(run);
}
@@ -1019,6 +1063,8 @@ impl Parser {
small_caps: false,
y_offset: None,
footnote_number: None,
});
}
@@ -1159,6 +1205,8 @@ impl Parser {
small_caps: false,
y_offset: None,
footnote_number: None,
});
}
@@ -1194,6 +1242,8 @@ impl Parser {
small_caps: false,
y_offset: None,
footnote_number: None,
});
}
@@ -1234,6 +1284,8 @@ impl Parser {
small_caps: false,
y_offset: None,
footnote_number: None,
});
}
@@ -1284,10 +1336,22 @@ impl Parser {
small_caps: false,
y_offset: None,
footnote_number: None,
});
}
fn target_runs(&mut self) -> &mut Vec<Run> {
if self.footnote_capture.is_some() {
return self
.footnote_capture
.as_mut()
.expect("footnote capture was checked");
}
self.target_runs_without_footnote()
}
fn target_runs_without_footnote(&mut self) -> &mut Vec<Run> {
let paragraph_target = self.inline_boxes.is_empty()
&& self.minipage.is_none()
&& self.centered.is_none()
@@ -1403,11 +1467,13 @@ impl Parser {
small_caps: false,
y_offset: None,
footnote_number: None,
});
}
fn paragraph_break(&mut self) {
if self.minipage.is_some() {
if self.footnote_capture.is_some() || self.minipage.is_some() {
self.append_text(" ");
} else if !self.lists.is_empty() || self.table.is_some() {
self.append_text(" ");
@@ -1740,6 +1806,7 @@ fn indent_run(indent: common::Scaled) -> Run {
sans: false,
small_caps: false,
y_offset: None,
footnote_number: None,
paragraph_indent: true,
}
}
@@ -1756,6 +1823,26 @@ fn strip_paragraph_indent(runs: &mut Vec<Run>) {
}
}
fn superscript_number(number: u32) -> String {
number
.to_string()
.chars()
.map(|digit| match digit {
'0' => '⁰',
'1' => '¹',
'2' => '²',
'3' => '³',
'4' => '⁴',
'5' => '⁵',
'6' => '⁶',
'7' => '⁷',
'8' => '⁸',
'9' => '⁹',
other => other,
})
.collect()
}
fn append_run(
runs: &mut Vec<Run>,
mut text: String,
@@ -1808,6 +1895,8 @@ fn append_run(
paragraph_indent: false,
y_offset: None,
footnote_number: None,
});
}