Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewich08044f4df4e3
diff --git a/crates/liteparse-ooxml/src/xlsx/mod.rs b/crates/liteparse-ooxml/src/xlsx/mod.rs
index 2b9bdc1..268fee4 100644
--- a/crates/liteparse-ooxml/src/xlsx/mod.rs
+++ b/crates/liteparse-ooxml/src/xlsx/mod.rs
@@ -56,7 +56,10 @@ pub use drawings::{CellAnchor, PicAnchor, SheetPic, SheetShape};
pub use package::{SheetEntry, WorkbookPackage, walk};
pub use refs::{CellRef, RangeRef, column_label, parse_cell, parse_column, parse_range};
pub use sheet::{Cell, CellValue, ColInfo, Hyperlink, Row, Sheet, SheetStats};
-pub use styles::{Alignment, CellXf, Font, HorizontalAlign, Styles};
+pub use styles::{
+ Alignment, Border, BorderEdge, BorderStyle, CellXf, ColorKind, ColorRef, Fill, Font,
+ HorizontalAlign, INDEXED_PALETTE, PatternType, Styles,
+};
pub use text::{RichText, RunProps, TextRun, VertAlign, parse_shared_strings};
use crate::docx::error::Result;
@@ -74,6 +77,11 @@ pub struct Workbook {
/// undo that.
pub shared_strings: Vec<RichText>,
pub styles: Styles,
+ /// `xl/theme/theme1.xml`'s colour scheme, the other half of colour
+ /// resolution — see [`Workbook::resolve_color`]. `None` when the workbook
+ /// ships no theme part (0.6% of the corpus), which makes every `theme=`
+ /// reference resolve to the consumer's default rather than to black.
+ pub theme: Option<crate::model::ThemeColorScheme>,
/// Serial dates count from 1904-01-01 rather than 1899-12-30.
pub date1904: bool,
/// Sheets named in `<sheets>` that hold no cell grid — chartsheets and
@@ -215,16 +223,40 @@ pub fn read(data: &[u8]) -> Result<Workbook> {
});
}
+ // The theme is DrawingML, identical to the part DOCX and PPTX read, so
+ // the vendored parser is reused rather than re-implemented. A broken
+ // theme costs the file its `theme=` colours, never its cells.
+ let theme =
+ pkg.theme_xml.as_deref().and_then(|xml| {
+ match crate::docx::parse::theme::parse_theme(xml) {
+ Ok(t) => Some(t.color_scheme),
+ Err(e) => {
+ log::warn!("unreadable theme part: {e}");
+ None
+ }
+ }
+ });
+
Ok(Workbook {
sheets,
shared_strings,
styles,
+ theme,
date1904: pkg.date1904,
non_worksheet_sheets,
})
}
impl Workbook {
+ /// Resolve a colour reference against this workbook's palette and theme.
+ ///
+ /// `None` means *automatic* — see [`Styles::resolve_color`]; the caller
+ /// supplies the default, because it is black for text and white for a
+ /// background.
+ pub fn resolve_color(&self, color: styles::ColorRef) -> Option<[u8; 3]> {
+ self.styles.resolve_color(color, self.theme.as_ref())
+ }
+
/// The text of a cell, resolving a shared-string index against the table.
///
/// Returns `None` for a cell that holds no text — a number, a boolean, an
diff --git a/crates/liteparse-ooxml/src/xlsx/package.rs b/crates/liteparse-ooxml/src/xlsx/package.rs
index 1f0030c..011ed3a 100644
--- a/crates/liteparse-ooxml/src/xlsx/package.rs
+++ b/crates/liteparse-ooxml/src/xlsx/package.rs
@@ -43,6 +43,10 @@ pub struct WorkbookPackage {
pub sheets: Vec<SheetEntry>,
pub shared_strings_xml: Option<Vec<u8>>,
pub styles_xml: Option<Vec<u8>>,
+ /// `xl/theme/theme1.xml`, present in 99.4% of corpus workbooks. Needed
+ /// only for colour: 29.8% of the colours `styles.xml` defines are
+ /// `theme=` references, which resolve to nothing without it.
+ pub theme_xml: Option<Vec<u8>>,
/// `<workbookPr date1904="1"/>`: the epoch serial dates count from. Excel
/// for Mac wrote 1904-based workbooks for years and 123 of the corpus's
/// 1,089 workbooks come from Mac Excel, so this is not a historical
@@ -111,11 +115,13 @@ pub fn walk(data: &[u8]) -> Result<WorkbookPackage> {
};
let shared_strings_xml = part_of_type(&RelationshipType::SharedStrings);
let styles_xml = part_of_type(&RelationshipType::Styles);
+ let theme_xml = part_of_type(&RelationshipType::Theme);
Ok(WorkbookPackage {
sheets,
shared_strings_xml,
styles_xml,
+ theme_xml,
date1904: parsed.date1904,
package,
})
diff --git a/crates/liteparse-ooxml/src/xlsx/sheet.rs b/crates/liteparse-ooxml/src/xlsx/sheet.rs
index fb85320..ef4ca44 100644
--- a/crates/liteparse-ooxml/src/xlsx/sheet.rs
+++ b/crates/liteparse-ooxml/src/xlsx/sheet.rs
@@ -147,6 +147,13 @@ pub struct Sheet {
pub frozen_cols: u32,
/// `<autoFilter ref=…>`, the other explicit header signal.
pub auto_filter: Option<RangeRef>,
+ /// `<sheetView showGridLines=…>`, from the first view only. `None` means
+ /// the file did not say, which is Excel's default of *visible* — read it
+ /// through [`Sheet::gridlines_visible`] rather than unwrapping to
+ /// `false`. It matters to the raster and nothing else: 12.1% of corpus
+ /// sheets declare neither a fill nor a border anywhere, so gridlines are
+ /// the only ink holding their numbers in a grid.
+ pub show_gridlines: Option<bool>,
/// `<drawing r:id>`: the sheet's DrawingML part, scoped to the sheet's
/// own relationships. At most one per §18.3.1.36.
pub drawing_rel_id: Option<String>,
@@ -178,6 +185,11 @@ impl Sheet {
self.merges.iter().find(|m| m.contains(at))
}
+ /// Are the sheet's gridlines drawn? Undeclared means yes (§18.3.1.87).
+ pub fn gridlines_visible(&self) -> bool {
+ self.show_gridlines.unwrap_or(true)
+ }
+
/// The declared width of a column, walking the `<col>` spans.
pub fn col_width(&self, col: u32) -> Option<f64> {
self.cols
@@ -230,7 +242,16 @@ pub fn parse(name: &str, visible: bool, data: &[u8]) -> Result<Sheet> {
sheet.default_col_width = attr_parse(e, b"defaultColWidth");
sheet.default_row_height = attr_parse(e, b"defaultRowHeight");
}
- b"sheetView" => in_sheet_view = !empty,
+ b"sheetView" => {
+ // First view only, for the reason `<pane>` below is
+ // gated: later `<sheetView>` elements describe other
+ // windows onto the same sheet, and a
+ // `<customSheetView>` a saved one.
+ if sheet.show_gridlines.is_none() {
+ sheet.show_gridlines = Some(attr_bool(e, b"showGridLines", true));
+ }
+ in_sheet_view = !empty;
+ }
b"pane" if in_sheet_view => {
// `state="split"` is a draggable divider, not a frozen
// header; only frozen panes state that rows above the
diff --git a/crates/liteparse-ooxml/src/xlsx/styles.rs b/crates/liteparse-ooxml/src/xlsx/styles.rs
index fe00f65..003c352 100644
--- a/crates/liteparse-ooxml/src/xlsx/styles.rs
+++ b/crates/liteparse-ooxml/src/xlsx/styles.rs
@@ -12,8 +12,28 @@
//! by a cell's `s=`. Reading the other array yields plausible-looking
//! formats attached to the wrong cells.
//! * `<dxfs>` (differential formats, used by conditional formatting) also
-//! holds `<font>` elements. Appending those to the font list shifts every
-//! `fontId` above them.
+//! holds `<font>`, `<fill>` and `<border>` elements. Appending those to the
+//! corresponding list shifts every id above them.
+//!
+//! # Colour
+//!
+//! Fills, borders and font colours are read for the raster (a bbox consumer
+//! never asks what colour a cell is). Every one of them names its colour
+//! through the same four-way `CT_Color` indirection, and the corpus shares
+//! them out unevenly enough that skipping any one of the four is visible —
+//! `xlsx_paint_census` over 1,248 workbooks: `indexed=` 49.5% of definitions,
+//! `theme=` 29.8%, `rgb=` 16.5%, `auto=` 4.2%.
+//!
+//! Two things about that table are load-bearing:
+//!
+//! * **`indexed=` is mostly not the palette.** 93.2% of its uses are 64/65/8
+//! — "the default colour" — so [`INDEXED_PALETTE`] is a vendored constant
+//! for a 6.8% tail rather than a subsystem. It cannot be *skipped*: 4.6% of
+//! workbooks override it with their own `<indexedColors>`.
+//! * **`theme=` indices are not `clrScheme` order.** SpreadsheetML swaps
+//! 0↔1 and 2↔3 (0 = lt1/background, 1 = dk1/text). Getting it backwards
+//! inverts every themed fill in the file, which looks deliberate rather
+//! than broken. See [`Styles::resolve_color`].
use std::collections::HashMap;
@@ -21,6 +41,7 @@ use quick_xml::Reader;
use quick_xml::events::Event;
use crate::docx::error::Result;
+use crate::model::ThemeColorScheme;
use crate::xlsx::xml::{attr, attr_bool, attr_parse, local_name};
/// The format code applied when a cell names no style at all.
@@ -73,6 +94,329 @@ pub fn builtin_format(id: u32) -> Option<&'static str> {
.find_map(|&(k, code)| (k == id).then_some(code))
}
+/// §18.8.27 `<indexedColors>`: the legacy palette a colour's `indexed=`
+/// points into, in index order. Indices 64 and 65 are past this table on
+/// purpose — they are the "system foreground" / "system background"
+/// sentinels, which resolve to whatever the consumer's default is rather than
+/// to a stored colour.
+pub const INDEXED_PALETTE: [[u8; 3]; 64] = [
+ [0x00, 0x00, 0x00],
+ [0xFF, 0xFF, 0xFF],
+ [0xFF, 0x00, 0x00],
+ [0x00, 0xFF, 0x00],
+ [0x00, 0x00, 0xFF],
+ [0xFF, 0xFF, 0x00],
+ [0xFF, 0x00, 0xFF],
+ [0x00, 0xFF, 0xFF],
+ [0x00, 0x00, 0x00],
+ [0xFF, 0xFF, 0xFF],
+ [0xFF, 0x00, 0x00],
+ [0x00, 0xFF, 0x00],
+ [0x00, 0x00, 0xFF],
+ [0xFF, 0xFF, 0x00],
+ [0xFF, 0x00, 0xFF],
+ [0x00, 0xFF, 0xFF],
+ [0x80, 0x00, 0x00],
+ [0x00, 0x80, 0x00],
+ [0x00, 0x00, 0x80],
+ [0x80, 0x80, 0x00],
+ [0x80, 0x00, 0x80],
+ [0x00, 0x80, 0x80],
+ [0xC0, 0xC0, 0xC0],
+ [0x80, 0x80, 0x80],
+ [0x99, 0x99, 0xFF],
+ [0x99, 0x33, 0x66],
+ [0xFF, 0xFF, 0xCC],
+ [0xCC, 0xFF, 0xFF],
+ [0x66, 0x00, 0x66],
+ [0xFF, 0x80, 0x80],
+ [0x00, 0x66, 0xCC],
+ [0xCC, 0xCC, 0xFF],
+ [0x00, 0x00, 0x80],
+ [0xFF, 0x00, 0xFF],
+ [0xFF, 0xFF, 0x00],
+ [0x00, 0xFF, 0xFF],
+ [0x80, 0x00, 0x80],
+ [0x80, 0x00, 0x00],
+ [0x00, 0x80, 0x80],
+ [0x00, 0x00, 0xFF],
+ [0x00, 0xCC, 0xFF],
+ [0xCC, 0xFF, 0xFF],
+ [0xCC, 0xFF, 0xCC],
+ [0xFF, 0xFF, 0x99],
+ [0x99, 0xCC, 0xFF],
+ [0xFF, 0x99, 0xCC],
+ [0xCC, 0x99, 0xFF],
+ [0xFF, 0xCC, 0x99],
+ [0x33, 0x66, 0xFF],
+ [0x33, 0xCC, 0xCC],
+ [0x99, 0xCC, 0x00],
+ [0xFF, 0xCC, 0x00],
+ [0xFF, 0x99, 0x00],
+ [0xFF, 0x66, 0x00],
+ [0x66, 0x66, 0x99],
+ [0x96, 0x96, 0x96],
+ [0x00, 0x33, 0x66],
+ [0x33, 0x99, 0x66],
+ [0x00, 0x33, 0x00],
+ [0x33, 0x33, 0x00],
+ [0x99, 0x33, 0x00],
+ [0x99, 0x33, 0x66],
+ [0x33, 0x33, 0x99],
+ [0x33, 0x33, 0x33],
+];
+
+/// How a `CT_Color` (§18.3.1.15) names its colour, before resolution.
+///
+/// Kept as a reference rather than resolved at parse time because two of the
+/// four kinds need context this part does not hold: `theme=` needs
+/// `xl/theme/theme1.xml`, and `auto=`/`indexed=64` mean "the consumer's
+/// default", which differs between a font (black) and a background (white).
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum ColorKind {
+ /// `rgb="FFRRGGBB"` — the only self-contained spelling.
+ Rgb([u8; 3]),
+ /// `theme="n"`, an index into the workbook theme's colour scheme after
+ /// the SpreadsheetML swap.
+ Theme(u32),
+ /// `indexed="n"` into [`INDEXED_PALETTE`] or the file's own override.
+ Indexed(u32),
+ /// `auto="1"`: whatever the system says.
+ Auto,
+}
+
+/// A colour reference and its §18.8.19 `tint`, which lightens (positive) or
+/// darkens (negative) the resolved base in HSL luminance.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct ColorRef {
+ pub kind: ColorKind,
+ pub tint: f64,
+}
+
+impl ColorRef {
+ /// Read a `<color>`/`<fgColor>`/`<bgColor>` element, or `None` when it
+ /// names nothing this reader understands.
+ ///
+ /// Attribute order is the spec's precedence, not the file's: a producer
+ /// that writes both `rgb=` and `theme=` means the explicit one.
+ fn parse(e: &quick_xml::events::BytesStart<'_>) -> Option<Self> {
+ let tint = attr_parse(e, b"tint").unwrap_or(0.0);
+ let kind = if let Some(rgb) = attr(e, b"rgb") {
+ ColorKind::Rgb(parse_argb(&rgb)?)
+ } else if let Some(t) = attr_parse::<u32>(e, b"theme") {
+ ColorKind::Theme(t)
+ } else if let Some(i) = attr_parse::<u32>(e, b"indexed") {
+ ColorKind::Indexed(i)
+ } else if attr_bool(e, b"auto", false) {
+ ColorKind::Auto
+ } else {
+ return None;
+ };
+ Some(ColorRef { kind, tint })
+ }
+}
+
+/// `"FFRRGGBB"` — and the `"RRGGBB"` some producers write instead. The alpha
+/// byte is dropped: SpreadsheetML has no transparency model at the cell level
+/// and every corpus value writes `FF`.
+fn parse_argb(hex: &str) -> Option<[u8; 3]> {
+ let hex = hex.trim();
+ let body = match hex.len() {
+ 8 => &hex[2..],
+ 6 => hex,
+ _ => return None,
+ };
+ let v = u32::from_str_radix(body, 16).ok()?;
+ Some([(v >> 16) as u8, ((v >> 8) & 0xff) as u8, (v & 0xff) as u8])
+}
+
+/// §18.18.55 `ST_PatternType`, collapsed to what a painter does about it.
+///
+/// The collapse is measured rather than lazy: of 2,386,621 filled cells in
+/// the corpus, **2,386,298 (100.0%) are `solid`** — every other pattern type
+/// put together is 323 cells. Keeping eighteen hatch variants distinct would
+/// be a pattern engine for three ten-thousandths of a percent of the ink.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub enum PatternType {
+ /// `none`, or no `<patternFill>` at all — the cell shows the sheet
+ /// background. Slot 0 of every file.
+ #[default]
+ None,
+ /// `solid`: the whole cell takes `fg`. (Excel's own quirk, kept here so
+ /// consumers do not have to know it: a solid fill's colour is `fgColor`,
+ /// not `bgColor`.)
+ Solid,
+ /// A declared hatch (`gray125`, `darkUp`, …). A painter with no pattern
+ /// engine approximates; slot 1 of every file is `gray125` and is Excel's
+ /// "no fill" placeholder rather than a request for hatching.
+ Hatch,
+ /// `<gradientFill>`: a fill with no `patternType` at all. Recorded so a
+ /// consumer can tell "gradient we do not paint" from "no fill"; the stops
+ /// are not read.
+ Gradient,
+}
+
+/// One `<fills>` entry.
+#[derive(Clone, Debug, Default, PartialEq)]
+pub struct Fill {
+ pub pattern: PatternType,
+ /// `<fgColor>` — the colour of a `solid` fill, and of a hatch's strokes.
+ pub fg: Option<ColorRef>,
+ /// `<bgColor>` — a hatch's background. Ignored by a solid fill.
+ pub bg: Option<ColorRef>,
+}
+
+impl Fill {
+ /// Does this fill put ink down?
+ pub fn paints(&self) -> bool {
+ matches!(self.pattern, PatternType::Solid | PatternType::Hatch)
+ }
+}
+
+/// §18.18.3 `ST_BorderStyle`.
+///
+/// The full enum is kept — reading `dashDot` as `none` would erase an edge —
+/// but the corpus makes clear what has to be *right*: thin 91.6%, hair 4.3%,
+/// medium 3.9% are 99.8% of 42,885,045 declared edges, and everything
+/// dashed/dotted together is under 0.05%.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub enum BorderStyle {
+ #[default]
+ None,
+ Hair,
+ Thin,
+ Medium,
+ Thick,
+ Double,
+ Dotted,
+ Dashed,
+ DashDot,
+ DashDotDot,
+ MediumDashed,
+ MediumDashDot,
+ MediumDashDotDot,
+ SlantDashDot,
+}
+
+impl BorderStyle {
+ fn parse(s: &str) -> Self {
+ match s {
+ "hair" => BorderStyle::Hair,
+ "thin" => BorderStyle::Thin,
+ "medium" => BorderStyle::Medium,
+ "thick" => BorderStyle::Thick,
+ "double" => BorderStyle::Double,
+ "dotted" => BorderStyle::Dotted,
+ "dashed" => BorderStyle::Dashed,
+ "dashDot" => BorderStyle::DashDot,
+ "dashDotDot" => BorderStyle::DashDotDot,
+ "mediumDashed" => BorderStyle::MediumDashed,
+ "mediumDashDot" => BorderStyle::MediumDashDot,
+ "mediumDashDotDot" => BorderStyle::MediumDashDotDot,
+ "slantDashDot" => BorderStyle::SlantDashDot,
+ _ => BorderStyle::None,
+ }
+ }
+
+ /// Stroke width in points. Excel draws these in device pixels at 96 DPI —
+ /// hair is sub-pixel, thin 1 px, medium 2 px, thick 3 px — so the point
+ /// values are those pixel widths at 0.75 pt each.
+ pub fn width_pt(self) -> f32 {
+ match self {
+ BorderStyle::None => 0.0,
+ BorderStyle::Hair => 0.5,
+ BorderStyle::Thin
+ | BorderStyle::Dotted
+ | BorderStyle::Dashed
+ | BorderStyle::DashDot
+ | BorderStyle::DashDotDot
+ | BorderStyle::Double => 0.75,
+ BorderStyle::Medium
+ | BorderStyle::MediumDashed
+ | BorderStyle::MediumDashDot
+ | BorderStyle::MediumDashDotDot
+ | BorderStyle::SlantDashDot => 1.5,
+ BorderStyle::Thick => 2.25,
+ }
+ }
+
+ /// Is this a broken line? A painter with no dash support draws it solid,
+ /// which is the right approximation for 0.05% of edges.
+ pub fn is_dashed(self) -> bool {
+ matches!(
+ self,
+ BorderStyle::Dotted
+ | BorderStyle::Dashed
+ | BorderStyle::DashDot
+ | BorderStyle::DashDotDot
+ | BorderStyle::MediumDashed
+ | BorderStyle::MediumDashDot
+ | BorderStyle::MediumDashDotDot
+ | BorderStyle::SlantDashDot
+ )
+ }
+
+ pub fn paints(self) -> bool {
+ !matches!(self, BorderStyle::None)
+ }
+}
+
+/// One edge of a `<border>`: its style and, when it names one, its colour.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+pub struct BorderEdge {
+ pub style: BorderStyle,
+ pub color: Option<ColorRef>,
+}
+
+/// One `<borders>` entry.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+pub struct Border {
+ pub left: BorderEdge,
+ pub right: BorderEdge,
+ pub top: BorderEdge,
+ pub bottom: BorderEdge,
+ /// The diagonal is drawn only in the directions the flags below name; an
+ /// edge with neither flag set is inert.
+ pub diagonal: BorderEdge,
+ pub diagonal_up: bool,
+ pub diagonal_down: bool,
+}
+
+impl Border {
+ /// Does this border put ink down? The diagonal counts only when a
+ /// direction flag says which way it runs.
+ pub fn paints(&self) -> bool {
+ self.left.style.paints()
+ || self.right.style.paints()
+ || self.top.style.paints()
+ || self.bottom.style.paints()
+ || (self.diagonal.style.paints() && (self.diagonal_up || self.diagonal_down))
+ }
+}
+
+/// Which edge of a [`Border`] an element writes into — the parse-time half of
+/// the five named fields, so the reader carries a slot instead of a name.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum EdgeSlot {
+ Left,
+ Right,
+ Top,
+ Bottom,
+ Diagonal,
+}
+
+impl EdgeSlot {
+ fn of(self, border: &mut Border) -> &mut BorderEdge {
+ match self {
+ EdgeSlot::Left => &mut border.left,
+ EdgeSlot::Right => &mut border.right,
+ EdgeSlot::Top => &mut border.top,
+ EdgeSlot::Bottom => &mut border.bottom,
+ EdgeSlot::Diagonal => &mut border.diagonal,
+ }
+ }
+}
+
/// Character formatting from `<fonts>`. Mirrors
/// [`RunProps`](crate::xlsx::text::RunProps), which is the run-level version of
/// the same properties — a cell takes this one, and a rich run overrides it.
@@ -84,6 +428,9 @@ pub struct Font {
pub underline: bool,
pub size: Option<f32>,
pub name: Option<String>,
+ /// `<color>`: unresolved, for the reason [`ColorRef`] documents. Absent
+ /// means the same thing `auto` does — the consumer's default text colour.
+ pub color: Option<ColorRef>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -117,6 +464,8 @@ pub struct Alignment {
pub struct CellXf {
pub num_fmt_id: u32,
pub font_id: u32,
+ pub fill_id: u32,
+ pub border_id: u32,
pub alignment: Alignment,
/// `quotePrefix="1"`: the user typed a leading apostrophe to force text.
/// The apostrophe is not in the cell value, so it must not be emitted, but
@@ -135,6 +484,11 @@ pub struct Styles {
custom_formats: HashMap<u32, String>,
cell_xfs: Vec<CellXf>,
fonts: Vec<Font>,
+ fills: Vec<Fill>,
+ borders: Vec<Border>,
+ /// `<indexedColors>` when the file replaces the legacy palette — 4.6% of
+ /// corpus workbooks do. `None` means [`INDEXED_PALETTE`] stands.
+ indexed_colors: Option<Vec<[u8; 3]>>,
}
impl Styles {
@@ -143,12 +497,26 @@ impl Styles {
let mut out = Styles::default();
let mut reader = Reader::from_reader(data);
let mut buf = Vec::new();
- // Which array we are inside. `<xf>` and `<font>` each appear under
- // more than one parent; see the module docs.
+ // Which array we are inside. `<xf>`, `<font>`, `<fill>` and
+ // `<border>` each appear under more than one parent; see the module
+ // docs.
let mut in_cell_xfs = false;
let mut in_fonts = false;
+ let mut in_fills = false;
+ let mut in_borders = false;
+ let mut in_indexed_colors = false;
+ // Inside a `<patternFill>`, where `<fgColor>`/`<bgColor>` live. A
+ // `<gradientFill>`'s stops carry `<color>` children too, and they must
+ // not be read as the pattern's.
+ let mut in_pattern = false;
let mut current_xf: Option<CellXf> = None;
let mut current_font: Option<Font> = None;
+ let mut current_fill: Option<Fill> = None;
+ let mut current_border: Option<Border> = None;
+ // The edge element being read, so a nested `<color>` lands on it. The
+ // slot is remembered rather than the edge written eagerly, because an
+ // edge's colour arrives after its style attribute.
+ let mut current_edge: Option<(EdgeSlot, BorderEdge)> = None;
loop {
let event = reader
@@ -162,6 +530,10 @@ impl Styles {
match local_name(e.name().as_ref()) {
b"cellXfs" => in_cell_xfs = false,
b"fonts" => in_fonts = false,
+ b"fills" => in_fills = false,
+ b"borders" => in_borders = false,
+ b"indexedColors" => in_indexed_colors = false,
+ b"patternFill" => in_pattern = false,
b"xf" => {
if let Some(xf) = current_xf.take() {
out.cell_xfs.push(xf);
@@ -172,6 +544,23 @@ impl Styles {
out.fonts.push(font);
}
}
+ b"fill" => {
+ if let Some(fill) = current_fill.take() {
+ out.fills.push(fill);
+ }
+ }
+ b"border" => {
+ if let Some(border) = current_border.take() {
+ out.borders.push(border);
+ }
+ }
+ b"left" | b"right" | b"top" | b"bottom" | b"diagonal" => {
+ if let (Some(border), Some((slot, edge))) =
+ (current_border.as_mut(), current_edge.take())
+ {
+ *slot.of(border) = edge;
+ }
+ }
_ => {}
}
buf.clear();
@@ -187,6 +576,23 @@ impl Styles {
match local_name(e.name().as_ref()) {
b"cellXfs" => in_cell_xfs = !empty,
b"fonts" => in_fonts = !empty,
+ b"fills" => in_fills = !empty,
+ b"borders" => in_borders = !empty,
+ b"indexedColors" => {
+ in_indexed_colors = !empty;
+ // The override replaces the palette wholesale, so an
+ // empty list is still an override — start it here rather
+ // than on the first `<rgbColor>`.
+ out.indexed_colors = Some(Vec::new());
+ }
+ b"rgbColor" if in_indexed_colors => {
+ if let (Some(list), Some(rgb)) = (
+ out.indexed_colors.as_mut(),
+ attr(e, b"rgb").as_deref().and_then(parse_argb),
+ ) {
+ list.push(rgb);
+ }
+ }
b"numFmt" => {
// `<numFmt>` appears both in the workbook-level `<numFmts>`
// and inside `<dxf>`. Collecting both is harmless — they
@@ -202,6 +608,8 @@ impl Styles {
let xf = CellXf {
num_fmt_id: attr_parse(e, b"numFmtId").unwrap_or(0),
font_id: attr_parse(e, b"fontId").unwrap_or(0),
+ fill_id: attr_parse(e, b"fillId").unwrap_or(0),
+ border_id: attr_parse(e, b"borderId").unwrap_or(0),
alignment: Alignment::default(),
quote_prefix: attr_bool(e, b"quotePrefix", false),
};
@@ -241,6 +649,86 @@ impl Styles {
current_font = Some(font);
}
}
+ b"fill" if in_fills => {
+ if empty {
+ out.fills.push(Fill::default());
+ } else {
+ current_fill = Some(Fill::default());
+ }
+ }
+ b"patternFill" if current_fill.is_some() => {
+ if let Some(fill) = current_fill.as_mut() {
+ fill.pattern = match attr(e, b"patternType").as_deref() {
+ Some("solid") => PatternType::Solid,
+ // No `patternType` on a `<patternFill>` means
+ // `none` (§18.8.32), not "unknown".
+ None | Some("none") => PatternType::None,
+ Some(_) => PatternType::Hatch,
+ };
+ }
+ in_pattern = !empty;
+ }
+ b"gradientFill" if current_fill.is_some() => {
+ if let Some(fill) = current_fill.as_mut() {
+ fill.pattern = PatternType::Gradient;
+ }
+ }
+ b"fgColor" | b"bgColor" if in_pattern => {
+ let color = ColorRef::parse(e);
+ if let Some(fill) = current_fill.as_mut() {
+ if local_name(e.name().as_ref()) == b"fgColor" {
+ fill.fg = color;
+ } else {
+ fill.bg = color;
+ }
+ }
+ }
+ b"border" if in_borders => {
+ let border = Border {
+ diagonal_up: attr_bool(e, b"diagonalUp", false),
+ diagonal_down: attr_bool(e, b"diagonalDown", false),
+ ..Border::default()
+ };
+ if empty {
+ out.borders.push(border);
+ } else {
+ current_border = Some(border);
+ }
+ }
+ b"left" | b"right" | b"top" | b"bottom" | b"diagonal"
+ if current_border.is_some() =>
+ {
+ let slot = match local_name(e.name().as_ref()) {
+ b"left" => EdgeSlot::Left,
+ b"right" => EdgeSlot::Right,
+ b"top" => EdgeSlot::Top,
+ b"bottom" => EdgeSlot::Bottom,
+ _ => EdgeSlot::Diagonal,
+ };
+ let edge = BorderEdge {
+ style: attr(e, b"style")
+ .map_or(BorderStyle::None, |s| BorderStyle::parse(&s)),
+ color: None,
+ };
+ if empty {
+ if let Some(border) = current_border.as_mut() {
+ *slot.of(border) = edge;
+ }
+ } else {
+ current_edge = Some((slot, edge));
+ }
+ }
+ // A `<color>` inside an edge belongs to that edge; one inside
+ // a `<font>` to that font. The two never nest, so the edge
+ // wins whenever one is open.
+ b"color" => {
+ let color = ColorRef::parse(e);
+ if let Some((_, edge)) = current_edge.as_mut() {
+ edge.color = color;
+ } else if in_fonts && let Some(font) = current_font.as_mut() {
+ font.color = color;
+ }
+ }
name if current_font.is_some() => {
let font = current_font.as_mut().expect("checked by the guard");
// Same CT_BooleanProperty rule as run properties: an
@@ -305,6 +793,100 @@ impl Styles {
.unwrap_or_default()
}
+ /// The fill for a cell's `s=` index. A missing link resolves to "no
+ /// fill", which is what slot 0 of every file holds anyway.
+ pub fn fill(&self, style_index: Option<u32>) -> Fill {
+ self.cell_xf(style_index)
+ .and_then(|xf| self.fills.get(xf.fill_id as usize))
+ .cloned()
+ .unwrap_or_default()
+ }
+
+ /// The border for a cell's `s=` index; a missing link is no border.
+ pub fn border(&self, style_index: Option<u32>) -> Border {
+ self.cell_xf(style_index)
+ .and_then(|xf| self.borders.get(xf.border_id as usize))
+ .copied()
+ .unwrap_or_default()
+ }
+
+ /// Resolve a colour reference to RGB, or `None` for "the consumer's
+ /// default".
+ ///
+ /// `None` is returned rather than black for every flavour of *automatic*
+ /// — `auto="1"`, `indexed=64`/`65`, an unresolvable theme index — because
+ /// the default differs by consumer: automatic text is black, an automatic
+ /// background is white, and collapsing them here would paint white text
+ /// on white cells. The census makes that concrete: 35.1% of colour-
+ /// carrying cells name an automatic colour.
+ ///
+ /// The theme mapping is the one that is easy to get wrong.
+ /// SpreadsheetML's indices are **not** `<a:clrScheme>` document order:
+ /// 0↔1 and 2↔3 are swapped, so index 0 is `lt1` (the background) and
+ /// index 1 is `dk1` (the text). Painting a file with them transposed
+ /// inverts every themed fill.
+ pub fn resolve_color(
+ &self,
+ color: ColorRef,
+ theme: Option<&ThemeColorScheme>,
+ ) -> Option<[u8; 3]> {
+ let base = match color.kind {
+ ColorKind::Rgb(rgb) => rgb,
+ ColorKind::Auto => return None,
+ ColorKind::Indexed(i) => self.indexed_color(i)?,
+ ColorKind::Theme(i) => {
+ let scheme = theme?;
+ let packed = match i {
+ 0 => scheme.light1,
+ 1 => scheme.dark1,
+ 2 => scheme.light2,
+ 3 => scheme.dark2,
+ 4 => scheme.accent1,
+ 5 => scheme.accent2,
+ 6 => scheme.accent3,
+ 7 => scheme.accent4,
+ 8 => scheme.accent5,
+ 9 => scheme.accent6,
+ 10 => scheme.hyperlink,
+ 11 => scheme.followed_hyperlink,
+ _ => return None,
+ };
+ [
+ (packed >> 16) as u8,
+ ((packed >> 8) & 0xff) as u8,
+ (packed & 0xff) as u8,
+ ]
+ }
+ };
+ Some(apply_tint(base, color.tint))
+ }
+
+ /// The palette entry an `indexed=` names — the file's own
+ /// `<indexedColors>` when it declares one, else [`INDEXED_PALETTE`].
+ ///
+ /// 64 and 65 (system foreground/background) and anything past the table
+ /// are `None`: they are automatic, not stored colours. That pair is
+ /// 89.4% of every `indexed=` in the corpus, which is why the palette
+ /// itself is a small tail rather than the main path.
+ pub fn indexed_color(&self, index: u32) -> Option<[u8; 3]> {
+ let i = index as usize;
+ match self.indexed_colors.as_deref() {
+ // An override shorter than the index falls back to the legacy
+ // table rather than to nothing: producers that write a partial
+ // `<indexedColors>` mean "these differ", not "the rest are gone".
+ Some(list) => list
+ .get(i)
+ .copied()
+ .or_else(|| INDEXED_PALETTE.get(i).copied()),
+ None => INDEXED_PALETTE.get(i).copied(),
+ }
+ }
+
+ /// Does the file replace the legacy palette? 4.6% of corpus workbooks do.
+ pub fn overrides_indexed_palette(&self) -> bool {
+ self.indexed_colors.is_some()
+ }
+
pub fn alignment(&self, style_index: Option<u32>) -> Alignment {
self.cell_xf(style_index)
.map(|xf| xf.alignment.clone())
@@ -323,6 +905,87 @@ impl Styles {
pub fn font_count(&self) -> usize {
self.fonts.len()
}
+
+ pub fn fill_count(&self) -> usize {
+ self.fills.len()
+ }
+
+ pub fn border_count(&self) -> usize {
+ self.borders.len()
+ }
+}
+
+/// §18.8.19 `tint`: shift a colour's HSL luminance toward white (positive) or
+/// black (negative), leaving hue and saturation alone.
+///
+/// The HSL round-trip matters rather than a straight RGB lerp: a themed
+/// accent tinted in RGB drifts toward grey as it lightens, and the corpus
+/// tints accents constantly — banded table styles are a single accent at four
+/// tints, and drift makes the bands read as different colours.
+fn apply_tint(base: [u8; 3], tint: f64) -> [u8; 3] {
+ if tint == 0.0 || !tint.is_finite() {
+ return base;
+ }
+ let (h, s, l) = rgb_to_hsl(base);
+ let tint = tint.clamp(-1.0, 1.0);
+ let l = if tint < 0.0 {
+ l * (1.0 + tint)
+ } else {
+ l * (1.0 - tint) + tint
+ };
+ hsl_to_rgb(h, s, l)
+}
+
+fn rgb_to_hsl([r, g, b]: [u8; 3]) -> (f64, f64, f64) {
+ let (r, g, b) = (r as f64 / 255.0, g as f64 / 255.0, b as f64 / 255.0);
+ let max = r.max(g).max(b);
+ let min = r.min(g).min(b);
+ let l = (max + min) / 2.0;
+ let d = max - min;
+ if d == 0.0 {
+ return (0.0, 0.0, l);
+ }
+ let s = if l > 0.5 {
+ d / (2.0 - max - min)
+ } else {
+ d / (max + min)
+ };
+ let h = if max == r {
+ ((g - b) / d).rem_euclid(6.0)
+ } else if max == g {
+ (b - r) / d + 2.0
+ } else {
+ (r - g) / d + 4.0
+ } / 6.0;
+ (h, s, l)
+}
+
+fn hsl_to_rgb(h: f64, s: f64, l: f64) -> [u8; 3] {
+ let l = l.clamp(0.0, 1.0);
+ if s == 0.0 {
+ let v = (l * 255.0).round() as u8;
+ return [v, v, v];
+ }
+ let q = if l < 0.5 {
+ l * (1.0 + s)
+ } else {
+ l + s - l * s
+ };
+ let p = 2.0 * l - q;
+ let channel = |t: f64| -> u8 {
+ let t = t.rem_euclid(1.0);
+ let v = if t < 1.0 / 6.0 {
+ p + (q - p) * 6.0 * t
+ } else if t < 0.5 {
+ q
+ } else if t < 2.0 / 3.0 {
+ p + (q - p) * (2.0 / 3.0 - t) * 6.0
+ } else {
+ p
+ };
+ (v.clamp(0.0, 1.0) * 255.0).round() as u8
+ };
+ [channel(h + 1.0 / 3.0), channel(h), channel(h - 1.0 / 3.0)]
}
#[cfg(test)]
@@ -335,21 +998,52 @@ mod tests {
<numFmt numFmtId="164" formatCode="&quot;$&quot;#,##0.00"/>
<numFmt numFmtId="165" formatCode="General"/>
</numFmts>
- <fonts count="3">
+ <fonts count="4">
<font><sz val="11"/><name val="Calibri"/></font>
<font><b/><sz val="14"/><name val="Arial"/></font>
<font><b val="0"/><i/></font>
+ <font><color theme="1"/><sz val="11"/></font>
</fonts>
<cellStyleXfs count="1">
<xf numFmtId="9" fontId="1"/>
</cellStyleXfs>
- <cellXfs count="4">
- <xf numFmtId="0" fontId="0"/>
- <xf numFmtId="164" fontId="1" applyNumberFormat="1"/>
+ <fills count="4">
+ <fill><patternFill patternType="none"/></fill>
+ <fill><patternFill patternType="gray125"/></fill>
+ <fill><patternFill patternType="solid">
+ <fgColor rgb="FFFFC000"/><bgColor indexed="64"/>
+ </patternFill></fill>
+ <fill><patternFill patternType="solid">
+ <fgColor theme="4" tint="0.3999755851924192"/><bgColor indexed="65"/>
+ </patternFill></fill>
+ </fills>
+ <borders count="3">
+ <border><left/><right/><top/><bottom/><diagonal/></border>
+ <border>
+ <left style="thin"><color indexed="8"/></left>
+ <right style="medium"><color rgb="FF00B050"/></right>
+ <top style="hair"/>
+ <bottom style="double"><color theme="1"/></bottom>
+ <diagonal/>
+ </border>
+ <border diagonalUp="1"><diagonal style="thin"/></border>
+ </borders>
+ <cellXfs count="5">
+ <xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
+ <xf numFmtId="164" fontId="1" fillId="2" borderId="1" applyNumberFormat="1"/>
<xf numFmtId="3" fontId="2" quotePrefix="1"/>
<xf numFmtId="0" fontId="0"><alignment horizontal="center" wrapText="1" indent="2"/></xf>
+ <xf numFmtId="0" fontId="3" fillId="3" borderId="2"/>
</cellXfs>
- <dxfs count="1"><dxf><font><b/></font></dxf></dxfs>
+ <dxfs count="1"><dxf>
+ <font><b/><color rgb="FF9C0006"/></font>
+ <fill><patternFill patternType="solid"><fgColor rgb="FFFFC7CE"/></patternFill></fill>
+ <border><left style="thick"/></border>
+ </dxf></dxfs>
+ <colors><indexedColors>
+ <rgbColor rgb="00FF0000"/>
+ <rgbColor rgb="0000FF00"/>
+ </indexedColors></colors>
</styleSheet>"#;
fn styles() -> Styles {
@@ -363,7 +1057,7 @@ mod tests {
#[test]
fn cell_style_xfs_do_not_leak_into_cell_xfs() {
let s = styles();
- assert_eq!(s.cell_xf_count(), 4);
+ assert_eq!(s.cell_xf_count(), 5);
assert_eq!(s.format_code(Some(0)), "General");
assert_eq!(s.format_code(Some(1)), "\"$\"#,##0.00");
assert_eq!(s.format_code(Some(2)), "#,##0");
@@ -376,7 +1070,7 @@ mod tests {
#[test]
fn dxf_fonts_do_not_shift_the_font_ids() {
let s = styles();
- assert_eq!(s.font_count(), 3);
+ assert_eq!(s.font_count(), 4);
assert_eq!(s.font(Some(1)).name.as_deref(), Some("Arial"));
assert!(s.font(Some(1)).bold);
}
@@ -434,6 +1128,254 @@ mod tests {
assert_eq!(s.font(Some(7)), Font::default());
}
+ /// A theme scheme with distinguishable slots, so a transposed index is a
+ /// wrong *colour* rather than a wrong shade.
+ fn theme() -> ThemeColorScheme {
+ ThemeColorScheme {
+ dark1: 0x00_00_00,
+ light1: 0xFF_FF_FF,
+ dark2: 0x44_54_6A,
+ light2: 0xE7_E6_E6,
+ accent1: 0x44_72_C4,
+ accent2: 0xED_7D_31,
+ accent3: 0xA5_A5_A5,
+ accent4: 0xFF_C0_00,
+ accent5: 0x5B_9B_D5,
+ accent6: 0x70_AD_47,
+ hyperlink: 0x05_63_C1,
+ followed_hyperlink: 0x95_4F_72,
+ }
+ }
+
+ /// `<dxfs>` holds `<fill>` and `<border>` children as well as `<font>`;
+ /// counting them shifts every `fillId`/`borderId` above the insertion
+ /// point, the same silent failure the font list has.
+ #[test]
+ fn dxf_fills_and_borders_do_not_shift_the_ids() {
+ let s = styles();
+ assert_eq!(s.fill_count(), 4, "the dxf's solid fill must not be here");
+ assert_eq!(s.border_count(), 3, "nor its thick left edge");
+ // Fill 2 is the yellow one; the dxf's pink would land at 4.
+ assert_eq!(
+ s.fill(Some(1)).fg.map(|c| c.kind),
+ Some(ColorKind::Rgb([0xFF, 0xC0, 0x00]))
+ );
+ }
+
+ #[test]
+ fn a_solid_fill_takes_its_colour_from_fg_not_bg() {
+ let fill = styles().fill(Some(1));
+ assert_eq!(fill.pattern, PatternType::Solid);
+ assert!(fill.paints());
+ // `bgColor indexed="64"` is the automatic sentinel that accompanies
+ // nearly every solid fill; reading it as the fill colour would paint
+ // the sheet in system background.
+ assert_eq!(fill.bg.map(|c| c.kind), Some(ColorKind::Indexed(64)));
+ assert_eq!(styles().resolve_color(fill.bg.unwrap(), None), None);
+ }
+
+ #[test]
+ fn the_placeholder_patterns_do_not_paint() {
+ let s = styles();
+ // Slot 0 (`none`) is genuinely nothing; slot 1 (`gray125`) is Excel's
+ // boilerplate, present in every file and used by almost none.
+ assert!(!s.fill(Some(0)).paints());
+ assert_eq!(s.fill(Some(0)).pattern, PatternType::None);
+ }
+
+ #[test]
+ fn border_edges_keep_their_style_and_their_own_colour() {
+ let b = styles().border(Some(1));
+ assert_eq!(b.left.style, BorderStyle::Thin);
+ assert_eq!(b.left.color.map(|c| c.kind), Some(ColorKind::Indexed(8)));
+ assert_eq!(b.right.style, BorderStyle::Medium);
+ assert_eq!(
+ b.right.color.map(|c| c.kind),
+ Some(ColorKind::Rgb([0x00, 0xB0, 0x50]))
+ );
+ // An edge with a style and no `<color>` child is still an edge.
+ assert_eq!(b.top.style, BorderStyle::Hair);
+ assert_eq!(b.top.color, None);
+ assert_eq!(b.bottom.style, BorderStyle::Double);
+ assert_eq!(b.bottom.color.map(|c| c.kind), Some(ColorKind::Theme(1)));
+ // A colour must not leak from one edge onto the next.
+ assert_eq!(b.diagonal, BorderEdge::default());
+ assert!(b.paints());
+ }
+
+ /// A diagonal with no direction flag draws nothing — Excel stores the
+ /// style and the direction separately.
+ #[test]
+ fn a_diagonal_needs_a_direction_to_paint() {
+ let b = styles().border(Some(4));
+ assert_eq!(b.diagonal.style, BorderStyle::Thin);
+ assert!(b.diagonal_up && !b.diagonal_down);
+ assert!(b.paints());
+ let inert = Border {
+ diagonal: BorderEdge {
+ style: BorderStyle::Thin,
+ color: None,
+ },
+ ..Border::default()
+ };
+ assert!(!inert.paints());
+ }
+
+ #[test]
+ fn a_font_colour_is_read_and_does_not_leak_between_fonts() {
+ let s = styles();
+ assert_eq!(
+ s.font(Some(4)).color.map(|c| c.kind),
+ Some(ColorKind::Theme(1))
+ );
+ // The dxf font's red must not have been appended, and an uncoloured
+ // font must not inherit the previous one's colour.
+ assert_eq!(s.font(Some(0)).color, None);
+ assert_eq!(s.font(Some(1)).color, None);
+ }
+
+ /// The headline colour trap: SpreadsheetML theme indices are not
+ /// `clrScheme` order. Index 0 is the *background*, index 1 the text.
+ #[test]
+ fn theme_indices_are_swapped_against_the_colour_scheme() {
+ let s = styles();
+ let at = |i: u32| {
+ s.resolve_color(
+ ColorRef {
+ kind: ColorKind::Theme(i),
+ tint: 0.0,
+ },
+ Some(&theme()),
+ )
+ };
+ assert_eq!(at(0), Some([0xFF, 0xFF, 0xFF]), "0 is lt1, not dk1");
+ assert_eq!(at(1), Some([0x00, 0x00, 0x00]), "1 is dk1, not lt1");
+ assert_eq!(at(2), Some([0xE7, 0xE6, 0xE6]), "2 is lt2");
+ assert_eq!(at(3), Some([0x44, 0x54, 0x6A]), "3 is dk2");
+ assert_eq!(at(4), Some([0x44, 0x72, 0xC4]), "4 is accent1");
+ assert_eq!(at(9), Some([0x70, 0xAD, 0x47]), "9 is accent6");
+ assert_eq!(at(10), Some([0x05, 0x63, 0xC1]), "10 is hlink");
+ assert_eq!(at(12), None, "past the scheme is automatic, not black");
+ }
+
+ /// Without the theme part, a themed colour is *automatic* rather than
+ /// black — resolving it to black would paint white-on-black cells in the
+ /// 0.6% of workbooks that ship no theme.
+ #[test]
+ fn a_themed_colour_with_no_theme_is_automatic() {
+ assert_eq!(
+ styles().resolve_color(
+ ColorRef {
+ kind: ColorKind::Theme(4),
+ tint: 0.0
+ },
+ None
+ ),
+ None
+ );
+ }
+
+ /// "Accent1, Lighter 40%" — the single most common tint in the corpus,
+ /// since it is what every banded table style is built from. Excel
+ /// renders `4472C4` at `tint=0.4` as `8EA9DB`.
+ ///
+ /// Matched to ±1 per channel rather than exactly: Excel converts through
+ /// an *integer* HLS space (luminance in 0..240 steps), so its answer is
+ /// its own rounding of the same curve. The tolerance is on the
+ /// quantization, not on the algorithm — an RGB lerp lands 6 off on the
+ /// blue channel here and drifts further as saturation rises, which is
+ /// what makes a four-tint band read as four different colours.
+ #[test]
+ fn tint_lightens_in_hsl() {
+ let s = styles();
+ let tinted = |t: f64| {
+ s.resolve_color(
+ ColorRef {
+ kind: ColorKind::Theme(4),
+ tint: t,
+ },
+ Some(&theme()),
+ )
+ .unwrap()
+ };
+ let near = |got: [u8; 3], want: [u8; 3]| {
+ assert!(
+ (0..3).all(|i| got[i].abs_diff(want[i]) <= 1),
+ "{got:02X?} is not within a rounding step of {want:02X?}"
+ );
+ };
+ near(tinted(0.3999755851924192), [0x8E, 0xA9, 0xDB]);
+ // Excel's "Accent1, Darker 25%", which this path reproduces exactly.
+ assert_eq!(tinted(-0.249977111117893), [0x2F, 0x55, 0x97]);
+ // Tint 0 must be the untouched base — no round-trip drift.
+ assert_eq!(tinted(0.0), [0x44, 0x72, 0xC4]);
+ }
+
+ /// 93.2% of `indexed=` is 64/65/8 — the automatic sentinels and black.
+ /// Resolving 64 to palette-position-64 (which does not exist) or to
+ /// black would repaint a third of the corpus's colour-carrying cells.
+ #[test]
+ fn the_automatic_indexed_sentinels_resolve_to_nothing() {
+ let s = styles();
+ assert_eq!(s.indexed_color(64), None);
+ assert_eq!(s.indexed_color(65), None);
+ assert_eq!(s.indexed_color(999), None);
+ }
+
+ /// The file's own `<indexedColors>` wins where it defines an entry, and
+ /// the legacy table carries the rest.
+ #[test]
+ fn an_indexed_colours_override_replaces_the_palette_entries_it_defines() {
+ let s = styles();
+ assert!(s.overrides_indexed_palette());
+ assert_eq!(s.indexed_color(0), Some([0xFF, 0x00, 0x00]));
+ assert_eq!(s.indexed_color(1), Some([0x00, 0xFF, 0x00]));
+ // Past the override, the vendored palette still answers.
+ assert_eq!(s.indexed_color(22), Some([0xC0, 0xC0, 0xC0]));
+ // A file with no `<colors>` gets the palette unchanged.
+ let plain = Styles::default();
+ assert!(!plain.overrides_indexed_palette());
+ assert_eq!(plain.indexed_color(0), Some([0x00, 0x00, 0x00]));
+ }
+
+ #[test]
+ fn rgb_is_read_with_or_without_its_alpha_byte() {
+ assert_eq!(parse_argb("FF00B050"), Some([0x00, 0xB0, 0x50]));
+ assert_eq!(parse_argb("00B050"), Some([0x00, 0xB0, 0x50]));
+ assert_eq!(parse_argb("nope"), None);
+ }
+
+ /// The three styles that are 99.8% of the corpus's edges must differ in
+ /// width, or every table reads as one weight.
+ #[test]
+ fn border_widths_separate_the_three_common_styles() {
+ assert!(BorderStyle::Hair.width_pt() < BorderStyle::Thin.width_pt());
+ assert!(BorderStyle::Thin.width_pt() < BorderStyle::Medium.width_pt());
+ assert!(BorderStyle::Medium.width_pt() < BorderStyle::Thick.width_pt());
+ assert!(!BorderStyle::None.paints());
+ assert!(BorderStyle::DashDot.is_dashed() && !BorderStyle::Thin.is_dashed());
+ // An unknown style must be inert rather than a default-width line.
+ assert_eq!(BorderStyle::parse("mediumWavy"), BorderStyle::None);
+ }
+
+ #[test]
+ fn a_gradient_fill_is_recorded_but_not_a_pattern() {
+ let s = Styles::parse(
+ br#"<styleSheet><fills count="1"><fill><gradientFill degree="90">
+ <stop position="0"><color rgb="FFFF0000"/></stop>
+ <stop position="1"><color rgb="FF0000FF"/></stop>
+ </gradientFill></fill></fills>
+ <cellXfs count="1"><xf fillId="0"/></cellXfs></styleSheet>"#,
+ )
+ .unwrap();
+ let fill = s.fill(Some(0));
+ assert_eq!(fill.pattern, PatternType::Gradient);
+ // The stops' `<color>` children must not be mistaken for the
+ // pattern's fg/bg.
+ assert_eq!(fill.fg, None);
+ assert_eq!(fill.bg, None);
+ }
+
#[test]
fn builtin_table_omits_reserved_ids() {
assert_eq!(builtin_format(14), Some("mm-dd-yy"));