Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1116)

Unified Diff: Source/core/html/canvas/CanvasPathMethods.cpp

Issue 14298022: Add support for new canvas ellipse method. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Complete degenerateEllipse. Make canvas-ellipse-zero-lineto.html cover various degenerate edge case… Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/html/canvas/CanvasPathMethods.cpp
diff --git a/Source/core/html/canvas/CanvasPathMethods.cpp b/Source/core/html/canvas/CanvasPathMethods.cpp
index e5b8648b00b5d8045dbbed483b73f5b6f52bb289..dfc36fdac01f58304cd7347016e286519070672d 100644
--- a/Source/core/html/canvas/CanvasPathMethods.cpp
+++ b/Source/core/html/canvas/CanvasPathMethods.cpp
@@ -37,6 +37,7 @@
#include "core/dom/ExceptionCode.h"
#include "core/platform/graphics/FloatRect.h"
+#include "core/platform/graphics/transforms/AffineTransform.h"
#include <wtf/MathExtras.h>
namespace WebCore {
@@ -133,15 +134,16 @@ void CanvasPathMethods::arcTo(float x1, float y1, float x2, float y2, float r, E
static float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise)
{
float twoPi = 2 * piFloat;
+ float newEndAngle = endAngle;
/* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-arc
* If the anticlockwise argument is false and endAngle-startAngle is equal to or greater than 2π, or,
* if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2π,
* then the arc is the whole circumference of this ellipse.
*/
if (!anticlockwise && endAngle - startAngle >= twoPi)
- return startAngle + twoPi + fmodf(endAngle - startAngle, twoPi);
- if (anticlockwise && startAngle - endAngle >= twoPi)
- return startAngle - twoPi - fmodf(startAngle - endAngle, twoPi);
+ newEndAngle = startAngle + twoPi + fmodf(endAngle - startAngle, twoPi);
+ else if (anticlockwise && startAngle - endAngle >= twoPi)
+ newEndAngle = startAngle - twoPi - fmodf(startAngle - endAngle, twoPi);
/*
* Otherwise, the arc is the path along the circumference of this ellipse from the start point to the end point,
@@ -149,12 +151,13 @@ static float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise
* Since the points are on the ellipse, as opposed to being simply angles from zero,
* the arc can never cover an angle greater than 2π radians.
*/
- if (!anticlockwise && startAngle > endAngle)
- return startAngle + (twoPi - fmodf(startAngle - endAngle, twoPi));
- if (anticlockwise && startAngle < endAngle)
- return startAngle - (twoPi - fmodf(endAngle - startAngle, twoPi));
+ else if (!anticlockwise && startAngle > endAngle)
+ newEndAngle = startAngle + (twoPi - fmodf(startAngle - endAngle, twoPi));
+ else if (anticlockwise && startAngle < endAngle)
+ newEndAngle = startAngle - (twoPi - fmodf(endAngle - startAngle, twoPi));
- return endAngle;
+ ASSERT(std::abs(newEndAngle - startAngle) < 4 * piFloat);
+ return newEndAngle;
}
void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, float endAngle, bool anticlockwise, ExceptionCode& ec)
@@ -181,6 +184,81 @@ void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, fl
m_path.addArc(FloatPoint(x, y), radius, startAngle, adjustedEndAngle, anticlockwise);
}
+inline static void lineToFloatPoint(CanvasPathMethods* path, const FloatPoint& p)
+{
+ path->lineTo(p.x(), p.y());
+}
+
+inline static FloatPoint getPointOnEllipse(float radiusX, float radiusY, float theta)
+{
+ return FloatPoint(radiusX * cosf(theta), radiusY * sinf(theta));
+}
+
+inline static void canonicalizeAngle(float* startAngle, float* endAngle)
+{
+ // Make 0 <= startAngle < 2*PI
+ float twoPi = 2 * piFloat;
+ float newStartAngle = *startAngle;
+ if (newStartAngle < 0)
+ newStartAngle = twoPi + fmodf(newStartAngle, -twoPi);
+ else
+ newStartAngle = fmodf(newStartAngle, twoPi);
+
+ float delta = newStartAngle - *startAngle;
+ *startAngle = newStartAngle;
+ *endAngle = *endAngle + delta;
+}
+
+static void degenerateEllipse(CanvasPathMethods* path, float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise)
+{
+ ASSERT(std::abs(endAngle - startAngle) < 4 * piFloat);
+
+ FloatPoint center(x, y);
+ AffineTransform rotationMatrix;
+ rotationMatrix.rotate(rad2deg(rotation));
+ lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, startAngle)));
+ if ((!radiusX && !radiusY) || startAngle == endAngle)
+ return;
+
+ canonicalizeAngle(&startAngle, &endAngle);
alph 2013/07/10 13:38:04 It seems that canonicalizeAngle should depend on a
dshwang 2013/07/10 14:19:57 canonicalizeAngle() is independent from whether an
alph 2013/07/10 15:22:30 Sorry, missed that.
+ ASSERT(std::abs(endAngle - startAngle) < 4 * piFloat);
+
+ float halfPiFloat = piFloat * 0.5;
+ if (!anticlockwise) {
+ for (float angle = startAngle - fmodf(startAngle, halfPiFloat) + halfPiFloat; angle < endAngle; angle += halfPiFloat)
+ lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
+ } else {
+ for (float angle = startAngle - fmodf(startAngle, halfPiFloat); angle > endAngle; angle -= halfPiFloat)
+ lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
+ }
+
+ lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, endAngle)));
+}
+
+void CanvasPathMethods::ellipse(float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise, ExceptionCode& ec)
+{
+ ec = 0;
+ if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radiusX) || !std::isfinite(radiusY) || !std::isfinite(rotation) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
+ return;
+
+ if (radiusX < 0 || radiusY < 0) {
+ ec = IndexSizeError;
+ return;
+ }
+
+ if (!isTransformInvertible())
+ return;
+
+ float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
+ if (!radiusX || !radiusY || startAngle == adjustedEndAngle) {
+ // The ellipse is empty but we still need to draw the connecting line to start point.
+ degenerateEllipse(this, x, y, radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
+ return;
+ }
+
+ m_path.addEllipse(FloatPoint(x, y), radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
+}
+
void CanvasPathMethods::rect(float x, float y, float width, float height)
{
if (!isTransformInvertible())

Powered by Google App Engine
This is Rietveld 408576698