Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / style / BasicShapes.cpp
1 /*
2  * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials
14  *    provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "core/rendering/style/BasicShapes.h"
32
33 #include "core/css/BasicShapeFunctions.h"
34 #include "platform/LengthFunctions.h"
35 #include "platform/geometry/FloatRect.h"
36 #include "platform/graphics/Path.h"
37
38 namespace WebCore {
39
40 bool BasicShape::canBlend(const BasicShape* other) const
41 {
42     // FIXME: Support animations between different shapes in the future.
43     if (!other || !isSameType(*other))
44         return false;
45
46     // Just polygons with same number of vertices can be animated.
47     if (type() == BasicShape::BasicShapePolygonType
48         && (static_cast<const BasicShapePolygon*>(this)->values().size() != static_cast<const BasicShapePolygon*>(other)->values().size()
49         || static_cast<const BasicShapePolygon*>(this)->windRule() != static_cast<const BasicShapePolygon*>(other)->windRule()))
50         return false;
51
52     // Circles with keywords for radii or center coordinates cannot be animated.
53     if (type() == BasicShape::BasicShapeCircleType) {
54         const BasicShapeCircle* thisCircle = static_cast<const BasicShapeCircle*>(this);
55         const BasicShapeCircle* otherCircle = static_cast<const BasicShapeCircle*>(other);
56         if (!thisCircle->radius().canBlend(otherCircle->radius())
57             || !thisCircle->centerX().canBlend(otherCircle->centerX())
58             || !thisCircle->centerY().canBlend(otherCircle->centerY()))
59             return false;
60     }
61
62     // Ellipses with keywords for radii or center coordinates cannot be animated.
63     if (type() != BasicShape::BasicShapeEllipseType)
64         return true;
65
66     const BasicShapeEllipse* thisEllipse = static_cast<const BasicShapeEllipse*>(this);
67     const BasicShapeEllipse* otherEllipse = static_cast<const BasicShapeEllipse*>(other);
68     return (thisEllipse->radiusX().canBlend(otherEllipse->radiusX())
69         && thisEllipse->radiusY().canBlend(otherEllipse->radiusY())
70         && thisEllipse->centerX().canBlend(otherEllipse->centerX())
71         && thisEllipse->centerY().canBlend(otherEllipse->centerY()));
72 }
73
74 void BasicShapeRectangle::path(Path& path, const FloatRect& boundingBox)
75 {
76     ASSERT(path.isEmpty());
77     path.addRoundedRect(
78         FloatRect(
79             floatValueForLength(m_x, boundingBox.width()) + boundingBox.x(),
80             floatValueForLength(m_y, boundingBox.height()) + boundingBox.y(),
81             floatValueForLength(m_width, boundingBox.width()),
82             floatValueForLength(m_height, boundingBox.height())
83         ),
84         FloatSize(
85             floatValueForLength(m_cornerRadiusX, boundingBox.width()),
86             floatValueForLength(m_cornerRadiusY, boundingBox.height())
87         )
88     );
89 }
90
91 PassRefPtr<BasicShape> BasicShapeRectangle::blend(const BasicShape* other, double progress) const
92 {
93     ASSERT(other && isSameType(*other));
94
95     const BasicShapeRectangle* o = static_cast<const BasicShapeRectangle*>(other);
96     RefPtr<BasicShapeRectangle> result =  BasicShapeRectangle::create();
97     result->setX(m_x.blend(o->x(), progress, ValueRangeAll));
98     result->setY(m_y.blend(o->y(), progress, ValueRangeAll));
99     result->setWidth(m_width.blend(o->width(), progress, ValueRangeNonNegative));
100     result->setHeight(m_height.blend(o->height(), progress, ValueRangeNonNegative));
101     result->setCornerRadiusX(m_cornerRadiusX.blend(o->cornerRadiusX(), progress, ValueRangeNonNegative));
102     result->setCornerRadiusY(m_cornerRadiusY.blend(o->cornerRadiusY(), progress, ValueRangeNonNegative));
103     return result.release();
104 }
105
106 bool BasicShapeRectangle::operator==(const BasicShape& o) const
107 {
108     if (!isSameType(o))
109         return false;
110     const BasicShapeRectangle& other = toBasicShapeRectangle(o);
111     return m_y == other.m_y && m_x == other.m_x && m_width == other.m_width && m_height == other.m_height && m_cornerRadiusX == other.m_cornerRadiusX && m_cornerRadiusY == other.m_cornerRadiusY;
112 }
113
114 bool DeprecatedBasicShapeCircle::operator==(const BasicShape& o) const
115 {
116     if (!isSameType(o))
117         return false;
118     const DeprecatedBasicShapeCircle& other = toDeprecatedBasicShapeCircle(o);
119     return m_centerX == other.m_centerX && m_centerY == other.m_centerY && m_radius == other.m_radius;
120 }
121
122 void DeprecatedBasicShapeCircle::path(Path& path, const FloatRect& boundingBox)
123 {
124     ASSERT(path.isEmpty());
125     float diagonal = hypotf(boundingBox.width(), boundingBox.height()) / sqrtf(2);
126     float centerX = floatValueForLength(m_centerX, boundingBox.width());
127     float centerY = floatValueForLength(m_centerY, boundingBox.height());
128     float radius = floatValueForLength(m_radius, diagonal);
129     path.addEllipse(FloatRect(
130         centerX - radius + boundingBox.x(),
131         centerY - radius + boundingBox.y(),
132         radius * 2,
133         radius * 2
134     ));
135 }
136
137 PassRefPtr<BasicShape> DeprecatedBasicShapeCircle::blend(const BasicShape* other, double progress) const
138 {
139     ASSERT(other && isSameType(*other));
140
141     const DeprecatedBasicShapeCircle* o = static_cast<const DeprecatedBasicShapeCircle*>(other);
142     RefPtr<DeprecatedBasicShapeCircle> result =  DeprecatedBasicShapeCircle::create();
143     result->setCenterX(m_centerX.blend(o->centerX(), progress, ValueRangeAll));
144     result->setCenterY(m_centerY.blend(o->centerY(), progress, ValueRangeAll));
145     result->setRadius(m_radius.blend(o->radius(), progress, ValueRangeNonNegative));
146     return result.release();
147 }
148
149 bool BasicShapeCircle::operator==(const BasicShape& o) const
150 {
151     if (!isSameType(o))
152         return false;
153     const BasicShapeCircle& other = toBasicShapeCircle(o);
154     return m_centerX == other.m_centerX && m_centerY == other.m_centerY && m_radius == other.m_radius;
155 }
156
157 float BasicShapeCircle::floatValueForRadiusInBox(FloatSize boxSize) const
158 {
159     if (m_radius.type() == BasicShapeRadius::Value)
160         return floatValueForLength(m_radius.value(), hypotf(boxSize.width(), boxSize.height()) / sqrtf(2));
161
162     FloatPoint center = floatPointForCenterCoordinate(m_centerX, m_centerY, boxSize);
163
164     if (m_radius.type() == BasicShapeRadius::ClosestSide)
165         return std::min(std::min(center.x(), boxSize.width() - center.x()), std::min(center.y(), boxSize.height() - center.y()));
166
167     // If radius.type() == BasicShapeRadius::FarthestSide.
168     return std::max(std::max(center.x(), boxSize.width() - center.x()), std::max(center.y(), boxSize.height() - center.y()));
169 }
170
171 void BasicShapeCircle::path(Path& path, const FloatRect& boundingBox)
172 {
173     ASSERT(path.isEmpty());
174     FloatPoint center = floatPointForCenterCoordinate(m_centerX, m_centerY, boundingBox.size());
175     float radius = floatValueForRadiusInBox(boundingBox.size());
176     path.addEllipse(FloatRect(
177         center.x() - radius + boundingBox.x(),
178         center.y() - radius + boundingBox.y(),
179         radius * 2,
180         radius * 2
181     ));
182 }
183
184 PassRefPtr<BasicShape> BasicShapeCircle::blend(const BasicShape* other, double progress) const
185 {
186     ASSERT(type() == other->type());
187     const BasicShapeCircle* o = static_cast<const BasicShapeCircle*>(other);
188     RefPtr<BasicShapeCircle> result =  BasicShapeCircle::create();
189
190     result->setCenterX(m_centerX.blend(o->centerX(), progress));
191     result->setCenterY(m_centerY.blend(o->centerY(), progress));
192     result->setRadius(m_radius.blend(o->radius(), progress));
193     return result.release();
194 }
195
196 void DeprecatedBasicShapeEllipse::path(Path& path, const FloatRect& boundingBox)
197 {
198     ASSERT(path.isEmpty());
199     float centerX = floatValueForLength(m_centerX, boundingBox.width());
200     float centerY = floatValueForLength(m_centerY, boundingBox.height());
201     float radiusX = floatValueForLength(m_radiusX, boundingBox.width());
202     float radiusY = floatValueForLength(m_radiusY, boundingBox.height());
203     path.addEllipse(FloatRect(
204         centerX - radiusX + boundingBox.x(),
205         centerY - radiusY + boundingBox.y(),
206         radiusX * 2,
207         radiusY * 2
208     ));
209 }
210
211 bool DeprecatedBasicShapeEllipse::operator==(const BasicShape& o) const
212 {
213     if (!isSameType(o))
214         return false;
215     const DeprecatedBasicShapeEllipse& other = toDeprecatedBasicShapeEllipse(o);
216     return m_centerX == other.m_centerX && m_centerY == other.m_centerY && m_radiusX == other.m_radiusX && m_radiusY == other.m_radiusY;
217 }
218
219 PassRefPtr<BasicShape> DeprecatedBasicShapeEllipse::blend(const BasicShape* other, double progress) const
220 {
221     ASSERT(other && isSameType(*other));
222
223     const DeprecatedBasicShapeEllipse* o = static_cast<const DeprecatedBasicShapeEllipse*>(other);
224     RefPtr<DeprecatedBasicShapeEllipse> result = DeprecatedBasicShapeEllipse::create();
225     result->setCenterX(m_centerX.blend(o->centerX(), progress, ValueRangeAll));
226     result->setCenterY(m_centerY.blend(o->centerY(), progress, ValueRangeAll));
227     result->setRadiusX(m_radiusX.blend(o->radiusX(), progress, ValueRangeNonNegative));
228     result->setRadiusY(m_radiusY.blend(o->radiusY(), progress, ValueRangeNonNegative));
229     return result.release();
230 }
231
232 bool BasicShapeEllipse::operator==(const BasicShape& o) const
233 {
234     if (!isSameType(o))
235         return false;
236     const BasicShapeEllipse& other = toBasicShapeEllipse(o);
237     return m_centerX == other.m_centerX && m_centerY == other.m_centerY && m_radiusX == other.m_radiusX && m_radiusY == other.m_radiusY;
238 }
239
240 float BasicShapeEllipse::floatValueForRadiusInBox(const BasicShapeRadius& radius, float center, float boxWidthOrHeight) const
241 {
242     if (radius.type() == BasicShapeRadius::Value)
243         return floatValueForLength(radius.value(), boxWidthOrHeight);
244
245     if (radius.type() == BasicShapeRadius::ClosestSide)
246         return std::min(center, boxWidthOrHeight - center);
247
248     ASSERT(radius.type() == BasicShapeRadius::FarthestSide);
249     return std::max(center, boxWidthOrHeight - center);
250 }
251
252 void BasicShapeEllipse::path(Path& path, const FloatRect& boundingBox)
253 {
254     ASSERT(path.isEmpty());
255     FloatPoint center = floatPointForCenterCoordinate(m_centerX, m_centerY, boundingBox.size());
256     float radiusX = floatValueForRadiusInBox(m_radiusX, center.x(), boundingBox.width());
257     float radiusY = floatValueForRadiusInBox(m_radiusY, center.y(), boundingBox.height());
258     path.addEllipse(FloatRect(
259         center.x() - radiusX + boundingBox.x(),
260         center.y() - radiusY + boundingBox.y(),
261         radiusX * 2,
262         radiusY * 2
263     ));
264 }
265
266 PassRefPtr<BasicShape> BasicShapeEllipse::blend(const BasicShape* other, double progress) const
267 {
268     ASSERT(type() == other->type());
269     const BasicShapeEllipse* o = static_cast<const BasicShapeEllipse*>(other);
270     RefPtr<BasicShapeEllipse> result =  BasicShapeEllipse::create();
271
272     if (m_radiusX.type() != BasicShapeRadius::Value || o->radiusX().type() != BasicShapeRadius::Value
273         || m_radiusY.type() != BasicShapeRadius::Value || o->radiusY().type() != BasicShapeRadius::Value) {
274         result->setCenterX(o->centerX());
275         result->setCenterY(o->centerY());
276         result->setRadiusX(o->radiusX());
277         result->setRadiusY(o->radiusY());
278         return result;
279     }
280
281     result->setCenterX(m_centerX.blend(o->centerX(), progress));
282     result->setCenterY(m_centerY.blend(o->centerY(), progress));
283     result->setRadiusX(m_radiusX.blend(o->radiusX(), progress));
284     result->setRadiusY(m_radiusY.blend(o->radiusY(), progress));
285     return result.release();
286 }
287
288 void BasicShapePolygon::path(Path& path, const FloatRect& boundingBox)
289 {
290     ASSERT(path.isEmpty());
291     ASSERT(!(m_values.size() % 2));
292     size_t length = m_values.size();
293
294     if (!length)
295         return;
296
297     path.moveTo(FloatPoint(floatValueForLength(m_values.at(0), boundingBox.width()) + boundingBox.x(),
298         floatValueForLength(m_values.at(1), boundingBox.height()) + boundingBox.y()));
299     for (size_t i = 2; i < length; i = i + 2) {
300         path.addLineTo(FloatPoint(floatValueForLength(m_values.at(i), boundingBox.width()) + boundingBox.x(),
301             floatValueForLength(m_values.at(i + 1), boundingBox.height()) + boundingBox.y()));
302     }
303     path.closeSubpath();
304 }
305
306 PassRefPtr<BasicShape> BasicShapePolygon::blend(const BasicShape* other, double progress) const
307 {
308     ASSERT(other && isSameType(*other));
309
310     const BasicShapePolygon* o = static_cast<const BasicShapePolygon*>(other);
311     ASSERT(m_values.size() == o->values().size());
312     ASSERT(!(m_values.size() % 2));
313
314     size_t length = m_values.size();
315     RefPtr<BasicShapePolygon> result = BasicShapePolygon::create();
316     if (!length)
317         return result.release();
318
319     result->setWindRule(o->windRule());
320
321     for (size_t i = 0; i < length; i = i + 2) {
322         result->appendPoint(m_values.at(i).blend(o->values().at(i), progress, ValueRangeAll),
323             m_values.at(i + 1).blend(o->values().at(i + 1), progress, ValueRangeAll));
324     }
325
326     return result.release();
327 }
328
329 bool BasicShapePolygon::operator==(const BasicShape& o) const
330 {
331     if (!isSameType(o))
332         return false;
333     const BasicShapePolygon& other = toBasicShapePolygon(o);
334     return m_windRule == other.m_windRule && m_values == other.m_values;
335 }
336
337 void BasicShapeInsetRectangle::path(Path& path, const FloatRect& boundingBox)
338 {
339     ASSERT(path.isEmpty());
340     float left = floatValueForLength(m_left, boundingBox.width());
341     float top = floatValueForLength(m_top, boundingBox.height());
342     path.addRoundedRect(
343         FloatRect(
344             left + boundingBox.x(),
345             top + boundingBox.y(),
346             std::max<float>(boundingBox.width() - left - floatValueForLength(m_right, boundingBox.width()), 0),
347             std::max<float>(boundingBox.height() - top - floatValueForLength(m_bottom, boundingBox.height()), 0)
348         ),
349         FloatSize(
350             floatValueForLength(m_cornerRadiusX, boundingBox.width()),
351             floatValueForLength(m_cornerRadiusY, boundingBox.height())
352         )
353     );
354 }
355
356 PassRefPtr<BasicShape> BasicShapeInsetRectangle::blend(const BasicShape* other, double progress) const
357 {
358     ASSERT(other && isSameType(*other));
359
360     const BasicShapeInsetRectangle* o = static_cast<const BasicShapeInsetRectangle*>(other);
361     RefPtr<BasicShapeInsetRectangle> result =  BasicShapeInsetRectangle::create();
362     result->setTop(m_top.blend(o->top(), progress, ValueRangeNonNegative));
363     result->setRight(m_right.blend(o->right(), progress, ValueRangeNonNegative));
364     result->setBottom(m_bottom.blend(o->bottom(), progress, ValueRangeNonNegative));
365     result->setLeft(m_left.blend(o->left(), progress, ValueRangeNonNegative));
366     result->setCornerRadiusX(m_cornerRadiusX.blend(o->cornerRadiusX(), progress, ValueRangeNonNegative));
367     result->setCornerRadiusY(m_cornerRadiusY.blend(o->cornerRadiusY(), progress, ValueRangeNonNegative));
368     return result.release();
369 }
370
371 bool BasicShapeInsetRectangle::operator==(const BasicShape& o) const
372 {
373     if (!isSameType(o))
374         return false;
375     const BasicShapeInsetRectangle& other = toBasicShapeInsetRectangle(o);
376     return m_right == other.m_right && m_top == other.m_top && m_bottom == other.m_bottom && m_left == other.m_left && m_cornerRadiusX == other.m_cornerRadiusX && m_cornerRadiusY == other.m_cornerRadiusY;
377 }
378
379 static FloatSize floatSizeForLengthSize(const LengthSize& lengthSize, const FloatRect& boundingBox)
380 {
381     return FloatSize(floatValueForLength(lengthSize.width(), boundingBox.width()),
382         floatValueForLength(lengthSize.height(), boundingBox.height()));
383 }
384
385 void BasicShapeInset::path(Path& path, const FloatRect& boundingBox)
386 {
387     ASSERT(path.isEmpty());
388     float left = floatValueForLength(m_left, boundingBox.width());
389     float top = floatValueForLength(m_top, boundingBox.height());
390     path.addRoundedRect(
391         FloatRect(
392             left + boundingBox.x(),
393             top + boundingBox.y(),
394             std::max<float>(boundingBox.width() - left - floatValueForLength(m_right, boundingBox.width()), 0),
395             std::max<float>(boundingBox.height() - top - floatValueForLength(m_bottom, boundingBox.height()), 0)
396         ),
397         floatSizeForLengthSize(m_topLeftRadius, boundingBox),
398         floatSizeForLengthSize(m_topRightRadius, boundingBox),
399         floatSizeForLengthSize(m_bottomLeftRadius, boundingBox),
400         floatSizeForLengthSize(m_bottomRightRadius, boundingBox)
401     );
402 }
403
404 PassRefPtr<BasicShape> BasicShapeInset::blend(const BasicShape* other, double) const
405 {
406     ASSERT(type() == other->type());
407     // FIXME: Implement blend for BasicShapeInset.
408     return 0;
409 }
410
411 bool BasicShapeInset::operator==(const BasicShape& o) const
412 {
413     if (!isSameType(o))
414         return false;
415     const BasicShapeInset& other = toBasicShapeInset(o);
416     return m_right == other.m_right
417         && m_top == other.m_top
418         && m_bottom == other.m_bottom
419         && m_left == other.m_left
420         && m_topLeftRadius == other.m_topLeftRadius
421         && m_topRightRadius == other.m_topRightRadius
422         && m_bottomRightRadius == other.m_bottomRightRadius
423         && m_bottomLeftRadius == other.m_bottomLeftRadius;
424 }
425
426 }