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

Side by Side Diff: Source/core/html/canvas/CanvasPathMethods.cpp

Issue 18286007: Fix edge case bugs of canvas arc. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@ellipse
Patch Set: Add comment for alph's edge case. Make canvas-arc-circumference.html cover 160 cases as alph sugges… 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 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) 3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> 6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
8 * Copyright (C) 2012 Intel Corporation. All rights reserved. 8 * Copyright (C) 2012 Intel Corporation. All rights reserved.
9 * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved. 9 * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved.
10 * 10 *
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 FloatPoint p2 = FloatPoint(x2, y2); 123 FloatPoint p2 = FloatPoint(x2, y2);
124 124
125 if (!m_path.hasCurrentPoint()) 125 if (!m_path.hasCurrentPoint())
126 m_path.moveTo(p1); 126 m_path.moveTo(p1);
127 else if (p1 == m_path.currentPoint() || p1 == p2 || !r) 127 else if (p1 == m_path.currentPoint() || p1 == p2 || !r)
128 lineTo(x1, y1); 128 lineTo(x1, y1);
129 else 129 else
130 m_path.addArcTo(p1, p2, r); 130 m_path.addArcTo(p1, p2, r);
131 } 131 }
132 132
133 void CanvasPathMethods::arc(float x, float y, float r, float sa, float ea, bool anticlockwise, ExceptionCode& ec) 133 static float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise )
134 {
135 float twoPi = 2 * piFloat;
136 float newEndAngle = endAngle;
137 /* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-el ement.html#dom-context-2d-arc
138 * If the anticlockwise argument is false and endAngle-startAngle is equal t o or greater than 2π, or,
Stephen White 2013/08/22 15:08:29 I'm not sure if unicode characters in C++ source a
139 * if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2π,
140 * then the arc is the whole circumference of this ellipse.
141 */
142 if (!anticlockwise && endAngle - startAngle >= twoPi)
143 newEndAngle = startAngle + twoPi + fmodf(endAngle - startAngle, twoPi);
144 else if (anticlockwise && startAngle - endAngle >= twoPi)
145 newEndAngle = startAngle - twoPi - fmodf(startAngle - endAngle, twoPi);
146
147 /*
148 * Otherwise, the arc is the path along the circumference of this ellipse fr om the start point to the end point,
149 * going anti-clockwise if the anticlockwise argument is true, and clockwise otherwise.
150 * Since the points are on the ellipse, as opposed to being simply angles fr om zero,
151 * the arc can never cover an angle greater than 2π radians.
152 */
153 /* NOTE: When startAngle = 0, endAngle = 2Pi and anticlockwise = true, the s pec does not indicate clearly.
154 * We draw the entire circle, because some web sites use arc(x, y, r, 0, 2*M ath.PI, true) to draw circle.
155 * We preserve backward-compatibility.
156 */
dshwang 2013/07/10 16:53:02 Add comment for alph's edge case.
157 else if (!anticlockwise && startAngle > endAngle)
158 newEndAngle = startAngle + (twoPi - fmodf(startAngle - endAngle, twoPi)) ;
159 else if (anticlockwise && startAngle < endAngle)
160 newEndAngle = startAngle - (twoPi - fmodf(endAngle - startAngle, twoPi)) ;
161
162 ASSERT(std::abs(newEndAngle - startAngle) < 4 * piFloat);
163 return newEndAngle;
164 }
165
166 void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, fl oat endAngle, bool anticlockwise, ExceptionCode& ec)
134 { 167 {
135 ec = 0; 168 ec = 0;
136 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(r) || !std::isf inite(sa) || !std::isfinite(ea)) 169 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius) || !std ::isfinite(startAngle) || !std::isfinite(endAngle))
137 return; 170 return;
138 171
139 if (r < 0) { 172 if (radius < 0) {
140 ec = IndexSizeError; 173 ec = IndexSizeError;
141 return; 174 return;
142 } 175 }
143 176
144 if (!r || sa == ea) {
145 // The arc is empty but we still need to draw the connecting line.
146 lineTo(x + r * cosf(sa), y + r * sinf(sa));
147 return;
148 }
149
150 if (!isTransformInvertible()) 177 if (!isTransformInvertible())
151 return; 178 return;
152 179
153 // If 'sa' and 'ea' differ by more than 2Pi, just add a circle starting/endi ng at 'sa'. 180 if (!radius || startAngle == endAngle) {
154 if (anticlockwise && sa - ea >= 2 * piFloat) { 181 // The arc is empty but we still need to draw the connecting line.
155 m_path.addArc(FloatPoint(x, y), r, sa, sa - 2 * piFloat, anticlockwise); 182 lineTo(x + radius * cosf(startAngle), y + radius * sinf(startAngle));
156 return;
157 }
158 if (!anticlockwise && ea - sa >= 2 * piFloat) {
159 m_path.addArc(FloatPoint(x, y), r, sa, sa + 2 * piFloat, anticlockwise);
160 return; 183 return;
161 } 184 }
162 185
163 m_path.addArc(FloatPoint(x, y), r, sa, ea, anticlockwise); 186 float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise) ;
187 m_path.addArc(FloatPoint(x, y), radius, startAngle, adjustedEndAngle, anticl ockwise);
164 } 188 }
165 189
166 void CanvasPathMethods::rect(float x, float y, float width, float height) 190 void CanvasPathMethods::rect(float x, float y, float width, float height)
167 { 191 {
168 if (!isTransformInvertible()) 192 if (!isTransformInvertible())
169 return; 193 return;
170 194
171 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) || !std: :isfinite(height)) 195 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) || !std: :isfinite(height))
172 return; 196 return;
173 197
174 if (!width && !height) { 198 if (!width && !height) {
175 m_path.moveTo(FloatPoint(x, y)); 199 m_path.moveTo(FloatPoint(x, y));
176 return; 200 return;
177 } 201 }
178 202
179 m_path.addRect(FloatRect(x, y, width, height)); 203 m_path.addRect(FloatRect(x, y, width, height));
180 } 204 }
181 } 205 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698