zen_rs/layouts/html/
icon.rs

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
use crate::components::icon::Icon;
// TODO: Create type for color for ICON
// - CurrenColor
// - None
// - HEX
pub fn icon_html(component: &Icon) -> String {
    // data
    let fg = component.get_foreground_color().to_string();
    let bg = component.get_background_color().to_string();
    let w = component.get_width();
    let h = component.get_height();
    let (bl, bt, bb, br) = component.get_view_box();

    // content
    let content = component.get_content();

    // content formating
    let first_path_wrap = |path: &str| format!(r#"<path stroke="none" d="{path}" "fill="{bg}" />"#);
    let path_wrap = |path: &str| format!(r#"<path d="{path}"/>"#);
    let first = if let Some(path) = content.first() {
        first_path_wrap(path)
    } else {
        "".to_string()
    };
    let content = &content[1..];
    let paths: String = content.iter().map(|x| path_wrap(x)).collect();

    // Specific svg attributes
    let slp = component
        .get_stroke_linecap()
        .map(|x| format!(r#"stroke-linecap="{x}""#))
        .unwrap_or_default();
    let slj = component
        .get_stroke_linejoin()
        .map(|x| format!(r#"stroke-linejoin="{x}""#))
        .unwrap_or_default();
    let sw = component
        .get_stroke_width()
        .map(|x| format!(r#"stroke-width="{x}""#))
        .unwrap_or_default();
    let vb = format!(r#"viewBox="{bl} {bt} {bb} {br}""#);

    // out
    format!(
        r#"
        <svg xmlns="http://www.w3.org/2000/svg"
        width="{w}"
        height="{h}"
        {vb}
        fill="{bg}"
        stroke="{fg}"
        {slp}
        {slj}
        {sw}>
        {first}
        {paths}</svg>
    "#
    )
}