fd-core 0.1.18

FD (Fast Draft) — core data model, parser, emitter, and layout solver
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
//! Excalidraw JSON export: SceneGraph → Excalidraw v2 JSON.
//!
//! Maps FD nodes to Excalidraw elements:
//! - Rect/Frame → `rectangle`
//! - Ellipse → `ellipse`
//! - Text → `text`
//! - Path → `freedraw`
//! - Group → (children emitted individually)

use crate::id::NodeId;
use crate::model::*;
use petgraph::graph::NodeIndex;
use std::collections::HashMap;
use std::fmt::Write;

/// Convert a SceneGraph (or a subset of selected nodes) to Excalidraw v2 JSON.
///
/// If `selected_ids` is empty, all top-level nodes are exported.
/// Returns a valid JSON string ready to paste into excalidraw.com.
pub fn export_excalidraw(
    graph: &SceneGraph,
    bounds: &HashMap<NodeIndex, ResolvedBounds>,
    selected_ids: &[String],
) -> String {
    let root_selection = collect_root_selection(graph, selected_ids);

    let mut elements = Vec::new();
    let mut id_counter: u64 = 1;

    for &idx in &root_selection {
        collect_elements(graph, idx, bounds, &mut elements, &mut id_counter);
    }

    // Build JSON manually to avoid serde dependency for this one feature
    let mut out = String::with_capacity(2048);
    out.push_str("{\n  \"type\": \"excalidraw\",\n  \"version\": 2,\n  \"elements\": [\n");

    for (i, el) in elements.iter().enumerate() {
        if i > 0 {
            out.push_str(",\n");
        }
        write_element_json(&mut out, el);
    }

    out.push_str("\n  ]\n}");
    out
}

/// An intermediate Excalidraw element representation.
struct ExcalidrawElement {
    id: String,
    element_type: &'static str,
    x: f32,
    y: f32,
    width: f32,
    height: f32,
    stroke_color: String,
    background_color: String,
    fill_style: &'static str,
    stroke_width: f32,
    opacity: f32,
    /// For text elements only.
    text: Option<String>,
    font_size: Option<f32>,
    /// For freedraw elements only.
    points: Option<Vec<(f32, f32)>>,
    corner_radius: Option<f32>,
}

/// Collect root-level node indices to export, filtering by selection.
fn collect_root_selection(graph: &SceneGraph, selected_ids: &[String]) -> Vec<NodeIndex> {
    if selected_ids.is_empty() {
        return graph.children(graph.root);
    }

    let mut roots = Vec::new();
    for id_str in selected_ids {
        let id = NodeId::intern(id_str);
        if let Some(idx) = graph.index_of(id) {
            // Skip if an ancestor is already selected
            let has_selected_ancestor = selected_ids
                .iter()
                .any(|other| other != id_str && graph.is_ancestor_of(NodeId::intern(other), id));
            if !has_selected_ancestor {
                roots.push(idx);
            }
        }
    }
    roots
}

