Merge "[CherryPick] [WEBGL] Rename WEBKIT_WEBGL_lose_context to WEBGL_lose_context...
[framework/web/webkit-efl.git] / Source / WebCore / css / BasicShapeFunctions.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 "BasicShapeFunctions.h"
32
33 #include "BasicShapes.h"
34 #include "CSSBasicShapes.h"
35 #include "CSSPrimitiveValueMappings.h"
36 #include "CSSValuePool.h"
37 #include "StyleResolver.h"
38
39 namespace WebCore {
40
41 PassRefPtr<CSSValue> valueForBasicShape(const BasicShape* basicShape)
42 {
43     RefPtr<CSSBasicShape> basicShapeValue;
44     switch (basicShape->type()) {
45     case BasicShape::BASIC_SHAPE_RECTANGLE: {
46         const BasicShapeRectangle* rectangle = static_cast<const BasicShapeRectangle*>(basicShape);
47         RefPtr<CSSBasicShapeRectangle> rectangleValue = CSSBasicShapeRectangle::create();
48
49         rectangleValue->setX(cssValuePool().createValue(rectangle->x()));
50         rectangleValue->setY(cssValuePool().createValue(rectangle->y()));
51         rectangleValue->setWidth(cssValuePool().createValue(rectangle->width()));
52         rectangleValue->setHeight(cssValuePool().createValue(rectangle->height()));
53         if (!rectangle->cornerRadiusX().isUndefined()) {
54             rectangleValue->setRadiusX(cssValuePool().createValue(rectangle->cornerRadiusX()));
55             if (!rectangle->cornerRadiusY().isUndefined())
56                 rectangleValue->setRadiusY(cssValuePool().createValue(rectangle->cornerRadiusY()));
57         }
58
59         basicShapeValue = rectangleValue.release();
60         break;
61     }
62     case BasicShape::BASIC_SHAPE_CIRCLE: {
63         const BasicShapeCircle* circle = static_cast<const BasicShapeCircle*>(basicShape);
64         RefPtr<CSSBasicShapeCircle> circleValue = CSSBasicShapeCircle::create();
65
66         circleValue->setCenterX(cssValuePool().createValue(circle->centerX()));
67         circleValue->setCenterY(cssValuePool().createValue(circle->centerY()));
68         circleValue->setRadius(cssValuePool().createValue(circle->radius()));
69
70         basicShapeValue = circleValue.release();
71         break;
72     }
73     case BasicShape::BASIC_SHAPE_ELLIPSE: {
74         const BasicShapeEllipse* ellipse = static_cast<const BasicShapeEllipse*>(basicShape);
75         RefPtr<CSSBasicShapeEllipse> ellipseValue = CSSBasicShapeEllipse::create();
76
77         ellipseValue->setCenterX(cssValuePool().createValue(ellipse->centerX()));
78         ellipseValue->setCenterY(cssValuePool().createValue(ellipse->centerY()));
79         ellipseValue->setRadiusX(cssValuePool().createValue(ellipse->radiusX()));
80         ellipseValue->setRadiusY(cssValuePool().createValue(ellipse->radiusY()));
81
82         basicShapeValue = ellipseValue.release();
83         break;
84     }
85     case BasicShape::BASIC_SHAPE_POLYGON: {
86         const BasicShapePolygon* polygon = static_cast<const BasicShapePolygon*>(basicShape);
87         RefPtr<CSSBasicShapePolygon> polygonValue = CSSBasicShapePolygon::create();
88
89         polygonValue->setWindRule(polygon->windRule());
90         const Vector<Length>& values = polygon->values();
91         for (unsigned i = 0; i < values.size(); i += 2)
92             polygonValue->appendPoint(cssValuePool().createValue(values.at(i)), cssValuePool().createValue(values.at(i + 1)));
93
94         basicShapeValue = polygonValue.release();
95         break;
96     }
97     default:
98         break;
99     }
100     return cssValuePool().createValue<PassRefPtr<CSSBasicShape> >(basicShapeValue.release());
101 }
102
103 static Length convertToLength(const StyleResolver* styleResolver, CSSPrimitiveValue* value)
104 {
105     return value->convertToLength<FixedIntegerConversion | FixedFloatConversion | PercentConversion | ViewportPercentageConversion>(styleResolver->style(), styleResolver->rootElementStyle(), styleResolver->style()->effectiveZoom());
106 }
107
108 PassRefPtr<BasicShape> basicShapeForValue(const StyleResolver* styleResolver, const CSSBasicShape* basicShapeValue)
109 {
110     RefPtr<BasicShape> basicShape;
111
112     switch (basicShapeValue->type()) {
113     case CSSBasicShape::CSS_BASIC_SHAPE_RECTANGLE: {
114         const CSSBasicShapeRectangle* rectValue = static_cast<const CSSBasicShapeRectangle *>(basicShapeValue);
115         RefPtr<BasicShapeRectangle> rect = BasicShapeRectangle::create();
116
117         rect->setX(convertToLength(styleResolver, rectValue->x()));
118         rect->setY(convertToLength(styleResolver, rectValue->y()));
119         rect->setWidth(convertToLength(styleResolver, rectValue->width()));
120         rect->setHeight(convertToLength(styleResolver, rectValue->height()));
121         if (rectValue->radiusX()) {
122             rect->setCornerRadiusX(convertToLength(styleResolver, rectValue->radiusX()));
123             if (rectValue->radiusY())
124                 rect->setCornerRadiusY(convertToLength(styleResolver, rectValue->radiusY()));
125         }
126         basicShape = rect.release();
127         break;
128     }
129     case CSSBasicShape::CSS_BASIC_SHAPE_CIRCLE: {
130         const CSSBasicShapeCircle* circleValue = static_cast<const CSSBasicShapeCircle *>(basicShapeValue);
131         RefPtr<BasicShapeCircle> circle = BasicShapeCircle::create();
132
133         circle->setCenterX(convertToLength(styleResolver, circleValue->centerX()));
134         circle->setCenterY(convertToLength(styleResolver, circleValue->centerY()));
135         circle->setRadius(convertToLength(styleResolver, circleValue->radius()));
136
137         basicShape = circle.release();
138         break;
139     }
140     case CSSBasicShape::CSS_BASIC_SHAPE_ELLIPSE: {
141         const CSSBasicShapeEllipse* ellipseValue = static_cast<const CSSBasicShapeEllipse *>(basicShapeValue);
142         RefPtr<BasicShapeEllipse> ellipse = BasicShapeEllipse::create();
143
144         ellipse->setCenterX(convertToLength(styleResolver, ellipseValue->centerX()));
145         ellipse->setCenterY(convertToLength(styleResolver, ellipseValue->centerY()));
146         ellipse->setRadiusX(convertToLength(styleResolver, ellipseValue->radiusX()));
147         ellipse->setRadiusY(convertToLength(styleResolver, ellipseValue->radiusY()));
148
149         basicShape = ellipse.release();
150         break;
151     }
152     case CSSBasicShape::CSS_BASIC_SHAPE_POLYGON: {
153         const CSSBasicShapePolygon* polygonValue = static_cast<const CSSBasicShapePolygon *>(basicShapeValue);
154         RefPtr<BasicShapePolygon> polygon = BasicShapePolygon::create();
155
156         polygon->setWindRule(polygonValue->windRule());
157         const Vector<RefPtr<CSSPrimitiveValue> >& values = polygonValue->values();
158         for (unsigned i = 0; i < values.size(); i += 2)
159             polygon->appendPoint(convertToLength(styleResolver, values.at(i).get()), convertToLength(styleResolver, values.at(i + 1).get()));
160
161         basicShape = polygon.release();
162         break;
163     }
164     default:
165         break;
166     }
167     return basicShape.release();
168 }
169 }