1use std::{
4 any::Any,
5 borrow::Cow,
6 cell::RefCell,
7 collections::HashMap,
8 rc::Rc,
9};
10
11use bytes::Bytes;
12use freya_engine::prelude::{
13 ClipOp,
14 LocalResourceProvider,
15 Paint,
16 SkRect,
17 svg,
18};
19use rustc_hash::FxHashMap;
20use torin::{
21 prelude::Size2D,
22 size::Size,
23};
24
25use crate::{
26 data::{
27 AccessibilityData,
28 EffectData,
29 LayoutData,
30 StyleState,
31 TextStyleData,
32 },
33 diff_key::DiffKey,
34 element::{
35 ClipContext,
36 Element,
37 ElementExt,
38 EventHandlerType,
39 LayoutContext,
40 RenderContext,
41 },
42 events::name::EventName,
43 layers::Layer,
44 prelude::{
45 AccessibilityExt,
46 Color,
47 ContainerExt,
48 EventHandlersExt,
49 KeyExt,
50 LayerExt,
51 LayoutExt,
52 MaybeExt,
53 },
54 tree::DiffModifies,
55};
56
57#[derive(Clone, PartialEq)]
60pub struct SvgBytes(Bytes);
61
62impl From<Bytes> for SvgBytes {
63 fn from(bytes: Bytes) -> Self {
64 Self(bytes)
65 }
66}
67
68impl From<Vec<u8>> for SvgBytes {
69 fn from(bytes: Vec<u8>) -> Self {
70 Self(Bytes::from(bytes))
71 }
72}
73
74impl From<&'static [u8]> for SvgBytes {
75 fn from(bytes: &'static [u8]) -> Self {
76 Self(Bytes::from_static(bytes))
77 }
78}
79
80impl<const N: usize> From<&'static [u8; N]> for SvgBytes {
81 fn from(bytes: &'static [u8; N]) -> Self {
82 Self(Bytes::from_static(bytes))
83 }
84}
85
86pub fn svg(bytes: impl Into<SvgBytes>) -> Svg {
97 let mut accessibility = AccessibilityData::default();
98 accessibility.builder.set_role(accesskit::Role::SvgRoot);
99
100 Svg {
101 key: DiffKey::None,
102 element: SvgElement {
103 accessibility,
104 layout: LayoutData::default(),
105 event_handlers: HashMap::default(),
106 bytes: bytes.into(),
107 effect: None,
108 color: Color::BLACK,
109 stroke: None,
110 stroke_width: None,
111 fill: None,
112 relative_layer: Layer::default(),
113 },
114 }
115}
116
117#[derive(PartialEq, Clone)]
118pub struct SvgElement {
119 pub accessibility: AccessibilityData,
120 pub layout: LayoutData,
121 pub event_handlers: FxHashMap<EventName, EventHandlerType>,
122 pub bytes: SvgBytes,
123 pub color: Color,
124 pub stroke: Option<Color>,
125 pub stroke_width: Option<f32>,
126 pub fill: Option<Color>,
127 pub effect: Option<EffectData>,
128 pub relative_layer: Layer,
129}
130
131impl ElementExt for SvgElement {
132 fn changed(&self, other: &Rc<dyn ElementExt>) -> bool {
133 let Some(image) = (other.as_ref() as &dyn Any).downcast_ref::<SvgElement>() else {
134 return false;
135 };
136 self != image
137 }
138
139 fn diff(&self, other: &Rc<dyn ElementExt>) -> DiffModifies {
140 let Some(svg) = (other.as_ref() as &dyn Any).downcast_ref::<SvgElement>() else {
141 return DiffModifies::all();
142 };
143
144 let mut diff = DiffModifies::empty();
145
146 if self.accessibility != svg.accessibility {
147 diff.insert(DiffModifies::ACCESSIBILITY);
148 }
149
150 if self.relative_layer != svg.relative_layer {
151 diff.insert(DiffModifies::LAYER);
152 }
153
154 if self.layout != svg.layout || self.bytes != svg.bytes {
155 diff.insert(DiffModifies::LAYOUT);
156 diff.insert(DiffModifies::STYLE);
157 }
158
159 if self.color != svg.color
160 || self.fill != svg.fill
161 || self.stroke != svg.stroke
162 || self.stroke_width != svg.stroke_width
163 {
164 diff.insert(DiffModifies::STYLE);
165 }
166
167 if self.effect != svg.effect {
168 diff.insert(DiffModifies::EFFECT);
169 }
170
171 diff
172 }
173
174 fn layout(&'_ self) -> Cow<'_, LayoutData> {
175 Cow::Borrowed(&self.layout)
176 }
177
178 fn effect(&'_ self) -> Option<Cow<'_, EffectData>> {
179 self.effect.as_ref().map(Cow::Borrowed)
180 }
181
182 fn style(&'_ self) -> Cow<'_, StyleState> {
183 Cow::Owned(StyleState::default())
184 }
185
186 fn text_style(&'_ self) -> Cow<'_, TextStyleData> {
187 Cow::Owned(TextStyleData::default())
188 }
189
190 fn accessibility(&'_ self) -> Cow<'_, AccessibilityData> {
191 Cow::Borrowed(&self.accessibility)
192 }
193
194 fn events_handlers(&'_ self) -> Option<Cow<'_, FxHashMap<EventName, EventHandlerType>>> {
195 Some(Cow::Borrowed(&self.event_handlers))
196 }
197
198 fn layer(&self) -> Layer {
199 self.relative_layer
200 }
201
202 fn should_measure_inner_children(&self) -> bool {
203 false
204 }
205
206 fn should_hook_measurement(&self) -> bool {
207 true
208 }
209
210 fn measure(&self, context: LayoutContext) -> Option<(Size2D, Rc<dyn Any>)> {
211 let resource_provider = LocalResourceProvider::new(context.font_manager);
212 let svg_dom = svg::Dom::from_bytes(&self.bytes.0, resource_provider);
213 if let Ok(mut svg_dom) = svg_dom {
214 svg_dom.set_container_size(context.area_size.to_i32().to_tuple());
215 let mut root = svg_dom.root();
216 match self.layout.width {
217 Size::Pixels(px) => {
218 root.set_width(svg::Length::new(px.get(), svg::LengthUnit::PX));
219 }
220 Size::Percentage(per) => {
221 root.set_width(svg::Length::new(per.get(), svg::LengthUnit::Percentage));
222 }
223 Size::Fill => {
224 root.set_width(svg::Length::new(100., svg::LengthUnit::Percentage));
225 }
226 _ => {}
227 }
228 match self.layout.height {
229 Size::Pixels(px) => {
230 root.set_height(svg::Length::new(px.get(), svg::LengthUnit::PX));
231 }
232 Size::Percentage(per) => {
233 root.set_height(svg::Length::new(per.get(), svg::LengthUnit::Percentage));
234 }
235 Size::Fill => {
236 root.set_height(svg::Length::new(100., svg::LengthUnit::Percentage));
237 }
238 _ => {}
239 }
240 if let Some(stroke_width) = self.stroke_width {
241 root.set_stroke_width(svg::Length::new(stroke_width, svg::LengthUnit::PX));
242 }
243 Some((
244 Size2D::new(root.width().value, root.height().value),
245 Rc::new(RefCell::new(svg_dom)),
246 ))
247 } else {
248 None
249 }
250 }
251
252 fn clip(&self, context: ClipContext) {
253 let area = context.visible_area;
254 context.canvas.clip_rect(
255 SkRect::new(area.min_x(), area.min_y(), area.max_x(), area.max_y()),
256 ClipOp::Intersect,
257 true,
258 );
259 }
260
261 fn render(&self, context: RenderContext) {
262 let mut paint = Paint::default();
263 paint.set_anti_alias(true);
264
265 let svg_dom = context
266 .layout_node
267 .data
268 .as_ref()
269 .unwrap()
270 .downcast_ref::<RefCell<svg::Dom>>()
271 .unwrap();
272 let svg_dom = svg_dom.borrow();
273
274 let mut root = svg_dom.root();
275 context.canvas.save();
276 context
277 .canvas
278 .translate(context.layout_node.visible_area().origin.to_tuple());
279 context
280 .canvas
281 .scale((context.scale_factor as f32, context.scale_factor as f32));
282
283 root.set_color(self.color.into());
284 if let Some(fill) = self.fill {
285 root.set_fill(svg::Paint::from_color(fill.into()));
286 }
287 if let Some(stroke) = self.stroke {
288 root.set_stroke(svg::Paint::from_color(stroke.into()));
289 }
290 if let Some(stroke_width) = self.stroke_width {
291 root.set_stroke_width(svg::Length::new(stroke_width, svg::LengthUnit::PX));
292 }
293 svg_dom.render(context.canvas);
294 context.canvas.restore();
295 }
296}
297
298impl From<Svg> for Element {
299 fn from(value: Svg) -> Self {
300 Element::Element {
301 key: value.key,
302 element: Rc::new(value.element),
303 elements: vec![],
304 }
305 }
306}
307
308impl KeyExt for Svg {
309 fn write_key(&mut self) -> &mut DiffKey {
310 &mut self.key
311 }
312}
313
314impl EventHandlersExt for Svg {
315 fn get_event_handlers(&mut self) -> &mut FxHashMap<EventName, EventHandlerType> {
316 &mut self.element.event_handlers
317 }
318}
319
320impl LayoutExt for Svg {
321 fn get_layout(&mut self) -> &mut LayoutData {
322 &mut self.element.layout
323 }
324}
325
326impl ContainerExt for Svg {}
327
328impl AccessibilityExt for Svg {
329 fn get_accessibility_data(&mut self) -> &mut AccessibilityData {
330 &mut self.element.accessibility
331 }
332}
333
334impl MaybeExt for Svg {}
335
336impl LayerExt for Svg {
337 fn get_layer(&mut self) -> &mut Layer {
338 &mut self.element.relative_layer
339 }
340}
341
342pub struct Svg {
343 key: DiffKey,
344 element: SvgElement,
345}
346
347impl Svg {
348 pub fn try_downcast(element: &dyn ElementExt) -> Option<SvgElement> {
349 (element as &dyn Any).downcast_ref::<SvgElement>().cloned()
350 }
351
352 pub fn color(mut self, color: impl Into<Color>) -> Self {
353 self.element.color = color.into();
354 self
355 }
356
357 pub fn fill(mut self, fill: impl Into<Color>) -> Self {
358 self.element.fill = Some(fill.into());
359 self
360 }
361
362 pub fn stroke(mut self, stroke: impl Into<Color>) -> Self {
363 self.element.stroke = Some(stroke.into());
364 self
365 }
366
367 pub fn stroke_width(mut self, stroke_width: impl Into<f32>) -> Self {
369 self.element.stroke_width = Some(stroke_width.into());
370 self
371 }
372
373 pub fn rotate(mut self, rotation: impl Into<f32>) -> Self {
374 self.element
375 .effect
376 .get_or_insert_with(Default::default)
377 .rotation = Some(rotation.into());
378 self
379 }
380}