Newer
Older
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
use bevy::{
prelude::{Color, Component, Vec2},
reflect::Reflect,
};
mod corner;
mod edge;
mod options_ref;
mod render_command;
mod style;
mod units;
pub use corner::Corner;
pub use edge::Edge;
use fancy_regex::Matches;
pub use options_ref::AsRefOption;
pub use render_command::RenderCommand;
pub use style::*;
pub use units::*;
#[derive(Component, Reflect, Debug, Default, Clone, PartialEq)]
pub struct ComputedStyles(pub KStyle);
impl From<KStyle> for ComputedStyles {
fn from(val: KStyle) -> Self {
ComputedStyles(val)
}
}
#[derive(Reflect, Clone, Copy, Default, Debug, PartialEq)]
pub struct BoxShadow {
pub color: Color,
pub radius: f32,
pub offset: Vec2,
pub spread: Vec2,
}
fn is_length(v: &str) -> bool {
v == "0"
|| fancy_regex::Regex::new(r"^[0-9]+[a-zA-Z%]+?$")
.map(|m| m.is_match(v).unwrap_or(false))
.unwrap_or(false)
}
impl BoxShadow {
pub fn from_string(s: impl ToString) -> Vec<BoxShadow> {
let box_shadow_string: String = s.to_string();
let box_shadow_string = box_shadow_string
.replace("box-shadow: ", "")
.replace(';', "");
let values_parsed = fancy_regex::Regex::new(r",(?![^\(]*\))").unwrap();
let split_regex = fancy_regex::Regex::new(r"\s(?![^(]*\))").unwrap();
let mut box_shadows = vec![];
let values_split = Split {
finder: values_parsed.find_iter(&box_shadow_string),
last: 0,
};
for value in values_split.map(|s| s.trim()) {
// Split single shadow
let parts = Split {
finder: split_regex.find_iter(value),
last: 0,
}
.collect::<Vec<_>>();
let _inset = parts.contains(&"inset");
let color = parts
.last()
.map(|last| {
if last.contains("rgb") || last.contains('#') {
Some(*last)
} else {
None
}
})
.and_then(|s| s)
.unwrap_or(parts.first().cloned().unwrap());
let nums = parts
.iter()
.filter(|n| **n != "inset")
.filter(|n| color != **n)
.map(|v| v.replace("px", "").parse::<f32>().unwrap_or(0.0))
.collect::<Vec<f32>>();
let offset_x = nums.first().copied().unwrap_or(0.0);
let offset_y = nums.get(1).copied().unwrap_or(0.0);
let blur_radius = nums.get(2).copied().unwrap_or(0.0);
let spread = nums.get(3).copied().unwrap_or(0.0);
let color = if is_rgba(color) {
parse_rgba(color)
} else {
Color::hex(color).unwrap_or_default()
};
box_shadows.push(BoxShadow {
color,
radius: blur_radius,
offset: Vec2::new(offset_x, offset_y),
spread: Vec2::splat(spread),
});
}
box_shadows
}
}
fn is_rgba(s: &str) -> bool {
s.contains("rgba") || s.contains("rgb")
}
fn parse_rgba(s: &str) -> Color {
let s = s.replace("rgba(", "").replace("rgb(", "").replace(')', "");
let values = s.split(',').collect::<Vec<_>>();
let r = values
.first()
.map(|s| s.trim().parse::<f32>().map(|v| v / 255.0).unwrap_or(0.0))
.unwrap_or(0.0);
let g = values
.get(1)
.map(|s| s.trim().parse::<f32>().map(|v| v / 255.0).unwrap_or(0.0))
.unwrap_or(0.0);
let b = values
.get(2)
.map(|s| s.trim().parse::<f32>().map(|v| v / 255.0).unwrap_or(0.0))
.unwrap_or(0.0);
let a = values
.get(3)
.map(|s| s.trim().parse::<f32>().unwrap_or(1.0))
.unwrap_or(1.0);
Color::rgba(r, g, b, a)
}
mod tests {
#[test]
fn test_box_shadow_from_string() {
let box_shadow_string = "box-shadow: rgba(50, 50, 93, 0.25) 0px 50px 100px -20px, rgba(0, 0, 0, 0.3) 0px 30px 60px -30px;";
let result = crate::styles::BoxShadow::from_string(box_shadow_string);
dbg!(result);
// assert!(true == false);
}
}
/// Yields all substrings delimited by a regular expression match.
///
/// `'r` is the lifetime of the compiled regular expression and `'t` is the
/// lifetime of the string being split.
#[derive(Debug)]
pub struct Split<'r, 't> {
finder: Matches<'r, 't>,
last: usize,
}
impl<'r, 't> Iterator for Split<'r, 't> {
type Item = &'t str;
fn next(&mut self) -> Option<&'t str> {
let text = self.finder.text();
match self.finder.next() {
None => {
if self.last > text.len() {
None
} else {
let s = &text[self.last..];
self.last = text.len() + 1; // Next call will return None
Some(s)
}
}
Some(m) => match m {
Ok(m) => {
let matched = &text[self.last..m.start()];
self.last = m.end();
Some(matched)
}
Err(_) => None,
},
}
}
}