diff --git a/Cargo.lock b/Cargo.lock index 56e5fbf5f..d51d6ce04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1329,6 +1329,7 @@ name = "deckcraft-pptx" version = "0.19.0" dependencies = [ "craft-cfb", + "craft-drawingml", "craft-opc", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10)", "deckcraft-color", diff --git a/crates/pptx/Cargo.toml b/crates/pptx/Cargo.toml index 7e66abf13..aceba9f72 100644 --- a/crates/pptx/Cargo.toml +++ b/crates/pptx/Cargo.toml @@ -12,6 +12,7 @@ workspace = true craft-opc.workspace = true craft-xml.workspace = true craft-cfb.workspace = true +craft-drawingml.workspace = true deckcraft-model = { workspace = true } deckcraft-geom = { workspace = true } deckcraft-color = { workspace = true } diff --git a/crates/pptx/src/tables.rs b/crates/pptx/src/tables.rs index 2f627796a..62dc983c4 100644 --- a/crates/pptx/src/tables.rs +++ b/crates/pptx/src/tables.rs @@ -4,83 +4,14 @@ use crate::opc::NS_A; use crate::xml::{A, W}; -/// Well-known built-in table style GUIDs (format identifiers) and our equivalent ids. -const KNOWN: &[(&str, &str)] = &[ - ("{2D5ABB26-0587-4C30-8999-92F81FD0307C}", "none"), - ("{5940675A-B579-460E-94D1-54222C63F5DA}", "grid"), - ("{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}", "medium2-accent1"), - ("{073A0DAA-6AF3-43AB-8588-CEC1D06C72B9}", "medium2-tx1"), - ("{21E4AEA4-8DFA-4A89-87EB-49C32662AFE8}", "medium2-accent2"), - ("{F5AB1C69-6EDB-4FF4-983F-18BD219EF322}", "medium2-accent3"), - ("{00A15C55-8517-42AA-B614-E9B94910E393}", "medium2-accent4"), - ("{7DF18680-E054-41AD-8BC1-D1AEF772440D}", "medium2-accent5"), - ("{93296810-A885-4BE3-A3E7-6D5BEEA58F35}", "medium2-accent6"), -]; - -/// GUIDs we write as built-in references (no definition needed). -const BUILTIN_WRITE: [&str; 3] = ["none", "grid", "medium2-accent1"]; - -fn fnv(s: &str, seed: u64) -> u64 { - let mut h = 0xcbf2_9ce4_8422_2325u64 ^ seed; - for b in s.bytes() { - h ^= b as u64; - h = h.wrapping_mul(0x0100_0000_01b3); - } - h -} - -/// A stable GUID derived from text (version-4 layout). -pub fn guid(text: &str) -> String { - let a = fnv(text, 0x5163_7261_6674); - let b = fnv(text, 0x7461_626c_6573); - format!( - "{{{:08X}-{:04X}-4{:03X}-8{:03X}-{:012X}}}", - (a >> 32) as u32, - (a >> 16) as u16, - (a & 0x0fff) as u16, - (b >> 48) as u16 & 0x0fff, - b & 0xffff_ffff_ffff - ) -} +// Keep file-format identifiers in the shared DrawingML implementation. The local +// writer below still owns DeckCraft's table style definitions and XML policy. +pub use craft_drawingml::table::styles::{guid, guid_for_style, is_own_guid, style_from_guid}; fn own_guid(style: &str) -> String { guid(&format!("deckcraft-table-style:{style}")) } -/// Is `g` the GUID DeckCraft writes for one of its own styles? -pub fn is_own_guid(g: &str) -> bool { - let up = g.to_ascii_uppercase(); - deckcraft_model::table::table_styles().iter().any(|(id, _)| own_guid(id) == up) -} - -/// Table style GUID → our style id (or the GUID itself when unknown). -pub fn style_from_guid(g: &str) -> String { - let up = g.to_ascii_uppercase(); - if let Some((_, s)) = KNOWN.iter().find(|(k, _)| *k == up) { - return s.to_string(); - } - for (id, _) in deckcraft_model::table::table_styles() { - if own_guid(&id) == up { - return id; - } - } - if g.is_empty() { "none".into() } else { g.to_string() } -} - -/// GUID to write for our style id, and whether a definition must be written. -pub fn guid_for_style(style: &str) -> (String, bool) { - if style.starts_with('{') { - return (style.to_string(), false); - } - if BUILTIN_WRITE.contains(&style) - && let Some((g, _)) = KNOWN.iter().find(|(_, s)| *s == style) - { - return (g.to_string(), false); - } - let known = deckcraft_model::table::table_styles().iter().any(|(id, _)| id == style); - if known { (own_guid(style), true) } else { ("{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}".into(), false) } -} - fn clr(w: &mut W, slot: &str, mods: &[(&str, i32)]) { if mods.is_empty() { w.val("a:schemeClr", slot); @@ -254,6 +185,23 @@ pub fn table_styles_xml(used: &[String], defs: &[deckcraft_model::table::TableSt mod tests { use super::*; + #[test] + fn shared_style_catalog_matches_deckcraft() { + assert_eq!(deckcraft_model::table::table_styles(), craft_drawingml::table::table_styles()); + assert_eq!(style_from_guid(""), "none"); + let custom = "{abcdef00-0000-0000-0000-000000000000}"; + assert_eq!(style_from_guid(custom), custom); + assert_eq!(guid_for_style(custom), (custom.to_string(), false)); + assert_eq!(guid_for_style("unknown"), guid_for_style("medium2-accent1")); + for (id, _) in deckcraft_model::table::table_styles() { + let generated = own_guid(&id); + assert!(is_own_guid(&generated.to_ascii_lowercase())); + if guid_for_style(&id).1 { + assert_eq!(guid_for_style(&id).0, generated); + } + } + } + #[test] fn style_parts_follow_schema_order() { const ORDER: [&str; 14] = [