i_slint_compiler/passes/
compile_paths.rs

1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4//! This pass converts the verbose markup used for paths, such as
5//!    Path {
6//!        LineTo { ... } ArcTo { ... }
7//!    }
8//! to a vector of path elements (PathData) that is assigned to the
9//! elements property of the Path element. That way the generators have to deal
10//! with path embedding only as part of the property assignment.
11
12use crate::diagnostics::BuildDiagnostics;
13use crate::expression_tree::*;
14use crate::langtype::{Struct, Type};
15use crate::object_tree::*;
16use crate::EmbedResourcesKind;
17use smol_str::SmolStr;
18use std::cell::RefCell;
19use std::rc::Rc;
20
21pub fn compile_paths(
22    component: &Rc<Component>,
23    tr: &crate::typeregister::TypeRegister,
24    _embed_resources: EmbedResourcesKind,
25    diag: &mut BuildDiagnostics,
26) {
27    let path_type = tr.lookup_element("Path").unwrap();
28    let path_type = path_type.as_builtin();
29
30    recurse_elem_including_sub_components_no_borrow(component, &(), &mut |elem_, _| {
31        if !elem_.borrow().builtin_type().is_some_and(|bt| bt.name == "Path") {
32            return;
33        }
34
35        #[cfg(feature = "software-renderer")]
36        if _embed_resources == EmbedResourcesKind::EmbedTextures {
37            diag.push_warning(
38                "Path element is not supported with the software renderer".into(),
39                &*elem_.borrow(),
40            )
41        }
42
43        let element_types = &path_type.additional_accepted_child_types;
44
45        let commands_binding =
46            elem_.borrow_mut().bindings.remove("commands").map(RefCell::into_inner);
47
48        let path_data_binding = if let Some(commands_expr) = commands_binding {
49            if let Some(path_child) = elem_.borrow().children.iter().find(|child| {
50                element_types
51                    .contains_key(&child.borrow().base_type.as_builtin().native_class.class_name)
52            }) {
53                diag.push_error(
54                    "Path elements cannot be mixed with the use of the SVG commands property"
55                        .into(),
56                    &*path_child.borrow(),
57                );
58                return;
59            }
60
61            match &commands_expr.expression {
62                Expression::StringLiteral(commands) => {
63                    match compile_path_from_string_literal(commands) {
64                        Ok(binding) => binding,
65                        Err(e) => {
66                            diag.push_error(
67                                format!("Error parsing SVG commands ({e})"),
68                                &commands_expr,
69                            );
70                            return;
71                        }
72                    }
73                }
74                expr if expr.ty() == Type::String => Expression::PathData(
75                    crate::expression_tree::Path::Commands(Box::new(commands_expr.expression)),
76                )
77                .into(),
78                _ => {
79                    diag.push_error(
80                        "The commands property only accepts strings".into(),
81                        &*elem_.borrow(),
82                    );
83                    return;
84                }
85            }
86        } else {
87            let mut elem = elem_.borrow_mut();
88            let enclosing_component = elem.enclosing_component.upgrade().unwrap();
89            let new_children = Vec::with_capacity(elem.children.len());
90            let old_children = std::mem::replace(&mut elem.children, new_children);
91
92            let mut path_data = Vec::new();
93
94            for child in old_children {
95                let element_name =
96                    &child.borrow().base_type.as_builtin().native_class.class_name.clone();
97
98                if let Some(element_type) = element_types.get(element_name).cloned() {
99                    if child.borrow().repeated.is_some() {
100                        diag.push_error(
101                            "Path elements are not supported with `for`-`in` syntax, yet (https://github.com/slint-ui/slint/issues/754)".into(),
102                            &*child.borrow(),
103                        );
104                    } else {
105                        let mut bindings = std::collections::BTreeMap::new();
106                        {
107                            let mut child = child.borrow_mut();
108                            for k in element_type.properties.keys() {
109                                if let Some(binding) = child.bindings.remove(k) {
110                                    bindings.insert(k.clone(), binding);
111                                }
112                            }
113                        }
114                        path_data.push(PathElement { element_type, bindings });
115                        enclosing_component.optimized_elements.borrow_mut().push(child);
116                    }
117                } else {
118                    elem.children.push(child);
119                }
120            }
121
122            if elem.is_binding_set("elements", false) {
123                if path_data.is_empty() {
124                    // Just Path subclass that had elements declared earlier, since path_data is empty we should retain the
125                    // existing elements
126                    return;
127                } else {
128                    diag.push_error(
129                        "The Path was already populated in the base type and it can't be re-populated again"
130                            .into(),
131                        &*elem,
132                    );
133                    return;
134                }
135            }
136
137            Expression::PathData(crate::expression_tree::Path::Elements(path_data)).into()
138        };
139
140        elem_
141            .borrow_mut()
142            .bindings
143            .insert(SmolStr::new_static("elements"), RefCell::new(path_data_binding));
144    });
145}
146
147fn compile_path_from_string_literal(
148    commands: &str,
149) -> Result<BindingExpression, lyon_extra::parser::ParseError> {
150    let mut builder = lyon_path::Path::builder();
151    let mut parser = lyon_extra::parser::PathParser::new();
152    parser.parse(
153        &lyon_extra::parser::ParserOptions::DEFAULT,
154        &mut lyon_extra::parser::Source::new(commands.chars()),
155        &mut builder,
156    )?;
157    let path = builder.build();
158
159    let event_enum = crate::typeregister::BUILTIN.with(|e| e.enums.PathEvent.clone());
160    let point_type = Rc::new(Struct {
161        fields: IntoIterator::into_iter([
162            (SmolStr::new_static("x"), Type::Float32),
163            (SmolStr::new_static("y"), Type::Float32),
164        ])
165        .collect(),
166        name: Some("slint::private_api::Point".into()),
167        node: None,
168        rust_attributes: None,
169    });
170
171    let mut points = Vec::new();
172    let events = path
173        .into_iter()
174        .map(|event| {
175            Expression::EnumerationValue(match event {
176                lyon_path::Event::Begin { at } => {
177                    points.push(at);
178                    event_enum.clone().try_value_from_string("begin").unwrap()
179                }
180                lyon_path::Event::Line { from, to } => {
181                    points.push(from);
182                    points.push(to);
183
184                    event_enum.clone().try_value_from_string("line").unwrap()
185                }
186                lyon_path::Event::Quadratic { from, ctrl, to } => {
187                    points.push(from);
188                    points.push(ctrl);
189                    points.push(to);
190
191                    event_enum.clone().try_value_from_string("quadratic").unwrap()
192                }
193                lyon_path::Event::Cubic { from, ctrl1, ctrl2, to } => {
194                    points.push(from);
195                    points.push(ctrl1);
196                    points.push(ctrl2);
197                    points.push(to);
198                    event_enum.clone().try_value_from_string("cubic").unwrap()
199                }
200                lyon_path::Event::End { first: _, last: _, close } => {
201                    if close {
202                        event_enum.clone().try_value_from_string("end-closed").unwrap()
203                    } else {
204                        event_enum.clone().try_value_from_string("end-open").unwrap()
205                    }
206                }
207            })
208        })
209        .collect();
210
211    let points = points
212        .into_iter()
213        .map(|point| Expression::Struct {
214            ty: point_type.clone(),
215            values: IntoIterator::into_iter([
216                (SmolStr::new_static("x"), Expression::NumberLiteral(point.x as _, Unit::None)),
217                (SmolStr::new_static("y"), Expression::NumberLiteral(point.y as _, Unit::None)),
218            ])
219            .collect(),
220        })
221        .collect();
222
223    Ok(Expression::PathData(Path::Events(events, points)).into())
224}