mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-09 12:00:01 -06:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0dab91a2fb | ||
|
|
0355c88a8c |
@@ -695,79 +695,6 @@ fn bands_unit() {
|
||||
assert!(w.len() > 2);
|
||||
}
|
||||
|
||||
/// The wrapper `wrap_lines` replaced, re-measuring strings, as the reference for its lines.
|
||||
fn wrap_lines_remeasuring(text: &str, font: Font, size: f32, width: f32) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
for para in text.split('\n') {
|
||||
let mut line = String::new();
|
||||
for word in para.split(' ') {
|
||||
let cand = if line.is_empty() { word.to_string() } else { format!("{line} {word}") };
|
||||
if line.is_empty() || gridcraft_pdf::text_width(font, size, &cand) <= width {
|
||||
line = cand;
|
||||
} else {
|
||||
out.push(std::mem::take(&mut line));
|
||||
line = word.to_string();
|
||||
}
|
||||
while gridcraft_pdf::text_width(font, size, &line) > width && line.chars().count() > 1 {
|
||||
let chars: Vec<char> = line.chars().collect();
|
||||
let (mut lo, mut hi) = (1usize, chars.len());
|
||||
while lo < hi {
|
||||
let mid = lo + (hi - lo).div_ceil(2);
|
||||
let cut: String = chars[..mid].iter().collect();
|
||||
if gridcraft_pdf::text_width(font, size, &cut) <= width {
|
||||
lo = mid;
|
||||
} else {
|
||||
hi = mid - 1;
|
||||
}
|
||||
}
|
||||
if lo == chars.len() {
|
||||
break;
|
||||
}
|
||||
out.push(chars[..lo].iter().collect());
|
||||
line = chars[lo..].iter().collect();
|
||||
}
|
||||
if out.len() > 2000 {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
out.push(line);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrap_lines_matches_the_remeasuring_wrapper() {
|
||||
let texts = [
|
||||
"the quick brown fox jumps",
|
||||
"Supercalifragilistic",
|
||||
"a b c\n\nverylongwordthatneedsbreaking short\nend ",
|
||||
"Wide WWWWWWWWWW words, iiiiiiii narrow ones; mixed MMMMiiiiMMMM",
|
||||
"€ ü ß — unicode Ünïcödé text",
|
||||
"",
|
||||
" ",
|
||||
];
|
||||
for text in texts {
|
||||
for font in [Font::Helvetica, Font::HelveticaBold, Font::Courier] {
|
||||
for (size, width) in [(10.0, 50.0), (10.0, 20.0), (11.0, 1.0), (8.0, 300.0), (12.0, 0.0)] {
|
||||
assert_eq!(wrap_lines(text, font, size, width), wrap_lines_remeasuring(text, font, size, width), "{text:?} {font:?} {size} {width}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrapping_a_long_word_in_a_narrow_box_stops_at_the_line_limit() {
|
||||
// One character per line: the old wrapper re-measured and copied the rest of the word for
|
||||
// each of the 100,000 lines before checking the limit.
|
||||
let word = "w".repeat(100_000);
|
||||
let lines = wrap_lines(&word, Font::Helvetica, 10.0, 1.0);
|
||||
assert!(lines.len() <= 2002, "{}", lines.len());
|
||||
assert!(lines.iter().all(|l| l == "w"));
|
||||
// A long paragraph in a very wide box is one line.
|
||||
let para = vec!["word"; 50_000].join(" ");
|
||||
assert_eq!(wrap_lines(¶, Font::Helvetica, 10.0, 1.0e9), vec![para.clone()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn center_across_spans_following_empty_center_across_cells() {
|
||||
let mut styles = StyleTable::default();
|
||||
|
||||
@@ -3,57 +3,42 @@ use super::{CellRef, Color, Font, HAlign, MAX_COLS, Page, Point, RangeRef, Sheet
|
||||
|
||||
const PAD_PX: f32 = 2.0;
|
||||
|
||||
/// Most lines [`wrap_lines`] returns (it may return one more, the rest of a paragraph).
|
||||
const MAX_WRAP_LINES: usize = 2000;
|
||||
|
||||
/// Word-wraps `text` to `width` points.
|
||||
///
|
||||
/// Widths are kept as running sums of glyph widths (the sum `text_width` makes), so each
|
||||
/// character is measured once: re-measuring the line for every word, or the rest of an
|
||||
/// over-long word for every piece cut from it, made a long paragraph or word quadratic.
|
||||
pub(super) fn wrap_lines(text: &str, font: Font, size: f32, width: f32) -> Vec<String> {
|
||||
let units_of = |s: &str| -> u64 { s.chars().map(|c| u64::from(font.char_width(c))).sum() };
|
||||
let fits = |units: u64| units as f32 * size / 1000.0 <= width;
|
||||
let space = units_of(" ");
|
||||
let mut out = Vec::new();
|
||||
for para in text.split('\n') {
|
||||
let mut line = String::new();
|
||||
let mut line_units: u64 = 0;
|
||||
for word in para.split(' ') {
|
||||
let word_units = units_of(word);
|
||||
if line.is_empty() {
|
||||
line.push_str(word);
|
||||
line_units = word_units;
|
||||
} else if fits(line_units + space + word_units) {
|
||||
line.push(' ');
|
||||
line.push_str(word);
|
||||
line_units += space + word_units;
|
||||
let cand = if line.is_empty() { word.to_string() } else { format!("{line} {word}") };
|
||||
if line.is_empty() || gridcraft_pdf::text_width(font, size, &cand) <= width {
|
||||
line = cand;
|
||||
} else {
|
||||
out.push(std::mem::take(&mut line));
|
||||
line.push_str(word);
|
||||
line_units = word_units;
|
||||
line = word.to_string();
|
||||
}
|
||||
// Break a single over-long word by characters: each piece is the longest that fits
|
||||
// (at least one character).
|
||||
if !fits(line_units) && line.chars().nth(1).is_some() {
|
||||
let mut piece = String::new();
|
||||
let mut units: u64 = 0;
|
||||
for c in line.chars() {
|
||||
let w = u64::from(font.char_width(c));
|
||||
if !piece.is_empty() && !fits(units + w) {
|
||||
out.push(std::mem::take(&mut piece));
|
||||
units = 0;
|
||||
if out.len() > MAX_WRAP_LINES {
|
||||
return out;
|
||||
}
|
||||
// Break a single over-long word by characters. The cut that fits is found by binary
|
||||
// search (each try measures one candidate prefix), so an over-long word costs
|
||||
// O(n log n) measurements instead of re-measuring the whole cut per character for
|
||||
// every emitted line (`a` × 100k pasted text would be cubic worst case).
|
||||
while gridcraft_pdf::text_width(font, size, &line) > width && line.chars().count() > 1 {
|
||||
let chars: Vec<char> = line.chars().collect();
|
||||
let (mut lo, mut hi) = (1usize, chars.len());
|
||||
while lo < hi {
|
||||
let mid = lo + (hi - lo).div_ceil(2);
|
||||
let cut: String = chars[..mid].iter().collect();
|
||||
if gridcraft_pdf::text_width(font, size, &cut) <= width {
|
||||
lo = mid;
|
||||
} else {
|
||||
hi = mid - 1;
|
||||
}
|
||||
piece.push(c);
|
||||
units += w;
|
||||
}
|
||||
line = piece;
|
||||
line_units = units;
|
||||
if lo == chars.len() {
|
||||
break;
|
||||
}
|
||||
out.push(chars[..lo].iter().collect());
|
||||
line = chars[lo..].iter().collect();
|
||||
}
|
||||
if out.len() > MAX_WRAP_LINES {
|
||||
if out.len() > 2000 {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,10 @@ fn rows_ref(sheet: &str, origin: CellRef, col: u32, rows: &[u32]) -> Option<Stri
|
||||
/// Most category rows a PivotChart plots.
|
||||
const MAX_CHART_ROWS: usize = 10_000;
|
||||
|
||||
/// Most series a PivotChart gets: the chart renderer reads at most 255 series, and each one owns
|
||||
/// reference text as long as its category rows, copied into every linked chart.
|
||||
const MAX_CHART_SERIES: usize = 255;
|
||||
|
||||
/// A PivotChart's series for a report written at `origin`: categories are the innermost row
|
||||
/// items, one series per item column (column items × value fields); totals are left out.
|
||||
pub fn chart_series(sheet: &str, origin: CellRef, out: &Output) -> Vec<Series> {
|
||||
@@ -64,6 +68,7 @@ pub fn chart_series(sheet: &str, origin: CellRef, out: &Output) -> Vec<Series> {
|
||||
let categories = out.label_col.and_then(|c| rows_ref(sheet, origin, c, &rows));
|
||||
out.chart_cols
|
||||
.iter()
|
||||
.take(MAX_CHART_SERIES)
|
||||
.filter_map(|(c, name)| {
|
||||
Some(Series {
|
||||
name: Some(format!("\"{}\"", name.replace('"', "\"\""))),
|
||||
@@ -106,3 +111,22 @@ pub fn clear_output(wb: &mut Workbook, sheet: usize, idx: usize) {
|
||||
p.last_range = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_pivot_chart_gets_at_most_the_series_the_renderer_reads() {
|
||||
let out = Output {
|
||||
grid: vec![],
|
||||
row_kinds: vec![],
|
||||
body_offset: 0,
|
||||
width: 1000,
|
||||
chart_rows: vec![1, 2, 3],
|
||||
chart_cols: (1..1000).map(|c| (c, format!("S{c}"))).collect(),
|
||||
label_col: Some(0),
|
||||
};
|
||||
assert_eq!(chart_series("Sheet1", CellRef::new(0, 0), &out).len(), MAX_CHART_SERIES);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::items::{Acc, FieldItems, build_tree, collect_siblings, is_collapsed, item_label};
|
||||
use super::output::{AggMap, CellKind, ColKind, ColSpec, Ctx, Grid, Line, LineKind, Output, RowKind, ValSrc, data_cols_exceed_sheet, flatten_cols};
|
||||
use super::output::{
|
||||
AggMap, CellKind, ColKind, ColSpec, Ctx, Grid, Line, LineKind, Output, RowKind, ValSrc, caption, data_cols_exceed_sheet, flatten_cols,
|
||||
};
|
||||
use super::{
|
||||
CalcSet, CellError, CellRef, HashMap, HashSet, MAX_AXIS_FIELDS, MAX_COLS, MAX_VALUE_FIELDS, PivotDateGroup, PivotField, PivotFilter, PivotFunc,
|
||||
PivotLayout, PivotShowAs, PivotTable, PivotValue, Source, Value, Workbook, read_source,
|
||||
@@ -92,6 +94,10 @@ struct AggregateData {
|
||||
col_leaves: HashSet<Vec<u32>>,
|
||||
}
|
||||
|
||||
/// Most accumulators (groups times value columns) one report may hold: twice what the 5 million
|
||||
/// group limit allowed at one value field.
|
||||
const MAX_ACCUMULATORS: usize = 16_000_000;
|
||||
|
||||
fn aggregate_record(
|
||||
input: &AggregateInput<'_>,
|
||||
data: &mut AggregateData,
|
||||
@@ -150,7 +156,9 @@ fn aggregate_record(
|
||||
if !input.cols.is_empty() && data_cols_exceed_sheet(data.col_leaves.len(), input.pt.grand_totals_rows, input.vals.len()) {
|
||||
return Err("The PivotTable report will not fit on the sheet.".into());
|
||||
}
|
||||
if data.aggs.len() > 5_000_000 {
|
||||
// Each entry holds one accumulator per value field and calculated-field source column, so the
|
||||
// width counts too: a handful of groups over hundreds of fields is large as well.
|
||||
if data.aggs.len() > 5_000_000 || data.aggs.len().saturating_mul(input.vals.len() + input.extra.len()) > MAX_ACCUMULATORS {
|
||||
return Err("The PivotTable report is too large.".into());
|
||||
}
|
||||
Ok(())
|
||||
@@ -257,8 +265,8 @@ fn write_headers(g: &mut Grid, input: HeaderInput<'_>) -> Result<(), String> {
|
||||
let lv = spec.path.len().saturating_sub(1);
|
||||
let label = spec.path.last().and_then(|it| col_items.get(lv).map(|fi| fi.label(*it))).unwrap_or_default();
|
||||
let text = match vi {
|
||||
Some(vi) if k > 1 => format!("{label} {}", val_name(*vi)),
|
||||
_ => format!("{label} Total"),
|
||||
Some(vi) if k > 1 => caption(&label, &val_name(*vi)),
|
||||
_ => caption(&label, "Total"),
|
||||
};
|
||||
g.set(first_level + lv, c, Value::text(text), CellKind::ColLabel, 0, None);
|
||||
}
|
||||
@@ -316,6 +324,7 @@ struct ShowContext<'a> {
|
||||
value_index: usize,
|
||||
level0: &'a [u32],
|
||||
siblings: &'a HashMap<Vec<u32>, Vec<u32>>,
|
||||
cache: &'a ShowCache,
|
||||
}
|
||||
|
||||
fn number(value: &Option<Value>) -> Option<f64> {
|
||||
@@ -332,42 +341,67 @@ fn ratio(a: Value, b: Option<Value>) -> Value {
|
||||
}
|
||||
}
|
||||
|
||||
/// What Running Total and Rank need from other cells, computed once per sibling set instead of
|
||||
/// once per displayed value (which rescanned the whole set every time).
|
||||
type ShowKey = (Vec<u32>, Vec<u32>, usize);
|
||||
/// Cumulative values by top-level item, and the total of all of them.
|
||||
type RunningTotals = (HashMap<u32, f64>, f64);
|
||||
|
||||
#[derive(Default)]
|
||||
struct ShowCache {
|
||||
/// Ranked values' sibling numbers, sorted ascending without repeats, by (parent, column, value).
|
||||
ranks: std::cell::RefCell<HashMap<ShowKey, Vec<f64>>>,
|
||||
/// Running totals by top-level item, with the total of all of them, by (rest of path, column, value).
|
||||
running: std::cell::RefCell<HashMap<ShowKey, RunningTotals>>,
|
||||
}
|
||||
|
||||
fn running_total(raw: Value, lookup: &ValueLookup<'_>, context: &ShowContext<'_>) -> Value {
|
||||
let Some((first, _)) = context.row_path.split_first() else { return raw };
|
||||
let Some((first, rest)) = context.row_path.split_first() else { return raw };
|
||||
if !matches!(raw, Value::Number(_)) {
|
||||
return raw;
|
||||
}
|
||||
let mut total = 0.0;
|
||||
for item in context.level0 {
|
||||
let mut path = context.row_path.to_vec();
|
||||
if let Some(first_item) = path.first_mut() {
|
||||
*first_item = *item;
|
||||
let mut cache = context.cache.running.borrow_mut();
|
||||
let (by_item, all) = cache.entry((rest.to_vec(), context.col_path.to_vec(), context.value_index)).or_insert_with(|| {
|
||||
let mut total = 0.0;
|
||||
let mut by_item = HashMap::new();
|
||||
for item in context.level0 {
|
||||
let mut path = Vec::with_capacity(rest.len() + 1);
|
||||
path.push(*item);
|
||||
path.extend_from_slice(rest);
|
||||
// An item with no number here adds nothing and is not stored (the shown cell always has one).
|
||||
let Some(value) = number(&lookup.get(&path, context.col_path, context.value_index)) else { continue };
|
||||
total += value;
|
||||
by_item.entry(*item).or_insert(total);
|
||||
}
|
||||
total += number(&lookup.get(&path, context.col_path, context.value_index)).unwrap_or(0.0);
|
||||
if item == first {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Value::number(total)
|
||||
(by_item, total)
|
||||
});
|
||||
Value::number(by_item.get(first).copied().unwrap_or(*all))
|
||||
}
|
||||
|
||||
fn ranked_value(raw: Value, lookup: &ValueLookup<'_>, context: &ShowContext<'_>) -> Value {
|
||||
let Some((_, parent)) = context.row_path.split_last() else { return Value::Empty };
|
||||
let Some(me) = raw.as_f64() else { return raw };
|
||||
let siblings = context.siblings.get(parent).cloned().unwrap_or_default();
|
||||
let mut cache = context.cache.ranks.borrow_mut();
|
||||
let sorted = cache.entry((parent.to_vec(), context.col_path.to_vec(), context.value_index)).or_insert_with(|| {
|
||||
let mut all: Vec<f64> = context
|
||||
.siblings
|
||||
.get(parent)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|sibling| {
|
||||
let mut path = parent.to_vec();
|
||||
path.push(*sibling);
|
||||
number(&lookup.get(&path, context.col_path, context.value_index))
|
||||
})
|
||||
.collect();
|
||||
all.sort_by(f64::total_cmp);
|
||||
all.dedup();
|
||||
all
|
||||
});
|
||||
// Excel's ranks are dense: ties share a rank and the next value takes the next one
|
||||
// (5, 4, 3, 3, 4 rank 1, 2, 3, 3, 2), so count the distinct larger values.
|
||||
let mut larger: Vec<f64> = siblings
|
||||
.into_iter()
|
||||
.filter_map(|sibling| {
|
||||
let mut path = parent.to_vec();
|
||||
path.push(sibling);
|
||||
number(&lookup.get(&path, context.col_path, context.value_index)).filter(|value| *value > me)
|
||||
})
|
||||
.collect();
|
||||
larger.sort_by(f64::total_cmp);
|
||||
larger.dedup();
|
||||
Value::number(larger.len() as f64 + 1.0)
|
||||
let larger = sorted.len() - sorted.partition_point(|value| *value <= me);
|
||||
Value::number(larger as f64 + 1.0)
|
||||
}
|
||||
|
||||
fn show_value(definition: &PivotValue, raw: Value, lookup: &ValueLookup<'_>, context: &ShowContext<'_>) -> Value {
|
||||
@@ -637,6 +671,7 @@ pub(super) fn compute_from(wb: &Workbook, pt: &PivotTable, src: &Source, mut pro
|
||||
})
|
||||
.collect();
|
||||
let lookup = ValueLookup { wb, source: src, calc: &calc, vals: &vals, extra: &extra, aggs: &aggs, value_count: k };
|
||||
let show_cache = ShowCache::default();
|
||||
let mut chart_rows: Vec<u32> = Vec::new();
|
||||
let chart_cols: Vec<(u32, String)> = data_cols
|
||||
.iter()
|
||||
@@ -694,7 +729,7 @@ pub(super) fn compute_from(wb: &Workbook, pt: &PivotTable, src: &Source, mut pro
|
||||
let Some((_, vdef)) = vals.get(*vi) else { continue };
|
||||
let cp = spec.path.as_slice();
|
||||
let Some(raw) = lookup.get(rp, cp, *vi) else { continue };
|
||||
let context = ShowContext { row_path: rp, col_path: cp, value_index: *vi, level0: &level0, siblings: &siblings };
|
||||
let context = ShowContext { row_path: rp, col_path: cp, value_index: *vi, level0: &level0, siblings: &siblings, cache: &show_cache };
|
||||
let shown = show_value(vdef, raw, &lookup, &context);
|
||||
if let Some(pr) = probe.as_deref_mut() {
|
||||
if pr.at == Some((r, l + j)) {
|
||||
|
||||
@@ -121,6 +121,18 @@ pub(super) enum ColKind {
|
||||
Grand,
|
||||
}
|
||||
|
||||
/// Longest item caption Excel keeps; a subtotal caption repeats its parent's label for every value
|
||||
/// field at every level, so a long source label is cut rather than multiplied.
|
||||
const MAX_CAPTION_LABEL_CHARS: usize = 255;
|
||||
|
||||
/// `label` (cut to [`MAX_CAPTION_LABEL_CHARS`]) and `suffix`, as in "East Total".
|
||||
pub(super) fn caption(label: &str, suffix: &str) -> String {
|
||||
let mut out: String = label.chars().take(MAX_CAPTION_LABEL_CHARS).collect();
|
||||
out.push(' ');
|
||||
out.push_str(suffix);
|
||||
out
|
||||
}
|
||||
|
||||
pub(super) struct ColSpec {
|
||||
pub(super) kind: ColKind,
|
||||
pub(super) path: Vec<u32>,
|
||||
@@ -162,7 +174,7 @@ impl Ctx<'_> {
|
||||
leaf: false,
|
||||
path: prefix.to_vec(),
|
||||
values: true,
|
||||
labels: vec![(col, Value::text(format!("{label} {name}")), indent, None)],
|
||||
labels: vec![(col, Value::text(caption(label, name)), indent, None)],
|
||||
vi: Some(vi),
|
||||
});
|
||||
}
|
||||
@@ -249,7 +261,7 @@ impl Ctx<'_> {
|
||||
leaf: false,
|
||||
path: prefix.clone(),
|
||||
values: true,
|
||||
labels: vec![(col, Value::text(format!("{label} Total")), indent, None)],
|
||||
labels: vec![(col, Value::text(caption(&label, "Total")), indent, None)],
|
||||
vi,
|
||||
});
|
||||
}
|
||||
@@ -316,7 +328,7 @@ impl Ctx<'_> {
|
||||
leaf: false,
|
||||
path: prefix.clone(),
|
||||
values: true,
|
||||
labels: vec![(col, Value::text(format!("{label} Total")), 0, None)],
|
||||
labels: vec![(col, Value::text(caption(&label, "Total")), 0, None)],
|
||||
vi,
|
||||
});
|
||||
}
|
||||
@@ -438,3 +450,17 @@ pub(super) fn cell_style(lk: &Look, rk: RowKind, cell: &OutCell, last_header: bo
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn captions_cut_a_long_parent_label() {
|
||||
assert_eq!(caption("East", "Total"), "East Total");
|
||||
let long = "é".repeat(100_000);
|
||||
let c = caption(&long, "Sum of Sales");
|
||||
assert_eq!(c.chars().count(), MAX_CAPTION_LABEL_CHARS + " Sum of Sales".len());
|
||||
assert!(c.ends_with(" Sum of Sales"));
|
||||
}
|
||||
}
|
||||
|
||||
+24
-46
@@ -142,22 +142,13 @@ pub fn read(cx: &mut Ctx<'_>, workbook_rels: &[Rel]) -> Result<Vec<Option<Arc<Ce
|
||||
let Some(image_rel_list) = read_xml(cx, &image_rels_path, "richValueRels")? else { return Ok(vec![]) };
|
||||
let image_rels = cx.pkg.rels(&image_rels_path)?;
|
||||
let structures = entries(cx, &structures, "s")?;
|
||||
// Each structure's key layout, worked out once: every value would otherwise walk its
|
||||
// structure's keys again. Only local-image structures matter here.
|
||||
let layouts: Vec<Option<Layout>> = structures.iter().map(|s| Layout::of(s)).collect();
|
||||
let image_rel_list = entries(cx, &image_rel_list, "rel")?;
|
||||
// Relationships by id, a repeated id recorded as invalid (`None`), so a value's lookup is
|
||||
// one probe rather than a scan of the whole table for duplicates.
|
||||
let mut rels_by_id: HashMap<&str, Option<&Rel>> = HashMap::new();
|
||||
for rel in &image_rels {
|
||||
rels_by_id.entry(rel.id.as_str()).and_modify(|r| *r = None).or_insert(Some(rel));
|
||||
}
|
||||
let values = entries(cx, &data, "rv")?;
|
||||
let mut pictures = Vec::with_capacity(values.len());
|
||||
let mut cache = HashMap::new();
|
||||
let mut total = 0usize;
|
||||
for value in values {
|
||||
let picture = read_value(cx, value, &layouts, &image_rel_list, &rels_by_id, &mut cache, &mut total)?;
|
||||
let picture = read_value(cx, value, &structures, &image_rel_list, &image_rels, &mut cache, &mut total)?;
|
||||
if picture.is_none() {
|
||||
cx.warn("some rich values are unsupported or malformed; their scalar fallbacks were retained (cell pictures may be missing)");
|
||||
}
|
||||
@@ -197,56 +188,43 @@ pub fn read(cx: &mut Ctx<'_>, workbook_rels: &[Rel]) -> Result<Vec<Option<Arc<Ce
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Where a `_localImage` structure keeps the values a picture needs.
|
||||
struct Layout {
|
||||
keys: usize,
|
||||
/// Index of the one `_rvRel:LocalImageIdentifier` key of type `i`, if exactly one.
|
||||
image: Option<usize>,
|
||||
/// Index of the one `Text` key of type `s`, if exactly one.
|
||||
text: Option<usize>,
|
||||
}
|
||||
|
||||
impl Layout {
|
||||
fn of(structure: &El) -> Option<Layout> {
|
||||
if structure.attr("t") != Some("_localImage") {
|
||||
return None;
|
||||
}
|
||||
let keys: Vec<&El> = structure.kids("k").collect();
|
||||
let one = |name: &str, ty: &str| {
|
||||
let mut matches = keys.iter().enumerate().filter(|(_, k)| k.attr("n") == Some(name));
|
||||
let (i, key) = matches.next()?;
|
||||
(matches.next().is_none() && key.attr("t") == Some(ty)).then_some(i)
|
||||
};
|
||||
Some(Layout { keys: keys.len(), image: one("_rvRel:LocalImageIdentifier", "i"), text: one("Text", "s") })
|
||||
}
|
||||
}
|
||||
|
||||
fn read_value(
|
||||
cx: &mut Ctx<'_>,
|
||||
value: &El,
|
||||
layouts: &[Option<Layout>],
|
||||
structures: &[&El],
|
||||
image_rel_list: &[&El],
|
||||
rels_by_id: &HashMap<&str, Option<&Rel>>,
|
||||
image_rels: &[Rel],
|
||||
cache: &mut HashMap<(String, String), Arc<CellPicture>>,
|
||||
total: &mut usize,
|
||||
) -> Result<Option<Arc<CellPicture>>, IoError> {
|
||||
let Some(Some(layout)) = value.attr_u32("s").and_then(|n| layouts.get(n as usize)) else { return Ok(None) };
|
||||
let values: Vec<_> = value.kids("v").collect();
|
||||
if layout.keys != values.len() {
|
||||
let Some(structure) = value.attr_u32("s").and_then(|n| structures.get(n as usize)) else { return Ok(None) };
|
||||
if structure.attr("t") != Some("_localImage") {
|
||||
return Ok(None);
|
||||
}
|
||||
let lookup = |slot: Option<usize>| slot.and_then(|i| values.get(i)).map(|v| v.text.as_str());
|
||||
let Some(rel_id) =
|
||||
lookup(layout.image).and_then(|v| v.trim().parse::<usize>().ok()).and_then(|n| image_rel_list.get(n)).and_then(|rel| rel.attr("id"))
|
||||
let keys: Vec<_> = structure.kids("k").collect();
|
||||
let values: Vec<_> = value.kids("v").collect();
|
||||
if keys.len() != values.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
let lookup = |name: &str, ty: &str| {
|
||||
let mut matches = keys.iter().zip(&values).filter(|(k, _)| k.attr("n") == Some(name));
|
||||
let (key, value) = matches.next()?;
|
||||
(matches.next().is_none() && key.attr("t") == Some(ty)).then_some(value.text.as_str())
|
||||
};
|
||||
let Some(rel_id) = lookup("_rvRel:LocalImageIdentifier", "i")
|
||||
.and_then(|v| v.trim().parse::<usize>().ok())
|
||||
.and_then(|n| image_rel_list.get(n))
|
||||
.and_then(|rel| rel.attr("id"))
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let Some(found) = rels_by_id.get(rel_id) else { return Ok(None) };
|
||||
let Some(rel) = found.filter(|rel| rel.kind == "image" && !rel.external) else {
|
||||
let mut matches = image_rels.iter().filter(|rel| rel.id == rel_id);
|
||||
let Some(rel) = matches.next() else { return Ok(None) };
|
||||
if matches.next().is_some() || rel.kind != "image" || rel.external {
|
||||
cx.warn("a cell picture has an external or invalid image relationship; kept its scalar fallback");
|
||||
return Ok(None);
|
||||
};
|
||||
let alt = lookup(layout.text).unwrap_or("").to_string();
|
||||
}
|
||||
let alt = lookup("Text", "s").unwrap_or("").to_string();
|
||||
let key = (normalize(&rel.target), alt.clone());
|
||||
if let Some(picture) = cache.get(&key) {
|
||||
return Ok(Some(picture.clone()));
|
||||
|
||||
@@ -294,27 +294,3 @@ fn picture_metadata_limits_and_relationship_read_errors_do_not_block_workbooks()
|
||||
assert!(report.warnings.iter().any(|w| w.contains("cell-picture data could not be read")), "{:?}", report.warnings);
|
||||
}
|
||||
}
|
||||
|
||||
/// Many values over one wide structure, and a repeated relationship id: the structure's layout
|
||||
/// is worked out once and relationships are indexed, so the import is linear in the records;
|
||||
/// the repeated id is invalid and keeps the scalar fallback.
|
||||
#[test]
|
||||
fn wide_structures_and_repeated_relationship_ids_are_resolved_through_indexes() {
|
||||
let mut parts = independent_parts(false);
|
||||
// 200,000 keys in one structure and 50,000 short values that name it: rebuilding the key
|
||||
// list per value would be ten billion steps.
|
||||
let padding = r#"<k n="pad" t="s"/>"#.repeat(200_000);
|
||||
replace(&mut parts, "rich/structures.xml", r#"<k n="CalcOrigin" t="i"/>"#, &format!(r#"<k n="CalcOrigin" t="i"/>{padding}"#));
|
||||
let values = r#"<rv s="1"><v/></rv>"#.repeat(50_000);
|
||||
replace(&mut parts, "rich/values.xml", r#"<rv s="1"><v>green & <image></v><v>1</v><v>5</v></rv>"#, &values);
|
||||
let (wb, report) = read_xlsx(&zip(&parts)).unwrap();
|
||||
assert!(!wb.sheet(0).unwrap().cell_pictures.contains_key(&at("A1")));
|
||||
assert!(report.warnings.iter().any(|w| w.contains("picture")), "{:?}", report.warnings);
|
||||
// The same id twice makes every reference to it invalid.
|
||||
let mut parts = independent_parts(false);
|
||||
let dup = r#"<Relationship Id="opaqueImageId" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/original.bin"/>"#;
|
||||
replace(&mut parts, "rich/_rels/images.xml.rels", dup, &dup.repeat(2));
|
||||
let (wb, report) = read_xlsx(&zip(&parts)).unwrap();
|
||||
assert!(!wb.sheet(0).unwrap().cell_pictures.contains_key(&at("A1")));
|
||||
assert!(report.warnings.iter().any(|w| w.contains("invalid image relationship")), "{:?}", report.warnings);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user