Skip to content

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

Merge PR #918

Brandon Thomas2aeeea345a8e
diff --cc crates/compose/src/lib.rs
index cab17ce87,320519e60..674259efe
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@@ -1056,11 -1021,10 +1056,11 @@@ fn compose_with_db(doc: &Document, stor
// Footnotes referenced on this line need room at the bottom of the column too
// (a line at the top of a column is set anyway).
let line_notes = if notes.active() { notes.refs_in(line_glyphs) } else { Vec::new() };
- let col_w = cols[cur.fi][cur.col.min(cols[cur.fi].len() - 1)].width();
+ let col_w = notes.area(&cols[cur.fi], cur.col).map_or(0.0, |r| r.width());
let reserve = notes.reserve(doc, cur.fi, cur.col, &line_notes, col_w, f, opts);
let reserve = if cur.last_baseline.is_none() { notes.reserve(doc, cur.fi, cur.col, &[], col_w, f, opts) } else { reserve };
- let fits = baseline + desc <= col.y1 - reserve + 0.01 && !capped;
+ // As in InDesign, a line is set when its baseline fits: its descenders may hang below the frame.
+ let fits = baseline <= col.y1 - reserve + 0.01 && !capped;
if !fits {
if cur.can_next_sub() {
g0 = s;
diff --cc crates/compose/src/notes.rs
index ad48f62a2,19138a460..1425ced11
--- a/crates/compose/src/notes.rs
+++ b/crates/compose/src/notes.rs
@@@ -6,10 -6,8 +6,13 @@@
//! 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.
++//!
+ //! With Span Footnotes Across Columns the notes of a frame share one area across all its columns,
+ //! composed at the frame's text width: every column of the frame makes room for it.
use std::collections::HashMap;
use std::ops::Range;
@@@ -69,8 -39,13 +72,13 @@@ 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>>,
+ /// Footnotes span the columns of their frame.
+ span: bool,
+ /// Room a frame's earlier columns had to leave for spanning notes placed in a later column;
+ /// kept across re-lays so the columns before the reference make room on the next pass.
+ floor: HashMap<usize, f64>,
}
impl Notes {
@@@ -164,12 -154,33 +193,28 @@@
#[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| self.same_area(p, fi, 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();
++ let mut ms: Vec<NoteMetrics> = self.placed.iter().filter(|p| self.same_area(p, fi, 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 ms.is_empty() { 0.0 } else { stack_height(&ms, space_between) + space_before }
- let r = if n == 0 { 0.0 } else { h + o.space_between * (n - 1) as f64 + o.space_before };
++ let r = if ms.is_empty() { 0.0 } else { stack_height(&ms, space_between) + space_before };
+ if self.span { r.max(self.floor.get(&fi).copied().unwrap_or(0.0)) } else { r }
+ }
+
+ /// After spanning notes were placed in column `col` of frame `fi`: whether a line of an earlier
+ /// column now runs into their area. The area is then remembered for the frame, to re-lay it.
+ pub fn earlier_columns_collide(&mut self, lines: &[crate::Line], fi: usize, col: usize, cols: &[Rect], reserve: f64) -> bool {
+ if !self.span || col == 0 || reserve <= self.floor.get(&fi).copied().unwrap_or(0.0) + 0.01 {
+ return false;
+ }
+ let hit = lines
+ .iter()
+ .any(|l| (l.column as usize) < col && cols.get(l.column as usize).is_some_and(|c| l.baseline + l.descent > c.y1 - reserve + 0.01));
+ if hit {
+ self.floor.insert(fi, reserve);
+ }
+ hit
}
pub fn place(&mut self, doc: &Document, k: usize, fi: usize, col: usize, w: f64, f: &FrameSpec, opts: &ComposeOptions) {
@@@ -191,9 -202,9 +236,9 @@@
groups.sort_unstable();
groups.dedup();
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 (Some(_), Some(col)) = (frames.get(fi), cols.get(fi).filter(|c| ci < c.len()).and_then(|c| self.area(c, ci))) else { continue };
+ let here: Vec<&Placed> = self.placed.iter().filter(|p| self.same_area(p, fi, 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 {
@@@ -220,10 -222,10 +265,10 @@@
ft.notes.push(PlacedNote {
id: note.id,
index: p.k,
- column: ci as u32,
+ column: p.col 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()),
});