Page-set footnotes as numbered insertions
This commit is contained in:
1 parent
6176edccc4
commit
d4efee043c
5 files changed
+158
-8
No files matched your search
+138
-4
@@ -12,7 +12,9 @@ use std::sync::{Mutex, OnceLock};
|
||||
use texrra_tex_core::cmr10::Cmr10Metrics;
|
||||
use texrra_tex_core::math::{AlignedRow, MathAtom, parse_math, split_math_lines};
|
||||
use texrra_tex_core::pdf::{PageOrigin, display_lists_to_pdf_with_cm_type1};
|
||||
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit};
|
||||
use texrra_tex_core::{
|
||||
Document, Engine, InsertionClass, ParagraphFont, ParagraphStyle, Unit, VerticalItem,
|
||||
};
|
||||
|
||||
const SCALE: i64 = 65_536;
|
||||
|
||||
@@ -55,7 +57,15 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
|
||||
Some(mut session) => {
|
||||
if session.paragraphs == paragraphs && session.geometry == geometry {
|
||||
(session.engine, Vec::new(), 0)
|
||||
} else if session.geometry == geometry {
|
||||
} else if session.geometry == geometry
|
||||
&& session
|
||||
.paragraphs
|
||||
.iter()
|
||||
.all(|paragraph| paragraph.footnotes.is_empty())
|
||||
&& paragraphs
|
||||
.iter()
|
||||
.all(|paragraph| paragraph.footnotes.is_empty())
|
||||
{
|
||||
let prefix = session
|
||||
.paragraphs
|
||||
.iter()
|
||||
@@ -860,6 +870,8 @@ struct SourceParagraph {
|
||||
heading_starred: bool,
|
||||
/// The section number assigned by the numbering pass.
|
||||
heading_number: Option<String>,
|
||||
/// Footnote bodies extracted from this paragraph, with their numbers.
|
||||
footnotes: Vec<(u32, String)>,
|
||||
font_size: Option<i64>,
|
||||
}
|
||||
|
||||
@@ -940,6 +952,7 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
});
|
||||
pending = (0, 0, 0);
|
||||
@@ -1085,6 +1098,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: Some((12.0 * SCALE as f64) as i64),
|
||||
};
|
||||
let body = SourceParagraph {
|
||||
@@ -1106,6 +1120,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: Some((9.0 * SCALE as f64) as i64),
|
||||
};
|
||||
if body.text.is_empty() {
|
||||
@@ -1175,6 +1190,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
}
|
||||
})
|
||||
@@ -1208,6 +1224,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
}];
|
||||
}
|
||||
@@ -1250,6 +1267,7 @@ fn parse_blocks_raw(raw: &str) -> Vec<SourceParagraph> {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
}
|
||||
})
|
||||
@@ -1772,6 +1790,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
};
|
||||
}
|
||||
@@ -1797,6 +1816,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
};
|
||||
}
|
||||
if let Some(inner) = raw
|
||||
@@ -1822,6 +1842,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
};
|
||||
}
|
||||
@@ -1848,6 +1869,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
};
|
||||
}
|
||||
@@ -1878,6 +1900,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
};
|
||||
}
|
||||
@@ -1905,6 +1928,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
heading_level: Some(level),
|
||||
heading_starred: starred,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
};
|
||||
}
|
||||
@@ -1936,6 +1960,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
};
|
||||
}
|
||||
@@ -1959,6 +1984,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: None,
|
||||
}
|
||||
}
|
||||
@@ -2057,6 +2083,7 @@ fn title_block(text: String, size: f64, space_before: i64) -> SourceParagraph {
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
footnotes: Vec::new(),
|
||||
font_size: Some((size * SCALE as f64) as i64),
|
||||
}
|
||||
}
|
||||
@@ -2220,12 +2247,39 @@ fn heading_font_size(level: u8, base: i64) -> i64 {
|
||||
base.saturating_mul(percent) / 100
|
||||
}
|
||||
|
||||
/// Pull `\footnote{...}` bodies out of a paragraph, leaving a numbered
|
||||
/// reference in the running text. Footnote numbers run across the document.
|
||||
fn number_footnotes(paragraphs: &mut [SourceParagraph]) {
|
||||
let mut number = 0u32;
|
||||
for paragraph in paragraphs {
|
||||
let source = paragraph.text.clone();
|
||||
let mut text = String::with_capacity(source.len());
|
||||
let mut rest = source.as_str();
|
||||
while let Some(index) = rest.find("\\footnote") {
|
||||
text.push_str(&rest[..index]);
|
||||
let after = rest[index + "\\footnote".len()..].trim_start();
|
||||
let Some((body, consumed)) = braced_group(after) else {
|
||||
text.push_str(&rest[index..]);
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
number += 1;
|
||||
let _ = write!(text, " ({number})");
|
||||
paragraph.footnotes.push((number, body.trim().to_string()));
|
||||
rest = &after[consumed..];
|
||||
}
|
||||
text.push_str(rest);
|
||||
paragraph.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
|
||||
let mut paragraphs = expand_maketitle(paragraphs, geometry);
|
||||
let section_labels = number_sections(&mut paragraphs);
|
||||
resolve_section_refs(&mut paragraphs, §ion_labels);
|
||||
number_footnotes(&mut paragraphs);
|
||||
let line_width = geometry.line_width.unwrap_or(480 * SCALE);
|
||||
let units = paragraphs
|
||||
let base_units: Vec<Unit> = paragraphs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, paragraph)| {
|
||||
@@ -2371,7 +2425,56 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
unit
|
||||
})
|
||||
.collect();
|
||||
// Footnotes follow their anchor paragraph as insertion units, so the page
|
||||
// builder reserves room for them on the page that mentions them.
|
||||
let mut units: Vec<Unit> = Vec::with_capacity(base_units.len());
|
||||
let mut unit_for_paragraph = Vec::with_capacity(paragraphs.len());
|
||||
let mut footnote_count = 0usize;
|
||||
for (paragraph, unit) in paragraphs.iter().zip(base_units) {
|
||||
unit_for_paragraph.push(units.len());
|
||||
units.push(unit);
|
||||
for (number, body) in ¶graph.footnotes {
|
||||
footnote_count += 1;
|
||||
let style = ParagraphStyle {
|
||||
line_width,
|
||||
font_size: 9 * SCALE,
|
||||
par_indent: 0,
|
||||
baselineskip: 11 * SCALE,
|
||||
line_height: 11 * SCALE,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let id = 1_000_000 + *number as u64;
|
||||
let footnote = match inline_runs(body, ParagraphFont::Roman) {
|
||||
Some(runs) => Unit::paragraph_with_style_and_runs(id, body.clone(), style, runs),
|
||||
None => Unit::paragraph_with_style_and_face(
|
||||
id,
|
||||
body.clone(),
|
||||
style,
|
||||
ParagraphFont::Roman,
|
||||
),
|
||||
};
|
||||
let height = footnote
|
||||
.items
|
||||
.iter()
|
||||
.map(|item| match item {
|
||||
VerticalItem::Paragraph(paragraph) => paragraph.height + paragraph.depth,
|
||||
_ => 0,
|
||||
})
|
||||
.sum::<i64>();
|
||||
units.push(Unit::insertion(id, 0, height, footnote.items));
|
||||
}
|
||||
}
|
||||
let mut document = Document::new(700 * SCALE, units);
|
||||
if footnote_count > 0 {
|
||||
document.insertion_classes.push(InsertionClass {
|
||||
number: 0,
|
||||
fraction_percent: 100,
|
||||
max_height: None,
|
||||
skip: Some(12 * SCALE),
|
||||
multiplier: 1000,
|
||||
});
|
||||
document.insertion_skip = 12 * SCALE;
|
||||
}
|
||||
// LaTeX sets \maxdepth=.5\topskip; sources may override it later.
|
||||
document.max_depth = 5 * SCALE;
|
||||
document.text_width = line_width;
|
||||
@@ -2379,7 +2482,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, paragraph)| paragraph.page_break)
|
||||
.map(|(index, _)| index)
|
||||
.map(|(index, _)| unit_for_paragraph[index])
|
||||
.collect();
|
||||
document
|
||||
}
|
||||
@@ -2600,6 +2703,37 @@ mod tests {
|
||||
mod source_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn footnotes_become_page_insertions() {
|
||||
let blocks = paragraphs("Body\\footnote{Note} tail").unwrap();
|
||||
let document = build_document(&blocks, &PageGeometry::default());
|
||||
assert_eq!(document.units.len(), 2);
|
||||
assert_eq!(document.units[0].source, "Body (1) tail");
|
||||
let Some(VerticalItem::Insertion(insertion)) = document.units[1].items.first() else {
|
||||
panic!("expected an insertion unit");
|
||||
};
|
||||
assert_eq!(insertion.class, 0);
|
||||
assert!(insertion.height > 0);
|
||||
assert_eq!(document.insertion_classes.len(), 1);
|
||||
let engine = Engine::layout_paged(&document).expect("paged layout");
|
||||
assert_eq!(engine.pages().len(), 1);
|
||||
assert_eq!(engine.pages()[0].insertions.len(), 1);
|
||||
let list =
|
||||
engine.pages()[0].display_list_with_layout(document.page_goal, document.text_float_sep);
|
||||
assert!(list.items.iter().any(|item| {
|
||||
matches!(item, texrra_tex_core::DisplayItem::Text { text, .. } if text == "N")
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn footnotes_number_across_paragraphs() {
|
||||
let blocks = paragraphs("One\\footnote{A}\n\nTwo\\footnote{B}").unwrap();
|
||||
let document = build_document(&blocks, &PageGeometry::default());
|
||||
assert_eq!(document.units[0].source, "One (1)");
|
||||
assert_eq!(document.units[2].source, "Two (2)");
|
||||
assert_eq!(document.units.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_hang_registers_from_the_preamble() {
|
||||
let (body, geometry) = geometry_and_body(
|
||||
|
||||
Reference in new issue
Block a user