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
// Copyright 2024 the Vello Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT
use crate::util;
use anyrender::PaintScene;
use kurbo::{Affine, BezPath};
use peniko::{BlendMode, Fill};
use usvg::{Node, Path};
pub(crate) fn render_group<S: PaintScene, F: FnMut(&mut S, &usvg::Node)>(
scene: &mut S,
group: &usvg::Group,
transform: Affine,
global_transform: Affine,
error_handler: &mut F,
) {
for node in group.children() {
let transform = transform * util::to_affine(&node.abs_transform());
match node {
usvg::Node::Group(g) => {
let alpha = g.opacity().get();
let is_fully_opaque = alpha >= 1.0;
let mix = util::to_mix(g.blend_mode());
// Support clip-path with a single path
let clip_path = g
.clip_path()
.and_then(|path| path.root().children().first());
let did_push_layer = match clip_path {
// If there is a clip path, then push a layer that clips using it
Some(usvg::Node::Path(clip_path)) => {
let local_path = util::to_bez_path(clip_path);
scene.push_layer(
BlendMode {
mix,
compose: peniko::Compose::SrcOver,
},
alpha,
global_transform * transform,
&local_path,
);
true
}
// Else if there is blending to be done then push a layer with a rectangular clip
#[allow(deprecated)]
_ if mix != peniko::Mix::Normal || !is_fully_opaque => {
// Use bounding box as the clip path.
let bounding_box = g.layer_bounding_box();
let rect = kurbo::Rect::from_origin_size(
(bounding_box.x(), bounding_box.y()),
(bounding_box.width() as f64, bounding_box.height() as f64),
);
scene.push_layer(
BlendMode {
mix,
compose: peniko::Compose::SrcOver,
},
alpha,
global_transform * transform,
&rect,
);
true
}
// Else if there is no clip or blending then don't push a layer
_ => false,
};
render_group(scene, g, Affine::IDENTITY, global_transform, error_handler);
if did_push_layer {
scene.pop_layer();
}
}
usvg::Node::Path(path) => {
if !path.is_visible() {
continue;
}
let local_path = util::to_bez_path(path);
let transform = global_transform * transform;
match path.paint_order() {
usvg::PaintOrder::FillAndStroke => {
fill(scene, error_handler, path, transform, &local_path, node);
stroke(scene, error_handler, path, transform, &local_path, node);
}
usvg::PaintOrder::StrokeAndFill => {
stroke(scene, error_handler, path, transform, &local_path, node);
fill(scene, error_handler, path, transform, &local_path, node);
}
}
}
usvg::Node::Image(img) => {
if !img.is_visible() {
continue;
}
match img.kind() {
usvg::ImageKind::JPEG(_)
| usvg::ImageKind::PNG(_)
| usvg::ImageKind::GIF(_)
| usvg::ImageKind::WEBP(_) => {
#[cfg(feature = "image")]
{
let Ok(decoded_image) = util::decode_raw_raster_image(img.kind())
else {
error_handler(scene, node);
continue;
};
let image = util::into_image(decoded_image);
let image_ts = global_transform * util::to_affine(&img.abs_transform());
scene.draw_image(image.as_ref(), image_ts);
}
#[cfg(not(feature = "image"))]
{
error_handler(scene, node);
continue;
}
}
usvg::ImageKind::SVG(svg) => {
render_group(
scene,
svg.root(),
transform,
global_transform,
error_handler,
);
}
}
}
usvg::Node::Text(text) => {
render_group(
scene,
text.flattened(),
transform,
global_transform,
error_handler,
);
}
}
}
}
fn fill<S: PaintScene, F: FnMut(&mut S, &usvg::Node)>(
scene: &mut S,
error_handler: &mut F,
path: &Path,
transform: Affine,
local_path: &BezPath,
node: &Node,
) {
if let Some(fill) = &path.fill() {
if let Some((paint, brush_transform)) = util::to_brush(fill.paint(), fill.opacity()) {
scene.fill(
match fill.rule() {
usvg::FillRule::NonZero => Fill::NonZero,
usvg::FillRule::EvenOdd => Fill::EvenOdd,
},
transform,
paint.as_ref(),
Some(brush_transform),
local_path,
);
} else {
error_handler(scene, node);
}
}
}
fn stroke<S: PaintScene, F: FnMut(&mut S, &usvg::Node)>(
scene: &mut S,
error_handler: &mut F,
path: &Path,
transform: Affine,
local_path: &BezPath,
node: &Node,
) {
if let Some(stroke) = &path.stroke() {
if let Some((paint, brush_transform)) = util::to_brush(stroke.paint(), stroke.opacity()) {
let conv_stroke = util::to_stroke(stroke);
scene.stroke(
&conv_stroke,
transform,
paint.as_ref(),
Some(brush_transform),
local_path,
);
} else {
error_handler(scene, node);
}
}
}