Compare commits

...
4 changed files with 38 additions and 139 deletions
Generated
+1
View File
@@ -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",
+1
View File
@@ -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 }
+16 -67
View File
@@ -348,73 +348,7 @@ fn arc_to(d: &mut String, cur: (f64, f64), wr: f64, hr: f64, st: f64, sw: f64) -
}
/// Parse our path syntax into commands.
pub fn parse_path(d: &str) -> Vec<(char, Vec<f64>)> {
let mut out = vec![];
let mut cmd: Option<char> = None;
let mut nums: Vec<f64> = vec![];
let flush = |cmd: Option<char>, nums: &mut Vec<f64>, out: &mut Vec<(char, Vec<f64>)>| {
if let Some(c) = cmd {
let n = match c {
'M' | 'L' => 2,
'C' => 6,
'Q' => 4,
_ => 0,
};
if n == 0 {
out.push((c, vec![]));
} else {
for (i, chunk) in nums.chunks(n).enumerate() {
if chunk.len() == n && out.len() < 1_000_000 {
// Repeated coordinates after M continue as L.
let c2 = if c == 'M' && i > 0 { 'L' } else { c };
out.push((c2, chunk.to_vec()));
}
}
}
}
nums.clear();
};
let mut token = String::new();
let push_tok = |token: &mut String, nums: &mut Vec<f64>| {
if !token.is_empty() {
if let Ok(v) = token.parse::<f64>()
&& v.is_finite()
{
nums.push(v);
}
token.clear();
}
};
let cs: Vec<char> = d.chars().collect();
for (i, &ch) in cs.iter().enumerate() {
match ch {
// An 'E' inside a number followed by a digit (or a sign and a digit) is an exponent,
// not the end command.
'E' if !token.is_empty() && {
let n1 = cs.get(i + 1).copied().unwrap_or(' ');
let n2 = cs.get(i + 2).copied().unwrap_or(' ');
n1.is_ascii_digit() || ((n1 == '-' || n1 == '+') && n2.is_ascii_digit())
} =>
{
token.push(ch)
}
'M' | 'L' | 'C' | 'Q' | 'Z' | 'm' | 'l' | 'c' | 'q' | 'z' | 'E' => {
push_tok(&mut token, &mut nums);
flush(cmd, &mut nums, &mut out);
cmd = Some(ch.to_ascii_uppercase());
}
' ' | ',' | '\t' | '\n' => push_tok(&mut token, &mut nums),
'-' if !token.is_empty() && !token.ends_with(['e', 'E']) => {
push_tok(&mut token, &mut nums);
token.push(ch);
}
c => token.push(c),
}
}
push_tok(&mut token, &mut nums);
flush(cmd, &mut nums, &mut out);
out
}
pub use craft_drawingml::custgeom::parse_path;
/// Write custom paths as `a:custGeom`. Coordinates are integers in the file, so small path spaces
/// are scaled up.
@@ -596,3 +530,18 @@ mod tests {
assert!(parse_path("garbage ### C 1").is_empty() || parse_path("garbage").is_empty());
}
}
#[cfg(test)]
mod shared_path_tests {
use super::parse_path;
#[test]
fn shared_path_tokens_preserve_exponents_repeats_and_dropped_tails() {
assert_eq!(
parse_path("M1E2 -3E-1 4 5 L6-7 Q8 9 10 11 Z"),
vec![('M', vec![100.0, -0.3]), ('L', vec![4.0, 5.0]), ('L', vec![6.0, -7.0]), ('Q', vec![8.0, 9.0, 10.0, 11.0]), ('Z', vec![]),]
);
assert_eq!(parse_path("M0 0 L1"), vec![('M', vec![0.0, 0.0])]);
assert_eq!(parse_path("MNaN 0 1 2"), vec![('M', vec![0.0, 1.0])]);
}
}
+20 -72
View File
@@ -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] = [