- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / SVGCSSComputedStyleDeclaration.cpp
1 /*
2     Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3     Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public License
16     along with this library; see the file COPYING.LIB.  If not, write to
17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #include "core/css/CSSComputedStyleDeclaration.h"
24
25 #include "CSSPropertyNames.h"
26 #include "core/css/CSSPrimitiveValueMappings.h"
27 #include "core/dom/Document.h"
28 #include "core/rendering/style/RenderStyle.h"
29 #include "core/svg/SVGPaint.h"
30
31 namespace WebCore {
32
33 static PassRefPtr<CSSPrimitiveValue> glyphOrientationToCSSPrimitiveValue(EGlyphOrientation orientation)
34 {
35     switch (orientation) {
36         case GO_0DEG:
37             return CSSPrimitiveValue::create(0.0f, CSSPrimitiveValue::CSS_DEG);
38         case GO_90DEG:
39             return CSSPrimitiveValue::create(90.0f, CSSPrimitiveValue::CSS_DEG);
40         case GO_180DEG:
41             return CSSPrimitiveValue::create(180.0f, CSSPrimitiveValue::CSS_DEG);
42         case GO_270DEG:
43             return CSSPrimitiveValue::create(270.0f, CSSPrimitiveValue::CSS_DEG);
44         default:
45             return 0;
46     }
47 }
48
49 static PassRefPtr<CSSValue> strokeDashArrayToCSSValueList(const Vector<SVGLength>& dashes)
50 {
51     if (dashes.isEmpty())
52         return CSSPrimitiveValue::createIdentifier(CSSValueNone);
53
54     RefPtr<CSSValueList> list = CSSValueList::createCommaSeparated();
55     const Vector<SVGLength>::const_iterator end = dashes.end();
56     for (Vector<SVGLength>::const_iterator it = dashes.begin(); it != end; ++it)
57         list->append(SVGLength::toCSSPrimitiveValue(*it));
58
59     return list.release();
60 }
61
62 static PassRefPtr<CSSValue> paintOrderToCSSValueList(EPaintOrder paintorder)
63 {
64     RefPtr<CSSValueList> list = CSSValueList::createSpaceSeparated();
65     do {
66         EPaintOrderType paintOrderType = (EPaintOrderType)(paintorder & ((1 << kPaintOrderBitwidth) - 1));
67         switch (paintOrderType) {
68         case PT_FILL:
69         case PT_STROKE:
70         case PT_MARKERS:
71             list->append(CSSPrimitiveValue::create(paintOrderType));
72             break;
73         case PT_NONE:
74         default:
75             ASSERT_NOT_REACHED();
76             break;
77         }
78     } while (paintorder >>= kPaintOrderBitwidth);
79
80     return list.release();
81 }
82
83 PassRefPtr<SVGPaint> CSSComputedStyleDeclaration::adjustSVGPaintForCurrentColor(PassRefPtr<SVGPaint> newPaint, RenderStyle* style) const
84 {
85     RefPtr<SVGPaint> paint = newPaint;
86     if (paint->paintType() == SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR || paint->paintType() == SVGPaint::SVG_PAINTTYPE_URI_CURRENTCOLOR)
87         paint->setColor(style->color());
88     return paint.release();
89 }
90
91 PassRefPtr<CSSValue> CSSComputedStyleDeclaration::getSVGPropertyCSSValue(CSSPropertyID propertyID, EUpdateLayout updateLayout) const
92 {
93     Node* node = m_node.get();
94     if (!node)
95         return 0;
96
97     // Make sure our layout is up to date before we allow a query on these attributes.
98     if (updateLayout)
99         node->document().updateLayout();
100
101     RenderStyle* style = node->computedStyle();
102     if (!style)
103         return 0;
104
105     const SVGRenderStyle* svgStyle = style->svgStyle();
106     if (!svgStyle)
107         return 0;
108
109     switch (propertyID) {
110         case CSSPropertyClipRule:
111             return CSSPrimitiveValue::create(svgStyle->clipRule());
112         case CSSPropertyFloodOpacity:
113             return CSSPrimitiveValue::create(svgStyle->floodOpacity(), CSSPrimitiveValue::CSS_NUMBER);
114         case CSSPropertyStopOpacity:
115             return CSSPrimitiveValue::create(svgStyle->stopOpacity(), CSSPrimitiveValue::CSS_NUMBER);
116         case CSSPropertyColorInterpolation:
117             return CSSPrimitiveValue::create(svgStyle->colorInterpolation());
118         case CSSPropertyColorInterpolationFilters:
119             return CSSPrimitiveValue::create(svgStyle->colorInterpolationFilters());
120         case CSSPropertyFillOpacity:
121             return CSSPrimitiveValue::create(svgStyle->fillOpacity(), CSSPrimitiveValue::CSS_NUMBER);
122         case CSSPropertyFillRule:
123             return CSSPrimitiveValue::create(svgStyle->fillRule());
124         case CSSPropertyColorRendering:
125             return CSSPrimitiveValue::create(svgStyle->colorRendering());
126         case CSSPropertyShapeRendering:
127             return CSSPrimitiveValue::create(svgStyle->shapeRendering());
128         case CSSPropertyStrokeLinecap:
129             return CSSPrimitiveValue::create(svgStyle->capStyle());
130         case CSSPropertyStrokeLinejoin:
131             return CSSPrimitiveValue::create(svgStyle->joinStyle());
132         case CSSPropertyStrokeMiterlimit:
133             return CSSPrimitiveValue::create(svgStyle->strokeMiterLimit(), CSSPrimitiveValue::CSS_NUMBER);
134         case CSSPropertyStrokeOpacity:
135             return CSSPrimitiveValue::create(svgStyle->strokeOpacity(), CSSPrimitiveValue::CSS_NUMBER);
136         case CSSPropertyAlignmentBaseline:
137             return CSSPrimitiveValue::create(svgStyle->alignmentBaseline());
138         case CSSPropertyDominantBaseline:
139             return CSSPrimitiveValue::create(svgStyle->dominantBaseline());
140         case CSSPropertyTextAnchor:
141             return CSSPrimitiveValue::create(svgStyle->textAnchor());
142         case CSSPropertyWritingMode:
143             return CSSPrimitiveValue::create(svgStyle->writingMode());
144         case CSSPropertyClipPath:
145             if (!svgStyle->clipperResource().isEmpty())
146                 return CSSPrimitiveValue::create(svgStyle->clipperResource(), CSSPrimitiveValue::CSS_URI);
147             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
148         case CSSPropertyMask:
149             if (!svgStyle->maskerResource().isEmpty())
150                 return CSSPrimitiveValue::create(svgStyle->maskerResource(), CSSPrimitiveValue::CSS_URI);
151             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
152         case CSSPropertyFilter:
153             if (!svgStyle->filterResource().isEmpty())
154                 return CSSPrimitiveValue::create(svgStyle->filterResource(), CSSPrimitiveValue::CSS_URI);
155             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
156         case CSSPropertyFloodColor:
157             return currentColorOrValidColor(style, svgStyle->floodColor());
158         case CSSPropertyLightingColor:
159             return currentColorOrValidColor(style, svgStyle->lightingColor());
160         case CSSPropertyStopColor:
161             return currentColorOrValidColor(style, svgStyle->stopColor());
162         case CSSPropertyFill:
163             return adjustSVGPaintForCurrentColor(SVGPaint::create(svgStyle->fillPaintType(), svgStyle->fillPaintUri(), svgStyle->fillPaintColor()), style);
164         case CSSPropertyKerning:
165             return SVGLength::toCSSPrimitiveValue(svgStyle->kerning());
166         case CSSPropertyMarkerEnd:
167             if (!svgStyle->markerEndResource().isEmpty())
168                 return CSSPrimitiveValue::create(svgStyle->markerEndResource(), CSSPrimitiveValue::CSS_URI);
169             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
170         case CSSPropertyMarkerMid:
171             if (!svgStyle->markerMidResource().isEmpty())
172                 return CSSPrimitiveValue::create(svgStyle->markerMidResource(), CSSPrimitiveValue::CSS_URI);
173             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
174         case CSSPropertyMarkerStart:
175             if (!svgStyle->markerStartResource().isEmpty())
176                 return CSSPrimitiveValue::create(svgStyle->markerStartResource(), CSSPrimitiveValue::CSS_URI);
177             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
178         case CSSPropertyStroke:
179             return adjustSVGPaintForCurrentColor(SVGPaint::create(svgStyle->strokePaintType(), svgStyle->strokePaintUri(), svgStyle->strokePaintColor()), style);
180         case CSSPropertyStrokeDasharray:
181             return strokeDashArrayToCSSValueList(svgStyle->strokeDashArray());
182         case CSSPropertyStrokeDashoffset:
183             return SVGLength::toCSSPrimitiveValue(svgStyle->strokeDashOffset());
184         case CSSPropertyStrokeWidth:
185             return SVGLength::toCSSPrimitiveValue(svgStyle->strokeWidth());
186         case CSSPropertyBaselineShift: {
187             switch (svgStyle->baselineShift()) {
188                 case BS_BASELINE:
189                     return CSSPrimitiveValue::createIdentifier(CSSValueBaseline);
190                 case BS_SUPER:
191                     return CSSPrimitiveValue::createIdentifier(CSSValueSuper);
192                 case BS_SUB:
193                     return CSSPrimitiveValue::createIdentifier(CSSValueSub);
194                 case BS_LENGTH:
195                     return SVGLength::toCSSPrimitiveValue(svgStyle->baselineShiftValue());
196             }
197             ASSERT_NOT_REACHED();
198             return 0;
199         }
200         case CSSPropertyBufferedRendering:
201             return CSSPrimitiveValue::create(svgStyle->bufferedRendering());
202         case CSSPropertyGlyphOrientationHorizontal:
203             return glyphOrientationToCSSPrimitiveValue(svgStyle->glyphOrientationHorizontal());
204         case CSSPropertyGlyphOrientationVertical: {
205             if (RefPtr<CSSPrimitiveValue> value = glyphOrientationToCSSPrimitiveValue(svgStyle->glyphOrientationVertical()))
206                 return value.release();
207
208             if (svgStyle->glyphOrientationVertical() == GO_AUTO)
209                 return CSSPrimitiveValue::createIdentifier(CSSValueAuto);
210
211             return 0;
212         }
213         case CSSPropertyPaintOrder:
214             return paintOrderToCSSValueList(svgStyle->paintOrder());
215         case CSSPropertyVectorEffect:
216             return CSSPrimitiveValue::create(svgStyle->vectorEffect());
217         case CSSPropertyMaskType:
218             return CSSPrimitiveValue::create(svgStyle->maskType());
219         case CSSPropertyMarker:
220         case CSSPropertyEnableBackground:
221         case CSSPropertyColorProfile:
222             // the above properties are not yet implemented in the engine
223             break;
224     default:
225         // If you crash here, it's because you added a css property and are not handling it
226         // in either this switch statement or the one in CSSComputedStyleDelcaration::getPropertyCSSValue
227         ASSERT_WITH_MESSAGE(0, "unimplemented propertyID: %d", propertyID);
228     }
229     LOG_ERROR("unimplemented propertyID: %d", propertyID);
230     return 0;
231 }
232
233 }