-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add elliptical radial gradient support using aspect ratio #4054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||
| use crate::renderer::{RenderParams, format_transform_matrix}; | ||||||||||
| use core_types::uuid::generate_uuid; | ||||||||||
| use glam::DAffine2; | ||||||||||
| use glam::{DAffine2, DVec2}; | ||||||||||
| use graphic_types::vector_types::gradient::{Gradient, GradientType}; | ||||||||||
| use graphic_types::vector_types::vector::style::{Fill, PaintOrder, PathStyle, Stroke, StrokeAlign, StrokeCap, StrokeJoin}; | ||||||||||
| use std::fmt::Write; | ||||||||||
|
|
@@ -36,17 +36,11 @@ impl RenderExt for Gradient { | |||||||||
| let start = transform_points.transform_point2(self.start); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Duplicated Prompt for AI agents |
||||||||||
| let end = transform_points.transform_point2(self.end); | ||||||||||
|
|
||||||||||
| let gradient_transform = if transformed_bounds.matrix2.determinant() != 0. { | ||||||||||
| let gradient_transform_raw = if transformed_bounds.matrix2.determinant() != 0. { | ||||||||||
| transformed_bounds.inverse() | ||||||||||
| } else { | ||||||||||
| DAffine2::IDENTITY // Ignore if the transform cannot be inverted (the bounds are zero). See issue #1944. | ||||||||||
| }; | ||||||||||
| let gradient_transform = format_transform_matrix(gradient_transform); | ||||||||||
| let gradient_transform = if gradient_transform.is_empty() { | ||||||||||
| String::new() | ||||||||||
| } else { | ||||||||||
| format!(r#" gradientTransform="{gradient_transform}""#) | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| let spread_method = if self.spread_method == GradientSpreadMethod::Pad { | ||||||||||
| String::new() | ||||||||||
|
|
@@ -58,14 +52,42 @@ impl RenderExt for Gradient { | |||||||||
|
|
||||||||||
| match self.gradient_type { | ||||||||||
| GradientType::Linear => { | ||||||||||
| let gradient_transform = format_transform_matrix(gradient_transform_raw); | ||||||||||
| let gradient_transform = if gradient_transform.is_empty() { | ||||||||||
| String::new() | ||||||||||
| } else { | ||||||||||
| format!(r#" gradientTransform="{gradient_transform}""#) | ||||||||||
| }; | ||||||||||
| let _ = write!( | ||||||||||
| svg_defs, | ||||||||||
| r#"<linearGradient id="{}" x1="{}" y1="{}" x2="{}" y2="{}"{spread_method}{gradient_transform}>{}</linearGradient>"#, | ||||||||||
| gradient_id, start.x, start.y, end.x, end.y, stop | ||||||||||
| ); | ||||||||||
| } | ||||||||||
| GradientType::Radial => { | ||||||||||
| let radius = (f64::powi(start.x - end.x, 2) + f64::powi(start.y - end.y, 2)).sqrt(); | ||||||||||
| let radius = start.distance(end); | ||||||||||
|
|
||||||||||
| let ellipse_transform = if (self.aspect - 1.).abs() > f64::EPSILON { | ||||||||||
| let major_vec = end - start; | ||||||||||
| let angle = major_vec.y.atan2(major_vec.x); | ||||||||||
| let squash = DAffine2::from_translation(start) | ||||||||||
| * DAffine2::from_angle(angle) | ||||||||||
| * DAffine2::from_scale(DVec2::new(1., self.aspect)) | ||||||||||
| * DAffine2::from_angle(-angle) | ||||||||||
| * DAffine2::from_translation(-start); | ||||||||||
|
|
||||||||||
| squash * gradient_transform_raw | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order of transform multiplication is incorrect.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Transform multiplication order is reversed. Prompt for AI agents
Suggested change
|
||||||||||
| } else { | ||||||||||
| gradient_transform_raw | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| let gradient_transform = format_transform_matrix(ellipse_transform); | ||||||||||
| let gradient_transform = if gradient_transform.is_empty() { | ||||||||||
| String::new() | ||||||||||
| } else { | ||||||||||
| format!(r#" gradientTransform="{gradient_transform}""#) | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| let _ = write!( | ||||||||||
| svg_defs, | ||||||||||
| r#"<radialGradient id="{}" cx="{}" cy="{}" r="{}"{spread_method}{gradient_transform}>{}</radialGradient>"#, | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1174,7 +1174,22 @@ impl Render for Table<Vector> { | |||||
| } else { | ||||||
| Default::default() | ||||||
| }; | ||||||
| let brush_transform = kurbo::Affine::new((inverse_element_transform * parent_transform).to_cols_array()); | ||||||
| let mut brush_transform = kurbo::Affine::new((inverse_element_transform * parent_transform).to_cols_array()); | ||||||
|
|
||||||
| if gradient.gradient_type == GradientType::Radial && (gradient.aspect - 1.).abs() > f64::EPSILON { | ||||||
| let major_vec = end - start; | ||||||
| let angle = major_vec.y.atan2(major_vec.x); | ||||||
| let center = kurbo::Vec2::new(start.x, start.y); | ||||||
|
|
||||||
| let ellipse_affine = kurbo::Affine::translate(center) | ||||||
| * kurbo::Affine::rotate(angle) | ||||||
| * kurbo::Affine::scale_non_uniform(1., gradient.aspect) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Radial gradient aspect ratio is used as a raw scale factor without validation, allowing zero/negative values to create degenerate or mirrored transforms. Prompt for AI agents |
||||||
| * kurbo::Affine::rotate(-angle) | ||||||
| * kurbo::Affine::translate(-center); | ||||||
|
|
||||||
| brush_transform = ellipse_affine * brush_transform; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Elliptical radial gradient affine is composed in the wrong order, causing coordinate-space mismatch under transforms. Prompt for AI agents
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| scene.fill(fill_rule, kurbo::Affine::new(element_transform.to_cols_array()), &fill, Some(brush_transform), path); | ||||||
| } | ||||||
| Fill::None => {} | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
calculate_insertionfunction is missing its closing brace and the finalNonereturn value. This will cause a compilation error because the function is expected to returnOption<f64>, but it currently only returns a value inside theifblock and is never closed.