mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 17:15:56 +00:00
EPS import: a radial shading's circle is cut to enough sides for large radii
The end circle of a radial shading not extended was cut to 64 sides, so its chords lay up to 0.12% of the radius inside it and left a visible unpainted band on large circles. The side count now comes from a 0.01 pt tolerance in the document (64 to 1024 sides).
This commit is contained in:
@@ -429,6 +429,11 @@ fn is_gradient(sh: &DictRef) -> bool {
|
||||
matches!(sh.borrow().get(&Key::name("ShadingType")).and_then(Obj::as_num), Some(2.0 | 3.0))
|
||||
}
|
||||
|
||||
/// How far (points) the sides of the polygon a radial shading's circle is cut to may lie inside it.
|
||||
const RING_TOLERANCE: f64 = 0.01;
|
||||
/// Most sides of that polygon.
|
||||
const MAX_RING: usize = 1024;
|
||||
|
||||
/// The part of `area` (in the document) that axial or radial shading `sh`, mapped into the
|
||||
/// document by `m`, paints, as a convex polygon; `None` when it paints all of it. Without
|
||||
/// `Extend` an axial shading paints only between the lines through its ends across its axis, and
|
||||
@@ -473,7 +478,12 @@ pub(super) fn shading_extent(sh: &DictRef, m: Affine, area: Rect) -> Option<Vec<
|
||||
Some(3.0) if !e0 && !e1 && at(2) <= 0.0 && at(5) > 0.0 => {
|
||||
// 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));
|
||||
let ring: Vec<Point> = (0..64).map(|i| c1 + Vec2::from_angle(i as f64 * std::f64::consts::TAU / 64.0) * r1).collect();
|
||||
// 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();
|
||||
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();
|
||||
for edge in hull(&[ring, vec![p0]].concat()).windows(2) {
|
||||
if let [a, b] = edge {
|
||||
let (a, b) = (*a, *b);
|
||||
|
||||
@@ -1166,6 +1166,19 @@ fn shadings_not_extended_paint_only_between_their_ends() {
|
||||
// Radial from the centre point to a circle of radius 30.
|
||||
let b = shade(3, "50 50 0 50 50 30", "");
|
||||
assert!((b.width() - 60.0).abs() < 0.5 && (b.height() - 60.0).abs() < 0.5 && (b.center().x - 50.0).abs() < 0.5, "{b:?}");
|
||||
// Its circle is cut to a polygon whose sides lie within 0.01 pt of it, also for a large one.
|
||||
let r = read(
|
||||
"<< /ShadingType 3 /ColorSpace /DeviceRGB /Coords [50 50 0 50 50 45] \
|
||||
/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 ring: Vec<Point> = path.subpaths.iter().flat_map(|s| s.anchors.iter().map(|a| a.p)).collect();
|
||||
let centre = Point::new(50.0, 50.0);
|
||||
for (a, b) in ring.iter().zip(ring.iter().cycle().skip(1)) {
|
||||
assert!((a.distance(centre) - 45.0).abs() < 1e-6, "{a:?}");
|
||||
assert!(a.midpoint(*b).distance(centre) > 45.0 - 0.01, "{a:?} {b:?}");
|
||||
}
|
||||
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:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user