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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use eyre::{Context, Result};
use kurbo::{Arc, BezPath, CubicBez, Line, QuadBez, SvgArc, Vec2};
use piet::kurbo::{Point, Size};
use piet::{Color, FixedLinearGradient, FixedRadialGradient, GradientStop, RenderContext};
use crate::format::{Command, OutlineStyle, Segment, SegmentCommand, SegmentCommandKind, Style};
impl crate::format::Image {
/// Render PNG data to the given `std::io::Write`.
///
/// ```
/// # use tinyvg::Decoder;
/// # use std::fs::File;
/// let mut decoder = Decoder::new(File::open("data/shield.tvg").unwrap());
///
/// let image = decoder.decode().unwrap();
/// let mut file = File::create("data/shield.png").unwrap();
///
/// image.render_png(&mut file).unwrap();
/// ```
#[cfg(feature = "render-png")]
pub fn render_png(&self, writer: &mut impl std::io::Write) -> Result<()> {
use cairo::{Format, ImageSurface};
use piet_cairo::CairoRenderContext;
let size = Size {
width: self.header.width as f64,
height: self.header.height as f64,
};
let surface = ImageSurface::create(Format::ARgb32, size.width as i32, size.height as i32)
.wrap_err("failed to create cairo surface")?;
let cr = cairo::Context::new(&surface).unwrap();
let render_result = {
let mut piet_context = CairoRenderContext::new(&cr);
let result = self
.draw(&mut piet_context)
.wrap_err("failed to draw tinyvg file");
piet_context
.finish()
.map_err(|e| eyre::eyre!("{}", e))
.wrap_err("failed to finalize piet context")?;
result
};
surface.flush();
surface.write_to_png(writer)?;
render_result?;
Ok(())
}
fn outline_style<R>(&self, rc: &mut R, o: &Option<OutlineStyle>) -> Result<(f64, R::Brush)>
where
R: RenderContext,
{
match o {
Some(style) => Ok((style.line_width, self.brush(rc, &style.line_style)?)),
None => Ok((0.0, nil_brush(rc))),
}
}
fn color(&self, index: usize) -> Result<Color> {
self.color_table.get(index).cloned().ok_or_else(|| {
eyre::eyre!(
"file has {} colors but tried to get index {}",
self.color_table.len(),
index
)
})
}
fn brush<R>(&self, rc: &mut R, style: &Style) -> Result<R::Brush>
where
R: RenderContext,
{
let brush = match style {
Style::FlatColor { color_index } => rc.solid_brush(self.color(*color_index)?),
Style::LinearGradient {
point_0,
point_1,
color_index_0,
color_index_1,
} => rc
.gradient(FixedLinearGradient {
start: *point_0,
end: *point_1,
stops: vec![
GradientStop {
pos: 0.0,
color: self.color(*color_index_0)?,
},
GradientStop {
pos: 1.0,
color: self.color(*color_index_1)?,
},
],
})
.map_err(|e| eyre::eyre!("{}", e))?,
Style::RadialGradient {
point_0,
point_1,
color_index_0,
color_index_1,
} => rc
.gradient(FixedRadialGradient {
center: *point_0,
origin_offset: Vec2 { x: 0.0, y: 0.0 },
radius: point_0.distance(*point_1),
stops: vec![
GradientStop {
pos: 0.0,
color: self.color(*color_index_0)?,
},
GradientStop {
pos: 1.0,
color: self.color(*color_index_1)?,
},
],
})
.map_err(|e| eyre::eyre!("{}", e))?,
};
Ok(brush)
}
/// Draw a TinyVG image onto the given `piet::RenderContext`. This is
/// exposed as an internal so that people who need formats other than PNGs
/// can utilize it. PNGs can more easily be generated by using
/// `Image::render_png`.
pub fn draw(&self, rc: &mut impl RenderContext) -> Result<()> {
for cmd in &self.commands {
match cmd {
Command::FillPath {
fill_style,
path,
outline,
} => {
let fill = self.brush(rc, fill_style)?;
let (line_width, line_brush) = self.outline_style(rc, outline)?;
draw_path(rc, fill, line_brush, line_width, path)?;
}
Command::FillRectangles {
fill_style,
rectangles,
outline,
} => {
let brush = self.brush(rc, fill_style)?;
let (line_width, line_brush) = self.outline_style(rc, outline)?;
for rect in rectangles {
rc.fill(rect, &brush);
rc.stroke(rect, &line_brush, line_width);
}
}
Command::FillPolygon {
fill_style,
polygon,
outline,
} => {
let brush = self.brush(rc, fill_style)?;
let (line_width, line_brush) = self.outline_style(rc, outline)?;
let mut bez = BezPath::new();
bez.move_to(polygon[0]);
for point in polygon {
bez.line_to(*point);
}
rc.fill(&bez, &brush);
rc.stroke(&bez, &line_brush, line_width);
}
Command::DrawLines {
line_style,
line_width,
lines,
} => {
let brush = self.brush(rc, line_style)?;
for line in lines {
rc.stroke(line, &brush, *line_width);
}
}
Command::DrawLineLoop {
line_style,
line_width,
close_path,
points,
} => {
let line = self.brush(rc, line_style)?;
let mut bez = BezPath::new();
let start = points[0];
bez.move_to(start);
for p in points {
bez.line_to(*p);
}
if *close_path {
bez.line_to(start);
}
rc.stroke(bez, &line, *line_width);
}
Command::DrawLinePath {
line_style,
line_width,
path,
} => {
let line = self.brush(rc, line_style)?;
let fill = nil_brush(rc);
draw_path(rc, fill, line, *line_width, path)?;
}
}
}
Ok(())
}
}
fn draw_path<R>(
rc: &mut R,
fill: R::Brush,
line: R::Brush,
mut line_width: f64,
path: &[Segment],
) -> Result<()>
where
R: RenderContext,
{
let mut bezier = BezPath::new();
for Segment { start, commands } in path {
let mut pen = *start;
bezier.move_to(pen);
for SegmentCommand {
kind,
line_width: next_line_width,
} in commands
{
if let Some(lw) = next_line_width {
line_width = *lw;
}
match kind {
SegmentCommandKind::Line { end } => {
bezier.line_to(*end);
rc.stroke(Line { p0: pen, p1: *end }, &line, line_width);
pen = *end;
}
SegmentCommandKind::VerticalLine { y } => {
let end = Point { x: pen.x, y: *y };
bezier.line_to(end);
rc.stroke(Line { p0: pen, p1: end }, &line, line_width);
pen = end;
}
SegmentCommandKind::CubicBezier {
control_0,
control_1,
point_1,
} => {
bezier.curve_to(*control_0, *control_1, *point_1);
rc.stroke(
CubicBez::new(pen, *control_0, *control_1, *point_1),
&line,
line_width,
);
pen = *point_1;
}
SegmentCommandKind::HorizontalLine { x } => {
let end = Point { x: *x, y: pen.y };
bezier.line_to(end);
rc.stroke(Line { p0: pen, p1: end }, &line, line_width);
pen = end;
}
SegmentCommandKind::ArcEllipse {
large,
sweep,
radius_x,
radius_y,
rotation,
target,
} => {
let svg_arc = SvgArc {
from: pen,
to: *target,
radii: Vec2 {
x: *radius_x,
y: *radius_y,
},
x_rotation: *rotation,
large_arc: *large,
sweep: *sweep,
};
let arc = Arc::from_svg_arc(&svg_arc).ok_or_else(|| {
eyre::eyre!("failed to create arc from svg arc {:?}", svg_arc)
})?;
for segment in arc.append_iter(0.2) {
bezier.push(segment);
}
rc.stroke(&arc, &line, line_width);
pen = *target;
}
SegmentCommandKind::ClosePath => {
bezier.line_to(*start);
rc.stroke(
Line {
p0: pen,
p1: *start,
},
&line,
line_width,
);
pen = *start;
}
SegmentCommandKind::QuadraticBezier { control, point_1 } => {
bezier.quad_to(*control, *point_1);
rc.stroke(QuadBez::new(pen, *control, *point_1), &line, line_width);
pen = *point_1;
}
}
}
}
rc.fill(&bezier, &fill);
Ok(())
}
fn nil_brush<R>(rc: &mut R) -> R::Brush
where
R: RenderContext,
{
rc.solid_brush(Color::rgba(0.0, 0.0, 0.0, 0.0))
}