ppt-rs 0.2.12

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
//! Flowchart diagram parsing and rendering

use super::types::{DiagramBounds, *};
use crate::generator::connectors::{
    ArrowType, ConnectionSite, Connector, ConnectorLine, ConnectorType, LineDash,
};
use crate::generator::{Shape, ShapeFill, ShapeLine, ShapeType};
use std::collections::HashMap;

/// Parse flowchart direction from first line
fn parse_direction(first_line: &str) -> FlowDirection {
    let line = first_line.to_uppercase();
    if line.contains("LR") {
        FlowDirection::LeftToRight
    } else if line.contains("RL") {
        FlowDirection::RightToLeft
    } else if line.contains("BT") {
        FlowDirection::BottomToTop
    } else {
        FlowDirection::TopToBottom
    }
}

/// Parse a flowchart from Mermaid code
pub fn parse(code: &str) -> Flowchart {
    let mut lines = code.lines();
    let first_line = lines.next().unwrap_or("");
    let direction = parse_direction(first_line);

    let mut nodes: HashMap<String, FlowNode> = HashMap::new();
    let mut connections: Vec<FlowConnection> = Vec::new();
    let mut subgraphs: Vec<Subgraph> = Vec::new();
    let mut current_subgraph: Option<Subgraph> = None;

    for line in lines {
        let line = line.trim();
        if line.is_empty() || line.starts_with("%%") {
            continue;
        }

        // Handle subgraph start
        if line.starts_with("subgraph") {
            let name = line
                .strip_prefix("subgraph")
                .unwrap_or("")
                .trim()
                .to_string();
            current_subgraph = Some(Subgraph {
                name,
                nodes: Vec::new(),
            });
            continue;
        }

        // Handle subgraph end
        if line == "end" {
            if let Some(sg) = current_subgraph.take() {
                subgraphs.push(sg);
            }
            continue;
        }

        // Parse connections: A --> B, A --> B[Label], A[Text] --> B[Text]
        if let Some((from_part, rest)) = split_connection(line) {
            let (arrow_type, to_part) = parse_arrow_and_rest(&rest);

            // Parse from node
            let (from_id, from_node) = parse_node_def(&from_part);
            if let Some(node) = from_node {
                nodes.entry(from_id.clone()).or_insert(node);
                if let Some(ref mut sg) = current_subgraph {
                    if !sg.nodes.contains(&from_id) {
                        sg.nodes.push(from_id.clone());
                    }
                }
            }

            // Parse to node (may have label on arrow)
            let (to_part_clean, arrow_label) = extract_arrow_label(&to_part);
            let (to_id, to_node) = parse_node_def(&to_part_clean);
            if let Some(node) = to_node {
                nodes.entry(to_id.clone()).or_insert(node);
                if let Some(ref mut sg) = current_subgraph {
                    if !sg.nodes.contains(&to_id) {
                        sg.nodes.push(to_id.clone());
                    }
                }
            }

            connections.push(FlowConnection {
                from: from_id,
                to: to_id,
                label: arrow_label,
                arrow_type,
            });
        } else {
            // Standalone node definition
            let (id, node) = parse_node_def(line);
            if let Some(n) = node {
                nodes.entry(id.clone()).or_insert(n);
                if let Some(ref mut sg) = current_subgraph {
                    if !sg.nodes.contains(&id) {
                        sg.nodes.push(id);
                    }
                }
            }
        }
    }

    Flowchart {
        direction,
        nodes: nodes.into_values().collect(),
        connections,
        subgraphs,
    }
}

/// Split line at connection arrow
fn split_connection(line: &str) -> Option<(String, String)> {
    for arrow in ["==>", "-.->", "-->", "---", "->"] {
        if let Some(pos) = line.find(arrow) {
            let from = line[..pos].trim().to_string();
            let rest = line[pos..].to_string();
            return Some((from, rest));
        }
    }
    None
}

