ppt-rs 0.2.14

Create, read, and update PowerPoint 2007+ (.pptx) files with rich formatting, bullet styles, themes, and templates.
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Connector support for linking shapes in PPTX
//!
//! Provides connector types and XML generation for connecting shapes.

use crate::core::escape_xml;

/// Connector types available in PPTX
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum ConnectorType {
    /// Straight line connector
    Straight,
    /// Elbow (bent) connector
    Elbow,
    /// Curved connector
    Curved,
}

impl ConnectorType {
    /// Get the preset geometry name for the connector
    pub fn preset_name(&self) -> &'static str {
        match self {
            ConnectorType::Straight => "straightConnector1",
            ConnectorType::Elbow => "bentConnector3",
            ConnectorType::Curved => "curvedConnector3",
        }
    }

    /// Get display name
    pub fn display_name(&self) -> &'static str {
        match self {
            ConnectorType::Straight => "Straight Connector",
            ConnectorType::Elbow => "Elbow Connector",
            ConnectorType::Curved => "Curved Connector",
        }
    }
}

/// Arrow head types for connectors
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum ArrowType {
    /// No arrow
    None,
    /// Triangle arrow
    Triangle,
    /// Stealth arrow
    Stealth,
    /// Diamond arrow
    Diamond,
    /// Oval arrow
    Oval,
    /// Open arrow
    Open,
}

impl ArrowType {
    /// Get OOXML arrow type value
    pub fn xml_value(&self) -> &'static str {
        match self {
            ArrowType::None => "none",
            ArrowType::Triangle => "triangle",
            ArrowType::Stealth => "stealth",
            ArrowType::Diamond => "diamond",
            ArrowType::Oval => "oval",
            ArrowType::Open => "arrow",
        }
    }
}

/// Arrow size
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum ArrowSize {
    Small,
    Medium,
    Large,
}

impl ArrowSize {
    /// Get OOXML size value
    pub fn xml_value(&self) -> &'static str {
        match self {
            ArrowSize::Small => "sm",
            ArrowSize::Medium => "med",
            ArrowSize::Large => "lg",
        }
    }
}

/// Connection point on a shape
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum ConnectionSite {
    /// Top center
    Top,
    /// Bottom center
    Bottom,
    /// Left center
    Left,
    /// Right center
    Right,
    /// Top left corner
    TopLeft,
    /// Top right corner
    TopRight,
    /// Bottom left corner
    BottomLeft,
    /// Bottom right corner
    BottomRight,
    /// Center
    Center,
}

impl ConnectionSite {
    /// Get connection site index (0-based)
    pub fn index(&self) -> u32 {
        match self {
            ConnectionSite::Top => 0,
            ConnectionSite::Right => 1,
            ConnectionSite::Bottom => 2,
            ConnectionSite::Left => 3,
            ConnectionSite::TopLeft => 4,
            ConnectionSite::TopRight => 5,
            ConnectionSite::BottomRight => 6,
            ConnectionSite::BottomLeft => 7,
            ConnectionSite::Center => 8,
        }
    }
}

/// Connector line style
#[derive(Clone, Debug)]
pub struct ConnectorLine {
    /// Line color (RGB hex)
    pub color: String,
    /// Line width in EMU
    pub width: u32,
    /// Dash style
    pub dash: LineDash,
}

impl Default for ConnectorLine {
    fn default() -> Self {
        ConnectorLine {
            color: "000000".to_string(),
            width: 12700, // 1pt
            dash: LineDash::Solid,
        }
    }
}

impl ConnectorLine {
    /// Create new connector line
    pub fn new(color: &str, width: u32) -> Self {
        ConnectorLine {
            color: color.trim_start_matches('#').to_uppercase(),
            width,
            dash: LineDash::Solid,
        }
    }

    /// Set dash style
    pub fn with_dash(mut self, dash: LineDash) -> Self {
        self.dash = dash;
        self
    }
}

/// Line dash styles
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum LineDash {
    Solid,
    Dash,
    Dot,
    DashDot,
    DashDotDot,
    LongDash,
    LongDashDot,
}

impl LineDash {
    /// Get OOXML dash value
    pub fn xml_value(&self) -> &'static str {
        match self {
            LineDash::Solid => "solid",
            LineDash::Dash => "dash",
            LineDash::Dot => "dot",
            LineDash::DashDot => "dashDot",
            LineDash::DashDotDot => "lgDashDotDot",
            LineDash::LongDash => "lgDash",
            LineDash::LongDashDot => "lgDashDot",
        }
    }
}

