deckcraft-model 0.6.1

DeckCraft document model: presentations, masters, layouts, slides, shapes, text, themes, transitions, animations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Colours, fills, lines and effects.

use deckcraft_color::{ColorScheme, ColorTransform, Rgba, SchemeSlot};
use serde::{Deserialize, Serialize};

use crate::MediaId;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum ColorBase {
    Rgb {
        rgb: Rgba,
    },
    Scheme {
        slot: SchemeSlot,
    },
    Preset {
        name: String,
    },
    /// A system colour (`windowText`…) with the value it had when saved.
    System {
        name: String,
        last: Rgba,
    },
}

/// A colour as written in a document: a base (RGB, theme slot…) plus transforms.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ColorRef {
    pub base: ColorBase,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub mods: Vec<ColorTransform>,
}

impl ColorRef {
    pub fn rgb(c: Rgba) -> Self {
        ColorRef {
            base: ColorBase::Rgb { rgb: c },
            mods: vec![],
        }
    }
    pub fn scheme(slot: SchemeSlot) -> Self {
        ColorRef {
            base: ColorBase::Scheme { slot },
            mods: vec![],
        }
    }
    pub fn with(mut self, m: ColorTransform) -> Self {
        self.mods.push(m);
        self
    }
    /// Resolve against a colour scheme; `ph` substitutes `phClr` (style matrix colour).
    pub fn resolve(&self, scheme: &ColorScheme, ph: Option<Rgba>) -> Rgba {
        let base = match &self.base {
            ColorBase::Rgb { rgb } => *rgb,
            ColorBase::Scheme {
                slot: SchemeSlot::PhClr,
            } => ph.unwrap_or_else(|| scheme.get(SchemeSlot::Accent1)),
            ColorBase::Scheme { slot } => scheme.get(*slot),
            ColorBase::Preset { name } => deckcraft_color::preset(name).unwrap_or(Rgba::BLACK),
            ColorBase::System { last, .. } => *last,
        };
        deckcraft_color::apply(base, &self.mods)
    }
    /// Alpha (0–1) from an `alpha` transform, if any.
    pub fn alpha(&self) -> f64 {
        self.mods.iter().fold(1.0, |a, m| match m {
            ColorTransform::Alpha(v) => (*v as f64 / 100_000.0).clamp(0.0, 1.0),
            _ => a,
        })
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GradientStop {
    /// 0–1.
    pub pos: f64,
    pub color: ColorRef,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum GradientShape {
    /// Angle in degrees clockwise from left→right.
    Linear {
        angle: f64,
        #[serde(default)]
        scaled: bool,
    },
    /// `circle`, `rect` or `shape` path gradient; `focus` is the fill-to rect as fractions (l, t, r, b).
    Path { path: String, focus: [f64; 4] },
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Gradient {
    pub stops: Vec<GradientStop>,
    pub shape: GradientShape,
    #[serde(default = "crate::yes")]
    pub rotate_with_shape: bool,
}

/// How a picture fills its box: stretched into a (fractional) inset rect, or tiled.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum PictureMode {
    Stretch {
        #[serde(default)]
        fill_rect: [f64; 4],
    },
    Tile {
        tx: f64,
        ty: f64,
        sx: f64,
        sy: f64,
        #[serde(default)]
        flip: String,
        #[serde(default)]
        align: String,
    },
}

impl Default for PictureMode {
    fn default() -> Self {
        PictureMode::Stretch {
            fill_rect: [0.0; 4],
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct PictureFill {
    pub media: MediaId,
    /// Source crop as fractions of the image (left, top, right, bottom); negative values pad.
    pub crop: [f64; 4],
    pub mode: PictureMode,
    /// 0–1 opacity of the picture.
    pub alpha: Option<f64>,
    pub adjust: PictureAdjust,
}

/// Picture corrections and colour (Picture Format ▸ Corrections / Color / Transparency).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct PictureAdjust {
    /// -1..1
    pub brightness: f64,
    /// -1..1
    pub contrast: f64,
    /// 0..4 (1 = unchanged)
    pub saturation: Option<f64>,
    pub grayscale: bool,
    /// Recolour duotone (dark, light).
    pub duotone: Option<(ColorRef, ColorRef)>,
    /// Make a colour transparent (Set Transparent Color).
    pub clear_color: Option<Rgba>,
    pub sharpen: f64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PatternFill {
    pub preset: String,
    pub fg: ColorRef,
    pub bg: ColorRef,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum Fill {
    None,
    Solid {
        color: ColorRef,
    },
    Gradient(Gradient),
    Picture(PictureFill),
    Pattern(PatternFill),
    /// Use the containing group's fill.
    Group,
    /// Use the slide background (`useBgFill`).
    Background,
}

impl Fill {
    pub fn solid(c: ColorRef) -> Self {
        Fill::Solid { color: c }
    }
    pub fn is_none(&self) -> bool {
        matches!(self, Fill::None)
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum LineCap {
    #[default]
    Flat,
    Round,
    Square,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum LineJoin {
    #[default]
    Round,
    Bevel,
    Miter,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Compound {
    #[default]
    Single,
    Double,
    ThickThin,
    ThinThick,
    Triple,
}

impl Compound {
    pub fn xml(self) -> &'static str {
        match self {
            Compound::Single => "sng",
            Compound::Double => "dbl",
            Compound::ThickThin => "thickThin",
            Compound::ThinThick => "thinThick",
            Compound::Triple => "tri",
        }
    }
    pub fn from_xml(s: &str) -> Self {
        match s {
            "dbl" => Compound::Double,
            "thickThin" => Compound::ThickThin,
            "thinThick" => Compound::ThinThick,
            "tri" => Compound::Triple,
            _ => Compound::Single,
        }
    }
}

/// Dash style: a preset name (`solid`, `dot`, `dash`, `lgDash`, `dashDot`, `lgDashDot`,
/// `lgDashDotDot`, `sysDash`, `sysDot`, `sysDashDot`, `sysDashDotDot`) or custom (dash, space) pairs
/// in multiples of the line width.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum Dash {
    Preset { name: String },
    Custom { pattern: Vec<(f64, f64)> },
}

impl Dash {
    pub const PRESETS: [&'static str; 11] = [
        "solid",
        "sysDot",
        "sysDash",
        "dash",
        "dashDot",
        "lgDash",
        "lgDashDot",
        "lgDashDotDot",
        "dot",
        "sysDashDot",
        "sysDashDotDot",
    ];
    /// Dash/space lengths in multiples of the line width.
    pub fn pattern(&self) -> Vec<f64> {
        match self {
            Dash::Custom { pattern } => pattern.iter().flat_map(|(d, s)| [*d, *s]).collect(),
            Dash::Preset { name } => match name.as_str() {
                "dot" => vec![1.0, 3.0],
                "dash" => vec![4.0, 3.0],
                "lgDash" => vec![8.0, 3.0],
                "dashDot" => vec![4.0, 3.0, 1.0, 3.0],
                "lgDashDot" => vec![8.0, 3.0, 1.0, 3.0],
                "lgDashDotDot" => vec![8.0, 3.0, 1.0, 3.0, 1.0, 3.0],
                "sysDash" => vec![3.0, 1.0],
                "sysDot" => vec![1.0, 1.0],
                "sysDashDot" => vec![3.0, 1.0, 1.0, 1.0],
                "sysDashDotDot" => vec![3.0, 1.0, 1.0, 1.0, 1.0, 1.0],
                _ => vec![],
            },
        }
    }
}

/// Arrowhead at a line end.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LineEnd {
    /// `none`, `triangle`, `stealth`, `diamond`, `oval`, `arrow`.
    pub kind: String,
    /// `sm`, `med`, `lg`.
    #[serde(default = "med")]
    pub w: String,
    #[serde(default = "med")]
    pub len: String,
}

fn med() -> String {
    "med".into()
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Line {
    pub fill: Option<Fill>,
    /// Width in points.
    pub width: Option<f64>,
    pub dash: Option<Dash>,
    pub cap: Option<LineCap>,
    pub join: Option<LineJoin>,
    pub compound: Option<Compound>,
    pub head: Option<LineEnd>,
    pub tail: Option<LineEnd>,
}

impl Line {
    pub fn solid(c: ColorRef, width: f64) -> Self {
        Line {
            fill: Some(Fill::solid(c)),
            width: Some(width),
            ..Default::default()
        }
    }
    pub fn none() -> Self {
        Line {
            fill: Some(Fill::None),
            ..Default::default()
        }
    }
    /// Fill unset fields from `parent`.
    pub fn inherit(&mut self, parent: &Line) {
        if self.fill.is_none() {
            self.fill.clone_from(&parent.fill);
        }
        self.width = self.width.or(parent.width);
        if self.dash.is_none() {
            self.dash.clone_from(&parent.dash);
        }
        self.cap = self.cap.or(parent.cap);
        self.join = self.join.or(parent.join);
        self.compound = self.compound.or(parent.compound);
        if self.head.is_none() {
            self.head.clone_from(&parent.head);
        }
        if self.tail.is_none() {
            self.tail.clone_from(&parent.tail);
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Shadow {
    pub color: ColorRef,
    /// Blur radius, points.
    pub blur: f64,
    /// Distance, points.
    pub dist: f64,
    /// Direction, degrees (0 = right, 90 = down).
    pub dir: f64,
    #[serde(default)]
    pub inner: bool,
    /// Scale (1 = 100%) for perspective shadows.
    #[serde(default = "one")]
    pub sx: f64,
    #[serde(default = "one")]
    pub sy: f64,
    #[serde(default)]
    pub kx: f64,
    #[serde(default)]
    pub ky: f64,
    #[serde(default)]
    pub align: String,
    #[serde(default = "crate::yes")]
    pub rotate_with_shape: bool,
}

fn one() -> f64 {
    1.0
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Glow {
    pub color: ColorRef,
    pub radius: f64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Reflection {
    pub blur: f64,
    pub start_alpha: f64,
    pub end_alpha: f64,
    pub end_pos: f64,
    pub dist: f64,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Effects {
    pub outer_shadow: Option<Shadow>,
    pub inner_shadow: Option<Shadow>,
    pub glow: Option<Glow>,
    /// Soft edges radius in points.
    pub soft_edge: Option<f64>,
    pub reflection: Option<Reflection>,
    /// Bevel preset name for 3-D format (rendered as an approximation).
    pub bevel: Option<String>,
    /// Unmodelled 3-D XML kept for round-trip (`scene3d`, `sp3d`).
    pub raw3d: Option<String>,
    /// `a:fillOverlay`: a fill blended over the shape's fill (`blend`: `over`, `mult`, `screen`,
    /// `darken`, `lighten`). Only solid and gradient overlays are kept.
    pub fill_overlay: Option<FillOverlay>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FillOverlay {
    pub blend: String,
    pub fill: Fill,
}

impl Effects {
    pub fn is_empty(&self) -> bool {
        self.outer_shadow.is_none()
            && self.inner_shadow.is_none()
            && self.glow.is_none()
            && self.soft_edge.is_none()
            && self.reflection.is_none()
            && self.bevel.is_none()
            && self.fill_overlay.is_none()
    }
}

/// A shape's theme style references (`p:style`): which fill/line/effect of the theme's format scheme
/// to use, with which colour, and the theme font and colour for its text.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeStyle {
    pub line_ref: (u32, ColorRef),
    pub fill_ref: (u32, ColorRef),
    pub effect_ref: (u32, ColorRef),
    /// `major`, `minor` or `none`, with the text colour.
    pub font_ref: (String, Option<ColorRef>),
}

impl ShapeStyle {
    /// The default for newly drawn shapes: accent 1 fill, darker accent 1 outline, light text.
    pub fn accent(slot: SchemeSlot) -> Self {
        ShapeStyle {
            line_ref: (2, ColorRef::scheme(slot).with(ColorTransform::Shade(50000))),
            fill_ref: (1, ColorRef::scheme(slot)),
            effect_ref: (0, ColorRef::scheme(slot)),
            font_ref: ("minor".into(), Some(ColorRef::scheme(SchemeSlot::Lt1))),
        }
    }
    /// The default for lines/connectors.
    pub fn line(slot: SchemeSlot) -> Self {
        ShapeStyle {
            line_ref: (1, ColorRef::scheme(slot)),
            fill_ref: (0, ColorRef::scheme(slot)),
            effect_ref: (0, ColorRef::scheme(slot)),
            font_ref: ("minor".into(), Some(ColorRef::scheme(SchemeSlot::Tx1))),
        }
    }
}