/// Recursively collect Excalidraw elements from the scene graph.
fn collect_elements(
    graph: &SceneGraph,
    idx: NodeIndex,
    bounds: &HashMap<NodeIndex, ResolvedBounds>,
    elements: &mut Vec<ExcalidrawElement>,
    id_counter: &mut u64,
) {
    let node = &graph.graph[idx];
    let b = match bounds.get(&idx) {
        Some(b) => b,
        None => return,
    };

    let style = graph.resolve_style(node, &[]);
    let opacity = style.opacity.unwrap_or(1.0);
    let stroke_color = style
        .stroke
        .as_ref()
        .map(|s| paint_to_hex(&s.paint))
        .unwrap_or_else(|| "#000000".to_string());
    let stroke_width = style.stroke.as_ref().map(|s| s.width).unwrap_or(1.0);
    let bg_color = style
        .fill
        .as_ref()
        .map(paint_to_hex)
        .unwrap_or_else(|| "transparent".to_string());
    let fill_style = if bg_color == "transparent" {
        "hachure"
    } else {
        "solid"
    };
    let corner_radius = style.corner_radius;

    let unique_id = format!("fd_{}", {
        let v = *id_counter;
        *id_counter += 1;
        v
    });

    match &node.kind {
        NodeKind::Rect { .. } | NodeKind::Frame { .. } => {
            elements.push(ExcalidrawElement {
                id: unique_id,
                element_type: "rectangle",
                x: b.x,
                y: b.y,
                width: b.width,
                height: b.height,
                stroke_color,
                background_color: bg_color,
                fill_style,
                stroke_width,
                opacity,
                text: None,
                font_size: None,
                points: None,
                corner_radius,
            });
        }
        NodeKind::Ellipse { .. } => {
            elements.push(ExcalidrawElement {
                id: unique_id,
                element_type: "ellipse",
                x: b.x,
                y: b.y,
                width: b.width,
                height: b.height,
                stroke_color,
                background_color: bg_color,
                fill_style,
                stroke_width,
                opacity,
                text: None,
                font_size: None,
                points: None,
                corner_radius: None,
            });
        }
        NodeKind::Text { content, .. } => {
            let font_size = style.font.as_ref().map(|f| f.size).unwrap_or(16.0);
            let text_color = style
                .fill
                .as_ref()
                .map(paint_to_hex)
                .unwrap_or_else(|| "#000000".to_string());

            elements.push(ExcalidrawElement {
                id: unique_id,
                element_type: "text",
                x: b.x,
                y: b.y,
                width: b.width,
                height: b.height,
                stroke_color: text_color,
                background_color: "transparent".to_string(),
                fill_style: "hachure",
                stroke_width: 0.0,
                opacity,
                text: Some(content.replace('\\', "\n")),
                font_size: Some(font_size),
                points: None,
                corner_radius: None,
            });
        }
        NodeKind::Path { commands } => {
            let mut points = Vec::new();
            for cmd in commands {
                match cmd {
                    PathCmd::MoveTo(x, y) | PathCmd::LineTo(x, y) => {
                        points.push((*x, *y));
                    }
                    PathCmd::QuadTo(_, _, x, y) => {
                        points.push((*x, *y));
                    }
                    PathCmd::CubicTo(_, _, _, _, x, y) => {
                        points.push((*x, *y));
                    }
                    PathCmd::Close => {
                        if let Some(&first) = points.first() {
                            points.push(first);
                        }
                    }
                }
            }

            elements.push(ExcalidrawElement {
                id: unique_id,
                element_type: "freedraw",
                x: b.x,
                y: b.y,
                width: b.width,
                height: b.height,
                stroke_color,
                background_color: "transparent".to_string(),
                fill_style: "hachure",
                stroke_width,
                opacity,
                text: None,
                font_size: None,
                points: Some(points),
                corner_radius: None,
            });
        }
        NodeKind::Image { .. } => {
            // Images export as placeholder rectangles (Excalidraw has no image type)
            elements.push(ExcalidrawElement {
                id: unique_id,
                element_type: "rectangle",
                x: b.x,
                y: b.y,
                width: b.width,
                height: b.height,
                stroke_color,
                background_color: bg_color,
                fill_style,
                stroke_width,
                opacity,
                text: None,
                font_size: None,
                points: None,
                corner_radius,
            });
        }
        NodeKind::Group | NodeKind::Root | NodeKind::Generic => {
            // Groups are organizational — emit children only
        }
    }

    // Recurse into children
    for child_idx in graph.children(idx) {
        collect_elements(graph, child_idx, bounds, elements, id_counter);
    }
}

/// Convert a Paint to a hex color string.
fn paint_to_hex(paint: &Paint) -> String {
    match paint {
        Paint::Solid(c) => c.to_hex(),
        Paint::LinearGradient { stops, .. } | Paint::RadialGradient { stops } => stops
            .first()
            .map(|s| s.color.to_hex())
            .unwrap_or_else(|| "#000000".to_string()),
    }
}

