2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
9 * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "core/html/canvas/CanvasPathMethods.h"
38 #include "bindings/core/v8/ExceptionState.h"
39 #include "core/dom/ExceptionCode.h"
40 #include "platform/geometry/FloatRect.h"
41 #include "platform/transforms/AffineTransform.h"
42 #include "wtf/MathExtras.h"
46 void CanvasPathMethods::closePath()
51 FloatRect boundRect = m_path.boundingRect();
52 if (boundRect.width() || boundRect.height())
53 m_path.closeSubpath();
56 void CanvasPathMethods::moveTo(float x, float y)
58 if (!std::isfinite(x) || !std::isfinite(y))
60 if (!isTransformInvertible())
62 m_path.moveTo(FloatPoint(x, y));
65 void CanvasPathMethods::lineTo(float x, float y)
67 if (!std::isfinite(x) || !std::isfinite(y))
69 if (!isTransformInvertible())
72 FloatPoint p1 = FloatPoint(x, y);
73 if (!m_path.hasCurrentPoint())
75 else if (p1 != m_path.currentPoint())
79 void CanvasPathMethods::quadraticCurveTo(float cpx, float cpy, float x, float y)
81 if (!std::isfinite(cpx) || !std::isfinite(cpy) || !std::isfinite(x) || !std::isfinite(y))
83 if (!isTransformInvertible())
85 if (!m_path.hasCurrentPoint())
86 m_path.moveTo(FloatPoint(cpx, cpy));
88 FloatPoint p1 = FloatPoint(x, y);
89 FloatPoint cp = FloatPoint(cpx, cpy);
90 if (p1 != m_path.currentPoint() || p1 != cp)
91 m_path.addQuadCurveTo(cp, p1);
94 void CanvasPathMethods::bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y)
96 if (!std::isfinite(cp1x) || !std::isfinite(cp1y) || !std::isfinite(cp2x) || !std::isfinite(cp2y) || !std::isfinite(x) || !std::isfinite(y))
98 if (!isTransformInvertible())
100 if (!m_path.hasCurrentPoint())
101 m_path.moveTo(FloatPoint(cp1x, cp1y));
103 FloatPoint p1 = FloatPoint(x, y);
104 FloatPoint cp1 = FloatPoint(cp1x, cp1y);
105 FloatPoint cp2 = FloatPoint(cp2x, cp2y);
106 if (p1 != m_path.currentPoint() || p1 != cp1 || p1 != cp2)
107 m_path.addBezierCurveTo(cp1, cp2, p1);
110 void CanvasPathMethods::arcTo(float x1, float y1, float x2, float y2, float r, ExceptionState& exceptionState)
112 if (!std::isfinite(x1) || !std::isfinite(y1) || !std::isfinite(x2) || !std::isfinite(y2) || !std::isfinite(r))
116 exceptionState.throwDOMException(IndexSizeError, "The radius provided (" + String::number(r) + ") is negative.");
120 if (!isTransformInvertible())
123 FloatPoint p1 = FloatPoint(x1, y1);
124 FloatPoint p2 = FloatPoint(x2, y2);
126 if (!m_path.hasCurrentPoint())
128 else if (p1 == m_path.currentPoint() || p1 == p2 || !r)
131 m_path.addArcTo(p1, p2, r);
136 float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise)
138 float newEndAngle = endAngle;
139 /* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-arc
140 * If the anticlockwise argument is false and endAngle-startAngle is equal to or greater than 2pi, or,
141 * if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2pi,
142 * then the arc is the whole circumference of this ellipse, and the point at startAngle along this circle's circumference,
143 * measured in radians clockwise from the ellipse's semi-major axis, acts as both the start point and the end point.
145 if (!anticlockwise && endAngle - startAngle >= twoPiFloat)
146 newEndAngle = startAngle + twoPiFloat;
147 else if (anticlockwise && startAngle - endAngle >= twoPiFloat)
148 newEndAngle = startAngle - twoPiFloat;
151 * Otherwise, the arc is the path along the circumference of this ellipse from the start point to the end point,
152 * going anti-clockwise if the anticlockwise argument is true, and clockwise otherwise.
153 * Since the points are on the ellipse, as opposed to being simply angles from zero,
154 * the arc can never cover an angle greater than 2pi radians.
156 /* NOTE: When startAngle = 0, endAngle = 2Pi and anticlockwise = true, the spec does not indicate clearly.
157 * We draw the entire circle, because some web sites use arc(x, y, r, 0, 2*Math.PI, true) to draw circle.
158 * We preserve backward-compatibility.
160 else if (!anticlockwise && startAngle > endAngle)
161 newEndAngle = startAngle + (twoPiFloat - fmodf(startAngle - endAngle, twoPiFloat));
162 else if (anticlockwise && startAngle < endAngle)
163 newEndAngle = startAngle - (twoPiFloat - fmodf(endAngle - startAngle, twoPiFloat));
165 ASSERT(ellipseIsRenderable(startAngle, newEndAngle));
169 inline void lineToFloatPoint(CanvasPathMethods* path, const FloatPoint& p)
171 path->lineTo(p.x(), p.y());
174 inline FloatPoint getPointOnEllipse(float radiusX, float radiusY, float theta)
176 return FloatPoint(radiusX * cosf(theta), radiusY * sinf(theta));
179 void canonicalizeAngle(float* startAngle, float* endAngle)
181 // Make 0 <= startAngle < 2*PI
182 float newStartAngle = *startAngle;
183 if (newStartAngle < 0)
184 newStartAngle = twoPiFloat + fmodf(newStartAngle, -twoPiFloat);
186 newStartAngle = fmodf(newStartAngle, twoPiFloat);
188 float delta = newStartAngle - *startAngle;
189 *startAngle = newStartAngle;
190 *endAngle = *endAngle + delta;
191 ASSERT(newStartAngle >= 0 && newStartAngle < twoPiFloat);
195 * degenerateEllipse() handles a degenerated ellipse using several lines.
197 * Let's see a following example: line to ellipse to line.
204 * If radiusX becomes zero, the ellipse of the example is degenerated.
212 * To draw the above example, need to get P that is a local maximum point.
213 * Angles for P are 0.5Pi and 1.5Pi in the ellipse coordinates.
215 * If radiusY becomes zero, the result is as follows.
220 * Angles for P are 0 and Pi in the ellipse coordinates.
222 * To handle both cases, degenerateEllipse() lines to start angle, local maximum points(every 0.5Pi), and end angle.
223 * NOTE: Before ellipse() calls this function, adjustEndAngle() is called, so endAngle - startAngle must be equal to or less than 2Pi.
225 void degenerateEllipse(CanvasPathMethods* path, float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise)
227 ASSERT(ellipseIsRenderable(startAngle, endAngle));
228 ASSERT(startAngle >= 0 && startAngle < twoPiFloat);
229 ASSERT((anticlockwise && (startAngle - endAngle) >= 0) || (!anticlockwise && (endAngle - startAngle) >= 0));
231 FloatPoint center(x, y);
232 AffineTransform rotationMatrix;
233 rotationMatrix.rotateRadians(rotation);
234 // First, if the object's path has any subpaths, then the method must add a straight line from the last point in the subpath to the start point of the arc.
235 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, startAngle)));
236 if ((!radiusX && !radiusY) || startAngle == endAngle)
239 if (!anticlockwise) {
240 // startAngle - fmodf(startAngle, piOverTwoFloat) + piOverTwoFloat is the one of (0, 0.5Pi, Pi, 1.5Pi, 2Pi)
241 // that is the closest to startAngle on the clockwise direction.
242 for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat) + piOverTwoFloat; angle < endAngle; angle += piOverTwoFloat)
243 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
245 for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat); angle > endAngle; angle -= piOverTwoFloat)
246 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
249 lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, endAngle)));
254 void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, float endAngle, bool anticlockwise, ExceptionState& exceptionState)
256 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
260 exceptionState.throwDOMException(IndexSizeError, "The radius provided (" + String::number(radius) + ") is negative.");
264 if (!isTransformInvertible())
267 if (!radius || startAngle == endAngle) {
268 // The arc is empty but we still need to draw the connecting line.
269 lineTo(x + radius * cosf(startAngle), y + radius * sinf(startAngle));
273 canonicalizeAngle(&startAngle, &endAngle);
274 float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
275 m_path.addArc(FloatPoint(x, y), radius, startAngle, adjustedEndAngle, anticlockwise);
278 void CanvasPathMethods::ellipse(float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise, ExceptionState& exceptionState)
280 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radiusX) || !std::isfinite(radiusY) || !std::isfinite(rotation) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
284 exceptionState.throwDOMException(IndexSizeError, "The major-axis radius provided (" + String::number(radiusX) + ") is negative.");
288 exceptionState.throwDOMException(IndexSizeError, "The minor-axis radius provided (" + String::number(radiusY) + ") is negative.");
292 if (!isTransformInvertible())
295 canonicalizeAngle(&startAngle, &endAngle);
296 float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
297 if (!radiusX || !radiusY || startAngle == adjustedEndAngle) {
298 // The ellipse is empty but we still need to draw the connecting line to start point.
299 degenerateEllipse(this, x, y, radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
303 m_path.addEllipse(FloatPoint(x, y), radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
306 void CanvasPathMethods::rect(float x, float y, float width, float height)
308 if (!isTransformInvertible())
311 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) || !std::isfinite(height))
314 if (!width && !height) {
315 m_path.moveTo(FloatPoint(x, y));
319 m_path.addRect(FloatRect(x, y, width, height));