2 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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
31 #include "core/rendering/style/BasicShapes.h"
33 #include "core/css/BasicShapeFunctions.h"
34 #include "platform/LengthFunctions.h"
35 #include "platform/geometry/FloatRect.h"
36 #include "platform/graphics/Path.h"
40 bool BasicShape::canBlend(const BasicShape* other) const
42 // FIXME: Support animations between different shapes in the future.
43 if (!other || !isSameType(*other))
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()))
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()))
62 // Ellipses with keywords for radii or center coordinates cannot be animated.
63 if (type() != BasicShape::BasicShapeEllipseType)
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()));
74 void BasicShapeRectangle::path(Path& path, const FloatRect& boundingBox)
76 ASSERT(path.isEmpty());
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())
85 floatValueForLength(m_cornerRadiusX, boundingBox.width()),
86 floatValueForLength(m_cornerRadiusY, boundingBox.height())
91 PassRefPtr<BasicShape> BasicShapeRectangle::blend(const BasicShape* other, double progress) const
93 ASSERT(other && isSameType(*other));
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();
106 bool BasicShapeRectangle::operator==(const BasicShape& o) const
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;
114 bool DeprecatedBasicShapeCircle::operator==(const BasicShape& o) const
118 const DeprecatedBasicShapeCircle& other = toDeprecatedBasicShapeCircle(o);
119 return m_centerX == other.m_centerX && m_centerY == other.m_centerY && m_radius == other.m_radius;
122 void DeprecatedBasicShapeCircle::path(Path& path, const FloatRect& boundingBox)
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(),
137 PassRefPtr<BasicShape> DeprecatedBasicShapeCircle::blend(const BasicShape* other, double progress) const
139 ASSERT(other && isSameType(*other));
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();
149 bool BasicShapeCircle::operator==(const BasicShape& o) const
153 const BasicShapeCircle& other = toBasicShapeCircle(o);
154 return m_centerX == other.m_centerX && m_centerY == other.m_centerY && m_radius == other.m_radius;
157 float BasicShapeCircle::floatValueForRadiusInBox(FloatSize boxSize) const
159 if (m_radius.type() == BasicShapeRadius::Value)
160 return floatValueForLength(m_radius.value(), hypotf(boxSize.width(), boxSize.height()) / sqrtf(2));
162 FloatPoint center = floatPointForCenterCoordinate(m_centerX, m_centerY, boxSize);
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()));
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()));
171 void BasicShapeCircle::path(Path& path, const FloatRect& boundingBox)
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(),
184 PassRefPtr<BasicShape> BasicShapeCircle::blend(const BasicShape* other, double progress) const
186 ASSERT(type() == other->type());
187 const BasicShapeCircle* o = static_cast<const BasicShapeCircle*>(other);
188 RefPtr<BasicShapeCircle> result = BasicShapeCircle::create();
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();
196 void DeprecatedBasicShapeEllipse::path(Path& path, const FloatRect& boundingBox)
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(),
211 bool DeprecatedBasicShapeEllipse::operator==(const BasicShape& o) const
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;
219 PassRefPtr<BasicShape> DeprecatedBasicShapeEllipse::blend(const BasicShape* other, double progress) const
221 ASSERT(other && isSameType(*other));
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();
232 bool BasicShapeEllipse::operator==(const BasicShape& o) const
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;
240 float BasicShapeEllipse::floatValueForRadiusInBox(const BasicShapeRadius& radius, float center, float boxWidthOrHeight) const
242 if (radius.type() == BasicShapeRadius::Value)
243 return floatValueForLength(radius.value(), boxWidthOrHeight);
245 if (radius.type() == BasicShapeRadius::ClosestSide)
246 return std::min(center, boxWidthOrHeight - center);
248 ASSERT(radius.type() == BasicShapeRadius::FarthestSide);
249 return std::max(center, boxWidthOrHeight - center);
252 void BasicShapeEllipse::path(Path& path, const FloatRect& boundingBox)
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(),
266 PassRefPtr<BasicShape> BasicShapeEllipse::blend(const BasicShape* other, double progress) const
268 ASSERT(type() == other->type());
269 const BasicShapeEllipse* o = static_cast<const BasicShapeEllipse*>(other);
270 RefPtr<BasicShapeEllipse> result = BasicShapeEllipse::create();
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());
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();
288 void BasicShapePolygon::path(Path& path, const FloatRect& boundingBox)
290 ASSERT(path.isEmpty());
291 ASSERT(!(m_values.size() % 2));
292 size_t length = m_values.size();
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()));
306 PassRefPtr<BasicShape> BasicShapePolygon::blend(const BasicShape* other, double progress) const
308 ASSERT(other && isSameType(*other));
310 const BasicShapePolygon* o = static_cast<const BasicShapePolygon*>(other);
311 ASSERT(m_values.size() == o->values().size());
312 ASSERT(!(m_values.size() % 2));
314 size_t length = m_values.size();
315 RefPtr<BasicShapePolygon> result = BasicShapePolygon::create();
317 return result.release();
319 result->setWindRule(o->windRule());
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));
326 return result.release();
329 bool BasicShapePolygon::operator==(const BasicShape& o) const
333 const BasicShapePolygon& other = toBasicShapePolygon(o);
334 return m_windRule == other.m_windRule && m_values == other.m_values;
337 void BasicShapeInsetRectangle::path(Path& path, const FloatRect& boundingBox)
339 ASSERT(path.isEmpty());
340 float left = floatValueForLength(m_left, boundingBox.width());
341 float top = floatValueForLength(m_top, boundingBox.height());
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)
350 floatValueForLength(m_cornerRadiusX, boundingBox.width()),
351 floatValueForLength(m_cornerRadiusY, boundingBox.height())
356 PassRefPtr<BasicShape> BasicShapeInsetRectangle::blend(const BasicShape* other, double progress) const
358 ASSERT(other && isSameType(*other));
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();
371 bool BasicShapeInsetRectangle::operator==(const BasicShape& o) const
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;
379 static FloatSize floatSizeForLengthSize(const LengthSize& lengthSize, const FloatRect& boundingBox)
381 return FloatSize(floatValueForLength(lengthSize.width(), boundingBox.width()),
382 floatValueForLength(lengthSize.height(), boundingBox.height()));
385 void BasicShapeInset::path(Path& path, const FloatRect& boundingBox)
387 ASSERT(path.isEmpty());
388 float left = floatValueForLength(m_left, boundingBox.width());
389 float top = floatValueForLength(m_top, boundingBox.height());
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)
397 floatSizeForLengthSize(m_topLeftRadius, boundingBox),
398 floatSizeForLengthSize(m_topRightRadius, boundingBox),
399 floatSizeForLengthSize(m_bottomLeftRadius, boundingBox),
400 floatSizeForLengthSize(m_bottomRightRadius, boundingBox)
404 PassRefPtr<BasicShape> BasicShapeInset::blend(const BasicShape* other, double) const
406 ASSERT(type() == other->type());
407 // FIXME: Implement blend for BasicShapeInset.
411 bool BasicShapeInset::operator==(const BasicShape& o) const
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;