/// Write a single Excalidraw element as JSON.
fn write_element_json(out: &mut String, el: &ExcalidrawElement) {
    out.push_str("    {\n");
    let _ = writeln!(out, "      \"id\": \"{}\",", el.id);
    let _ = writeln!(out, "      \"type\": \"{}\",", el.element_type);
    let _ = writeln!(out, "      \"x\": {},", format_num(el.x));
    let _ = writeln!(out, "      \"y\": {},", format_num(el.y));
    let _ = writeln!(out, "      \"width\": {},", format_num(el.width));
    let _ = writeln!(out, "      \"height\": {},", format_num(el.height));
    let _ = writeln!(out, "      \"angle\": 0,");
    let _ = writeln!(out, "      \"strokeColor\": \"{}\",", el.stroke_color);
    let _ = writeln!(
        out,
        "      \"backgroundColor\": \"{}\",",
        el.background_color
    );
    let _ = writeln!(out, "      \"fillStyle\": \"{}\",", el.fill_style);
    let _ = writeln!(
        out,
        "      \"strokeWidth\": {},",
        format_num(el.stroke_width)
    );
    let _ = writeln!(out, "      \"roughness\": 0,");
    let _ = writeln!(out, "      \"opacity\": {},", (el.opacity * 100.0) as u32);

    if let Some(r) = el.corner_radius
        && r > 0.0
    {
        let _ = writeln!(
            out,
            "      \"roundness\": {{ \"type\": 3, \"value\": {} }},",
            format_num(r)
        );
    }

    if let Some(ref text) = el.text {
        // Escape special JSON characters in text content
        let escaped = text
            .replace('\\', "\\\\")
            .replace('"', "\\\"")
            .replace('\n', "\\n")
            .replace('\r', "\\r")
            .replace('\t', "\\t");
        let _ = writeln!(out, "      \"text\": \"{}\",", escaped);
        let _ = writeln!(
            out,
            "      \"fontSize\": {},",
            format_num(el.font_size.unwrap_or(16.0))
        );
        let _ = writeln!(out, "      \"fontFamily\": 1,");
        let _ = writeln!(out, "      \"textAlign\": \"left\",");
        let _ = writeln!(out, "      \"verticalAlign\": \"top\",");
    }

    if let Some(ref points) = el.points {
        out.push_str("      \"points\": [");
        for (i, (x, y)) in points.iter().enumerate() {
            if i > 0 {
                out.push_str(", ");
            }
            let _ = write!(out, "[{}, {}]", format_num(*x), format_num(*y));
        }
        out.push_str("],\n");
    }

    // seed is required by Excalidraw for deterministic rendering
    let _ = writeln!(out, "      \"seed\": 1,");
    let _ = writeln!(out, "      \"version\": 1,");
    let _ = write!(out, "      \"versionNonce\": 1");

    out.push_str("\n    }");
}