/// Parse arrow type and get the rest of the string
fn parse_arrow_and_rest(s: &str) -> (ArrowStyle, String) {
    if s.starts_with("==>") {
        (ArrowStyle::Thick, s[3..].trim().to_string())
    } else if s.starts_with("-.->") {
        (ArrowStyle::Dotted, s[4..].trim().to_string())
    } else if s.starts_with("-->") {
        (ArrowStyle::Arrow, s[3..].trim().to_string())
    } else if s.starts_with("---") {
        (ArrowStyle::Open, s[3..].trim().to_string())
    } else if s.starts_with("->") {
        (ArrowStyle::Arrow, s[2..].trim().to_string())
    } else {
        (ArrowStyle::Arrow, s.to_string())
    }
}

/// Extract arrow label like |text|
fn extract_arrow_label(s: &str) -> (String, Option<String>) {
    if let Some(start) = s.find('|') {
        if let Some(end) = s[start + 1..].find('|') {
            let label = s[start + 1..start + 1 + end].to_string();
            let rest = s[start + 2 + end..].trim().to_string();
            return (rest, Some(label));
        }
    }
    (s.to_string(), None)
}

/// Parse a node definition like A[Text] or B(Text) or C{Text}
fn parse_node_def(s: &str) -> (String, Option<FlowNode>) {
    let s = s.trim();

    // Try different bracket types
    for (open, close, shape) in [
        ("((", "))", NodeShape::Circle),
        ("([", "])", NodeShape::Stadium),
        ("{{", "}}", NodeShape::Hexagon),
        ("[", "]", NodeShape::Rectangle),
        ("(", ")", NodeShape::RoundedRect),
        ("{", "}", NodeShape::Diamond),
    ] {
        if let Some(start) = s.find(open) {
            let id = s[..start].trim().to_string();
            if let Some(end) = s[start + open.len()..].find(close) {
                let label = s[start + open.len()..start + open.len() + end].to_string();
                return (id.clone(), Some(FlowNode { id, label, shape }));
            }
        }
    }

    // Plain node ID without brackets
    let id = s.to_string();
    if !id.is_empty() && id.chars().all(|c| c.is_alphanumeric() || c == '_') {
        return (
            id.clone(),
            Some(FlowNode {
                id: id.clone(),
                label: id,
                shape: NodeShape::Rectangle,
            }),
        );
    }

    (s.to_string(), None)
}

