14
14
15
15
//! An SVG widget.
16
16
17
+ use std:: error:: Error ;
18
+ use std:: marker:: PhantomData ;
19
+ use std:: str:: FromStr ;
20
+ use std:: sync:: Arc ;
21
+
22
+ use log:: error;
23
+
24
+ use usvg;
25
+
17
26
use crate :: {
18
27
kurbo:: BezPath ,
19
28
piet:: { Color , RenderContext } ,
20
29
BaseState , BoxConstraints , Data , Env , Event , EventCtx , LayoutCtx , PaintCtx , Point , Size ,
21
30
UpdateCtx , Widget ,
22
31
} ;
23
32
24
- use log:: error;
25
- use std:: error:: Error ;
26
- use std:: marker:: PhantomData ;
27
- use std:: str:: FromStr ;
28
-
29
- use usvg;
30
-
31
33
/// A widget that renders a SVG
32
34
pub struct Svg < T > {
33
35
// On construction the SVG string is parsed into a SvgData.
@@ -36,25 +38,13 @@ pub struct Svg<T> {
36
38
}
37
39
38
40
impl < T : Data > Svg < T > {
39
- /// Create an SVG-drawing widget from a valid SVG string literal .
41
+ /// Create an SVG-drawing widget from SvgData .
40
42
///
41
43
/// The SVG will scale to fit its box constraints.
42
- /// If SVG is invalid a blank SVG will be rendered instead.
43
- pub fn new ( svg_str : & str ) -> impl Widget < T > {
44
- let svg_data = SvgData :: from_str ( svg_str) ;
45
-
46
- match svg_data {
47
- Ok ( data) => Svg {
48
- svg_data : data,
49
- phantom : Default :: default ( ) ,
50
- } ,
51
- Err ( err) => {
52
- error ! ( "{}" , err) ;
53
- Svg {
54
- svg_data : SvgData :: empty ( ) ,
55
- phantom : Default :: default ( ) ,
56
- }
57
- }
44
+ pub fn new ( svg_data : SvgData ) -> impl Widget < T > {
45
+ Svg {
46
+ svg_data,
47
+ phantom : Default :: default ( ) ,
58
48
}
59
49
}
60
50
@@ -109,8 +99,9 @@ impl<T: Data> Widget<T> for Svg<T> {
109
99
110
100
/// Stored SVG data.
111
101
/// Implements `FromStr` and can be converted to piet draw instructions.
102
+ #[ derive( Clone ) ]
112
103
pub struct SvgData {
113
- tree : usvg:: Tree ,
104
+ tree : Arc < usvg:: Tree > ,
114
105
}
115
106
116
107
impl SvgData {
@@ -122,14 +113,14 @@ impl SvgData {
122
113
} ;
123
114
124
115
let empty_svg = r###"
125
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 0 0 ">
116
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20 ">
126
117
<g fill="none">
127
118
</g>
128
119
</svg>
129
120
"### ;
130
121
131
122
SvgData {
132
- tree : usvg:: Tree :: from_str ( empty_svg, & re_opt) . unwrap ( ) ,
123
+ tree : Arc :: new ( usvg:: Tree :: from_str ( empty_svg, & re_opt) . unwrap ( ) ) ,
133
124
}
134
125
}
135
126
@@ -203,6 +194,12 @@ impl SvgData {
203
194
}
204
195
}
205
196
197
+ impl Default for SvgData {
198
+ fn default ( ) -> Self {
199
+ SvgData :: empty ( )
200
+ }
201
+ }
202
+
206
203
impl FromStr for SvgData {
207
204
type Err = Box < dyn Error > ;
208
205
@@ -213,7 +210,9 @@ impl FromStr for SvgData {
213
210
} ;
214
211
215
212
match usvg:: Tree :: from_str ( svg_str, & re_opt) {
216
- Ok ( tree) => Ok ( SvgData { tree } ) ,
213
+ Ok ( tree) => Ok ( SvgData {
214
+ tree : Arc :: new ( tree) ,
215
+ } ) ,
217
216
Err ( err) => Err ( err. into ( ) ) ,
218
217
}
219
218
}
0 commit comments