Fix SkPath::arcTo when sweepAngle is tiny and radius is big
In this function, it first check whether this arc is a lone point
or not. If not, it converts angles to unit vectors. The problem
here is that when the radius is huge and the sweepAngle is small,
the function angles_to_unit_vectors() could return a startV ==stopV.
When that happens, it will draw a dot at the point that corresponding
to the startAngle. This CL adds a special branch for this case, and
draw a connecting line between the points at startAngle and endAngle.
BUG=640031, skia:5807
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2388833002
Review-Url: https://codereview.chromium.org/2388833002
diff --git a/gm/addarc.cpp b/gm/addarc.cpp
index 0a1876b..ff67af2 100644
--- a/gm/addarc.cpp
+++ b/gm/addarc.cpp
@@ -219,10 +219,13 @@
//////////////////////
static void html_canvas_arc(SkPath* path, SkScalar x, SkScalar y, SkScalar r, SkScalar start,
- SkScalar end, bool ccw) {
+ SkScalar end, bool ccw, bool callArcTo) {
SkRect bounds = { x - r, y - r, x + r, y + r };
SkScalar sweep = ccw ? end - start : start - end;
- path->arcTo(bounds, start, sweep, false);
+ if (callArcTo)
+ path->arcTo(bounds, start, sweep, false);
+ else
+ path->addArc(bounds, start, sweep);
}
// Lifted from canvas-arc-circumference-fill-diffs.html
@@ -269,7 +272,7 @@
SkPath path;
path.moveTo(0, 2);
html_canvas_arc(&path, 18, 15, 10, startAngle, startAngle + (sweepAngles[j] * sign),
- anticlockwise);
+ anticlockwise, true);
path.lineTo(0, 28);
canvas->drawPath(path, paint);
canvas->translate(30, 0);
@@ -283,3 +286,37 @@
typedef skiagm::GM INHERITED;
};
DEF_GM( return new ManyArcsGM; )
+
+// Lifted from https://bugs.chromium.org/p/chromium/issues/detail?id=640031
+class TinyAngleBigRadiusArcsGM : public skiagm::GM {
+public:
+ TinyAngleBigRadiusArcsGM() {}
+
+protected:
+ SkString onShortName() override { return SkString("tinyanglearcs"); }
+
+ SkISize onISize() override { return SkISize::Make(620, 330); }
+
+ void onDraw(SkCanvas* canvas) override {
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ paint.setStyle(SkPaint::kStroke_Style);
+
+ canvas->translate(50, 50);
+
+ SkPath path;
+ path.moveTo(50, 20);
+ path.lineTo(50, 0);
+ // A combination of tiny sweepAngle + large radius, we should draw a line.
+ html_canvas_arc(&path, 50, 100000, 100000, 270, 270.0f - 0.00572957795f,
+ false, true);
+ path.lineTo(60, 20);
+ html_canvas_arc(&path, 50, 100000, 99980, 270.0f - 0.00572957795f, 270,
+ false, false);
+ canvas->drawPath(path, paint);
+ }
+
+private:
+ typedef skiagm::GM INHERITED;
+};
+DEF_GM( return new TinyAngleBigRadiusArcsGM; )
diff --git a/src/core/SkPath.cpp b/src/core/SkPath.cpp
index 3cbc76a..4c49e6b 100644
--- a/src/core/SkPath.cpp
+++ b/src/core/SkPath.cpp
@@ -1290,6 +1290,25 @@
angles_to_unit_vectors(startAngle, sweepAngle, &startV, &stopV, &dir);
SkPoint singlePt;
+
+ // At this point, we know that the arc is not a lone point, but startV == stopV
+ // indicates that the sweepAngle is too small such that angles_to_unit_vectors
+ // cannot handle it.
+ if (startV == stopV) {
+ SkScalar endAngle = SkDegreesToRadians(startAngle + sweepAngle);
+ SkScalar radiusX = oval.width() / 2;
+ SkScalar radiusY = oval.height() / 2;
+ // We cannot use SkScalarSinCos function in the next line because
+ // SkScalarSinCos has a threshold *SkScalarNearlyZero*. When sin(startAngle)
+ // is 0 and sweepAngle is very small and radius is huge, the expected
+ // behavior here is to draw a line. But calling SkScalarSinCos will
+ // make sin(endAngle) to be 0 which will then draw a dot.
+ singlePt.set(oval.centerX() + radiusX * sk_float_cos(endAngle),
+ oval.centerY() + radiusY * sk_float_sin(endAngle));
+ forceMoveTo ? this->moveTo(singlePt) : this->lineTo(singlePt);
+ return;
+ }
+
SkConic conics[SkConic::kMaxConicsForArc];
int count = build_arc_conics(oval, startV, stopV, dir, conics, &singlePt);
if (count) {