/// Connector definition
#[derive(Clone, Debug)]
pub struct Connector {
    /// Connector type
    pub connector_type: ConnectorType,
    /// Start X position in EMU
    pub start_x: u32,
    /// Start Y position in EMU
    pub start_y: u32,
    /// End X position in EMU
    pub end_x: u32,
    /// End Y position in EMU
    pub end_y: u32,
    /// Line style
    pub line: ConnectorLine,
    /// Start arrow
    pub start_arrow: ArrowType,
    /// End arrow
    pub end_arrow: ArrowType,
    /// Arrow size
    pub arrow_size: ArrowSize,
    /// Connected shape ID at start (optional)
    pub start_shape_id: Option<u32>,
    /// Connection site at start shape
    pub start_site: Option<ConnectionSite>,
    /// Connected shape ID at end (optional)
    pub end_shape_id: Option<u32>,
    /// Connection site at end shape
    pub end_site: Option<ConnectionSite>,
    /// Optional label text
    pub label: Option<String>,
}

impl Connector {
    /// Create a new connector
    pub fn new(
        connector_type: ConnectorType,
        start_x: u32,
        start_y: u32,
        end_x: u32,
        end_y: u32,
    ) -> Self {
        Connector {
            connector_type,
            start_x,
            start_y,
            end_x,
            end_y,
            line: ConnectorLine::default(),
            start_arrow: ArrowType::None,
            end_arrow: ArrowType::None,
            arrow_size: ArrowSize::Medium,
            start_shape_id: None,
            start_site: None,
            end_shape_id: None,
            end_site: None,
            label: None,
        }
    }

    /// Create a straight connector
    pub fn straight(start_x: u32, start_y: u32, end_x: u32, end_y: u32) -> Self {
        Self::new(ConnectorType::Straight, start_x, start_y, end_x, end_y)
    }

    /// Create an elbow connector
    pub fn elbow(start_x: u32, start_y: u32, end_x: u32, end_y: u32) -> Self {
        Self::new(ConnectorType::Elbow, start_x, start_y, end_x, end_y)
    }

    /// Create a curved connector
    pub fn curved(start_x: u32, start_y: u32, end_x: u32, end_y: u32) -> Self {
        Self::new(ConnectorType::Curved, start_x, start_y, end_x, end_y)
    }

    /// Set line style
    pub fn with_line(mut self, line: ConnectorLine) -> Self {
        self.line = line;
        self
    }

    /// Set line color
    pub fn with_color(mut self, color: &str) -> Self {
        self.line.color = color.trim_start_matches('#').to_uppercase();
        self
    }

    /// Set line width in EMU
    pub fn with_width(mut self, width: u32) -> Self {
        self.line.width = width;
        self
    }

    /// Set start arrow
    pub fn with_start_arrow(mut self, arrow: ArrowType) -> Self {
        self.start_arrow = arrow;
        self
    }

    /// Set end arrow
    pub fn with_end_arrow(mut self, arrow: ArrowType) -> Self {
        self.end_arrow = arrow;
        self
    }

    /// Set both arrows
    pub fn with_arrows(mut self, start: ArrowType, end: ArrowType) -> Self {
        self.start_arrow = start;
        self.end_arrow = end;
        self
    }

    /// Set arrow size
    pub fn with_arrow_size(mut self, size: ArrowSize) -> Self {
        self.arrow_size = size;
        self
    }

    /// Connect to start shape
    pub fn connect_start(mut self, shape_id: u32, site: ConnectionSite) -> Self {
        self.start_shape_id = Some(shape_id);
        self.start_site = Some(site);
        self
    }

    /// Connect to end shape
    pub fn connect_end(mut self, shape_id: u32, site: ConnectionSite) -> Self {
        self.end_shape_id = Some(shape_id);
        self.end_site = Some(site);
        self
    }

    /// Add label text
    pub fn with_label(mut self, label: &str) -> Self {
        self.label = Some(label.to_string());
        self
    }

    /// Calculate width for XML
    fn width(&self) -> u32 {
        self.end_x.abs_diff(self.start_x)
    }

    /// Calculate height for XML
    fn height(&self) -> u32 {
        self.end_y.abs_diff(self.start_y)
    }

    /// Check if connector is flipped horizontally
    fn flip_h(&self) -> bool {
        self.end_x < self.start_x
    }

    /// Check if connector is flipped vertically
    fn flip_v(&self) -> bool {
        self.end_y < self.start_y
    }
}