/// Generate shapes and connectors for a flowchart
pub fn generate_elements(flowchart: &Flowchart) -> DiagramElements {
    let mut shapes = Vec::new();
    let mut connectors = Vec::new();
    let node_count = flowchart.nodes.len();

    if node_count == 0 {
        return DiagramElements {
            shapes,
            connectors,
            bounds: None,
            grouped: false,
        };
    }

    // Track element positions for bounding box calculation
    let mut element_bounds: Vec<(u32, u32, u32, u32)> = Vec::new();

    // Layout parameters (in EMUs)
    let node_width = 1_400_000u32;
    let node_height = 500_000u32;
    let h_spacing = 1_800_000u32;
    let v_spacing = 900_000u32;

    let is_horizontal = matches!(
        flowchart.direction,
        FlowDirection::LeftToRight | FlowDirection::RightToLeft
    );

    // Track node positions and their shape IDs for connector anchoring
    let mut node_positions: HashMap<String, (u32, u32)> = HashMap::new();
    let mut node_shape_ids: HashMap<String, u32> = HashMap::new();
    let mut shape_id = 10u32; // Starting shape ID

    // If we have subgraphs, layout by subgraph
    if !flowchart.subgraphs.is_empty() {
        let mut subgraph_x = 500_000u32;
        let subgraph_start_y = 1_600_000u32;

        for (sg_idx, subgraph) in flowchart.subgraphs.iter().enumerate() {
            let sg_width = node_width + 400_000;
            let title_height = 250_000u32;
            let sg_height = (subgraph.nodes.len() as u32) * v_spacing + 400_000 + title_height;
            let sg_x = subgraph_x;
            let sg_y = subgraph_start_y;

            // Subgraph background shape (no text - just background)
            let sg_shape = Shape::new(ShapeType::RoundedRectangle, sg_x, sg_y, sg_width, sg_height)
                .with_fill(ShapeFill::new(get_subgraph_color(sg_idx)))
                .with_line(ShapeLine::new("757575", 1));
            shapes.push(sg_shape);
            element_bounds.push((sg_x, sg_y, sg_width, sg_height));

            // Subgraph title at top
            let title_shape = Shape::new(
                ShapeType::Rectangle,
                sg_x + 50_000,
                sg_y + 50_000,
                sg_width - 100_000,
                title_height,
            )
            .with_id(shape_id)
            .with_text(&subgraph.name);
            shapes.push(title_shape);
            shape_id += 1;

            // Layout nodes within subgraph (offset by title height)
            for (node_idx, node_id) in subgraph.nodes.iter().enumerate() {
                if let Some(node) = flowchart.nodes.iter().find(|n| &n.id == node_id) {
                    let x = sg_x + 200_000;
                    let y = sg_y + title_height + 200_000 + (node_idx as u32) * v_spacing;

                    node_positions.insert(node.id.clone(), (x, y));
                    node_shape_ids.insert(node.id.clone(), shape_id);

                    let shape = create_node_shape(node, x, y, node_width, node_height, shape_id);
                    shapes.push(shape);
                    element_bounds.push((x, y, node_width, node_height));
                    shape_id += 1;
                }
            }

            subgraph_x += sg_width + 600_000;
        }

        // Layout any nodes not in subgraphs
        let mut orphan_y = subgraph_start_y;
        for node in &flowchart.nodes {
            if !node_positions.contains_key(&node.id) {
                let x = subgraph_x;
                let y = orphan_y;

                node_positions.insert(node.id.clone(), (x, y));
                node_shape_ids.insert(node.id.clone(), shape_id);

                let shape = create_node_shape(node, x, y, node_width, node_height, shape_id);
                shapes.push(shape);
                element_bounds.push((x, y, node_width, node_height));
                shape_id += 1;

                orphan_y += v_spacing;
            }
        }
    } else {
        // Simple grid layout without subgraphs
        let start_x = 1_000_000u32;
        let start_y = 1_800_000u32;
        let cols = if is_horizontal { node_count.min(5) } else { 1 };

        for (i, node) in flowchart.nodes.iter().enumerate() {
            let col = i % cols;
            let row = i / cols;

            let (x, y) = if is_horizontal {
                (
                    start_x + (col as u32) * h_spacing,
                    start_y + (row as u32) * v_spacing,
                )
            } else {
                (
                    start_x + (col as u32) * h_spacing,
                    start_y + (i as u32) * v_spacing,
                )
            };

            node_positions.insert(node.id.clone(), (x, y));
            node_shape_ids.insert(node.id.clone(), shape_id);

            let shape = create_node_shape(node, x, y, node_width, node_height, shape_id);
            shapes.push(shape);
            element_bounds.push((x, y, node_width, node_height));
            shape_id += 1;
        }
    }

    // Create connectors for connections with shape anchoring
    for conn in &flowchart.connections {
        if let (Some(&(from_x, from_y)), Some(&(to_x, to_y))) =
            (node_positions.get(&conn.from), node_positions.get(&conn.to))
        {
            // Get shape IDs for anchoring
            let from_shape_id = node_shape_ids.get(&conn.from).copied();
            let to_shape_id = node_shape_ids.get(&conn.to).copied();

            // Determine connection sites and positions based on relative positions
            let (start_site, end_site, start_x, start_y, end_x, end_y) = if is_horizontal {
                // Horizontal flow: connect Right -> Left
                (
                    ConnectionSite::Right,
                    ConnectionSite::Left,
                    from_x + node_width,
                    from_y + node_height / 2,
                    to_x,
                    to_y + node_height / 2,
                )
            } else {
                // Vertical flow: connect Bottom -> Top
                (
                    ConnectionSite::Bottom,
                    ConnectionSite::Top,
                    from_x + node_width / 2,
                    from_y + node_height,
                    to_x + node_width / 2,
                    to_y,
                )
            };

            // Use elbow connector for better auto-routing when shapes are not aligned
            let connector_type = if (start_x as i32 - end_x as i32).abs() < 100_000
                || (start_y as i32 - end_y as i32).abs() < 100_000
            {
                ConnectorType::Straight
            } else {
                ConnectorType::Elbow
            };

            let (line_color, line_dash) = match conn.arrow_type {
                ArrowStyle::Thick => ("E65100", LineDash::Solid),
                ArrowStyle::Dotted => ("757575", LineDash::Dash),
                ArrowStyle::Open => ("1565C0", LineDash::Solid),
                ArrowStyle::Arrow => ("1565C0", LineDash::Solid),
            };

            let mut connector = Connector::new(connector_type, start_x, start_y, end_x, end_y)
                .with_line(ConnectorLine::new(line_color, 19050).with_dash(line_dash))
                .with_end_arrow(ArrowType::Triangle);

            // Anchor connector to shapes for auto-routing
            if let Some(from_id) = from_shape_id {
                connector = connector.connect_start(from_id, start_site);
            }
            if let Some(to_id) = to_shape_id {
                connector = connector.connect_end(to_id, end_site);
            }

            // Create separate label shape for better font control
            if let Some(label) = &conn.label {
                let label_width = 600_000u32;
                let label_height = 250_000u32;
                let mid_x = (start_x + end_x) / 2;
                let mid_y = (start_y + end_y) / 2;

                let label_shape = Shape::new(
                    ShapeType::Rectangle,
                    mid_x.saturating_sub(label_width / 2),
                    mid_y.saturating_sub(label_height / 2),
                    label_width,
                    label_height,
                )
                .with_id(shape_id)
                .with_fill(ShapeFill::new("FFFFFF"))
                .with_line(ShapeLine::new("757575", 1))
                .with_text(label);

                shapes.push(label_shape);
                element_bounds.push((
                    mid_x - label_width / 2,
                    mid_y - label_height / 2,
                    label_width,
                    label_height,
                ));
                shape_id += 1;
            }

            connectors.push(connector);
        }
    }

    // Calculate bounding box for the entire diagram
    let bounds = DiagramBounds::from_elements(&element_bounds);

    DiagramElements {
        shapes,
        connectors,
        bounds,
        grouped: true, // Flowcharts should be grouped
    }
}

