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 "BasicShapeFunctions.h"
33 #include "BasicShapes.h"
34 #include "CSSBasicShapes.h"
35 #include "CSSPrimitiveValueMappings.h"
36 #include "CSSValuePool.h"
37 #include "StyleResolver.h"
41 PassRefPtr<CSSValue> valueForBasicShape(const BasicShape* basicShape)
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();
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()));
59 basicShapeValue = rectangleValue.release();
62 case BasicShape::BASIC_SHAPE_CIRCLE: {
63 const BasicShapeCircle* circle = static_cast<const BasicShapeCircle*>(basicShape);
64 RefPtr<CSSBasicShapeCircle> circleValue = CSSBasicShapeCircle::create();
66 circleValue->setCenterX(cssValuePool().createValue(circle->centerX()));
67 circleValue->setCenterY(cssValuePool().createValue(circle->centerY()));
68 circleValue->setRadius(cssValuePool().createValue(circle->radius()));
70 basicShapeValue = circleValue.release();
73 case BasicShape::BASIC_SHAPE_ELLIPSE: {
74 const BasicShapeEllipse* ellipse = static_cast<const BasicShapeEllipse*>(basicShape);
75 RefPtr<CSSBasicShapeEllipse> ellipseValue = CSSBasicShapeEllipse::create();
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()));
82 basicShapeValue = ellipseValue.release();
85 case BasicShape::BASIC_SHAPE_POLYGON: {
86 const BasicShapePolygon* polygon = static_cast<const BasicShapePolygon*>(basicShape);
87 RefPtr<CSSBasicShapePolygon> polygonValue = CSSBasicShapePolygon::create();
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)));
94 basicShapeValue = polygonValue.release();
100 return cssValuePool().createValue<PassRefPtr<CSSBasicShape> >(basicShapeValue.release());
103 static Length convertToLength(const StyleResolver* styleResolver, CSSPrimitiveValue* value)
105 return value->convertToLength<FixedIntegerConversion | FixedFloatConversion | PercentConversion | ViewportPercentageConversion>(styleResolver->style(), styleResolver->rootElementStyle(), styleResolver->style()->effectiveZoom());
108 PassRefPtr<BasicShape> basicShapeForValue(const StyleResolver* styleResolver, const CSSBasicShape* basicShapeValue)
110 RefPtr<BasicShape> basicShape;
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();
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()));
126 basicShape = rect.release();
129 case CSSBasicShape::CSS_BASIC_SHAPE_CIRCLE: {
130 const CSSBasicShapeCircle* circleValue = static_cast<const CSSBasicShapeCircle *>(basicShapeValue);
131 RefPtr<BasicShapeCircle> circle = BasicShapeCircle::create();
133 circle->setCenterX(convertToLength(styleResolver, circleValue->centerX()));
134 circle->setCenterY(convertToLength(styleResolver, circleValue->centerY()));
135 circle->setRadius(convertToLength(styleResolver, circleValue->radius()));
137 basicShape = circle.release();
140 case CSSBasicShape::CSS_BASIC_SHAPE_ELLIPSE: {
141 const CSSBasicShapeEllipse* ellipseValue = static_cast<const CSSBasicShapeEllipse *>(basicShapeValue);
142 RefPtr<BasicShapeEllipse> ellipse = BasicShapeEllipse::create();
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()));
149 basicShape = ellipse.release();
152 case CSSBasicShape::CSS_BASIC_SHAPE_POLYGON: {
153 const CSSBasicShapePolygon* polygonValue = static_cast<const CSSBasicShapePolygon *>(basicShapeValue);
154 RefPtr<BasicShapePolygon> polygon = BasicShapePolygon::create();
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()));
161 basicShape = polygon.release();
167 return basicShape.release();