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
// Copyright © SixtyFPS GmbH <info@sixtyfps.io>
// SPDX-License-Identifier: (GPL-3.0-only OR LicenseRef-SixtyFPS-commercial)
//! This pass converts the verbose markup used for paths, such as
//! Path {
//! LineTo { ... } ArcTo { ... }
//! }
//! to a vector of path elements (PathData) that is assigned to the
//! elements property of the Path element. That way the generators have to deal
//! with path embedding only as part of the property assignment.
use crate::diagnostics::BuildDiagnostics;
use crate::expression_tree::*;
use crate::langtype::Type;
use crate::object_tree::*;
use std::cell::RefCell;
use std::rc::Rc;
pub fn compile_paths(
component: &Rc<Component>,
tr: &crate::typeregister::TypeRegister,
diag: &mut BuildDiagnostics,
) {
let path_type = tr.lookup("Path");
let path_type = path_type.as_builtin();
let pathlayout_type = tr.lookup("PathLayout");
let pathlayout_type = pathlayout_type.as_builtin();
recurse_elem(&component.root_element, &(), &mut |elem_, _| {
let accepted_type = match &elem_.borrow().base_type {
Type::Builtin(be)
if be.native_class.class_name == path_type.native_class.class_name =>
{
path_type
}
Type::Builtin(be)
if be.native_class.class_name == pathlayout_type.native_class.class_name =>
{
pathlayout_type
}
_ => return,
};
let element_types = &accepted_type.additional_accepted_child_types;
let mut elem = elem_.borrow_mut();
let path_data = if let Some(commands_expr) =
elem.bindings.remove("commands").map(RefCell::into_inner)
{
if let Some(path_child) = elem.children.iter().find(|child| {
element_types
.contains_key(&child.borrow().base_type.as_builtin().native_class.class_name)
}) {
diag.push_error(
"Path elements cannot be mixed with the use of the SVG commands property"
.into(),
&*path_child.borrow(),
);
return;
}
let commands = match &commands_expr.expression {
Expression::StringLiteral(commands) => commands,
_ => {
diag.push_error(
"The commands property only accepts string literals".into(),
&*elem,
);
return;
}
};
let path_builder = lyon_path::Path::builder().with_svg();
let path = lyon_svg::path_utils::build_path(path_builder, commands);
match path {
Ok(path) => {
let event_enum = crate::typeregister::PATH_EVENT_ENUM.with(|e| e.clone());
let point_type = Type::Struct {
fields: IntoIterator::into_iter([
("x".to_owned(), Type::Float32),
("y".to_owned(), Type::Float32),
])
.collect(),
name: Some("Point".into()),
node: None,
};
let mut points = Vec::new();
let events = path
.into_iter()
.map(|event| {
Expression::EnumerationValue(match event {
lyon_path::Event::Begin { at } => {
points.push(at);
event_enum.clone().try_value_from_string("begin").unwrap()
}
lyon_path::Event::Line { from, to } => {
points.push(from);
points.push(to);
event_enum.clone().try_value_from_string("line").unwrap()
}
lyon_path::Event::Quadratic { from, ctrl, to } => {
points.push(from);
points.push(ctrl);
points.push(to);
event_enum.clone().try_value_from_string("quadratic").unwrap()
}
lyon_path::Event::Cubic { from, ctrl1, ctrl2, to } => {
points.push(from);
points.push(ctrl1);
points.push(ctrl2);
points.push(to);
event_enum.clone().try_value_from_string("cubic").unwrap()
}
lyon_path::Event::End { first: _, last: _, close } => {
if close {
event_enum
.clone()
.try_value_from_string("end_closed")
.unwrap()
} else {
event_enum
.clone()
.try_value_from_string("end_open")
.unwrap()
}
}
})
})
.collect();
let points = points
.into_iter()
.map(|point| Expression::Struct {
ty: point_type.clone(),
values: IntoIterator::into_iter([
(
"x".to_owned(),
Expression::NumberLiteral(point.x as _, Unit::None),
),
(
"y".to_owned(),
Expression::NumberLiteral(point.y as _, Unit::None),
),
])
.collect(),
})
.collect();
Path::Events(events, points)
}
Err(_) => {
diag.push_error("Error parsing SVG commands".into(), &commands_expr);
return;
}
}
} else {
let new_children = Vec::with_capacity(elem.children.len());
let old_children = std::mem::replace(&mut elem.children, new_children);
let mut path_data = Vec::new();
for child in old_children {
let element_name =
&child.borrow().base_type.as_builtin().native_class.class_name.clone();
if let Some(path_element) = element_types.get(element_name) {
let element_type = match path_element {
Type::Builtin(b) => b.clone(),
_ => panic!(
"Incorrect type registry -- expected built-in type for path element {}",
element_name
),
};
if child.borrow().repeated.is_some() {
diag.push_error(
"Path elements are not supported with `for`-`in` syntax, yet (https://github.com/sixtyfpsui/sixtyfps/issues/754)".into(),
&*child.borrow(),
);
} else {
let bindings = std::mem::take(&mut child.borrow_mut().bindings);
path_data.push(PathElement { element_type, bindings });
}
} else {
elem.children.push(child);
}
}
crate::expression_tree::Path::Elements(path_data)
};
elem.bindings
.insert("elements".into(), RefCell::new(Expression::PathData(path_data).into()));
});
}