Align halign cells and respect topskip on float pages
This commit is contained in:
1 parent
bf24a7c1da
commit
753aac632d
2 files changed
+106
-5
No files matched your search
@@ -59,6 +59,51 @@ pub enum VerticalItem {
|
|||||||
Halign(Halign),
|
Halign(Halign),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Halign {
|
||||||
|
/// The horizontal rectangle occupied by a cell's text: `(x, width)`.
|
||||||
|
///
|
||||||
|
/// Spanned cells include the intercolumn gaps they cover. The x offset is
|
||||||
|
/// the aligned start inside that rectangle.
|
||||||
|
pub fn cell_box(&self, row_index: usize, cell_index: usize) -> Option<(i64, i64)> {
|
||||||
|
let row = self.rows.get(row_index)?;
|
||||||
|
let cell = row.cells.get(cell_index)?;
|
||||||
|
let mut column = 0_usize;
|
||||||
|
for previous in &row.cells[..cell_index] {
|
||||||
|
column += previous.span.max(1);
|
||||||
|
}
|
||||||
|
let span = cell
|
||||||
|
.span
|
||||||
|
.max(1)
|
||||||
|
.min(self.column_widths.len().saturating_sub(column));
|
||||||
|
if span == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let intercolumn = if self.column_widths.len() > 1 {
|
||||||
|
(self.width - self.column_widths.iter().copied().sum::<i64>())
|
||||||
|
/ (self.column_widths.len() as i64 - 1)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
let gaps = intercolumn * (span.saturating_sub(1) as i64);
|
||||||
|
let width = self.column_widths[column..column + span]
|
||||||
|
.iter()
|
||||||
|
.sum::<i64>()
|
||||||
|
+ gaps;
|
||||||
|
let x: i64 = self.column_widths[..column].iter().sum::<i64>() + intercolumn * column as i64;
|
||||||
|
let text_width = cell
|
||||||
|
.text
|
||||||
|
.chars()
|
||||||
|
.map(|character| if character.is_whitespace() { 4 } else { 10 })
|
||||||
|
.sum::<i64>();
|
||||||
|
let offset = match cell.alignment {
|
||||||
|
CellAlignment::Left => 0,
|
||||||
|
CellAlignment::Center => (width - text_width).max(0) / 2,
|
||||||
|
CellAlignment::Right => (width - text_width).max(0),
|
||||||
|
};
|
||||||
|
Some((x + offset, width))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Vertical material reserved at the bottom of a page (footnotes etc.).
|
/// Vertical material reserved at the bottom of a page (footnotes etc.).
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct Insertion {
|
pub struct Insertion {
|
||||||
@@ -424,9 +469,12 @@ impl Engine {
|
|||||||
if float.placement == FloatPlacement::Top {
|
if float.placement == FloatPlacement::Top {
|
||||||
if !current.items.is_empty() || !current.insertions.is_empty() {
|
if !current.items.is_empty() || !current.insertions.is_empty() {
|
||||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||||
used = 0;
|
used = document.top_skip;
|
||||||
insertion_height = 0;
|
insertion_height = 0;
|
||||||
}
|
}
|
||||||
|
if current.items.is_empty() {
|
||||||
|
used = used.max(document.top_skip);
|
||||||
|
}
|
||||||
used += float.height;
|
used += float.height;
|
||||||
current.floats.push(float.clone());
|
current.floats.push(float.clone());
|
||||||
} else if !current.items.is_empty()
|
} else if !current.items.is_empty()
|
||||||
@@ -438,7 +486,7 @@ impl Engine {
|
|||||||
current.floats.push(float.clone());
|
current.floats.push(float.clone());
|
||||||
} else {
|
} else {
|
||||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||||
used = 0;
|
used = document.top_skip;
|
||||||
insertion_height = 0;
|
insertion_height = 0;
|
||||||
current.floats.push(float.clone());
|
current.floats.push(float.clone());
|
||||||
}
|
}
|
||||||
@@ -610,7 +658,7 @@ impl Engine {
|
|||||||
if float.placement == FloatPlacement::Top {
|
if float.placement == FloatPlacement::Top {
|
||||||
if !current.items.is_empty() || !current.insertions.is_empty() {
|
if !current.items.is_empty() || !current.insertions.is_empty() {
|
||||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||||
used = 0;
|
used = document.top_skip;
|
||||||
insertion_height = 0;
|
insertion_height = 0;
|
||||||
}
|
}
|
||||||
used += float.height;
|
used += float.height;
|
||||||
@@ -622,7 +670,7 @@ impl Engine {
|
|||||||
current.floats.push(float.clone());
|
current.floats.push(float.clone());
|
||||||
} else {
|
} else {
|
||||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||||
used = 0;
|
used = document.top_skip;
|
||||||
insertion_height = 0;
|
insertion_height = 0;
|
||||||
current.floats.push(float.clone());
|
current.floats.push(float.clone());
|
||||||
}
|
}
|
||||||
@@ -920,6 +968,57 @@ mod tests {
|
|||||||
assert_eq!(layout.pages()[1].insertions.len(), 0);
|
assert_eq!(layout.pages()[1].insertions.len(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn halign_aligns_cells_inside_their_columns() {
|
||||||
|
let rows = vec![
|
||||||
|
HalignRow {
|
||||||
|
cells: vec![
|
||||||
|
HalignCell {
|
||||||
|
text: "A".to_owned(),
|
||||||
|
span: 1,
|
||||||
|
alignment: CellAlignment::Left,
|
||||||
|
},
|
||||||
|
HalignCell {
|
||||||
|
text: "B".to_owned(),
|
||||||
|
span: 1,
|
||||||
|
alignment: CellAlignment::Left,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
rule: false,
|
||||||
|
},
|
||||||
|
HalignRow {
|
||||||
|
cells: vec![
|
||||||
|
HalignCell {
|
||||||
|
text: "longword".to_owned(),
|
||||||
|
span: 1,
|
||||||
|
alignment: CellAlignment::Left,
|
||||||
|
},
|
||||||
|
HalignCell {
|
||||||
|
text: "C".to_owned(),
|
||||||
|
span: 1,
|
||||||
|
alignment: CellAlignment::Right,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
rule: true,
|
||||||
|
},
|
||||||
|
HalignRow {
|
||||||
|
cells: vec![HalignCell {
|
||||||
|
text: "TOTAL".to_owned(),
|
||||||
|
span: 2,
|
||||||
|
alignment: CellAlignment::Center,
|
||||||
|
}],
|
||||||
|
rule: false,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let unit = Unit::halign(0, rows, 2, 20);
|
||||||
|
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||||
|
panic!("expected a halign item");
|
||||||
|
};
|
||||||
|
assert_eq!(halign.cell_box(0, 0), Some((0, 80)));
|
||||||
|
assert_eq!(halign.cell_box(1, 1), Some((100, 10)));
|
||||||
|
assert_eq!(halign.cell_box(2, 0), Some((30, 110)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn halign_computes_natural_column_widths() {
|
fn halign_computes_natural_column_widths() {
|
||||||
let rows = vec![
|
let rows = vec![
|
||||||
|
|||||||
+3
-1
@@ -102,7 +102,9 @@ its tolerance but still emits the moderately overfull 5-word line. Reproducing
|
|||||||
this needs a two-pass tolerance algorithm with an emergency overfull pass, not
|
this needs a two-pass tolerance algorithm with an emergency overfull pass, not
|
||||||
a single cost function.
|
a single cost function.
|
||||||
- `Unit::halign` lays out natural column widths, multi-column spans and rule
|
- `Unit::halign` lays out natural column widths, multi-column spans and rule
|
||||||
rows as a `VerticalItem::Halign`; it is not yet painted by the v2 PDF path.
|
rows as a `VerticalItem::Halign`; `Halign::cell_box` returns each cell's
|
||||||
|
aligned rectangle for a future painter. It is not yet painted by v2.
|
||||||
|
- Pages that start with a float also honour `Document::top_skip`.
|
||||||
- `Unit::vertical` and `Unit::insertion` build arbitrary vertical material and
|
- `Unit::vertical` and `Unit::insertion` build arbitrary vertical material and
|
||||||
reserved-height page insertions; insertion height is deducted from the
|
reserved-height page insertions; insertion height is deducted from the
|
||||||
usable page goal and the material is attached to `Page::insertions`.
|
usable page goal and the material is attached to `Page::insertions`.
|
||||||
|
|||||||
Reference in new issue
Block a user