EPS import: a radial shading's ring is sized for its greatest stretch

The side count used the radius scaled by the square root of the
transform's determinant, which a non-uniform scale (4 by 0.25, say)
leaves at 1 while some sides stretch 4 times. It now uses the greatest
stretch of the transform.
This commit is contained in:
Don Omondi
2026-11-07 15:02:03 -05:00
parent 60953b5077
commit 1cdea65482
2 changed files with 17 additions and 2 deletions
+5 -2
View File
@@ -479,8 +479,11 @@ pub(super) fn shading_extent(sh: &DictRef, m: Affine, area: Rect) -> Option<Vec<
// The circles from a point to a circle cover the hull of that point and circle.
let (p0, c1, r1) = (Point::new(at(0), at(1)), Point::new(at(3), at(4)), at(5));
// Enough sides that each lies within `RING_TOLERANCE` of the circle in the document
// (the radius scaled by how `m` scales areas), from 64 to `MAX_RING`.
let r = r1 * m.determinant().abs().sqrt();
// (the radius scaled by the most `m` stretches any direction), from 64 to `MAX_RING`.
let [a, b, c, d, ..] = m.as_coeffs();
let (sum, det) = (a * a + b * b + c * c + d * d, a * d - b * c);
let stretch = ((sum + (sum * sum - 4.0 * det * det).max(0.0).sqrt()) / 2.0).sqrt();
let r = r1 * stretch;
let sides = (std::f64::consts::PI / (1.0 - RING_TOLERANCE / r).clamp(-1.0, 1.0).acos()).ceil();
let sides = if sides.is_finite() { (sides as usize).clamp(64, MAX_RING) } else { MAX_RING };
let ring: Vec<Point> = (0..sides).map(|i| c1 + Vec2::from_angle(i as f64 * std::f64::consts::TAU / sides as f64) * r1).collect();
+12
View File
@@ -1179,6 +1179,18 @@ fn shadings_not_extended_paint_only_between_their_ends() {
assert!((a.distance(centre) - 45.0).abs() < 1e-6, "{a:?}");
assert!(a.midpoint(*b).distance(centre) > 45.0 - 0.01, "{a:?} {b:?}");
}
// Stretched 4 times across and squashed to a quarter down (areas unchanged), the sides still
// lie within 0.01 pt of the ellipse it becomes.
let r = read(
"50 50 translate 4 0.25 scale << /ShadingType 3 /ColorSpace /DeviceRGB /Coords [0 0 0 0 0 10] \
/Function << /FunctionType 2 /Domain [0 1] /C0 [1 0 0] /C1 [0 0 1] /N 1 >> >> shfill",
);
let n = all(&r.document).into_iter().find(|n| matches!(fill(n), Some(Paint::Gradient(_)))).expect("a gradient fill");
let NodeKind::Path { path, .. } = &n.kind else { panic!("{n:?}") };
let sides = path.subpaths.iter().map(|s| s.anchors.len()).sum::<usize>();
// A side of a regular polygon in the circle lies 1 - cos(pi / sides) of the radius inside it;
// stretched to 40 pt across, that is at most 0.01 pt.
assert!(40.0 * (1.0 - (std::f64::consts::PI / sides as f64).cos()) <= 0.01 + 1e-9, "{sides} sides");
let b = shade(3, "50 50 0 50 50 30", "/Extend [true true]");
assert!(near(b, Rect::new(0.0, 0.0, 100.0, 100.0)), "{b:?}");
}