Repeat longtable heads on continuation pages
This commit is contained in:
1 parent
8c3e410ffc
commit
20011eadd3
4 files changed
+251
-5
No files matched your search
@@ -87,6 +87,15 @@ pub struct Halign {
|
||||
/// Extra vertical glue after each row (TeX `\noalign{\vskip ...}`).
|
||||
#[serde(default)]
|
||||
pub row_gaps: Vec<i64>,
|
||||
/// Leading rows repeated on the first page only (longtable `\endfirsthead`).
|
||||
#[serde(default)]
|
||||
pub first_head_len: usize,
|
||||
/// Rows after the first head repeated on continuation pages (`\endhead`).
|
||||
#[serde(default)]
|
||||
pub head_len: usize,
|
||||
/// Trailing rows painted only on the last page (`\endfoot`/`\endlastfoot`).
|
||||
#[serde(default)]
|
||||
pub foot_len: usize,
|
||||
pub width: i64,
|
||||
pub height: i64,
|
||||
pub line_height: i64,
|
||||
@@ -121,7 +130,137 @@ impl Halign {
|
||||
///
|
||||
/// Column widths are preserved so the fragments still line up when the
|
||||
/// page builder places them on consecutive pages.
|
||||
/// Split the table into page-sized row chunks, repeating longtable heads
|
||||
/// and painting the foot block only on the last chunk.
|
||||
pub fn split_for_height(&self, max_height: i64) -> Vec<Halign> {
|
||||
if self.first_head_len > 0 || self.head_len > 0 || self.foot_len > 0 {
|
||||
self.split_for_height_with_heads(max_height)
|
||||
} else {
|
||||
self.split_for_height_without_heads(max_height)
|
||||
}
|
||||
}
|
||||
|
||||
fn split_for_height_with_heads(&self, max_height: i64) -> Vec<Halign> {
|
||||
let max_height = max_height.max(self.line_height);
|
||||
let first_len = self.first_head_len.min(self.rows.len());
|
||||
let head_len = self.head_len.min(self.rows.len().saturating_sub(first_len));
|
||||
let body_start = first_len + head_len;
|
||||
let foot_len = self
|
||||
.foot_len
|
||||
.min(self.rows.len().saturating_sub(body_start));
|
||||
let body_end = self.rows.len() - foot_len;
|
||||
let first_prefix: Vec<usize> = (0..first_len).collect();
|
||||
let continuation_prefix: Vec<usize> = if first_len == 0 {
|
||||
(0..head_len).collect()
|
||||
} else {
|
||||
(first_len..body_start).collect()
|
||||
};
|
||||
let height_of = |rows: &[usize]| -> i64 {
|
||||
rows.iter()
|
||||
.map(|&index| self.row_height(index) + self.row_gap(index))
|
||||
.sum()
|
||||
};
|
||||
let foot_rows: Vec<usize> = (body_end..self.rows.len()).collect();
|
||||
let foot_height = height_of(&foot_rows);
|
||||
let mut chunks = Vec::new();
|
||||
let mut cursor = body_start;
|
||||
let mut first_chunk = true;
|
||||
loop {
|
||||
let prefix = if first_chunk {
|
||||
&first_prefix
|
||||
} else {
|
||||
&continuation_prefix
|
||||
};
|
||||
let mut body_rows: Vec<usize> = Vec::new();
|
||||
let mut height = height_of(prefix);
|
||||
while cursor < body_end {
|
||||
let row_height = self.row_height(cursor) + self.row_gap(cursor);
|
||||
if !body_rows.is_empty() && height + row_height > max_height {
|
||||
break;
|
||||
}
|
||||
body_rows.push(cursor);
|
||||
height += row_height;
|
||||
cursor += 1;
|
||||
}
|
||||
let last = cursor >= body_end;
|
||||
let mut chunk_rows: Vec<usize> = Vec::new();
|
||||
chunk_rows.extend(prefix.iter().copied());
|
||||
chunk_rows.extend(body_rows.iter().copied());
|
||||
if last && foot_len > 0 {
|
||||
chunk_rows.extend(foot_rows.iter().copied());
|
||||
height += foot_height;
|
||||
}
|
||||
let mut rules = Vec::new();
|
||||
if let (Some(first), Some(last)) = (prefix.first(), prefix.last()) {
|
||||
for rule in self
|
||||
.rules
|
||||
.iter()
|
||||
.filter(|rule| rule.row >= *first && rule.row <= last + 1)
|
||||
{
|
||||
rules.push(HalignRule {
|
||||
row: rule.row - first,
|
||||
..*rule
|
||||
});
|
||||
}
|
||||
}
|
||||
if let (Some(first), Some(last)) = (body_rows.first(), body_rows.last()) {
|
||||
let offset = prefix.len();
|
||||
let lower = if prefix.is_empty() { *first } else { first + 1 };
|
||||
for rule in self
|
||||
.rules
|
||||
.iter()
|
||||
.filter(|rule| rule.row >= lower && rule.row <= last + 1)
|
||||
{
|
||||
rules.push(HalignRule {
|
||||
row: offset + rule.row - first,
|
||||
..*rule
|
||||
});
|
||||
}
|
||||
}
|
||||
if last {
|
||||
if let (Some(first), Some(last)) = (foot_rows.first(), foot_rows.last()) {
|
||||
let offset = prefix.len() + body_rows.len();
|
||||
for rule in self
|
||||
.rules
|
||||
.iter()
|
||||
.filter(|rule| rule.row > *first && rule.row <= last + 1)
|
||||
{
|
||||
rules.push(HalignRule {
|
||||
row: offset + rule.row - first,
|
||||
..*rule
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
chunks.push(Self {
|
||||
rows: chunk_rows
|
||||
.iter()
|
||||
.map(|&index| self.rows[index].clone())
|
||||
.collect(),
|
||||
column_widths: self.column_widths.clone(),
|
||||
column_wrap: self.column_wrap.clone(),
|
||||
column_skips: self.column_skips.clone(),
|
||||
rules,
|
||||
row_gaps: chunk_rows
|
||||
.iter()
|
||||
.map(|&index| self.row_gap(index))
|
||||
.collect(),
|
||||
first_head_len: 0,
|
||||
head_len: 0,
|
||||
foot_len: 0,
|
||||
width: self.width,
|
||||
height,
|
||||
line_height: self.line_height,
|
||||
});
|
||||
first_chunk = false;
|
||||
if last {
|
||||
break;
|
||||
}
|
||||
}
|
||||
chunks
|
||||
}
|
||||
|
||||
fn split_for_height_without_heads(&self, max_height: i64) -> Vec<Halign> {
|
||||
if self.rows.is_empty() {
|
||||
return vec![self.clone()];
|
||||
}
|
||||
@@ -143,6 +282,9 @@ impl Halign {
|
||||
.get(start..index)
|
||||
.map(<[_]>::to_vec)
|
||||
.unwrap_or_default(),
|
||||
first_head_len: 0,
|
||||
head_len: 0,
|
||||
foot_len: 0,
|
||||
width: self.width,
|
||||
height,
|
||||
line_height: self.line_height,
|
||||
@@ -163,6 +305,9 @@ impl Halign {
|
||||
.get(start..)
|
||||
.map(<[_]>::to_vec)
|
||||
.unwrap_or_default(),
|
||||
first_head_len: 0,
|
||||
head_len: 0,
|
||||
foot_len: 0,
|
||||
width: self.width,
|
||||
height,
|
||||
line_height: self.line_height,
|
||||
@@ -807,6 +952,9 @@ impl Unit {
|
||||
column_skips,
|
||||
rules: Vec::new(),
|
||||
row_gaps: Vec::new(),
|
||||
first_head_len: 0,
|
||||
head_len: 0,
|
||||
foot_len: 0,
|
||||
width,
|
||||
height: 0,
|
||||
line_height,
|
||||
@@ -867,6 +1015,9 @@ impl Unit {
|
||||
let mut rules: Vec<HalignRule> = Vec::new();
|
||||
let mut row_gaps: Vec<i64> = Vec::new();
|
||||
let mut pending_gap = 0_i64;
|
||||
let mut first_head_end: Option<usize> = None;
|
||||
let mut head_end: Option<usize> = None;
|
||||
let mut body_start: Option<usize> = None;
|
||||
for raw_row in source.split(r"\\") {
|
||||
if raw_row.trim().is_empty() {
|
||||
continue;
|
||||
@@ -886,6 +1037,19 @@ impl Unit {
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(rest) = raw_row.strip_prefix(r"\endfirsthead") {
|
||||
first_head_end = Some(rows.len());
|
||||
raw_row = rest.trim();
|
||||
} else if let Some(rest) = raw_row.strip_prefix(r"\endhead") {
|
||||
head_end = Some(rows.len());
|
||||
raw_row = rest.trim();
|
||||
} else if let Some(rest) = raw_row.strip_prefix(r"\endlastfoot") {
|
||||
body_start = Some(rows.len());
|
||||
raw_row = rest.trim();
|
||||
} else if let Some(rest) = raw_row.strip_prefix(r"\endfoot") {
|
||||
head_end.get_or_insert(rows.len());
|
||||
raw_row = rest.trim();
|
||||
}
|
||||
let mut rule = false;
|
||||
if let Some(rest) = raw_row.strip_prefix(r"\hline") {
|
||||
rule = true;
|
||||
@@ -937,6 +1101,28 @@ impl Unit {
|
||||
pending_gap = 0;
|
||||
rows.push(HalignRow { cells, rule: false });
|
||||
}
|
||||
let first_len = first_head_end.unwrap_or(0);
|
||||
let head_end = head_end.unwrap_or(first_len);
|
||||
let body_start = body_start.unwrap_or(head_end);
|
||||
let head_len = head_end.saturating_sub(first_len);
|
||||
let foot_len = body_start.saturating_sub(head_end);
|
||||
let body_len = rows.len().saturating_sub(body_start);
|
||||
if foot_len > 0 {
|
||||
let mut reordered = Vec::with_capacity(rows.len());
|
||||
reordered.extend_from_slice(&rows[..head_end]);
|
||||
reordered.extend_from_slice(&rows[body_start..]);
|
||||
reordered.extend_from_slice(&rows[head_end..body_start]);
|
||||
rows = reordered;
|
||||
for rule in rules.iter_mut() {
|
||||
if rule.row <= head_end {
|
||||
// Head rows stay at the front.
|
||||
} else if rule.row >= body_start {
|
||||
rule.row -= foot_len;
|
||||
} else {
|
||||
rule.row += body_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut column_skips = tabskips;
|
||||
if at_glue.iter().any(|value| value.is_some()) {
|
||||
let default_gap = intercolumn.saturating_mul(65_536);
|
||||
@@ -955,6 +1141,10 @@ impl Unit {
|
||||
Self::halign_with_layout(id, rows, column_count, intercolumn, wraps, column_skips);
|
||||
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
|
||||
halign.rules = rules;
|
||||
halign.first_head_len = first_len.min(halign.rows.len());
|
||||
halign.head_len = head_len.min(halign.rows.len() - halign.first_head_len);
|
||||
halign.foot_len =
|
||||
foot_len.min(halign.rows.len() - halign.first_head_len - halign.head_len);
|
||||
if !row_gaps.is_empty() {
|
||||
halign.row_gaps = row_gaps;
|
||||
halign.height = (0..halign.rows.len())
|
||||
@@ -7079,6 +7269,62 @@ b & 2 \\
|
||||
assert_eq!(chunks[1].height, halign.row_height(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn longtable_heads_repeat_on_continuation_chunks() {
|
||||
let source = "H1 & H2 \\\\\n\\endfirsthead\nH3 & H4 \\\\\n\\endhead\nF1 & F2 \\\\\n\\endfoot\nL1 & L2 \\\\\n\\endlastfoot\nb1 & x \\\\\nb2 & y \\\\\nb3 & z \\\\\nb4 & w \\\\\n";
|
||||
let unit = Unit::halign_from_source_with_template(1, "ll", source, 0);
|
||||
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||
panic!("expected halign");
|
||||
};
|
||||
assert_eq!(halign.rows.len(), 8);
|
||||
assert_eq!(halign.first_head_len, 1);
|
||||
assert_eq!(halign.head_len, 1);
|
||||
assert_eq!(halign.foot_len, 2);
|
||||
let texts = |chunk: &Halign| -> Vec<String> {
|
||||
chunk
|
||||
.rows
|
||||
.iter()
|
||||
.flat_map(|row| row.cells.iter().map(|cell| cell.text.clone()))
|
||||
.collect()
|
||||
};
|
||||
let all = texts(halign);
|
||||
assert_eq!(&all[..4], ["H1", "H2", "H3", "H4"]);
|
||||
assert_eq!(&all[12..16], ["F1", "F2", "L1", "L2"]);
|
||||
|
||||
let chunks = halign.split_for_height(halign.line_height * 2);
|
||||
assert_eq!(chunks.len(), 4);
|
||||
assert_eq!(chunks[0].rows[0].cells[0].text, "H1");
|
||||
for chunk in &chunks[1..] {
|
||||
assert_eq!(chunk.rows[0].cells[0].text, "H3");
|
||||
}
|
||||
let chunk_text: Vec<Vec<String>> = chunks.iter().map(texts).collect();
|
||||
for body in ["b1", "b2", "b3", "b4"] {
|
||||
assert_eq!(
|
||||
chunk_text
|
||||
.iter()
|
||||
.filter(|texts| texts.iter().any(|text| text == body))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
chunk_text
|
||||
.iter()
|
||||
.filter(|texts| texts.iter().any(|text| text == "H1"))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
chunk_text
|
||||
.iter()
|
||||
.filter(|texts| texts.iter().any(|text| text == "H3"))
|
||||
.count(),
|
||||
3
|
||||
);
|
||||
assert!(chunk_text[3].iter().any(|text| text == "L1"));
|
||||
assert!(!chunk_text[0].iter().any(|text| text == "F1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkpoint_round_trips_through_serde() {
|
||||
let engine = Engine::layout(&document());
|
||||
|
||||
Reference in new issue
Block a user