betteroffice-pptx-edit 0.5.1

Collaborative yrs-backed editing core for PPTX decks.
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use std::collections::BTreeMap;

use ooxml_drawingml::{ShapeFill, ShapeOutline};
use pptx_parse::{BlipEffect, GraphicFrameData, Placeholder};

pub use pptx_parse::{CommentFlavor, TextCaps};
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EditOrigin {
    #[default]
    Local,
    Agent,
    Remote,
    System,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EditCtx {
    pub origin: EditOrigin,
    pub author: String,
}

impl EditCtx {
    pub fn local(author: impl Into<String>) -> Self {
        Self {
            origin: EditOrigin::Local,
            author: author.into(),
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextStyle {
    pub bold: Option<bool>,
    pub italic: Option<bool>,
    pub font_size_pt: Option<f64>,
    pub color: Option<String>,
    pub font_family: Option<String>,
    pub underline: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub spacing_pt: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub baseline_pct: Option<f64>,
    /// `a:rPr@kern`: the smallest size, in points, the run is kerned at.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub kern_pt: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub caps: Option<TextCaps>,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextStylePatch {
    pub bold: Option<bool>,
    pub italic: Option<bool>,
    pub font_size_pt: Option<f64>,
    pub color: Option<String>,
    pub font_family: Option<String>,
    pub underline: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub spacing_pt: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub baseline_pct: Option<f64>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextRunSnapshot {
    pub text: String,
    pub style: TextStyle,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ParagraphSnapshot {
    pub id: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub source_paragraphs: Vec<ParagraphSource>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub preserved_xml: Option<String>,
    pub alignment: Option<String>,
    pub level: u32,
    pub bullet_json: Option<String>,
    pub runs: Vec<TextRunSnapshot>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ParagraphSource {
    pub id: String,
    pub xml: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StorySnapshot {
    pub id: String,
    pub length: u32,
    pub paragraphs: Vec<ParagraphSnapshot>,
}

impl StorySnapshot {
    pub fn plain_text(&self) -> String {
        self.paragraphs
            .iter()
            .map(|paragraph| {
                paragraph
                    .runs
                    .iter()
                    .map(|run| run.text.as_str())
                    .collect::<String>()
            })
            .collect::<Vec<_>>()
            .join("\n")
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeSnapshot {
    pub id: String,
    pub source_id: u32,
    pub kind: ShapeKind,
    pub name: String,
    pub x: i64,
    pub y: i64,
    pub width: i64,
    pub height: i64,
    pub rotation_deg: f64,
    pub flip_h: bool,
    pub flip_v: bool,
    /// The geometry the shape draws at, present only while it has none of
    /// its own.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub inherited: Option<InheritedGeometry>,
    /// Hides this shape and its descendants.
    #[serde(default, skip_serializing_if = "is_false")]
    pub hidden: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    pub text_box: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    pub aspect_ratio_locked: bool,
    pub geometry: String,
    pub adjust_values: BTreeMap<String, f64>,
    pub placeholder: Option<Placeholder>,
    pub fill: Option<ShapeFill>,
    pub resolved_fill_color: Option<String>,
    pub outline: Option<ShapeOutline>,
    pub resolved_outline_color: Option<String>,
    pub media_part_path: Option<String>,
    /// Image data added to this session, retained across saves.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending_media: Option<PendingMedia>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub blip_effects: Vec<BlipEffect>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub table_structure: Option<pptx_parse::TableStructure>,
    pub graphic: Option<GraphicFrameData>,
    pub text_stories: Vec<StorySnapshot>,
    pub children: Vec<ShapeSnapshot>,
}

/// The transform a placeholder takes from its layout or master.
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InheritedGeometry {
    pub x: i64,
    pub y: i64,
    pub width: i64,
    pub height: i64,
    pub rotation_deg: f64,
    pub flip_h: bool,
    pub flip_v: bool,
}

/// Image data shared by editing peers.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PendingMedia {
    pub content_type: String,
    pub base64: String,
}

fn is_false(value: &bool) -> bool {
    !value
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ShapeKind {
    Shape,
    Picture,
    GraphicFrame,
    Group,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SlideLayoutSnapshot {
    pub name: Option<String>,
    pub r#type: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SlideSnapshot {
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub hidden: Option<bool>,
    #[serde(default)]
    pub layout: Option<SlideLayoutSnapshot>,
    pub id: String,
    pub source_part_path: Option<String>,
    pub layout_part_path: Option<String>,
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub notes: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub background: Option<ShapeFill>,
    /// An edit changed the background from what the source part declares, so
    /// `background` stands for the slide whatever `p:bg` that part holds.
    #[serde(default, skip_serializing_if = "is_false")]
    pub background_edited: bool,
    pub shapes: Vec<ShapeSnapshot>,
}

/// One slide's snapshot plus deck geometry — the slide-scoped render input.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SlideScope {
    /// The slide's position in deck order.
    pub index: usize,
    pub slide: SlideSnapshot,
    pub width_emu: i64,
    pub height_emu: i64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeckSnapshot {
    pub width_emu: i64,
    pub height_emu: i64,
    pub slides: Vec<SlideSnapshot>,
    #[serde(default, skip_serializing_if = "legacy_comment_flavor")]
    pub comment_flavor: CommentFlavor,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub comments: Vec<CommentSnapshot>,
}

fn legacy_comment_flavor(flavor: &CommentFlavor) -> bool {
    *flavor == CommentFlavor::Legacy
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommentSnapshot {
    pub id: String,
    pub slide_id: String,
    pub author: String,
    pub initials: String,
    pub text: String,
    pub created: Option<String>,
    pub x_emu: i64,
    pub y_emu: i64,
    pub parent_id: Option<String>,
    pub resolved: bool,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommentReceipt {
    pub comment_id: String,
    pub slide_id: String,
    pub parent_id: Option<String>,
    pub resolved: bool,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SlideReceipt {
    pub slide_id: String,
    pub from_index: Option<u32>,
    pub to_index: Option<u32>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeReceipt {
    pub slide_id: String,
    pub shape_id: String,
    pub index: u32,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeZOrderReceipt {
    pub slide_id: String,
    pub shape_id: String,
    pub from_index: u32,
    pub to_index: u32,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransformReceipt {
    pub slide_id: String,
    pub shape_id: String,
    pub before: ShapeRect,
    pub after: ShapeRect,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShapeRect {
    pub x: i64,
    pub y: i64,
    pub width: i64,
    pub height: i64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextReceipt {
    pub story_id: String,
    pub start: u32,
    pub end: u32,
    pub text: String,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeDraft {
    pub name: String,
    pub rect: ShapeRect,
    pub text: String,
    pub style: TextStyle,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PresetShapeDraft {
    pub name: String,
    pub geometry: String,
    pub rect: ShapeRect,
    pub fill: Option<String>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct PictureDraft {
    pub name: String,
    pub rect: ShapeRect,
    pub content_type: String,
    pub media_bytes: Vec<u8>,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeStroke {
    pub color: Option<String>,
    pub width_pt: Option<f64>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeFillReceipt {
    pub slide_id: String,
    pub shape_id: String,
    pub before: Option<String>,
    pub after: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SlideBackgroundReceipt {
    pub slide_id: String,
    pub before: Option<ShapeFill>,
    pub after: Option<ShapeFill>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeStrokeReceipt {
    pub slide_id: String,
    pub shape_id: String,
    pub before: Option<ShapeStroke>,
    pub after: Option<ShapeStroke>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShapeAdjustReceipt {
    pub slide_id: String,
    pub shape_id: String,
    pub before: BTreeMap<String, f64>,
    pub after: BTreeMap<String, f64>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UpdateOrigin {
    Local,
    Remote,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UpdateEvent {
    pub update: Vec<u8>,
    pub origin: UpdateOrigin,
}

#[derive(Debug, Error)]
pub enum EditError {
    #[error("invalid client ID {0}")]
    InvalidClientId(u64),
    #[error("could not parse PPTX: {0}")]
    Parse(String),
    #[error("invalid deck state: {0}")]
    InvalidState(String),
    #[error("invalid yrs update: {0}")]
    InvalidUpdate(String),
    #[error("invalid yrs state vector: {0}")]
    InvalidStateVector(String),
    #[error("slide {0:?} was not found")]
    SlideNotFound(String),
    #[error("shape {0:?} was not found")]
    ShapeNotFound(String),
    #[error("story {0:?} was not found")]
    StoryNotFound(String),
    #[error("comment {0:?} was not found")]
    CommentNotFound(String),
    #[error("invalid comment: {0}")]
    InvalidComment(String),
    #[error("index {index} is outside length {length}")]
    OutOfBounds { index: u32, length: u32 },
    #[error("text range {start}..{end} crosses a paragraph boundary")]
    ParagraphBoundary { start: u32, end: u32 },
    #[error("invalid shape geometry: {0}")]
    InvalidGeometry(String),
    #[error("invalid fill: {0}")]
    InvalidFill(String),
    #[error("invalid shape adjustment: {0}")]
    InvalidAdjustment(String),
    #[error("invalid table: {0}")]
    InvalidTable(String),
    #[error(
        "structural table operations are unavailable in collaborative mode; edit cell text instead, or change the table structure outside the shared session"
    )]
    CollaborativeStructureOperation,
    #[error("invalid text: {0}")]
    InvalidText(String),
    #[error("update observer failed: {0}")]
    Observer(String),
    #[error("JSON boundary error: {0}")]
    Json(String),
    #[error("could not write PPTX: {0}")]
    Write(String),
}

pub type EditResult<T> = Result<T, EditError>;

/// Rejects characters XML 1.0 cannot carry, so a bad edit fails loudly
/// instead of producing an unopenable file at save time.
pub(crate) fn validate_xml_text(value: &str) -> EditResult<()> {
    match value
        .chars()
        .find(|character| !legal_xml_character(*character))
    {
        Some(character) => Err(EditError::InvalidText(format!(
            "character U+{:04X} cannot be stored in a PPTX file",
            character as u32
        ))),
        None => Ok(()),
    }
}

fn legal_xml_character(character: char) -> bool {
    matches!(character, '\u{9}' | '\u{a}' | '\u{d}')
        || ('\u{20}'..='\u{d7ff}').contains(&character)
        || ('\u{e000}'..='\u{fffd}').contains(&character)
        || ('\u{10000}'..='\u{10ffff}').contains(&character)
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TextParagraphDraft {
    pub alignment: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bullet: Option<bool>,
    pub runs: Vec<TextRunDraft>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TextRunDraft {
    pub text: String,
    #[serde(default)]
    pub style: TextStyle,
}