diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index b2b581112..81941fac1 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -86,7 +86,8 @@ they were. A binary workbook is not written. A binary workbook gives the same `Workbook`, strings, rows and merged cells, and the same `Styles`: number formats, fonts, fills, borders, cell and cell style formats, cell styles, differential formats and the indexed colours, as Excel writes them in `styles.xml` (default fill -colours and empty settings left out, a cell style format's `apply*="0"`). Custom table styles and +colours and empty settings left out, a cell style format's `apply*="0"`, a number format with a +newer locale tag in its Excel 2007 spelling, as `formatCode` has it). Custom table styles and recent colours are not read; a damaged styles part is a warning and no styles. Its cell formulas are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 50da6185c..043b0743b 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -175,12 +175,16 @@ struct Records<'a, 'l> { bytes: Bytes<'a>, limits: &'l Limits, budget: &'l Budget, - blocks: Vec, + /// Each open block's end record, and whether its records are read. + blocks: Vec<(u16, bool)>, + /// Read the alternate content meant for Excel 2007 (version 12), as an `.xlsx` reader sees + /// the main attribute where Excel keeps a newer spelling in an extension. + excel_2007: bool, } impl<'a, 'l> Records<'a, 'l> { fn new(bytes: &'a [u8], limits: &'l Limits, budget: &'l Budget) -> Self { - Self { bytes: Bytes::new(bytes), limits, budget, blocks: vec![] } + Self { bytes: Bytes::new(bytes), limits, budget, blocks: vec![], excel_2007: false } } fn next(&mut self, warnings: &mut Warnings) -> Result, Error> { @@ -209,6 +213,7 @@ impl<'a, 'l> Records<'a, 'l> { // BrtFRTBegin, BrtACBegin. 35 | 37 => { let mut b = Bytes::new(payload); + let mut read = false; if id == 35 { b.take(4)?; } else { @@ -216,22 +221,32 @@ impl<'a, 'l> Records<'a, 'l> { if count == 0 { return Err(invalid("empty alternate-content version list")); } - b.take(usize::from(count) * 4)?; + // ACProductVersion: fileVersion, then fileProduct (15 bits) and + // fileExtension (the version and later). + for _ in 0..count { + let (version, product) = (b.u16()?, b.u16()?); + let later = product & 0x8000 != 0; + read |= self.excel_2007 && product & 0x7FFF == 0 && (version == 12 || later && version <= 12); + } } b.finish()?; if self.blocks.len() >= 64 { return Err(invalid("extension blocks nested too deeply")); } - self.blocks.push(id + 1); - warnings.warn("unsupported XLSB future-record and alternate-content blocks were skipped"); + // Inside a skipped block, everything is skipped. + let read = read && self.blocks.iter().all(|(_, r)| *r); + self.blocks.push((id + 1, read)); + if !read { + warnings.warn("unsupported XLSB future-record and alternate-content blocks were skipped"); + } } // BrtFRTEnd, BrtACEnd. 36 | 38 => { - if self.blocks.pop() != Some(id) || !payload.is_empty() { + if self.blocks.pop().map(|(end, _)| end) != Some(id) || !payload.is_empty() { return Err(invalid("mismatched extension block end")); } } - _ if !self.blocks.is_empty() => {} + _ if self.blocks.iter().any(|(_, read)| !read) => {} _ => return Ok(Some((id, payload))), } } diff --git a/crates/xlsx/src/xlsb/styles.rs b/crates/xlsx/src/xlsb/styles.rs index 094620a07..50673ef28 100644 --- a/crates/xlsx/src/xlsb/styles.rs +++ b/crates/xlsx/src/xlsb/styles.rs @@ -151,10 +151,11 @@ fn font(b: &mut Bytes<'_>) -> Result { extend: on(0x80), underline: (uls != 0).then(|| underline(u32::from(uls))), vert_align: (sss != 0).then(|| script(u32::from(sss))), - color, family: (family != 0).then_some(u32::from(family)), charset: (charset != 0).then_some(u32::from(charset)), scheme: scheme(u32::from(font_scheme)).filter(|s| *s != FontScheme::None), + // An automatic colour is a font with no `` in `.xlsx`. + color: color.filter(|c| c.source != ColorSource::Auto), }) } @@ -164,7 +165,8 @@ fn gradient(b: &mut Bytes<'_>, path: bool) -> Result { } /// `BrtFill`. Excel's `.xlsx` leaves out the default colours: system foreground (indexed 64) as -/// the pattern colour and system background (indexed 65) as the background. +/// the pattern colour and system background (indexed 65) as the background, unless the pattern +/// colour has a tint (as the built-in "20% - Accent1" styles do), when it states the background. fn fill(b: &mut Bytes<'_>) -> Result { let fls = b.u32()?; let fg = color(b)?; @@ -183,7 +185,8 @@ fn fill(b: &mut Bytes<'_>) -> Result { return Ok(Fill::Gradient(g)); } let system = |c: Color, i: u32| c.source == ColorSource::Indexed(i) && c.tint == 0.0; - Ok(Fill::Pattern(PatternFill { pattern: Some(pattern(fls)?), fg: fg.filter(|c| !system(*c, 64)), bg: bg.filter(|c| !system(*c, 65)) })) + let tinted = fg.is_some_and(|c| c.tint != 0.0); + Ok(Fill::Pattern(PatternFill { pattern: Some(pattern(fls)?), fg: fg.filter(|c| !system(*c, 64)), bg: bg.filter(|c| tinted || !system(*c, 65)) })) } /// `BrtBorder`: the five edges a cell border has, each stated (`` when it has no line). @@ -393,6 +396,9 @@ fn dxf(b: &mut Bytes<'_>) -> Result { /// The styles part: `BrtBeginStyleSheet` to `BrtEndStyleSheet`. pub(crate) fn styles(bytes: &[u8], limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Result { let mut records = Records::new(bytes, limits, budget); + // Excel keeps a number format with a newer locale tag in two alternate-content blocks: the + // newer spelling for itself, and for Excel 2007 the one `.xlsx` has as `formatCode`. + records.excel_2007 = true; records.first(278, warnings)?; let mut s = Styles::default(); // Inside BrtBeginCellStyleXFs (true) or BrtBeginCellXFs (false). diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index bc1a7f6bd..85643b0ee 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1014,6 +1014,8 @@ fn styles_bin() -> Vec { // character set 0 and scheme "none" are not written in `.xlsx`. let fancy = font(290, 0x0A, 700, 1, 0x22, 0, 2, &rgb(0, 128, 255), 0, "Wingdings"); let heading = font(220, 0, 400, 0, 0, 2, 0, &theme(4, 13106), 1, "Aptos Display"); + // An automatic colour: no `` in `.xlsx`. + let auto = font(220, 0x02, 400, 0, 0, 2, 0, &color(0, 64, 0, [0, 0, 0, 255]), 2, "Aptos Narrow"); let gradient = [ &0x28u32.to_le_bytes()[..], &indexed(64), @@ -1030,20 +1032,31 @@ fn styles_bin() -> Vec { let none = edge(0, &color(0, 0, 0, [0, 0, 0, 0])); let parts: Vec> = vec![ record(278, &[]), - begin(615, 1), + begin(615, 2), record(44, &[&164u16.to_le_bytes()[..], &wide(r"yyyy\-mm\-dd")].concat()), + // A format in two alternate-content blocks: the newer spelling for version 13 and later + // (skipped), and the one for versions 12 and 13, which `.xlsx` has as `formatCode`. + record(37, &[1, 0, 13, 0, 0, 0x80]), + record(44, &[&165u16.to_le_bytes()[..], &wide("[$-ja-JP-x-gannen]ge")].concat()), + record(38, &[]), + record(37, &[2, 0, 12, 0, 0, 0, 13, 0, 0, 0]), + record(44, &[&165u16.to_le_bytes()[..], &wide("[$]ge")].concat()), + record(38, &[]), record(616, &[]), - begin(611, 3), + begin(611, 4), normal, fancy, heading, + auto, record(612, &[]), - begin(603, 5), + begin(603, 6), pattern_fill(0, &indexed(64), &indexed(65)), pattern_fill(17, &indexed(64), &indexed(65)), pattern_fill(1, &rgb(255, 255, 0), &indexed(64)), pattern_fill(9, &theme(5, -8191), &indexed(65)), record(45, &gradient), + // A tinted pattern colour: `.xlsx` states the system background, as for darkGrid above. + pattern_fill(1, &theme(4, 26213), &indexed(65)), record(604, &[]), begin(613, 2), record(46, &[&[0][..], &none, &none, &none, &none, &none].concat()), @@ -1109,7 +1122,7 @@ fn styles_bin() -> Vec { parts.concat() } -const STYLES_XML: &str = r#""#; +const STYLES_XML: &str = r#""#; /// The same styles from a SpreadsheetML package. fn xml_styles(xml: &str) -> Styles {