Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Footnotes: notes stack baseline to baseline (#582)

aiongg152086b3ea1e
diff --git a/crates/compose/src/notes.rs b/crates/compose/src/notes.rs
index 32b2bbec2..e2614adab 100644
--- a/crates/compose/src/notes.rs
+++ b/crates/compose/src/notes.rs
@@ -5,6 +5,11 @@
//! the first footnote and the space between footnotes). A line that would collide moves to the
//! next column with its footnotes. After the story is laid out the footnotes of each column are
//! stacked against the column bottom, with the separator rule above the first one.
+//!
+//! The footnotes of a column read as one text: the first footnote's first baseline sits at the
+//! First Baseline offset below the top of the footnote area, and each later footnote's first
+//! baseline sits its first line's leading (plus Space Between Footnotes) below the previous
+//! footnote's last baseline. Only the last footnote's descent counts toward the area's height.
use std::collections::HashMap;
use std::ops::Range;
@@ -22,10 +27,38 @@ pub(crate) struct Placed {
k: usize,
fi: usize,
col: usize,
- h: f64,
+ m: NoteMetrics,
text: Arc<ComposedStory>,
}
+/// Vertical metrics of a composed footnote, relative to the top of its composed text.
+#[derive(Clone, Copy, Debug, Default)]
+pub(crate) struct NoteMetrics {
+ /// First and last baselines (the first one includes the First Baseline offset).
+ first: f64,
+ last: f64,
+ /// Leading of the first line: its baseline's distance below the previous footnote's last.
+ lead: f64,
+ /// Descent of the last line.
+ descent: f64,
+}
+
+/// Height of footnotes stacked baseline to baseline, from the top of the footnote area to the
+/// last footnote's descent.
+fn stack_height<'a>(notes: impl IntoIterator<Item = &'a NoteMetrics>, space_between: f64) -> f64 {
+ let mut h: Option<f64> = None;
+ let mut descent = 0.0;
+ for m in notes {
+ let last = match h {
+ None => m.last,
+ Some(prev) => prev + space_between + m.lead + (m.last - m.first),
+ };
+ h = Some(last);
+ descent = m.descent;
+ }
+ h.map_or(0.0, |h| h + descent)
+}
+
pub(crate) struct Notes {
/// Byte offsets of the reference characters (index = footnote index).
refs: Vec<usize>,
@@ -36,7 +69,7 @@ pub(crate) struct Notes {
pub key: Option<usize>,
pub placed: Vec<Placed>,
/// Composed footnote text by (index, label, width).
- cache: HashMap<(usize, String, u64), (Arc<ComposedStory>, f64)>,
+ cache: HashMap<(usize, String, u64), (Arc<ComposedStory>, NoteMetrics)>,
notes: Vec<Arc<designcraft_doc::Footnote>>,
}
@@ -88,7 +121,7 @@ impl Notes {
v
}
- fn compose(&mut self, doc: &Document, k: usize, w: f64, f: &FrameSpec, opts: &ComposeOptions) -> (Arc<ComposedStory>, f64) {
+ fn compose(&mut self, doc: &Document, k: usize, w: f64, f: &FrameSpec, opts: &ComposeOptions) -> (Arc<ComposedStory>, NoteMetrics) {
let o = &doc.footnote_options;
let label = format!("{}{}", o.label(self.numbers[k], true), o.separator);
let key = (k, label.clone(), w.to_bits());
@@ -115,8 +148,12 @@ impl Notes {
xrefs: opts.xrefs.clone(),
};
let cs = crate::compose(doc, &self.notes[k].text, std::slice::from_ref(&spec), &nopts);
- let h = cs.frames.first().and_then(|ft| ft.lines.last()).map_or(0.0, |l| l.baseline + l.descent);
- let v = (Arc::new(cs), h);
+ let lines = cs.frames.first().map(|ft| ft.lines.as_slice()).unwrap_or_default();
+ let m = match (lines.first(), lines.last()) {
+ (Some(a), Some(z)) => NoteMetrics { first: a.baseline, last: z.baseline, lead: a.leading, descent: z.descent },
+ _ => NoteMetrics::default(),
+ };
+ let v = (Arc::new(cs), m);
self.cache.insert(key, v.clone());
v
}
@@ -125,25 +162,20 @@ impl Notes {
#[allow(clippy::too_many_arguments)]
pub fn reserve(&mut self, doc: &Document, fi: usize, col: usize, extra: &[usize], w: f64, f: &FrameSpec, opts: &ComposeOptions) -> f64 {
let o = &doc.footnote_options;
- let mut n = 0usize;
- let mut h = 0.0;
- for p in self.placed.iter().filter(|p| p.fi == fi && p.col == col) {
- n += 1;
- h += p.h;
- }
+ let (space_between, space_before) = (o.space_between, o.space_before);
+ let mut ms: Vec<NoteMetrics> = self.placed.iter().filter(|p| p.fi == fi && p.col == col).map(|p| p.m).collect();
for &k in extra {
- n += 1;
- h += self.compose(doc, k, w, f, opts).1;
+ ms.push(self.compose(doc, k, w, f, opts).1);
}
- if n == 0 { 0.0 } else { h + o.space_between * (n - 1) as f64 + o.space_before }
+ if ms.is_empty() { 0.0 } else { stack_height(&ms, space_between) + space_before }
}
pub fn place(&mut self, doc: &Document, k: usize, fi: usize, col: usize, w: f64, f: &FrameSpec, opts: &ComposeOptions) {
if self.placed.iter().any(|p| p.k == k) {
return;
}
- let (text, h) = self.compose(doc, k, w, f, opts);
- self.placed.push(Placed { k, fi, col, h, text });
+ let (text, m) = self.compose(doc, k, w, f, opts);
+ self.placed.push(Placed { k, fi, col, m, text });
}
/// Stack each column's footnotes against its bottom, rule above the first.
@@ -159,7 +191,7 @@ impl Notes {
for (fi, ci) in groups {
let (Some(_), Some(col)) = (frames.get(fi), cols.get(fi).and_then(|c| c.get(ci))) else { continue };
let here: Vec<&Placed> = self.placed.iter().filter(|p| p.fi == fi && p.col == ci).collect();
- let total: f64 = here.iter().map(|p| p.h).sum::<f64>() + o.space_between * (here.len() - 1) as f64;
+ let total = stack_height(here.iter().map(|p| &p.m), o.space_between);
let top = col.y1 - total;
let ft = &mut out.frames[fi];
if o.rule.on && o.rule.weight > 0.0 {
@@ -171,20 +203,29 @@ impl Notes {
tint: o.rule.tint,
});
}
- let mut y = top;
+ // Previous footnote's last baseline and bottom.
+ let mut prev: Option<(f64, f64)> = None;
for p in here {
- let note = &story.notes[p.k];
+ let Some(note) = story.notes.get(p.k) else { continue };
+ let (y, y0) = match prev {
+ None => (top, top),
+ Some((last, bottom)) => {
+ let first = last + o.space_between + p.m.lead;
+ (first - p.m.first, (bottom + o.space_between).min(first))
+ }
+ };
+ let y1 = y + p.m.last + p.m.descent;
ft.notes.push(PlacedNote {
id: note.id,
index: p.k,
column: ci as u32,
label: o.label(self.numbers[p.k], true),
origin: Point::new(col.x0, y),
- rect: Rect::new(col.x0, y, col.x1, y + p.h),
+ rect: Rect::new(col.x0, y0, col.x1, y1),
text: p.text.clone(),
source: Arc::from(note.text.text.as_str()),
});
- y += p.h + o.space_between;
+ prev = Some((y + p.m.last, y1));
}
}
}
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index f5d0113c1..67f3592d1 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -832,6 +832,52 @@ fn footnotes_sit_at_the_column_bottom_and_push_text() {
assert!(note_caret(&cs, n.id, 0).is_some());
}
+#[test]
+fn footnotes_stack_baseline_to_baseline() {
+ // Single-line footnotes read as one text: consecutive baselines sit one leading (plus
+ // Space Between Footnotes) apart, and only the last footnote's descent adds to the area.
+ let text = format!("{LOREM} {LOREM} {LOREM} {LOREM}");
+ for space_between in [0.0, 3.0] {
+ let (mut d, sid, fid) = doc_with(&text, Rect::new(36.0, 36.0, 300.0, 236.0), ParaAttrs::default());
+ d.footnote_options.space_between = space_between;
+ d.footnote_options.space_before = 4.0;
+ {
+ let st = d.story_mut(sid).unwrap();
+ for at in [60, 40, 20, 10] {
+ st.insert_note(at, "Note.", ParaFormat::default());
+ }
+ st.check().unwrap();
+ }
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let ft = cs.frame(fid).unwrap();
+ assert_eq!(ft.notes.len(), 4);
+ let first_line = |n: &PlacedNote| {
+ let lines = &n.text.frames[0].lines;
+ assert_eq!(lines.len(), 1);
+ lines[0].clone()
+ };
+ let baselines: Vec<f64> = ft.notes.iter().map(|n| n.origin.y + first_line(n).baseline).collect();
+ let step = first_line(&ft.notes[1]).leading + space_between;
+ for w in baselines.windows(2) {
+ assert!((w[1] - w[0] - step).abs() < 1e-6, "baselines {baselines:?}, step {step}");
+ }
+ for w in ft.notes.windows(2) {
+ assert!(w[0].rect.y1 <= w[1].rect.y0 + 1e-6, "rects stack in order");
+ }
+ // Area: first-baseline offset + (n - 1) steps + the last descent, ending at the column bottom.
+ let col = ft.columns[0];
+ let top = ft.notes[0].rect.y0;
+ let (l0, l3) = (first_line(&ft.notes[0]), first_line(&ft.notes[3]));
+ assert!((col.y1 - top - (l0.baseline + 3.0 * step + l3.descent)).abs() < 1e-6);
+ assert!((ft.notes[3].rect.y1 - col.y1).abs() < 1e-6);
+ // The body text fills the room left above the footnotes and their space before.
+ let room = top - d.footnote_options.space_before;
+ let last = ft.lines.last().unwrap();
+ assert!(last.baseline + last.descent <= room + 0.01, "text clears the footnotes");
+ assert!(last.baseline + last.leading + last.descent > room, "one more line wouldn't fit");
+ }
+}
+
#[test]
fn footnote_first_line_wraps_at_the_column_edge_after_its_number() {
// InDesign sets the footnote number and separator as part of the first line: that line