1 | /* |
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 Intel Corporation. All rights reserved. |
9 | * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved. |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
14 | * |
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. |
20 | * |
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 |
32 | * SUCH DAMAGE. |
33 | */ |
34 | |
35 | #include "config.h" |
36 | #include "CanvasPath.h" |
37 | |
38 | #include "AffineTransform.h" |
39 | #include "FloatRect.h" |
40 | #include <wtf/MathExtras.h> |
41 | |
42 | namespace WebCore { |
43 | |
44 | void CanvasPath::closePath() |
45 | { |
46 | if (m_path.isEmpty()) |
47 | return; |
48 | |
49 | FloatRect boundRect = m_path.fastBoundingRect(); |
50 | if (boundRect.width() || boundRect.height()) |
51 | m_path.closeSubpath(); |
52 | } |
53 | |
54 | void CanvasPath::moveTo(float x, float y) |
55 | { |
56 | if (!std::isfinite(x) || !std::isfinite(y)) |
57 | return; |
58 | if (!hasInvertibleTransform()) |
59 | return; |
60 | m_path.moveTo(FloatPoint(x, y)); |
61 | } |
62 | |
63 | void CanvasPath::lineTo(FloatPoint point) |
64 | { |
65 | lineTo(point.x(), point.y()); |
66 | } |
67 | |
68 | void CanvasPath::lineTo(float x, float y) |
69 | { |
70 | if (!std::isfinite(x) || !std::isfinite(y)) |
71 | return; |
72 | if (!hasInvertibleTransform()) |
73 | return; |
74 | |
75 | FloatPoint p1 = FloatPoint(x, y); |
76 | if (!m_path.hasCurrentPoint()) |
77 | m_path.moveTo(p1); |
78 | else if (p1 != m_path.currentPoint()) |
79 | m_path.addLineTo(p1); |
80 | } |
81 | |
82 | void CanvasPath::quadraticCurveTo(float cpx, float cpy, float x, float y) |
83 | { |
84 | if (!std::isfinite(cpx) || !std::isfinite(cpy) || !std::isfinite(x) || !std::isfinite(y)) |
85 | return; |
86 | if (!hasInvertibleTransform()) |
87 | return; |
88 | if (!m_path.hasCurrentPoint()) |
89 | m_path.moveTo(FloatPoint(cpx, cpy)); |
90 | |
91 | FloatPoint p1 = FloatPoint(x, y); |
92 | FloatPoint cp = FloatPoint(cpx, cpy); |
93 | if (p1 != m_path.currentPoint() || p1 != cp) |
94 | m_path.addQuadCurveTo(cp, p1); |
95 | } |
96 | |
97 | void CanvasPath::bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y) |
98 | { |
99 | if (!std::isfinite(cp1x) || !std::isfinite(cp1y) || !std::isfinite(cp2x) || !std::isfinite(cp2y) || !std::isfinite(x) || !std::isfinite(y)) |
100 | return; |
101 | if (!hasInvertibleTransform()) |
102 | return; |
103 | if (!m_path.hasCurrentPoint()) |
104 | m_path.moveTo(FloatPoint(cp1x, cp1y)); |
105 | |
106 | FloatPoint p1 = FloatPoint(x, y); |
107 | FloatPoint cp1 = FloatPoint(cp1x, cp1y); |
108 | FloatPoint cp2 = FloatPoint(cp2x, cp2y); |
109 | if (p1 != m_path.currentPoint() || p1 != cp1 || p1 != cp2) |
110 | m_path.addBezierCurveTo(cp1, cp2, p1); |
111 | } |
112 | |
113 | ExceptionOr<void> CanvasPath::arcTo(float x1, float y1, float x2, float y2, float r) |
114 | { |
115 | if (!std::isfinite(x1) || !std::isfinite(y1) || !std::isfinite(x2) || !std::isfinite(y2) || !std::isfinite(r)) |
116 | return { }; |
117 | |
118 | if (r < 0) |
119 | return Exception { IndexSizeError }; |
120 | |
121 | if (!hasInvertibleTransform()) |
122 | return { }; |
123 | |
124 | FloatPoint p1 = FloatPoint(x1, y1); |
125 | FloatPoint p2 = FloatPoint(x2, y2); |
126 | |
127 | if (!m_path.hasCurrentPoint()) |
128 | m_path.moveTo(p1); |
129 | else if (p1 == m_path.currentPoint() || p1 == p2 || !r) |
130 | lineTo(x1, y1); |
131 | else |
132 | m_path.addArcTo(p1, p2, r); |
133 | |
134 | return { }; |
135 | } |
136 | |
137 | static void normalizeAngles(float& startAngle, float& endAngle, bool anticlockwise) |
138 | { |
139 | float newStartAngle = startAngle; |
140 | if (newStartAngle < 0) |
141 | newStartAngle = (2 * piFloat) + fmodf(newStartAngle, -(2 * piFloat)); |
142 | else |
143 | newStartAngle = fmodf(newStartAngle, 2 * piFloat); |
144 | |
145 | float delta = newStartAngle - startAngle; |
146 | startAngle = newStartAngle; |
147 | endAngle = endAngle + delta; |
148 | ASSERT(newStartAngle >= 0 && newStartAngle < 2 * piFloat); |
149 | |
150 | if (anticlockwise && startAngle - endAngle >= 2 * piFloat) |
151 | endAngle = startAngle - 2 * piFloat; |
152 | else if (!anticlockwise && endAngle - startAngle >= 2 * piFloat) |
153 | endAngle = startAngle + 2 * piFloat; |
154 | } |
155 | |
156 | ExceptionOr<void> CanvasPath::arc(float x, float y, float radius, float startAngle, float endAngle, bool anticlockwise) |
157 | { |
158 | if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius) || !std::isfinite(startAngle) || !std::isfinite(endAngle)) |
159 | return { }; |
160 | |
161 | if (radius < 0) |
162 | return Exception { IndexSizeError }; |
163 | |
164 | if (!hasInvertibleTransform()) |
165 | return { }; |
166 | |
167 | normalizeAngles(startAngle, endAngle, anticlockwise); |
168 | |
169 | if (!radius || startAngle == endAngle) { |
170 | // The arc is empty but we still need to draw the connecting line. |
171 | lineTo(x + radius * cosf(startAngle), y + radius * sinf(startAngle)); |
172 | return { }; |
173 | } |
174 | |
175 | m_path.addArc(FloatPoint(x, y), radius, startAngle, endAngle, anticlockwise); |
176 | return { }; |
177 | } |
178 | |
179 | ExceptionOr<void> CanvasPath::ellipse(float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise) |
180 | { |
181 | if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radiusX) || !std::isfinite(radiusY) || !std::isfinite(rotation) || !std::isfinite(startAngle) || !std::isfinite(endAngle)) |
182 | return { }; |
183 | |
184 | if (radiusX < 0 || radiusY < 0) |
185 | return Exception { IndexSizeError }; |
186 | |
187 | if (!hasInvertibleTransform()) |
188 | return { }; |
189 | |
190 | normalizeAngles(startAngle, endAngle, anticlockwise); |
191 | |
192 | if ((!radiusX && !radiusY) || startAngle == endAngle) { |
193 | AffineTransform transform; |
194 | transform.translate(x, y).rotate(rad2deg(rotation)); |
195 | |
196 | lineTo(transform.mapPoint(FloatPoint(radiusX * cosf(startAngle), radiusY * sinf(startAngle)))); |
197 | return { }; |
198 | } |
199 | |
200 | if (!radiusX || !radiusY) { |
201 | AffineTransform transform; |
202 | transform.translate(x, y).rotate(rad2deg(rotation)); |
203 | |
204 | lineTo(transform.mapPoint(FloatPoint(radiusX * cosf(startAngle), radiusY * sinf(startAngle)))); |
205 | |
206 | if (!anticlockwise) { |
207 | for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat) + piOverTwoFloat; angle < endAngle; angle += piOverTwoFloat) |
208 | lineTo(transform.mapPoint(FloatPoint(radiusX * cosf(angle), radiusY * sinf(angle)))); |
209 | } else { |
210 | for (float angle = startAngle - fmodf(startAngle, piOverTwoFloat); angle > endAngle; angle -= piOverTwoFloat) |
211 | lineTo(transform.mapPoint(FloatPoint(radiusX * cosf(angle), radiusY * sinf(angle)))); |
212 | } |
213 | |
214 | lineTo(transform.mapPoint(FloatPoint(radiusX * cosf(endAngle), radiusY * sinf(endAngle)))); |
215 | return { }; |
216 | } |
217 | |
218 | m_path.addEllipse(FloatPoint(x, y), radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise); |
219 | return { }; |
220 | } |
221 | |
222 | void CanvasPath::rect(float x, float y, float width, float height) |
223 | { |
224 | if (!hasInvertibleTransform()) |
225 | return; |
226 | |
227 | if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) || !std::isfinite(height)) |
228 | return; |
229 | |
230 | if (!width && !height) { |
231 | m_path.moveTo(FloatPoint(x, y)); |
232 | return; |
233 | } |
234 | |
235 | m_path.addRect(FloatRect(x, y, width, height)); |
236 | } |
237 | |
238 | float CanvasPath::currentX() const |
239 | { |
240 | return m_path.currentPoint().x(); |
241 | } |
242 | |
243 | float CanvasPath::currentY() const |
244 | { |
245 | return m_path.currentPoint().y(); |
246 | } |
247 | |
248 | } |
249 | |