Fonts: outlined letters kept through XLSX open and save

An XLSX font's <outline/> was never read, so outlined (hollow) letters came
back solid after a save. The model's Font gains `outline` (serde default
false, left out of JSON when off, part of the hand-written Hash), read in
read_font and written by font_xml after <strike/> as Excel orders it, for
cell fonts and differential formats. home.formatCells sets it through the
style JSON. GridCraft does not draw outlined text yet.

Split out of the ODS branch, which also maps style:text-outline onto it.
This commit is contained in:
alehel
2026-11-11 09:19:20 +00:00
parent 632e4a1bc5
commit fd664055df
6 changed files with 95 additions and 2 deletions
+12
View File
@@ -870,6 +870,18 @@ mod tests {
assert_eq!(s.execute("cell.get", json!({"cell": "A1"})).unwrap()["style"]["num_fmt"], "0.00");
}
/// Outlined letters have no Font-group command; Format Cells' style JSON sets and clears them.
#[test]
fn format_cells_sets_outlined_letters() {
let mut s = crate::Session::new();
s.new_workbook();
s.execute("cell.set", json!({"cell": "A1", "input": "x"})).unwrap();
s.execute("home.formatCells", json!({"range": "A1", "style": {"font": {"outline": true}}})).unwrap();
assert_eq!(s.execute("cell.get", json!({"cell": "A1"})).unwrap()["style"]["font"]["outline"], true);
s.execute("home.formatCells", json!({"range": "A1", "style": {"font": {"outline": false}}})).unwrap();
assert!(s.execute("cell.get", json!({"cell": "A1"})).unwrap()["style"]["font"].get("outline").is_none());
}
#[test]
fn width_units() {
assert_eq!(chars_to_points(8.43), 64.0);
+23 -1
View File
@@ -197,6 +197,10 @@ pub struct Font {
/// theme's minor font. Fonts derived from it (bold, a colour, a size) keep the link.
#[serde(default = "default_minor")]
pub minor: bool,
/// Outlined (hollow) letters: an XLSX `<outline/>`. Kept and saved; GridCraft does not
/// draw it yet (the letters render solid).
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub outline: bool,
}
fn default_minor() -> bool {
@@ -208,7 +212,7 @@ impl Hash for Font {
fn hash<H: Hasher>(&self, h: &mut H) {
self.name.hash(h);
self.size.to_bits().hash(h);
(self.bold, self.italic, self.underline, self.strike, self.color, self.vert, self.charset, self.family, self.minor).hash(h);
(self.bold, self.italic, self.underline, self.strike, self.color, self.vert, self.charset, self.family, self.minor, self.outline).hash(h);
}
}
@@ -226,6 +230,7 @@ impl Default for Font {
charset: None,
family: None,
minor: true,
outline: false,
}
}
}
@@ -499,6 +504,23 @@ mod tests {
assert!(!t.get(StyleId(999)).font.bold);
}
/// Two fonts differing only in outlined letters are different styles; JSON leaves the
/// flag out when off, and JSON saved before it existed reads as not outlined.
#[test]
fn outline_makes_a_distinct_style() -> Result<(), Box<dyn std::error::Error>> {
let mut t = StyleTable::default();
let outlined = t.derive(StyleId::DEFAULT, |s| s.font.outline = true);
assert_ne!(outlined, StyleId::DEFAULT);
assert_ne!(Font { outline: true, ..Font::default() }, Font::default());
assert_eq!(t.derive(outlined, |s| s.font.outline = false), StyleId::DEFAULT);
let json = serde_json::to_value(Font::default())?;
assert!(json.get("outline").is_none());
assert_eq!(serde_json::from_value::<Font>(json)?, Font::default());
let on = serde_json::to_value(Font { outline: true, ..Font::default() })?;
assert_eq!(on.get("outline"), Some(&serde_json::Value::Bool(true)));
Ok(())
}
#[test]
fn a_theme_saved_before_per_script_fonts_still_opens() -> Result<(), Box<dyn std::error::Error>> {
let mut json = serde_json::to_value(Theme::default())?;
+6
View File
@@ -71,6 +71,7 @@ pub fn read_font(e: &El, palette: &[u32]) -> Font {
f.bold = bool_prop(e, "b");
f.italic = bool_prop(e, "i");
f.strike = bool_prop(e, "strike");
f.outline = bool_prop(e, "outline");
if let Some(u) = e.child("u") {
f.underline = match u.attr("val").unwrap_or("single") {
"double" => Underline::Double,
@@ -436,6 +437,10 @@ fn font_xml(f: &Font, dxf: Option<&DxfStated>, minor: bool) -> String {
let _ = write!(s, "<{tag} val=\"0\"/>");
}
}
// Excel's order puts outline after strike (condense and extend aren't modelled).
if f.outline {
s.push_str("<outline/>");
}
match f.underline {
Underline::None if stated.underline => s.push_str("<u val=\"none\"/>"),
Underline::None => {}
@@ -709,6 +714,7 @@ pub fn write_styles(wb: &Workbook, dxfs: &[Style], extra_fmts: &[String]) -> (St
if d.font.bold
|| d.font.italic
|| d.font.strike
|| d.font.outline
|| d.font.underline != Underline::None
|| d.font.color != Color::Auto
|| d.font.vert != VertAlign::Baseline
+25
View File
@@ -236,6 +236,31 @@ fn dxf_font_and_parts_stated_at_their_defaults_survive_save() {
assert_eq!(written, Some(format!(r#" count="4">{dxfs}"#).as_str()));
}
/// A differential format whose only font part is outlined letters keeps it on save.
#[test]
fn dxf_outline_font_survives_save() {
let dxfs = r#"<dxf><font><outline/></font></dxf>"#;
let styles = format!(
r#"<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<fonts count="1"><font><sz val="11"/><name val="Calibri"/></font></fonts>
<fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>
<borders count="1"><border/></borders>
<cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellXfs>
<dxfs count="1">{dxfs}</dxfs>
</styleSheet>"#
);
let rels = r#"<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>"#;
let body = r#"<sheetData><row r="1"><c r="A1"><v>1</v></c></row></sheetData>
<conditionalFormatting sqref="A1"><cfRule type="cellIs" dxfId="0" priority="1" operator="greaterThan"><formula>0</formula></cfRule></conditionalFormatting>"#;
let bytes = minimal(body, &[("xl/styles.xml", &styles)], rels, "");
let (wb, _) = read_xlsx(&bytes).unwrap();
let out = crate::write_xlsx(&wb).unwrap();
let st = crate::package::Package::open(&out).unwrap().read("xl/styles.xml").unwrap().unwrap();
let st = String::from_utf8(st).unwrap();
let written = st.split_once("<dxfs").and_then(|(_, r)| r.split_once("</dxfs>")).map(|(d, _)| d);
assert_eq!(written, Some(format!(r#" count="1">{dxfs}"#).as_str()));
}
/// A dxf writes its parts in schema order: the border comes before the protection.
#[test]
fn dxf_border_is_written_before_protection() {
+27
View File
@@ -790,6 +790,33 @@ fn empty_workbook_roundtrip() {
assert!(crate::sniff(&bytes) == crate::Format::Xlsx);
}
/// Outlined (hollow) letters are kept on save and reopen, written after `<strike/>` as Excel does;
/// `<outline val="0"/>` reads as not outlined.
#[test]
fn outlined_font_survives_xlsx_roundtrip() {
let mut wb = Workbook::new();
let outlined = wb.styles.derive(StyleId::DEFAULT, |s| {
s.font.outline = true;
s.font.strike = true;
});
let sheet = wb.sheet_mut(0).unwrap();
sheet.cells.set(at("A1"), Cell { value: Value::text("hollow"), formula: None, style: outlined, py: None });
sheet.set_value(at("A2"), Value::text("solid"));
let bytes = write_xlsx(&wb).unwrap();
let styles = Package::open(&bytes).unwrap().read("xl/styles.xml").unwrap().unwrap();
let styles = String::from_utf8(styles).unwrap();
assert!(styles.contains("<strike/><outline/>"), "{styles}");
assert_eq!(styles.matches("<outline").count(), 1, "only the outlined font says so: {styles}");
let (back, _) = read_xlsx(&bytes).unwrap();
let sheet = &back.sheets[0];
let style_of = |a: &str| back.styles.get(sheet.cells.get(at(a)).map_or(StyleId::DEFAULT, |c| c.style));
assert!(style_of("A1").font.outline && style_of("A1").font.strike);
assert!(!style_of("A2").font.outline);
assert!(!crate::styles::read_font(&crate::xml::parse(br#"<font><outline val="0"/></font>"#).unwrap(), &[]).outline);
}
#[test]
fn cell_text_carriage_returns_survive_xlsx_roundtrip() {
let mut wb = Workbook::new();