Skip to content

Commit 46fc5a7

Browse files
committed
fix: quad rendering including border only inside of the bounds
1 parent 422b4de commit 46fc5a7

1 file changed

Lines changed: 42 additions & 4 deletions

File tree

tiny_skia/src/backend.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl Backend {
157157
border_width,
158158
border_color,
159159
} => {
160+
// XXX border seems to be contained by the bounds of the primitive in wgpu and for damage regions, so we do the same here
160161
let physical_bounds = (*bounds + translation) * scale_factor;
161162

162163
if !clip_bounds.intersects(&physical_bounds) {
@@ -172,7 +173,26 @@ impl Backend {
172173
)
173174
.post_scale(scale_factor, scale_factor);
174175

175-
let path = rounded_rectangle(*bounds, *border_radius);
176+
// Make sure the border radius is not larger than the bounds
177+
let border_width = border_width
178+
.min(bounds.width / 2.0)
179+
.min(bounds.height / 2.0);
180+
181+
// Offset the fill by the border width
182+
let path_bounds = Rectangle {
183+
x: bounds.x + border_width,
184+
y: bounds.y + border_width,
185+
width: bounds.width - 2.0 * border_width,
186+
height: bounds.height - 2.0 * border_width,
187+
};
188+
// fill border radius is the border radius minus the border width
189+
let mut fill_border_radius = *border_radius;
190+
for radius in &mut fill_border_radius {
191+
*radius = (*radius - border_width / 2.0)
192+
.min(path_bounds.width / 2.0)
193+
.min(path_bounds.height / 2.0);
194+
}
195+
let path = rounded_rectangle(path_bounds, fill_border_radius);
176196

177197
pixels.fill_path(
178198
&path,
@@ -192,9 +212,27 @@ impl Backend {
192212
clip_mask,
193213
);
194214

195-
if *border_width > 0.0 {
215+
// border path is offset by half the border width
216+
let path_bounds = Rectangle {
217+
x: bounds.x + border_width / 2.0,
218+
y: bounds.y + border_width / 2.0,
219+
width: bounds.width - border_width,
220+
height: bounds.height - border_width,
221+
};
222+
223+
let mut border_radius = *border_radius;
224+
for radius in &mut border_radius {
225+
*radius = radius
226+
.min(path_bounds.width / 2.0)
227+
.min(path_bounds.height / 2.0);
228+
}
229+
230+
let border_radius_path =
231+
rounded_rectangle(path_bounds, border_radius);
232+
233+
if border_width > 0.0 {
196234
pixels.stroke_path(
197-
&path,
235+
&border_radius_path,
198236
&tiny_skia::Paint {
199237
shader: tiny_skia::Shader::SolidColor(into_color(
200238
*border_color,
@@ -203,7 +241,7 @@ impl Backend {
203241
..tiny_skia::Paint::default()
204242
},
205243
&tiny_skia::Stroke {
206-
width: *border_width,
244+
width: border_width,
207245
..tiny_skia::Stroke::default()
208246
},
209247
transform,

0 commit comments

Comments
 (0)