Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / css / BasicShapeFunctions.cpp
blobdc59d0403ba141204693080cde582e8de7accc57
1 /*
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
6 * are met:
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.
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.
30 #include "config.h"
31 #include "core/css/BasicShapeFunctions.h"
33 #include "core/css/CSSBasicShapes.h"
34 #include "core/css/CSSPrimitiveValueMappings.h"
35 #include "core/css/CSSValuePair.h"
36 #include "core/css/CSSValuePool.h"
37 #include "core/css/resolver/StyleResolverState.h"
38 #include "core/style/BasicShapes.h"
39 #include "core/style/ComputedStyle.h"
41 namespace blink {
43 static PassRefPtrWillBeRawPtr<CSSValue> valueForCenterCoordinate(CSSValuePool& pool, const ComputedStyle& style, const BasicShapeCenterCoordinate& center, EBoxOrient orientation)
45 if (center.direction() == BasicShapeCenterCoordinate::TopLeft)
46 return pool.createValue(center.length(), style);
48 CSSValueID keyword = orientation == HORIZONTAL ? CSSValueRight : CSSValueBottom;
50 return CSSValuePair::create(pool.createIdentifierValue(keyword), pool.createValue(center.length(), style), CSSValuePair::DropIdenticalValues);
53 static PassRefPtrWillBeRawPtr<CSSPrimitiveValue> basicShapeRadiusToCSSValue(CSSValuePool& pool, const ComputedStyle& style, const BasicShapeRadius& radius)
55 switch (radius.type()) {
56 case BasicShapeRadius::Value:
57 return pool.createValue(radius.value(), style);
58 case BasicShapeRadius::ClosestSide:
59 return pool.createIdentifierValue(CSSValueClosestSide);
60 case BasicShapeRadius::FarthestSide:
61 return pool.createIdentifierValue(CSSValueFarthestSide);
64 ASSERT_NOT_REACHED();
65 return nullptr;
68 PassRefPtrWillBeRawPtr<CSSValue> valueForBasicShape(const ComputedStyle& style, const BasicShape* basicShape)
70 CSSValuePool& pool = cssValuePool();
72 RefPtrWillBeRawPtr<CSSBasicShape> basicShapeValue = nullptr;
73 switch (basicShape->type()) {
74 case BasicShape::BasicShapeCircleType: {
75 const BasicShapeCircle* circle = toBasicShapeCircle(basicShape);
76 RefPtrWillBeRawPtr<CSSBasicShapeCircle> circleValue = CSSBasicShapeCircle::create();
78 circleValue->setCenterX(valueForCenterCoordinate(pool, style, circle->centerX(), HORIZONTAL));
79 circleValue->setCenterY(valueForCenterCoordinate(pool, style, circle->centerY(), VERTICAL));
80 circleValue->setRadius(basicShapeRadiusToCSSValue(pool, style, circle->radius()));
81 basicShapeValue = circleValue.release();
82 break;
84 case BasicShape::BasicShapeEllipseType: {
85 const BasicShapeEllipse* ellipse = toBasicShapeEllipse(basicShape);
86 RefPtrWillBeRawPtr<CSSBasicShapeEllipse> ellipseValue = CSSBasicShapeEllipse::create();
88 ellipseValue->setCenterX(valueForCenterCoordinate(pool, style, ellipse->centerX(), HORIZONTAL));
89 ellipseValue->setCenterY(valueForCenterCoordinate(pool, style, ellipse->centerY(), VERTICAL));
90 ellipseValue->setRadiusX(basicShapeRadiusToCSSValue(pool, style, ellipse->radiusX()));
91 ellipseValue->setRadiusY(basicShapeRadiusToCSSValue(pool, style, ellipse->radiusY()));
92 basicShapeValue = ellipseValue.release();
93 break;
95 case BasicShape::BasicShapePolygonType: {
96 const BasicShapePolygon* polygon = toBasicShapePolygon(basicShape);
97 RefPtrWillBeRawPtr<CSSBasicShapePolygon> polygonValue = CSSBasicShapePolygon::create();
99 polygonValue->setWindRule(polygon->windRule());
100 const Vector<Length>& values = polygon->values();
101 for (unsigned i = 0; i < values.size(); i += 2)
102 polygonValue->appendPoint(pool.createValue(values.at(i), style), pool.createValue(values.at(i + 1), style));
104 basicShapeValue = polygonValue.release();
105 break;
107 case BasicShape::BasicShapeInsetType: {
108 const BasicShapeInset* inset = toBasicShapeInset(basicShape);
109 RefPtrWillBeRawPtr<CSSBasicShapeInset> insetValue = CSSBasicShapeInset::create();
111 insetValue->setTop(pool.createValue(inset->top(), style));
112 insetValue->setRight(pool.createValue(inset->right(), style));
113 insetValue->setBottom(pool.createValue(inset->bottom(), style));
114 insetValue->setLeft(pool.createValue(inset->left(), style));
116 insetValue->setTopLeftRadius(CSSValuePair::create(inset->topLeftRadius(), style));
117 insetValue->setTopRightRadius(CSSValuePair::create(inset->topRightRadius(), style));
118 insetValue->setBottomRightRadius(CSSValuePair::create(inset->bottomRightRadius(), style));
119 insetValue->setBottomLeftRadius(CSSValuePair::create(inset->bottomLeftRadius(), style));
121 basicShapeValue = insetValue.release();
122 break;
124 default:
125 break;
128 return pool.createValue(basicShapeValue.release());
131 static Length convertToLength(const StyleResolverState& state, CSSPrimitiveValue* value)
133 if (!value)
134 return Length(0, Fixed);
135 return value->convertToLength(state.cssToLengthConversionData());
138 static LengthSize convertToLengthSize(const StyleResolverState& state, CSSValuePair* value)
140 if (!value)
141 return LengthSize(Length(0, Fixed), Length(0, Fixed));
143 return LengthSize(convertToLength(state, &toCSSPrimitiveValue(value->first())), convertToLength(state, &toCSSPrimitiveValue(value->second())));
146 static BasicShapeCenterCoordinate convertToCenterCoordinate(const StyleResolverState& state, CSSValue* value)
148 BasicShapeCenterCoordinate::Direction direction;
149 Length offset = Length(0, Fixed);
151 CSSValueID keyword = CSSValueTop;
152 if (!value) {
153 keyword = CSSValueCenter;
154 } else if (value->isPrimitiveValue() && toCSSPrimitiveValue(value)->isValueID()) {
155 keyword = toCSSPrimitiveValue(value)->getValueID();
156 } else if (value->isValuePair()) {
157 keyword = toCSSPrimitiveValue(toCSSValuePair(value)->first()).getValueID();
158 offset = convertToLength(state, &toCSSPrimitiveValue(toCSSValuePair(value)->second()));
159 } else {
160 offset = convertToLength(state, toCSSPrimitiveValue(value));
163 switch (keyword) {
164 case CSSValueTop:
165 case CSSValueLeft:
166 direction = BasicShapeCenterCoordinate::TopLeft;
167 break;
168 case CSSValueRight:
169 case CSSValueBottom:
170 direction = BasicShapeCenterCoordinate::BottomRight;
171 break;
172 case CSSValueCenter:
173 direction = BasicShapeCenterCoordinate::TopLeft;
174 offset = Length(50, Percent);
175 break;
176 default:
177 ASSERT_NOT_REACHED();
178 direction = BasicShapeCenterCoordinate::TopLeft;
179 break;
182 return BasicShapeCenterCoordinate(direction, offset);
185 static BasicShapeRadius cssValueToBasicShapeRadius(const StyleResolverState& state, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> radius)
187 if (!radius)
188 return BasicShapeRadius(BasicShapeRadius::ClosestSide);
190 if (radius->isValueID()) {
191 switch (radius->getValueID()) {
192 case CSSValueClosestSide:
193 return BasicShapeRadius(BasicShapeRadius::ClosestSide);
194 case CSSValueFarthestSide:
195 return BasicShapeRadius(BasicShapeRadius::FarthestSide);
196 default:
197 ASSERT_NOT_REACHED();
198 break;
202 return BasicShapeRadius(convertToLength(state, radius.get()));
205 PassRefPtr<BasicShape> basicShapeForValue(const StyleResolverState& state, const CSSBasicShape* basicShapeValue)
207 RefPtr<BasicShape> basicShape;
209 switch (basicShapeValue->type()) {
210 case CSSBasicShape::CSSBasicShapeCircleType: {
211 const CSSBasicShapeCircle* circleValue = toCSSBasicShapeCircle(basicShapeValue);
212 RefPtr<BasicShapeCircle> circle = BasicShapeCircle::create();
214 circle->setCenterX(convertToCenterCoordinate(state, circleValue->centerX()));
215 circle->setCenterY(convertToCenterCoordinate(state, circleValue->centerY()));
216 circle->setRadius(cssValueToBasicShapeRadius(state, circleValue->radius()));
218 basicShape = circle.release();
219 break;
221 case CSSBasicShape::CSSBasicShapeEllipseType: {
222 const CSSBasicShapeEllipse* ellipseValue = toCSSBasicShapeEllipse(basicShapeValue);
223 RefPtr<BasicShapeEllipse> ellipse = BasicShapeEllipse::create();
225 ellipse->setCenterX(convertToCenterCoordinate(state, ellipseValue->centerX()));
226 ellipse->setCenterY(convertToCenterCoordinate(state, ellipseValue->centerY()));
227 ellipse->setRadiusX(cssValueToBasicShapeRadius(state, ellipseValue->radiusX()));
228 ellipse->setRadiusY(cssValueToBasicShapeRadius(state, ellipseValue->radiusY()));
230 basicShape = ellipse.release();
231 break;
233 case CSSBasicShape::CSSBasicShapePolygonType: {
234 const CSSBasicShapePolygon* polygonValue = toCSSBasicShapePolygon(basicShapeValue);
235 RefPtr<BasicShapePolygon> polygon = BasicShapePolygon::create();
237 polygon->setWindRule(polygonValue->windRule());
238 const WillBeHeapVector<RefPtrWillBeMember<CSSPrimitiveValue>>& values = polygonValue->values();
239 for (unsigned i = 0; i < values.size(); i += 2)
240 polygon->appendPoint(convertToLength(state, values.at(i).get()), convertToLength(state, values.at(i + 1).get()));
242 basicShape = polygon.release();
243 break;
245 case CSSBasicShape::CSSBasicShapeInsetType: {
246 const CSSBasicShapeInset* rectValue = toCSSBasicShapeInset(basicShapeValue);
247 RefPtr<BasicShapeInset> rect = BasicShapeInset::create();
249 rect->setTop(convertToLength(state, rectValue->top()));
250 rect->setRight(convertToLength(state, rectValue->right()));
251 rect->setBottom(convertToLength(state, rectValue->bottom()));
252 rect->setLeft(convertToLength(state, rectValue->left()));
254 rect->setTopLeftRadius(convertToLengthSize(state, rectValue->topLeftRadius()));
255 rect->setTopRightRadius(convertToLengthSize(state, rectValue->topRightRadius()));
256 rect->setBottomRightRadius(convertToLengthSize(state, rectValue->bottomRightRadius()));
257 rect->setBottomLeftRadius(convertToLengthSize(state, rectValue->bottomLeftRadius()));
259 basicShape = rect.release();
260 break;
262 default:
263 break;
266 return basicShape.release();
269 FloatPoint floatPointForCenterCoordinate(const BasicShapeCenterCoordinate& centerX, const BasicShapeCenterCoordinate& centerY, FloatSize boxSize)
271 float x = floatValueForLength(centerX.computedLength(), boxSize.width());
272 float y = floatValueForLength(centerY.computedLength(), boxSize.height());
273 return FloatPoint(x, y);