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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2019 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! An SVG widget.
use std::error::Error;
use std::str::FromStr;
use std::sync::Arc;
use log::error;
use usvg;
use crate::{
kurbo::BezPath, widget::common::FillStrat, Affine, BoxConstraints, Color, Data, Env, Event,
EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Rect, RenderContext, Size, UpdateCtx,
Widget,
};
/// A widget that renders a SVG
pub struct Svg {
svg_data: SvgData,
fill: FillStrat,
}
impl Svg {
/// Create an SVG-drawing widget from SvgData.
///
/// The SVG will scale to fit its box constraints.
pub fn new(svg_data: SvgData) -> Self {
Svg {
svg_data,
fill: FillStrat::default(),
}
}
/// Measure the SVG's size
#[allow(clippy::needless_return)]
fn get_size(&self) -> Size {
let root = self.svg_data.tree.root();
match *root.borrow() {
usvg::NodeKind::Svg(svg) => {
// Borrow checker gets confused without an explicit return
return Size::new(svg.size.width(), svg.size.height());
}
_ => {
//TODO: I don't think this is reachable?
error!("This SVG has no size for some reason.");
return Size::ZERO;
}
};
}
/// A builder-style method for specifying the fill strategy.
pub fn fill_mode(mut self, mode: FillStrat) -> Self {
self.fill = mode;
self
}
/// Modify the widget's `FillStrat`.
pub fn set_fill_mode(&mut self, newfil: FillStrat) {
self.fill = newfil;
}
}
impl<T: Data> Widget<T> for Svg {
fn event(&mut self, _ctx: &mut EventCtx, _event: &Event, _data: &mut T, _env: &Env) {}
fn lifecycle(&mut self, _ctx: &mut LifeCycleCtx, _event: &LifeCycle, _data: &T, _env: &Env) {}
fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: &T, _data: &T, _env: &Env) {}
fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &T,
_env: &Env,
) -> Size {
bc.debug_check("SVG");
if bc.is_width_bounded() {
bc.max()
} else {
bc.constrain(self.get_size())
}
}
fn paint(&mut self, ctx: &mut PaintCtx, _data: &T, _env: &Env) {
let offset_matrix = self.fill.affine_to_fill(ctx.size(), self.get_size());
let clip_rect = Rect::ZERO.with_size(ctx.size());
// The SvgData's to_piet function dose not clip to the svg's size
// CairoRenderContext is very like druids but with some extra goodies like clip
ctx.clip(clip_rect);
self.svg_data.to_piet(offset_matrix, ctx);
}
}
/// Stored SVG data.
/// Implements `FromStr` and can be converted to piet draw instructions.
#[derive(Clone)]
pub struct SvgData {
tree: Arc<usvg::Tree>,
}
impl SvgData {
/// Create an empty SVG
pub fn empty() -> Self {
let re_opt = usvg::Options {
keep_named_groups: false,
..usvg::Options::default()
};
let empty_svg = r###"
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<g fill="none">
</g>
</svg>
"###;
SvgData {
tree: Arc::new(usvg::Tree::from_str(empty_svg, &re_opt).unwrap()),
}
}
/// Convert SvgData into Piet draw instructions
pub fn to_piet(&self, offset_matrix: Affine, ctx: &mut PaintCtx) {
let root = self.tree.root();
for n in root.children() {
match *n.borrow() {
usvg::NodeKind::Path(ref p) => {
let mut path = BezPath::new();
for segment in p.data.iter() {
match *segment {
usvg::PathSegment::MoveTo { x, y } => {
path.move_to((x, y));
}
usvg::PathSegment::LineTo { x, y } => {
path.line_to((x, y));
}
usvg::PathSegment::CurveTo {
x1,
y1,
x2,
y2,
x,
y,
} => {
path.curve_to((x1, y1), (x2, y2), (x, y));
}
usvg::PathSegment::ClosePath => {
path.close_path();
}
}
}
path.apply_affine(Affine::new([
p.transform.a,
p.transform.b,
p.transform.c,
p.transform.d,
p.transform.e,
p.transform.f,
]));
path.apply_affine(offset_matrix);
match &p.fill {
Some(fill) => {
let brush = color_from_usvg(&fill.paint, fill.opacity);
ctx.fill(path.clone(), &brush);
}
None => {}
}
match &p.stroke {
Some(stroke) => {
let brush = color_from_usvg(&stroke.paint, stroke.opacity);
ctx.stroke(path.clone(), &brush, stroke.width.value());
}
None => {}
}
}
usvg::NodeKind::Defs => {
// TODO: implement defs
}
_ => {
// TODO: handle more of the SVG spec.
error!("{:?} is unimplemented", n.clone());
}
}
}
}
}
impl Default for SvgData {
fn default() -> Self {
SvgData::empty()
}
}
impl FromStr for SvgData {
type Err = Box<dyn Error>;
fn from_str(svg_str: &str) -> Result<Self, Self::Err> {
let re_opt = usvg::Options {
keep_named_groups: false,
..usvg::Options::default()
};
match usvg::Tree::from_str(svg_str, &re_opt) {
Ok(tree) => Ok(SvgData {
tree: Arc::new(tree),
}),
Err(err) => Err(err.into()),
}
}
}
fn color_from_usvg(paint: &usvg::Paint, opacity: usvg::Opacity) -> Color {
match paint {
usvg::Paint::Color(c) => Color::rgb8(c.red, c.green, c.blue).with_alpha(opacity.value()),
_ => {
//TODO: implement link
error!("We don't support Paint::Link yet, so here's some pink.");
Color::rgb8(255, 192, 203)
}
}
}