freedraw/
utils.rs

1/// Calculates the average of two numbers
2fn average(a: f64, b: f64) -> f64 {
3    (a + b) / 2.0
4}
5
6/// Converts a stroke's outline points to an SVG path data string
7///
8/// # Arguments
9/// * `points` - The outline points returned by `get_stroke`
10/// * `closed` - Whether to close the path with a 'Z' command
11///
12/// # Returns
13/// A string containing SVG path commands
14pub fn get_svg_path_from_stroke(points: &[[f64; 2]], closed: bool) -> String {
15    let len = points.len();
16
17    if len < 4 {
18        return String::new();
19    }
20
21    let a = points[0];
22    let b = points[1];
23    let c = points[2];
24
25    // Format with exactly 2 decimal places for consistent output
26    let mut result = format!(
27        "M{:.2},{:.2} Q{:.2},{:.2} {:.2},{:.2} T",
28        a[0],
29        a[1],
30        b[0],
31        b[1],
32        average(b[0], c[0]),
33        average(b[1], c[1])
34    );
35
36    for i in 2..(len - 1) {
37        let a = points[i];
38        let b = points[i + 1];
39        result.push_str(&format!(
40            "{:.2},{:.2} ",
41            average(a[0], b[0]),
42            average(a[1], b[1])
43        ));
44    }
45
46    if closed {
47        result.push('Z');
48    }
49
50    result
51}