Add gradient support to vector graphics

This commit is contained in:
Skyler Lehmkuhl
2026-03-10 00:57:47 -04:00
parent 8bd65e5904
commit 26f06da5bf
8 changed files with 330 additions and 96 deletions
@@ -66,6 +66,18 @@ pub struct ShapeGradient {
/// Ignored for Radial.
pub angle: f32,
pub extend: GradientExtend,
/// Explicit world-space start point set by the gradient drag tool.
/// For Linear: the start of the gradient axis.
/// For Radial: the center of the gradient circle.
/// When `None`, the renderer falls back to bbox-based computation.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub start_world: Option<(f64, f64)>,
/// Explicit world-space end point set by the gradient drag tool.
/// For Linear: the end of the gradient axis.
/// For Radial: a point on the edge of the gradient circle (defines radius).
/// When `None`, the renderer falls back to bbox-based computation.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub end_world: Option<(f64, f64)>,
}
impl Default for ShapeGradient {
@@ -73,11 +85,13 @@ impl Default for ShapeGradient {
Self {
kind: GradientType::Linear,
stops: vec![
GradientStop { position: 0.0, color: ShapeColor::rgba(0, 0, 0, 255) },
GradientStop { position: 1.0, color: ShapeColor::rgba(0, 0, 0, 0) },
GradientStop { position: 0.0, color: ShapeColor::rgba(255, 255, 255, 255) },
GradientStop { position: 1.0, color: ShapeColor::rgba(0, 0, 0, 255) },
],
angle: 0.0,
extend: GradientExtend::Pad,
start_world: None,
end_world: None,
}
}
}
@@ -94,7 +94,7 @@ pub fn hit_test_layer(
if face.deleted || i == 0 {
continue; // skip unbounded face
}
if face.fill_color.is_none() && face.image_fill.is_none() {
if face.fill_color.is_none() && face.image_fill.is_none() && face.gradient_fill.is_none() {
continue;
}
if face.outer_half_edge.is_none() {
@@ -472,7 +472,7 @@ pub fn hit_test_vector_editing(
if face.deleted || i == 0 {
continue;
}
if face.fill_color.is_none() && face.image_fill.is_none() {
if face.fill_color.is_none() && face.image_fill.is_none() && face.gradient_fill.is_none() {
continue;
}
if face.outer_half_edge.is_none() {
@@ -1092,8 +1092,24 @@ pub fn render_dcel(
if !filled {
if let Some(ref grad) = face.gradient_fill {
use kurbo::Rect;
use crate::gradient::GradientType;
let bbox: Rect = vello::kurbo::Shape::bounding_box(&path);
let (start, end) = gradient_bbox_endpoints(grad.angle, bbox);
let (start, end) = match (grad.start_world, grad.end_world) {
(Some((sx, sy)), Some((ex, ey))) => match grad.kind {
GradientType::Linear => {
(kurbo::Point::new(sx, sy), kurbo::Point::new(ex, ey))
}
GradientType::Radial => {
// start_world = center, end_world = edge point.
// to_peniko_brush uses midpoint(start, end) as center,
// so reflect the edge through the center to get the
// opposing diameter endpoint.
let opp = kurbo::Point::new(2.0 * sx - ex, 2.0 * sy - ey);
(opp, kurbo::Point::new(ex, ey))
}
},
_ => gradient_bbox_endpoints(grad.angle, bbox),
};
let brush = grad.to_peniko_brush(start, end, opacity_f32);
scene.fill(fill_rule, base_transform, &brush, None, &path);
filled = true;
@@ -366,6 +366,7 @@ impl Tool {
Tool::Rectangle,
Tool::Ellipse,
Tool::PaintBucket,
Tool::Gradient,
Tool::Eyedropper,
Tool::Line,
Tool::Polygon,