/// Format a float: strip trailing zeros, emit integers without decimal point.
fn format_num(v: f32) -> String {
    if v == v.floor() && v.abs() < 1e9 {
        format!("{}", v as i64)
    } else {
        let s = format!("{:.2}", v);
        s.trim_end_matches('0').trim_end_matches('.').to_string()
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────

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

    fn parse_and_resolve(input: &str) -> (SceneGraph, HashMap<NodeIndex, ResolvedBounds>) {
        let graph = parse_document(input).expect("test parse failed");
        let viewport = crate::layout::Viewport {
            width: 800.0,
            height: 600.0,
        };
        let bounds = crate::layout::resolve_layout(&graph, viewport);
        (graph, bounds)
    }

    #[test]
    fn export_excalidraw_rect() {
        let (graph, bounds) = parse_and_resolve("rect @box { w: 100 h: 60 fill: #FF0000 }");
        let json = export_excalidraw(&graph, &bounds, &[]);

        assert!(json.contains("\"type\": \"excalidraw\""));
        assert!(json.contains("\"type\": \"rectangle\""));
        assert!(json.contains("\"width\": 100"));
        assert!(json.contains("\"height\": 60"));
        assert!(json.contains("\"backgroundColor\": \"#FF0000\""));
    }

    #[test]
    fn export_excalidraw_ellipse() {
        let (graph, bounds) = parse_and_resolve("ellipse @circle { w: 80 h: 80 fill: #00FF00 }");
        let json = export_excalidraw(&graph, &bounds, &[]);

        assert!(json.contains("\"type\": \"ellipse\""));
        // Ellipse dimensions come from layout resolver (may differ from source w:/h:)
        assert!(json.contains("\"width\":"));
        assert!(json.contains("\"height\":"));
        assert!(json.contains("\"backgroundColor\": \"#00FF00\""));
    }

    #[test]
    fn export_excalidraw_text() {
        let (graph, bounds) = parse_and_resolve("text @label \"Hello World\" {}");
        let json = export_excalidraw(&graph, &bounds, &[]);

        assert!(json.contains("\"type\": \"text\""));
        assert!(json.contains("\"text\": \"Hello World\""));
        assert!(json.contains("\"fontSize\":"));
    }

    #[test]
    fn export_excalidraw_selection() {
        let input = "rect @a { w: 100 h: 50 }\nrect @b { w: 200 h: 80 }";
        let (graph, bounds) = parse_and_resolve(input);

        // Export only @a
        let json = export_excalidraw(&graph, &bounds, &["a".to_string()]);

        // Should have exactly one element
        let count = json.matches("\"type\": \"rectangle\"").count();
        assert_eq!(count, 1, "Should export only the selected node");
        assert!(json.contains("\"width\": 100"));
    }

    #[test]
    fn export_excalidraw_empty_graph() {
        let graph = SceneGraph::new();
        let bounds = HashMap::new();
        let json = export_excalidraw(&graph, &bounds, &[]);

        assert!(json.contains("\"type\": \"excalidraw\""));
        assert!(json.contains("\"elements\": ["));
        // No elements in empty graph
        assert!(!json.contains("\"seed\""));
    }

    #[test]
    fn export_excalidraw_valid_json() {
        let input = r#"
rect @card { w: 200 h: 150 fill: #3498DB corner: 12 }
ellipse @avatar { w: 60 h: 60 fill: #E74C3C }
text @title "Dashboard" { font: "Inter" bold 24 }
"#;
        let (graph, bounds) = parse_and_resolve(input);
        let json = export_excalidraw(&graph, &bounds, &[]);

        // Verify it parses as valid JSON by checking structural integrity
        assert!(json.starts_with('{'));
        assert!(json.ends_with('}'));
        assert!(json.contains("\"type\": \"excalidraw\""));
        assert!(json.contains("\"version\": 2"));

        // Count elements: rect + ellipse + text = 3
        let element_count = json.matches("\"seed\": 1").count();
        assert_eq!(element_count, 3);
    }

    #[test]
    fn export_excalidraw_group_children() {
        let input = r#"
group @g {
  rect @inner { w: 50 h: 30 }
}
"#;
        let (graph, bounds) = parse_and_resolve(input);
        let json = export_excalidraw(&graph, &bounds, &[]);

        // Group itself is not exported, but its child rect is
        assert!(json.contains("\"type\": \"rectangle\""));
        assert!(!json.contains("\"type\": \"group\""));
    }

    #[test]
    fn export_excalidraw_corner_radius() {
        let (graph, bounds) = parse_and_resolve("rect @r { w: 100 h: 50 corner: 8 }");
        let json = export_excalidraw(&graph, &bounds, &[]);

        assert!(json.contains("\"roundness\""));
        assert!(json.contains("\"value\": 8"));
    }

    #[test]
    fn export_excalidraw_opacity() {
        let (graph, bounds) = parse_and_resolve("rect @r { w: 100 h: 50 opacity: 0.5 }");
        let json = export_excalidraw(&graph, &bounds, &[]);

        // Excalidraw uses 0-100 for opacity
        assert!(json.contains("\"opacity\": 50"));
    }
}