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

+1 -2
View File
@@ -371,8 +371,7 @@ const PRELUDE: &str = r#"
\def\fnsymbol#1{*}
\def\thefootnote{*}
\def\footnoterule{}
\def\footnote#1{}
\def\thanks#1{}
\def\thanks#1{\footnote{#1}}
\def\@title{}
\def\@author{}
\def\@date{}
+2
View File
@@ -2570,6 +2570,8 @@ pub(super) fn render_pdf(
small_caps: false,
y_offset: None,
footnote_number: None,
}];
document.push(paragraph(
&heading,
+266
View File
@@ -1075,6 +1075,22 @@ pub(super) fn build_page_break_plan(
paragraph_cache,
)
};
let reserve = footnote_reserve(
model,
block_index,
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
paragraph_cache,
);
if reserve > 0 {
nodes.push(VerticalNode::Kern(reserve));
}
nodes.push(VerticalNode::fixed_glue(
(normal_line_height as f64 * 0.35).round() as i64,
));
@@ -1540,6 +1556,96 @@ pub(super) fn build_page_break_plan(
}
#[allow(clippy::too_many_arguments)]
fn footnote_marker(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 footnote_numbers_for_block(model: &DocumentModel, block_index: usize) -> Vec<u32> {
let Some(Block::Paragraph(runs)) = model.blocks.get(block_index) else {
return Vec::new();
};
let mut numbers = Vec::new();
for run in runs {
if let Some(number) = run.footnote_number
&& !numbers.contains(&number)
{
numbers.push(number);
}
}
numbers
}
fn footnote_note_runs(model: &DocumentModel, number: u32) -> Option<Vec<Run>> {
let note = model.footnotes.iter().find(|note| note.number == number)?;
let mut runs = vec![Run::plain(format!("{} ", footnote_marker(number)))];
runs.extend(note.runs.iter().cloned());
Some(runs)
}
#[allow(clippy::too_many_arguments)]
fn footnote_reserve(
model: &DocumentModel,
block_index: usize,
content_width_mm: f64,
metrics: &BoxFontMetrics,
font_cache: &fonts::FontCache,
cjk_family: Option<FontFamily<fonts::Font>>,
mono_family: Option<FontFamily<fonts::Font>>,
sans_family: Option<FontFamily<fonts::Font>>,
math_metrics: &math_font::MathFontMetrics,
math_layout: &MathLayoutEnvironment,
cache: &mut ParagraphLayoutCache,
) -> i64 {
let numbers = footnote_numbers_for_block(model, block_index);
if numbers.is_empty() {
return 0;
}
let width_mm = (content_width_mm - 8.0).max(20.0);
let mut height = 0_i64;
for number in numbers {
let Some(runs) = footnote_note_runs(model, number) else {
continue;
};
for line in measure_positioned_lines(
&runs,
9,
width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
) {
height = height.saturating_add(line.height);
}
}
if height == 0 {
return 0;
}
height
.saturating_add(i64::from(scaled_points(2.0).0))
.saturating_add(i64::from(scaled_points(1.0).0))
}
fn measure_positioned_lines(
runs: &[Run],
font_size: u8,
@@ -1677,6 +1783,133 @@ fn push_vertical_line(
nodes.push(VerticalNode::Penalty(penalty));
}
#[allow(clippy::too_many_arguments)]
fn append_page_footnotes(
model: &DocumentModel,
pages: &mut [PositionedPage],
block_end_pages: &HashMap<usize, usize>,
page_goal: i64,
content_width_mm: f64,
metrics: &BoxFontMetrics,
font_cache: &fonts::FontCache,
cjk_family: Option<FontFamily<fonts::Font>>,
mono_family: Option<FontFamily<fonts::Font>>,
sans_family: Option<FontFamily<fonts::Font>>,
math_metrics: &math_font::MathFontMetrics,
math_layout: &MathLayoutEnvironment,
cache: &mut ParagraphLayoutCache,
) {
let mut referenced = Vec::new();
for block in block_end_pages.keys() {
for number in footnote_numbers_for_block(model, *block) {
if !referenced.contains(&number) {
referenced.push(number);
}
}
}
let title_numbers: Vec<u32> = model
.footnotes
.iter()
.map(|note| note.number)
.filter(|number| !referenced.contains(number))
.collect();
let mut page_blocks: HashMap<usize, Vec<usize>> = HashMap::new();
for (block, page) in block_end_pages {
page_blocks.entry(*page).or_default().push(*block);
}
if !title_numbers.is_empty() {
page_blocks.entry(0).or_default().push(usize::MAX);
}
for blocks in page_blocks.values_mut() {
blocks.sort_unstable();
}
let width_mm = (content_width_mm - 8.0).max(20.0);
let separator = i64::from(scaled_points(0.4).0);
for (page_index, blocks) in page_blocks {
let Some(page) = pages.get_mut(page_index) else {
continue;
};
let mut note_lines = Vec::new();
let mut seen = Vec::new();
for block in blocks {
let numbers = if block == usize::MAX {
title_numbers.clone()
} else {
footnote_numbers_for_block(model, block)
};
for number in numbers {
if seen.contains(&number) {
continue;
}
seen.push(number);
let Some(runs) = footnote_note_runs(model, number) else {
continue;
};
note_lines.extend(measure_positioned_lines(
&runs,
9,
width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
));
}
}
if note_lines.is_empty() {
continue;
}
let total: i64 = note_lines.iter().map(|line| line.height).sum();
let separator_gap = i64::from(scaled_points(2.0).0);
let mut y = page_goal
.saturating_sub(total)
.saturating_sub(separator_gap);
let separator_width = millimeters_to_scaled(content_width_mm * 0.35);
let rule_line = text_layout::LineBox {
width: common::Scaled(separator_width as i32),
items: vec![text_layout::LineItem::Rule {
width: common::Scaled(separator_width as i32),
raise: common::Scaled::ZERO,
height: common::Scaled(separator as i32),
y_offset: common::Scaled::ZERO,
}],
pre_display_size: None,
};
page.items.push(PositionedPageItem::Line(PositionedLine {
anchor: PageAnchor {
block: usize::MAX,
line: 0,
},
content: PositionedLineContent::Text {
line: rule_line,
font_size: 9,
},
y,
x: 0,
centered: false,
}));
y = y.saturating_add(separator_gap);
for line in note_lines {
let height = line.height;
page.items.push(PositionedPageItem::Line(PositionedLine {
anchor: PageAnchor {
block: usize::MAX,
line: line.line_index,
},
content: line.content,
y,
x: millimeters_to_scaled(8.0),
centered: false,
}));
y = y.saturating_add(height);
}
}
}
pub(super) fn build_positioned_pages(
model: &DocumentModel,
source_dir: &Path,
@@ -1700,6 +1933,7 @@ pub(super) fn build_positioned_pages(
let heading_gap = (normal_line_height as f64 * 0.65).round() as i64;
let page_goal = scaled_points(content_height_mm * PDF_POINTS_PER_INCH / 25.4).0 as i64;
let mut pages = vec![PositionedPage::default()];
let mut block_end_pages: HashMap<usize, usize> = HashMap::new();
let mut cursor = 0_i64;
let mut has_previous_block = false;
let mut pending_gap = 0_i64;
@@ -1837,6 +2071,23 @@ pub(super) fn build_positioned_pages(
}));
cursor = cursor.saturating_add(line_height);
}
let reserve = footnote_reserve(
model,
block_index,
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
);
if reserve > 0 {
cursor = cursor.saturating_add(reserve);
block_end_pages.insert(block_index, pages.len().saturating_sub(1));
}
pending_gap = paragraph_gap;
has_previous_block = true;
}
@@ -2506,6 +2757,21 @@ pub(super) fn build_positioned_pages(
}
}
append_page_footnotes(
model,
&mut pages,
&block_end_pages,
page_goal,
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
);
pages.retain(|page| !page.items.is_empty());
Some(pages)
}
+37
View File
@@ -3022,6 +3022,35 @@ fn raisebox_primitive(
Ok(())
}
fn footnote_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let content = read_unexpanded_argument(input, token, "expects footnote content")?;
let inner = strip_braced_tokens(&content).to_vec();
write_event(input, token, FrontendEventKind::FootnoteStart);
let trace_key = token.trace_key();
let mut expansion = inner;
expansion.push(control_sequence_token(
input,
"texrrafootnoteend",
trace_key,
));
input
.as_mut()
.expansions_mut()
.extend(expansion.into_iter().rev());
Ok(())
}
fn footnote_end_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
write_event(input, token, FrontendEventKind::FootnoteEnd);
Ok(())
}
fn inline_box_end_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -6380,6 +6409,14 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"texrrainlineboxend",
texlang::command::BuiltIn::new_execution(inline_box_end_primitive),
);
built_ins.insert(
"footnote",
texlang::command::BuiltIn::new_execution(footnote_primitive),
);
built_ins.insert(
"texrrafootnoteend",
texlang::command::BuiltIn::new_execution(footnote_end_primitive),
);
built_ins.insert(
"fbox",
texlang::command::BuiltIn::new_execution(fbox_primitive),