1use std::sync::Arc;
5
6use svgtypes::{Length, LengthUnit};
7
8use super::svgtree::{AId, EId, SvgNode};
9use super::{converter, style};
10use crate::tree::ContextElement;
11use crate::{Group, IsValidLength, Node, NonZeroRect, Path, Size, Transform, ViewBox};
12
13pub(crate) fn convert(
14 node: SvgNode,
15 state: &converter::State,
16 cache: &mut converter::Cache,
17 parent: &mut Group,
18) {
19 let child = match node.first_child() {
20 Some(v) => v,
21 None => return,
22 };
23
24 if state.parent_clip_path.is_some() && child.tag_name() == Some(EId::Symbol) {
25 return;
29 }
30
31 let mut use_state = state.clone();
32 use_state.context_element = Some((
33 style::resolve_fill(node, true, state, cache).map(|mut f| {
34 f.context_element = Some(ContextElement::UseNode);
35 f
36 }),
37 style::resolve_stroke(node, true, state, cache).map(|mut s| {
38 s.context_element = Some(ContextElement::UseNode);
39 s
40 }),
41 ));
42
43 let mut orig_ts = node.resolve_transform(AId::Transform, state);
45 let mut new_ts = Transform::default();
46
47 {
48 let x = node.convert_user_length(AId::X, &use_state, Length::zero());
49 let y = node.convert_user_length(AId::Y, &use_state, Length::zero());
50 new_ts = new_ts.pre_translate(x, y);
51 }
52
53 let linked_to_symbol = child.tag_name() == Some(EId::Symbol);
54
55 if linked_to_symbol {
56 use_state.view_box = {
61 let def = Length::new(100.0, LengthUnit::Percent);
62 let x = use_state.view_box.x();
63 let y = use_state.view_box.y();
64
65 let width = if node.has_attribute(AId::Width) {
66 node.convert_user_length(AId::Width, &use_state, def)
67 } else {
68 use_state.view_box.width()
69 };
70
71 let height = if node.has_attribute(AId::Height) {
72 node.convert_user_length(AId::Height, &use_state, def)
73 } else {
74 use_state.view_box.height()
75 };
76
77 NonZeroRect::from_xywh(x, y, width, height)
78 .unwrap_or(use_state.view_box)
80 };
81
82 if let Some(ts) = viewbox_transform(node, child, &use_state) {
83 new_ts = new_ts.pre_concat(ts);
84 }
85
86 if let Some(clip_rect) = get_clip_rect(node, child, &use_state) {
87 let mut g = clip_element(node, clip_rect, orig_ts, &use_state, cache);
88 g.abs_transform = parent.abs_transform;
89
90 if let Some(mut g2) =
92 converter::convert_group(node, &use_state, true, cache, &mut g, &|cache, g2| {
93 convert_children(child, new_ts, &use_state, cache, false, g2);
94 })
95 {
96 g.is_context_element = true;
99 g2.id = String::new(); g2.transform = Transform::default();
101 g.children.push(Node::Group(Box::new(g2)));
102 }
103
104 if g.children.is_empty() {
105 return;
106 }
107
108 parent.children.push(Node::Group(Box::new(g)));
109 return;
110 }
111 }
112
113 orig_ts = orig_ts.pre_concat(new_ts);
114
115 if linked_to_symbol {
116 if let Some(mut g) =
118 converter::convert_group(node, &use_state, false, cache, parent, &|cache, g| {
119 convert_children(child, orig_ts, &use_state, cache, false, g);
120 })
121 {
122 g.is_context_element = true;
123 g.transform = Transform::default();
124 parent.children.push(Node::Group(Box::new(g)));
125 }
126 } else {
127 let linked_to_svg = child.tag_name() == Some(EId::Svg);
128 if linked_to_svg {
129 let def = Length::new(100.0, LengthUnit::Percent);
134 use_state.use_size = (None, None);
146
147 if node.has_attribute(AId::Width) {
149 use_state.use_size.0 = Some(node.convert_user_length(AId::Width, &use_state, def));
150 }
151 if node.has_attribute(AId::Height) {
152 use_state.use_size.1 = Some(node.convert_user_length(AId::Height, &use_state, def));
153 }
154
155 convert_children(node, orig_ts, &use_state, cache, true, parent);
156 } else {
157 convert_children(node, orig_ts, &use_state, cache, true, parent);
158 }
159 }
160}
161
162pub(crate) fn convert_svg(
163 node: SvgNode,
164 state: &converter::State,
165 cache: &mut converter::Cache,
166 parent: &mut Group,
167) {
168 let mut orig_ts = node.resolve_transform(AId::Transform, state);
170 let mut new_ts = Transform::default();
171
172 let x = node.convert_user_length(AId::X, state, Length::zero());
173 let y = node.convert_user_length(AId::Y, state, Length::zero());
174 new_ts = new_ts.pre_translate(x, y);
175
176 if let Some(ts) = viewbox_transform(node, node, state) {
177 new_ts = new_ts.pre_concat(ts);
178 }
179
180 let mut new_state = state.clone();
183 new_state.view_box = {
184 if let Some(vb) = node.parse_viewbox() {
185 vb
186 } else {
187 let (mut w, mut h) = use_node_size(node, state);
189
190 w = state.use_size.0.unwrap_or(w);
194 h = state.use_size.1.unwrap_or(h);
195
196 NonZeroRect::from_xywh(x, y, w, h).unwrap_or(state.view_box)
197 }
198 };
199
200 if let Some(clip_rect) = get_clip_rect(node, node, state) {
201 let mut g = clip_element(node, clip_rect, orig_ts, state, cache);
202 g.abs_transform = parent.abs_transform.pre_concat(orig_ts);
203 convert_children(node, new_ts, &new_state, cache, false, &mut g);
204 parent.children.push(Node::Group(Box::new(g)));
205 } else {
206 orig_ts = orig_ts.pre_concat(new_ts);
207 convert_children(node, orig_ts, &new_state, cache, false, parent);
208 }
209}
210
211fn clip_element(
212 node: SvgNode,
213 clip_rect: NonZeroRect,
214 transform: Transform,
215 state: &converter::State,
216 cache: &mut converter::Cache,
217) -> Group {
218 let mut clip_path = crate::ClipPath::empty(cache.gen_clip_path_id());
239
240 let mut path = Path::new_simple(Arc::new(tiny_skia_path::PathBuilder::from_rect(
241 clip_rect.to_rect(),
242 )));
243 path.fill = Some(crate::Fill::default());
244 clip_path.root.children.push(Node::Path(Box::new(path)));
245
246 let id = if !state.inside_marker {
248 node.element_id().to_string()
249 } else {
250 String::new()
251 };
252
253 Group {
254 id,
255 transform,
256 clip_path: Some(Arc::new(clip_path)),
257 ..Group::empty()
258 }
259}
260
261fn convert_children(
262 node: SvgNode,
263 transform: Transform,
264 state: &converter::State,
265 cache: &mut converter::Cache,
266 is_context_element: bool,
267 parent: &mut Group,
268) {
269 let parent_abs_transform = parent.abs_transform;
271
272 let required = !transform.is_identity();
273
274 let Some(g) = converter::convert_group(node, state, required, cache, parent, &|cache, g| {
275 g.transform = transform;
277 g.abs_transform = parent_abs_transform.pre_concat(transform);
278 g.is_context_element = is_context_element;
279
280 if state.parent_clip_path.is_some() {
281 converter::convert_clip_path_elements(node, state, cache, g);
282 } else {
283 converter::convert_children(node, state, cache, g);
284 }
285 }) else {
286 return;
287 };
288
289 parent.children.push(Node::Group(Box::new(g)));
290}
291
292fn get_clip_rect(
293 use_node: SvgNode,
294 symbol_node: SvgNode,
295 state: &converter::State,
296) -> Option<NonZeroRect> {
297 if matches!(
299 symbol_node.attribute(AId::Overflow),
300 Some("visible") | Some("auto")
301 ) {
302 return None;
303 }
304
305 if use_node.tag_name() == Some(EId::Svg) {
308 if state.use_size.0.is_none() && state.use_size.1.is_none() {
310 if !(use_node.has_attribute(AId::Width) && use_node.has_attribute(AId::Height)) {
311 return None;
312 }
313 }
314 }
315
316 let (x, y, mut w, mut h) = {
317 let x = use_node.convert_user_length(AId::X, state, Length::zero());
318 let y = use_node.convert_user_length(AId::Y, state, Length::zero());
319 let (w, h) = use_node_size(use_node, state);
320 (x, y, w, h)
321 };
322
323 if use_node.tag_name() == Some(EId::Svg) {
324 w = state.use_size.0.unwrap_or(w);
328 h = state.use_size.1.unwrap_or(h);
329 }
330
331 if !w.is_valid_length() || !h.is_valid_length() {
332 return None;
333 }
334
335 NonZeroRect::from_xywh(x, y, w, h)
336}
337
338fn use_node_size(node: SvgNode, state: &converter::State) -> (f32, f32) {
339 let def = Length::new(100.0, LengthUnit::Percent);
340 let w = node.convert_user_length(AId::Width, state, def);
341 let h = node.convert_user_length(AId::Height, state, def);
342 (w, h)
343}
344
345fn viewbox_transform(
346 node: SvgNode,
347 linked: SvgNode,
348 state: &converter::State,
349) -> Option<Transform> {
350 let (mut w, mut h) = use_node_size(node, state);
351
352 if node.tag_name() == Some(EId::Svg) {
353 w = state.use_size.0.unwrap_or(w);
357 h = state.use_size.1.unwrap_or(h);
358 }
359
360 let size = Size::from_wh(w, h)?;
361 let rect = linked.parse_viewbox()?;
362 let aspect = linked
363 .attribute(AId::PreserveAspectRatio)
364 .unwrap_or_default();
365 let view_box = ViewBox { rect, aspect };
366
367 Some(view_box.to_transform(size))
368}