mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-04 09:20:15 -06:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d2f5445cc |
+2
-2
File diff suppressed because one or more lines are too long
@@ -179,18 +179,9 @@ fn char_prop(w: &mut W, name: &str, c: Option<char>) {
|
||||
val(w, name, &s);
|
||||
}
|
||||
|
||||
/// Most cells a matrix is padded out to (rows × its widest row). A ragged matrix past it (a
|
||||
/// native document's, or one parsed from a few bytes of text) writes each row as it is, rather
|
||||
/// than output that grows with the square of its size.
|
||||
const MAX_PADDED_CELLS: usize = 1 << 20;
|
||||
|
||||
fn write_matrix(w: &mut W, n: &MNode, depth: usize) {
|
||||
let MNode::Matrix { rows, col_jc } = n else { return };
|
||||
let cols = rows.iter().map(|r| r.len()).max().unwrap_or(0).max(1);
|
||||
let pad = rows.len().saturating_mul(cols) <= MAX_PADDED_CELLS;
|
||||
if !pad {
|
||||
log::warn!("docx: a {} × {cols} matrix is too large to pad; its rows are written as they are", rows.len());
|
||||
}
|
||||
w.open("m:m", &[]);
|
||||
w.open("m:mPr", &[]);
|
||||
w.open("m:mcs", &[]);
|
||||
@@ -212,7 +203,7 @@ fn write_matrix(w: &mut W, n: &MNode, depth: usize) {
|
||||
w.close("m:mPr");
|
||||
for row in rows {
|
||||
w.open("m:mr", &[]);
|
||||
for c in 0..if pad { cols } else { row.len().max(1) } {
|
||||
for c in 0..cols {
|
||||
arg(w, "m:e", row.get(c).map(|v| v.as_slice()).unwrap_or(&[]), depth);
|
||||
}
|
||||
w.close("m:mr");
|
||||
@@ -489,24 +480,3 @@ fn run(w: &mut W, r: &MRun) {
|
||||
w.leaf("m:t", &[("xml:space", "preserve")], &r.text);
|
||||
w.close("m:r");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn cells(rows: Vec<Vec<Arg>>) -> usize {
|
||||
let mut w = W::new();
|
||||
write_matrix(&mut w, &MNode::Matrix { rows, col_jc: Vec::new() }, 0);
|
||||
w.s.matches("<m:e/>").count() + w.s.matches("<m:e>").count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ragged_matrix_is_padded_within_a_cell_budget() {
|
||||
// A small ragged matrix is padded to a rectangle, as before.
|
||||
assert_eq!(cells(vec![vec![Vec::new(); 3], vec![Vec::new()]]), 6);
|
||||
// 2,000 one-cell rows and one 2,000-cell row: 4 million cells padded, past the budget.
|
||||
let mut rows = vec![vec![Vec::new()]; 2000];
|
||||
rows.push(vec![Vec::new(); 2000]);
|
||||
assert_eq!(cells(rows), 4000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -474,14 +474,17 @@ fn parse_blocks(lines: &[String], depth: usize, level: u8, refs: &Refs) -> Vec<F
|
||||
let p = Para { align: a, inlines: parse_inlines(s, &Fmt::default(), depth, refs), ..Default::default() };
|
||||
Cell { blocks: vec![FBlock::Para(p)], header, ..Default::default() }
|
||||
};
|
||||
table.rows.push(head.iter().zip(&aligns).map(|(s, a)| cell(s, true, *a)).collect());
|
||||
// A document row keeps MAX_COLS cells: only those are built, so a wide header
|
||||
// doesn't pad every sparse row below it out to its width.
|
||||
let cols = aligns.len().min(wordcraft_doc::table::MAX_COLS);
|
||||
table.rows.push(head.iter().zip(&aligns).take(cols).map(|(s, a)| cell(s, true, *a)).collect());
|
||||
i += 2;
|
||||
while let Some(l) = lines.get(i) {
|
||||
if is_blank(l) || !l.contains('|') || interrupts(l) {
|
||||
break;
|
||||
}
|
||||
let mut cells = split_row(l);
|
||||
cells.resize(aligns.len(), String::new());
|
||||
cells.resize(cols, String::new());
|
||||
table.rows.push(cells.iter().zip(&aligns).map(|(s, a)| cell(s, false, *a)).collect());
|
||||
i += 1;
|
||||
}
|
||||
|
||||
@@ -714,3 +714,17 @@ fn exported_link_destinations_read_back_unchanged() {
|
||||
assert_eq!(md("https://example.com/a b"), "[x](<https://example.com/a b>)");
|
||||
assert_eq!(md("https://e.com/x\ny"), "[x](https://e.com/xy)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wide_table_builds_only_the_cells_a_row_keeps() {
|
||||
// A 5,000-column header padded every one-cell row below it to 5,000 cells.
|
||||
let n = 5000;
|
||||
let md = format!("{}|\n{}|\n{}", "|h".repeat(n), "|-".repeat(n), "|x|\n".repeat(200));
|
||||
let flow = parse(&md);
|
||||
let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") };
|
||||
assert_eq!(t.rows.len(), 201);
|
||||
assert!(t.rows.iter().all(|r| r.len() == wordcraft_doc::table::MAX_COLS));
|
||||
// A narrow table is unchanged.
|
||||
let FBlock::Table(t) = &parse("| a | b |\n| - | - |\n| x |\n").blocks[0] else { panic!("expected table") };
|
||||
assert_eq!(t.rows[1].len(), 2);
|
||||
}
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user