fn get_subgraph_color(index: usize) -> &'static str {
    const COLORS: [&str; 6] = ["E3F2FD", "F3E5F5", "E8F5E9", "FFF3E0", "E0F7FA", "FCE4EC"];
    COLORS[index % COLORS.len()]
}

fn create_node_shape(
    node: &FlowNode,
    x: u32,
    y: u32,
    width: u32,
    height: u32,
    shape_id: u32,
) -> Shape {
    let shape_type = match node.shape {
        NodeShape::Rectangle => ShapeType::Rectangle,
        NodeShape::RoundedRect => ShapeType::RoundedRectangle,
        NodeShape::Stadium => ShapeType::RoundedRectangle,
        NodeShape::Diamond => ShapeType::Diamond,
        NodeShape::Circle => ShapeType::Ellipse,
        NodeShape::Hexagon => ShapeType::Hexagon,
    };

    let fill_color = match node.shape {
        NodeShape::Diamond => "FFF3E0",
        NodeShape::Circle => "E3F2FD",
        _ => "FFFFFF",
    };

    Shape::new(shape_type, x, y, width, height)
        .with_id(shape_id)
        .with_fill(ShapeFill::new(fill_color))
        .with_line(ShapeLine::new("1565C0", 2))
        .with_text(&node.label)
}

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

    #[test]
    fn test_parse_flowchart_nodes() {
        let code = "flowchart LR\n    A[Start] --> B[Process] --> C[End]";
        let flowchart = parse(code);
        assert_eq!(flowchart.direction, FlowDirection::LeftToRight);
        assert!(!flowchart.nodes.is_empty());
        assert!(!flowchart.connections.is_empty());
    }

    #[test]
    fn test_parse_node_shapes() {
        let (id, node) = parse_node_def("A[Rectangle]");
        assert_eq!(id, "A");
        assert!(node.is_some());
        assert_eq!(node.unwrap().shape, NodeShape::Rectangle);

        let (id, node) = parse_node_def("B(Rounded)");
        assert_eq!(id, "B");
        assert_eq!(node.unwrap().shape, NodeShape::RoundedRect);

        let (id, node) = parse_node_def("C{Diamond}");
        assert_eq!(id, "C");
        assert_eq!(node.unwrap().shape, NodeShape::Diamond);
    }

    #[test]
    fn test_generate_flowchart_shapes() {
        let code = "flowchart LR\n    A[Start] --> B[End]";
        let flowchart = parse(code);
        let elements = generate_elements(&flowchart);
        assert!(!elements.shapes.is_empty());
    }
}