From 09d52cc7cd76a434431fc85d4e09ec4383e24ecf Mon Sep 17 00:00:00 2001 From: alehel Date: Wed, 11 Nov 2026 09:02:08 +0000 Subject: [PATCH] Style model: cell padding kept in Alignment `Alignment::padding: [Option; 4]` (left, right, top, bottom, in hundredths of a point; `None` where a file states none) holds an OpenDocument cell's padding, which LibreOffice lays rows out with. It is part of style equality and hashing, so styles that differ only by padding intern apart; serde default, skipped when all `None`, so older JSON reads. Excel has no cell padding: the XLSX and XLS readers set none, and the XLSX writer leaves it out (a style that differs from the default only by padding writes no ``). GridCraft does not draw padding yet. --- crates/model/src/style.rs | 33 ++++++++++++++++++++++++++ crates/xlsx/src/styles.rs | 5 +++- crates/xlsx/src/tests/roundtrip.rs | 38 +++++++++++++++++++++++++++++- crates/xlsx/src/xls/styles.rs | 1 + docs/file-format-parity.md | 3 ++- 5 files changed, 77 insertions(+), 3 deletions(-) diff --git a/crates/model/src/style.rs b/crates/model/src/style.rs index d2f10780e0..6318c557ab 100644 --- a/crates/model/src/style.rs +++ b/crates/model/src/style.rs @@ -359,6 +359,17 @@ pub struct Alignment { pub indent: u8, /// Degrees -90..=90, or 255 for vertical stacked text. pub rotation: i16, + /// Space between the cell's edges and its text, left, right, top, bottom, in hundredths + /// of a point; `None` where the file stated none (the application's own). An OpenDocument + /// cell's padding; Excel has none. Not drawn yet. + #[serde(default, skip_serializing_if = "Alignment::no_padding")] + pub padding: [Option; 4], +} + +impl Alignment { + fn no_padding(p: &[Option; 4]) -> bool { + p.iter().all(Option::is_none) + } } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -499,6 +510,28 @@ mod tests { assert!(!t.get(StyleId(999)).font.bold); } + #[test] + fn padding_keeps_styles_apart_and_survives_json() -> Result<(), Box> { + let mut t = StyleTable::default(); + let padded = t.derive(StyleId::DEFAULT, |s| s.align.padding = [Some(10), None, Some(0), Some(65535)]); + let other = t.derive(StyleId::DEFAULT, |s| s.align.padding = [Some(10), None, Some(0), Some(1)]); + assert_ne!(padded, StyleId::DEFAULT); + assert_ne!(padded, other); + assert_eq!(t.derive(StyleId::DEFAULT, |s| s.align.padding = [Some(10), None, Some(0), Some(65535)]), padded); + assert_eq!(t.derive(padded, |s| s.align.padding = [None; 4]), StyleId::DEFAULT); + + let style = t.get(padded).clone(); + let json = serde_json::to_string(&style)?; + assert!(json.contains("padding"), "{json}"); + assert_eq!(serde_json::from_str::