/// Generate connector XML for a slide
pub fn generate_connector_xml(connector: &Connector, shape_id: usize) -> String {
    let x = connector.start_x.min(connector.end_x);
    let y = connector.start_y.min(connector.end_y);
    let cx = connector.width();
    let cy = connector.height();

    let flip_h = if connector.flip_h() { " flipH=\"1\"" } else { "" };
    let flip_v = if connector.flip_v() { " flipV=\"1\"" } else { "" };

    let mut xml = format!(
        r#"<p:cxnSp>
<p:nvCxnSpPr>
<p:cNvPr id="{}" name="Connector {}"/>
<p:cNvCxnSpPr>"#,
        shape_id, shape_id
    );

    // Add connection references if connected to shapes
    if let (Some(start_id), Some(start_site)) = (connector.start_shape_id, connector.start_site) {
        xml.push_str(&format!(
            r#"
<a:stCxn id="{}" idx="{}"/>"#,
            start_id, start_site.index()
        ));
    }

    if let (Some(end_id), Some(end_site)) = (connector.end_shape_id, connector.end_site) {
        xml.push_str(&format!(
            r#"
<a:endCxn id="{}" idx="{}"/>"#,
            end_id, end_site.index()
        ));
    }

    xml.push_str(&format!(
        r#"
</p:cNvCxnSpPr>
<p:nvPr/>
</p:nvCxnSpPr>
<p:spPr>
<a:xfrm{}{}>
<a:off x="{}" y="{}"/>
<a:ext cx="{}" cy="{}"/>
</a:xfrm>
<a:prstGeom prst="{}">
<a:avLst/>
</a:prstGeom>
<a:ln w="{}">
<a:solidFill>
<a:srgbClr val="{}"/>
</a:solidFill>
<a:prstDash val="{}"/>"#,
        flip_h, flip_v,
        x, y, cx, cy,
        connector.connector_type.preset_name(),
        connector.line.width,
        connector.line.color,
        connector.line.dash.xml_value()
    ));

    // Add arrow heads
    if connector.start_arrow != ArrowType::None {
        xml.push_str(&format!(
            r#"
<a:headEnd type="{}" w="{}" len="{}"/>"#,
            connector.start_arrow.xml_value(),
            connector.arrow_size.xml_value(),
            connector.arrow_size.xml_value()
        ));
    }

    if connector.end_arrow != ArrowType::None {
        xml.push_str(&format!(
            r#"
<a:tailEnd type="{}" w="{}" len="{}"/>"#,
            connector.end_arrow.xml_value(),
            connector.arrow_size.xml_value(),
            connector.arrow_size.xml_value()
        ));
    }

    xml.push_str(r#"
</a:ln>
</p:spPr>"#);

    // Add label if present
    if let Some(label) = &connector.label {
        xml.push_str(&format!(
            r#"
<p:txBody>
<a:bodyPr/>
<a:lstStyle/>
<a:p>
<a:r>
<a:rPr lang="en-US" sz="1000"/>
<a:t>{}</a:t>
</a:r>
</a:p>
</p:txBody>"#,
            escape_xml(label)
        ));
    }

    xml.push_str(r#"
</p:cxnSp>"#);

    xml
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_connector_type_preset() {
        assert_eq!(ConnectorType::Straight.preset_name(), "straightConnector1");
        assert_eq!(ConnectorType::Elbow.preset_name(), "bentConnector3");
        assert_eq!(ConnectorType::Curved.preset_name(), "curvedConnector3");
    }

    #[test]
    fn test_arrow_type_xml() {
        assert_eq!(ArrowType::None.xml_value(), "none");
        assert_eq!(ArrowType::Triangle.xml_value(), "triangle");
        assert_eq!(ArrowType::Stealth.xml_value(), "stealth");
    }

    #[test]
    fn test_connector_builder() {
        let conn = Connector::straight(0, 0, 1000000, 500000)
            .with_color("FF0000")
            .with_end_arrow(ArrowType::Triangle);

        assert_eq!(conn.line.color, "FF0000");
        assert_eq!(conn.end_arrow, ArrowType::Triangle);
    }

    #[test]
    fn test_connector_with_connections() {
        let conn = Connector::elbow(0, 0, 1000000, 500000)
            .connect_start(1, ConnectionSite::Right)
            .connect_end(2, ConnectionSite::Left);

        assert_eq!(conn.start_shape_id, Some(1));
        assert_eq!(conn.start_site, Some(ConnectionSite::Right));
        assert_eq!(conn.end_shape_id, Some(2));
        assert_eq!(conn.end_site, Some(ConnectionSite::Left));
    }

    #[test]
    fn test_generate_connector_xml() {
        let conn = Connector::straight(0, 0, 1000000, 500000)
            .with_end_arrow(ArrowType::Triangle);

        let xml = generate_connector_xml(&conn, 1);
        assert!(xml.contains("p:cxnSp"));
        assert!(xml.contains("straightConnector1"));
        assert!(xml.contains("tailEnd"));
    }

    #[test]
    fn test_connector_with_label() {
        let conn = Connector::straight(0, 0, 1000000, 500000)
            .with_label("Connection");

        let xml = generate_connector_xml(&conn, 1);
        assert!(xml.contains("Connection"));
        assert!(xml.contains("p:txBody"));
    }

    #[test]
    fn test_line_dash_styles() {
        assert_eq!(LineDash::Solid.xml_value(), "solid");
        assert_eq!(LineDash::Dash.xml_value(), "dash");
        assert_eq!(LineDash::Dot.xml_value(), "dot");
        assert_eq!(LineDash::DashDot.xml_value(), "dashDot");
    }

    #[test]
    fn test_connection_site_index() {
        assert_eq!(ConnectionSite::Top.index(), 0);
        assert_eq!(ConnectionSite::Right.index(), 1);
        assert_eq!(ConnectionSite::Bottom.index(), 2);
        assert_eq!(ConnectionSite::Left.index(), 3);
    }
}