mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-07 10:49:47 -06:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a41095eed |
+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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +118,10 @@ struct Builder<'a> {
|
||||
revs: std::collections::HashMap<(u8, RevMark), u32>,
|
||||
/// Link and font bytes given to runs so far ([`MAX_RUN_STRINGS`]).
|
||||
run_strings: usize,
|
||||
/// The ids the next list's abstract and concrete numbering take: past every id in `doc`,
|
||||
/// found once rather than by a scan of every numbering per list.
|
||||
next_abstract: u32,
|
||||
next_num: u32,
|
||||
}
|
||||
|
||||
fn rev_key(kind: RevisionKind) -> u8 {
|
||||
@@ -137,7 +141,9 @@ impl<'a> Builder<'a> {
|
||||
revs.entry((rev_key(r.kind), mark)).or_insert(i as u32);
|
||||
}
|
||||
}
|
||||
Builder { doc, revs, run_strings: 0 }
|
||||
let next_abstract = doc.numbering.abstracts.iter().map(|a| a.id.saturating_add(1)).max().unwrap_or(0);
|
||||
let next_num = doc.numbering.nums.iter().map(|n| n.id.saturating_add(1)).max().unwrap_or(1).max(1);
|
||||
Builder { doc, revs, run_strings: 0, next_abstract, next_num }
|
||||
}
|
||||
|
||||
/// The document revision for a tracked change, added the first time it is seen.
|
||||
@@ -291,9 +297,10 @@ impl<'a> Builder<'a> {
|
||||
})
|
||||
.collect();
|
||||
let numbering = &mut self.doc.numbering;
|
||||
let aid = numbering.abstracts.iter().map(|a| a.id + 1).max().unwrap_or(0);
|
||||
let (aid, nid) = (self.next_abstract, self.next_num);
|
||||
self.next_abstract = aid.saturating_add(1);
|
||||
self.next_num = nid.saturating_add(1);
|
||||
numbering.abstracts.push(AbstractNum { id: aid, name: None, levels, ..Default::default() });
|
||||
let nid = numbering.nums.iter().map(|n| n.id + 1).max().unwrap_or(1).max(1);
|
||||
numbering.nums.push(Num { id: nid, abstract_id: aid, ..Default::default() });
|
||||
nid
|
||||
}
|
||||
|
||||
@@ -1019,3 +1019,14 @@ fn latex_export_keeps_repeated_spaces() {
|
||||
let d = crate::import("tex", tex.as_bytes()).unwrap().unwrap();
|
||||
assert_eq!(para_texts(&d), vec!["a b c".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_imported_lists_take_fresh_numbering_ids_in_turn() {
|
||||
// Each list found its ids by scanning every numbering made before it (quadratic in lists).
|
||||
let md: String = (0..4000).map(|i| if i % 2 == 0 { "- a\n\n" } else { "1. b\n\n" }).collect();
|
||||
let d = import("md", md.as_bytes()).expect("handled").expect("import");
|
||||
let ids: Vec<u32> = d.numbering.nums.iter().map(|n| n.id).collect();
|
||||
assert_eq!(ids, (1..=4000).collect::<Vec<u32>>());
|
||||
let abstracts: Vec<u32> = d.numbering.abstracts.iter().map(|a| a.id).collect();
|
||||
assert_eq!(abstracts, (0..4000).collect::<Vec<u32>>());
|
||||
}
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user