Draw border path for quad only if it has a border in iced_tiny_skia
This commit is contained in:
parent
102c78abd8
commit
bf7d636ebf
1 changed files with 108 additions and 104 deletions
|
|
@ -159,7 +159,6 @@ impl Backend {
|
||||||
border_width,
|
border_width,
|
||||||
border_color,
|
border_color,
|
||||||
} => {
|
} => {
|
||||||
// XXX border seems to be contained by the bounds of the primitive in wgpu and for damage regions, so we do the same here
|
|
||||||
let physical_bounds = (*bounds + translation) * scale_factor;
|
let physical_bounds = (*bounds + translation) * scale_factor;
|
||||||
|
|
||||||
if !clip_bounds.intersects(&physical_bounds) {
|
if !clip_bounds.intersects(&physical_bounds) {
|
||||||
|
|
@ -206,115 +205,120 @@ impl Backend {
|
||||||
clip_mask,
|
clip_mask,
|
||||||
);
|
);
|
||||||
|
|
||||||
// border path is offset by half the border width
|
if border_width > 0.0 {
|
||||||
let path_bounds = Rectangle {
|
// Border path is offset by half the border width
|
||||||
x: bounds.x + border_width / 2.0,
|
let border_bounds = Rectangle {
|
||||||
y: bounds.y + border_width / 2.0,
|
x: bounds.x + border_width / 2.0,
|
||||||
width: bounds.width - border_width,
|
y: bounds.y + border_width / 2.0,
|
||||||
height: bounds.height - border_width,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Make sure the border radius is correct
|
|
||||||
let mut border_radius = *border_radius;
|
|
||||||
let mut border_radius_gt_half_border_width =
|
|
||||||
[true, true, true, true];
|
|
||||||
for (i, radius) in &mut border_radius.iter_mut().enumerate() {
|
|
||||||
*radius = if *radius == 0.0 {
|
|
||||||
// Path should handle this fine
|
|
||||||
0.0
|
|
||||||
} else if *radius > border_width / 2.0 {
|
|
||||||
*radius - border_width / 2.0
|
|
||||||
} else {
|
|
||||||
border_radius_gt_half_border_width[i] = false;
|
|
||||||
0.0
|
|
||||||
}
|
|
||||||
.min(path_bounds.width / 2.0)
|
|
||||||
.min(path_bounds.height / 2.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stroking a path works well in this case.
|
|
||||||
if border_radius_gt_half_border_width.iter().all(|b| *b) {
|
|
||||||
let border_radius_path =
|
|
||||||
rounded_rectangle(path_bounds, border_radius);
|
|
||||||
|
|
||||||
pixels.stroke_path(
|
|
||||||
&border_radius_path,
|
|
||||||
&tiny_skia::Paint {
|
|
||||||
shader: tiny_skia::Shader::SolidColor(into_color(
|
|
||||||
*border_color,
|
|
||||||
)),
|
|
||||||
anti_alias: true,
|
|
||||||
..tiny_skia::Paint::default()
|
|
||||||
},
|
|
||||||
&tiny_skia::Stroke {
|
|
||||||
width: border_width,
|
|
||||||
..tiny_skia::Stroke::default()
|
|
||||||
},
|
|
||||||
transform,
|
|
||||||
clip_mask,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Draw corners that have to small border radii as having no border radius,
|
|
||||||
// but mask them with the rounded rectangle with the correct border radius.
|
|
||||||
|
|
||||||
let mut temp_pixmap =
|
|
||||||
Pixmap::new(bounds.width as u32, bounds.height as u32)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut quad_mask =
|
|
||||||
Mask::new(bounds.width as u32, bounds.height as u32)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let zero_bounds = Rectangle {
|
|
||||||
x: 0.0,
|
|
||||||
y: 0.0,
|
|
||||||
width: bounds.width,
|
|
||||||
height: bounds.height,
|
|
||||||
};
|
|
||||||
let path =
|
|
||||||
rounded_rectangle(zero_bounds, fill_border_radius);
|
|
||||||
|
|
||||||
quad_mask.fill_path(
|
|
||||||
&path,
|
|
||||||
tiny_skia::FillRule::EvenOdd,
|
|
||||||
true,
|
|
||||||
transform,
|
|
||||||
);
|
|
||||||
let path_bounds = Rectangle {
|
|
||||||
x: border_width / 2.0,
|
|
||||||
y: border_width / 2.0,
|
|
||||||
width: bounds.width - border_width,
|
width: bounds.width - border_width,
|
||||||
height: bounds.height - border_width,
|
height: bounds.height - border_width,
|
||||||
};
|
};
|
||||||
|
|
||||||
let border_radius_path =
|
// Make sure the border radius is correct
|
||||||
rounded_rectangle(path_bounds, border_radius);
|
let mut border_radius = *border_radius;
|
||||||
|
let mut is_simple_border = true;
|
||||||
|
|
||||||
temp_pixmap.stroke_path(
|
for radius in &mut border_radius {
|
||||||
&border_radius_path,
|
*radius = if *radius == 0.0 {
|
||||||
&tiny_skia::Paint {
|
// Path should handle this fine
|
||||||
shader: tiny_skia::Shader::SolidColor(into_color(
|
0.0
|
||||||
*border_color,
|
} else if *radius > border_width / 2.0 {
|
||||||
)),
|
*radius - border_width / 2.0
|
||||||
anti_alias: true,
|
} else {
|
||||||
..tiny_skia::Paint::default()
|
is_simple_border = false;
|
||||||
},
|
0.0
|
||||||
&tiny_skia::Stroke {
|
}
|
||||||
width: border_width,
|
.min(border_bounds.width / 2.0)
|
||||||
..tiny_skia::Stroke::default()
|
.min(border_bounds.height / 2.0);
|
||||||
},
|
}
|
||||||
transform,
|
|
||||||
Some(&quad_mask),
|
|
||||||
);
|
|
||||||
|
|
||||||
pixels.draw_pixmap(
|
// Stroking a path works well in this case
|
||||||
bounds.x as i32,
|
if is_simple_border {
|
||||||
bounds.y as i32,
|
let border_path =
|
||||||
temp_pixmap.as_ref(),
|
rounded_rectangle(border_bounds, border_radius);
|
||||||
&PixmapPaint::default(),
|
|
||||||
transform,
|
pixels.stroke_path(
|
||||||
clip_mask,
|
&border_path,
|
||||||
);
|
&tiny_skia::Paint {
|
||||||
|
shader: tiny_skia::Shader::SolidColor(
|
||||||
|
into_color(*border_color),
|
||||||
|
),
|
||||||
|
anti_alias: true,
|
||||||
|
..tiny_skia::Paint::default()
|
||||||
|
},
|
||||||
|
&tiny_skia::Stroke {
|
||||||
|
width: border_width,
|
||||||
|
..tiny_skia::Stroke::default()
|
||||||
|
},
|
||||||
|
transform,
|
||||||
|
clip_mask,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Draw corners that have too small border radii as having no border radius,
|
||||||
|
// but mask them with the rounded rectangle with the correct border radius.
|
||||||
|
let mut temp_pixmap = Pixmap::new(
|
||||||
|
bounds.width as u32,
|
||||||
|
bounds.height as u32,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut quad_mask = Mask::new(
|
||||||
|
bounds.width as u32,
|
||||||
|
bounds.height as u32,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let zero_bounds = Rectangle {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
width: bounds.width,
|
||||||
|
height: bounds.height,
|
||||||
|
};
|
||||||
|
let path =
|
||||||
|
rounded_rectangle(zero_bounds, fill_border_radius);
|
||||||
|
|
||||||
|
quad_mask.fill_path(
|
||||||
|
&path,
|
||||||
|
tiny_skia::FillRule::EvenOdd,
|
||||||
|
true,
|
||||||
|
transform,
|
||||||
|
);
|
||||||
|
let path_bounds = Rectangle {
|
||||||
|
x: border_width / 2.0,
|
||||||
|
y: border_width / 2.0,
|
||||||
|
width: bounds.width - border_width,
|
||||||
|
height: bounds.height - border_width,
|
||||||
|
};
|
||||||
|
|
||||||
|
let border_radius_path =
|
||||||
|
rounded_rectangle(path_bounds, border_radius);
|
||||||
|
|
||||||
|
temp_pixmap.stroke_path(
|
||||||
|
&border_radius_path,
|
||||||
|
&tiny_skia::Paint {
|
||||||
|
shader: tiny_skia::Shader::SolidColor(
|
||||||
|
into_color(*border_color),
|
||||||
|
),
|
||||||
|
anti_alias: true,
|
||||||
|
..tiny_skia::Paint::default()
|
||||||
|
},
|
||||||
|
&tiny_skia::Stroke {
|
||||||
|
width: border_width,
|
||||||
|
..tiny_skia::Stroke::default()
|
||||||
|
},
|
||||||
|
transform,
|
||||||
|
Some(&quad_mask),
|
||||||
|
);
|
||||||
|
|
||||||
|
pixels.draw_pixmap(
|
||||||
|
bounds.x as i32,
|
||||||
|
bounds.y as i32,
|
||||||
|
temp_pixmap.as_ref(),
|
||||||
|
&PixmapPaint::default(),
|
||||||
|
transform,
|
||||||
|
clip_mask,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Primitive::Text {
|
Primitive::Text {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue