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
//! Hello fellow Rustacians! Here is a crate with SVG Definitions.
//! This was mostly created to serve as a backend crate for [wasm_svg_graphics](https://crates.io/crates/wasm_svg_graphics),
//! but feel free to use it!
//!
//! I am open to pull requests so please contribute!
//!
//!
//! # Example
//! ## Creating a group with a triangle
//! ```
//! use svg_definitions::prelude::*;
//!
//! let triangle = SVGElem::new(Tag::Path)
//!     .set(Attr::StrokeWidth, 1)
//!     .set(Attr::Stroke, "#000")
//!     .set(Attr::Fill, "transparent")
//!     .set(Attr::D, PathData::new()
//!         .move_to((0.0, 0.0))
//!         .line_to((10.0, 0.0))
//!         .line_to((0.0, 10.0))
//!         .line_to((0.0, 0.0))
//!         .close_path()
//!     );
//!
//! let group = SVGElem::new(Tag::G)
//!     .append(triangle);
//! ```
//!
//! ## Getting a svg from a file
//! *The feature "parsing" needs to be enabled for this*
//! ```
//! use svg_definitions::prelude::*;
//!
//! let shape = SVGParseFile("/path/to/file.svg");
//!
//! // ...
//! ```
//!
//! ## Getting a svg from text
//! *The feature "parsing" needs to be enabled for this*
//! ```
//! use svg_definitions::prelude::*;
//!
//! let rect = SVGParseText("<rect width=\"50px\" height=\"50\" fill=\"black\" />");
//!
//! // ...
//! ```

pub mod prelude;

pub mod attributes;
pub mod path;
pub mod tag_name;

#[cfg(feature = "parsing")]
pub mod parser;

pub type Point2D = (f32, f32);

use std::collections::HashMap;
use std::hash::{Hash, Hasher};

use attributes::Attribute;
use tag_name::TagName;

type Attributes = HashMap<Attribute, String>;
type Children = Vec<Element>;

/// Element provides a way to simulate DOM SVG elements
#[derive(Debug)]
pub struct Element {
    tag_name: TagName,
    attributes: Attributes,
    children: Children,
    inner: Option<String>,
}

fn is_allowed_inner(character: char) -> bool {
    const NON_ALPHANUMERIC_ALLOWED_CHARACTERS: &'static str = r#"' \-_/.!?:;(){}[]`~&,""#;

    return character.is_ascii_alphanumeric()
        || NON_ALPHANUMERIC_ALLOWED_CHARACTERS.contains(character);
}

// Implementation of Element
impl Element {
    /// Creates a new Element with a certain tag_name
    pub fn new(tag_name: TagName) -> Element {
        Element {
            tag_name,
            attributes: HashMap::new(),
            children: Vec::new(),
            inner: None,
        }
    }

    /// Appends an element to the children of the self element
    /// and consumes both whilst returning the product
    #[inline]
    pub fn append(mut self, child: Element) -> Self {
        self.children.push(child);
        self
    }

    /// Sets the inner text to a plain string
    /// Allowed characters are *a-zA-Z0-9'" -_/\.!?:;(){}[]`~&,*
    #[inline]
    pub fn set_inner(mut self, text: &str) -> Self {
        if !text.chars().all(is_allowed_inner) {
            return self;
        }

        self.inner = Some(String::from(String::from(text).trim()));
        self
    }

    /// Sets an attribute of the self element to a certain value
    #[inline]
    pub fn set<T>(mut self, attribute: Attribute, value: T) -> Self
    where
        T: ToString,
    {
        self.attributes.insert(attribute, value.to_string());
        self
    }

    /// Gets an immutable reference to the tag_name of this Element
    #[inline]
    pub fn get_tag_name(&self) -> &TagName {
        &self.tag_name
    }

    /// Gets an immutable reference to the attributes of this Element
    #[inline]
    pub fn get_attributes(&self) -> &Attributes {
        &self.attributes
    }

    /// Gets an immutable reference to the children of this Element
    #[inline]
    pub fn get_children(&self) -> &Children {
        &self.children
    }

    /// Gets a clone of the inner text
    #[inline]
    pub fn get_inner(&self) -> &Option<String> {
        &self.inner
    }
}

impl Clone for Element {
    fn clone(&self) -> Self {
        let mut elem = Element::new(self.tag_name);
        for (key, value) in self.attributes.iter() {
            elem.attributes.insert(key.clone(), value.clone());
        }
        for child in self.children.iter() {
            elem = elem.append(child.clone());
        }
        if let Some(inr) = &self.inner {
            elem.inner = Some(inr.to_owned());
        }
        elem
    }
}

impl Hash for Element {
    fn hash<T: Hasher>(&self, state: &mut T) {
        self.tag_name.hash(state);
        self.attributes.iter().for_each(|(key, value)| {
            key.hash(state);
            value.hash(state);
        });
        self.children.iter().for_each(|child| child.hash(state));
    }
}

impl Into<Element> for TagName {
    fn into(self) -> Element {
        Element::new(self)
    }
}