Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / 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 #include "CSSComputedStyleDeclaration.h"
23
24 #include "CSSPrimitiveValueMappings.h"
25 #include "CSSPropertyNames.h"
26 #include "Document.h"
27 #include "RenderStyle.h"
28 #include "SVGPaint.h"
29
30 namespace WebCore {
31
32 static Ref<CSSValue> paintOrder(PaintOrder paintOrder)
33 {
34     Ref<CSSValueList> paintOrderList = CSSValueList::createSpaceSeparated();
35     Ref<CSSValue> fill = CSSPrimitiveValue::createIdentifier(CSSValueFill);
36     Ref<CSSValue> stroke = CSSPrimitiveValue::createIdentifier(CSSValueStroke);
37     Ref<CSSValue> markers = CSSPrimitiveValue::createIdentifier(CSSValueMarkers);
38
39     switch (paintOrder) {
40     case PaintOrderNormal:
41         return CSSPrimitiveValue::createIdentifier(CSSValueNormal);
42     case PaintOrderFill:
43         paintOrderList->append(WTFMove(fill));
44         break;
45     case PaintOrderFillMarkers:
46         paintOrderList->append(WTFMove(fill));
47         paintOrderList->append(WTFMove(markers));
48         break;
49     case PaintOrderStroke:
50         paintOrderList->append(WTFMove(stroke));
51         break;
52     case PaintOrderStrokeMarkers:
53         paintOrderList->append(WTFMove(stroke));
54         paintOrderList->append(WTFMove(markers));
55         break;
56     case PaintOrderMarkers:
57         paintOrderList->append(WTFMove(markers));
58         break;
59     case PaintOrderMarkersStroke:
60         paintOrderList->append(WTFMove(markers));
61         paintOrderList->append(WTFMove(stroke));
62         break;
63     }
64     return WTFMove(paintOrderList);
65 }
66
67 static RefPtr<CSSPrimitiveValue> glyphOrientationToCSSPrimitiveValue(EGlyphOrientation orientation)
68 {
69     switch (orientation) {
70         case GO_0DEG:
71             return CSSPrimitiveValue::create(0.0f, CSSPrimitiveValue::CSS_DEG);
72         case GO_90DEG:
73             return CSSPrimitiveValue::create(90.0f, CSSPrimitiveValue::CSS_DEG);
74         case GO_180DEG:
75             return CSSPrimitiveValue::create(180.0f, CSSPrimitiveValue::CSS_DEG);
76         case GO_270DEG:
77             return CSSPrimitiveValue::create(270.0f, CSSPrimitiveValue::CSS_DEG);
78         default:
79             return nullptr;
80     }
81 }
82
83 static RefPtr<CSSValue> strokeDashArrayToCSSValueList(const Vector<SVGLength>& dashes)
84 {
85     if (dashes.isEmpty())
86         return CSSPrimitiveValue::createIdentifier(CSSValueNone);
87
88     RefPtr<CSSValueList> list = CSSValueList::createCommaSeparated();
89     const Vector<SVGLength>::const_iterator end = dashes.end();
90     for (Vector<SVGLength>::const_iterator it = dashes.begin(); it != end; ++it)
91         list->append(SVGLength::toCSSPrimitiveValue(*it));
92
93     return list;
94 }
95
96 RefPtr<SVGPaint> ComputedStyleExtractor::adjustSVGPaintForCurrentColor(PassRefPtr<SVGPaint> newPaint, RenderStyle* style) const
97 {
98     RefPtr<SVGPaint> paint = newPaint;
99     if (paint->paintType() == SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR || paint->paintType() == SVGPaint::SVG_PAINTTYPE_URI_CURRENTCOLOR)
100         paint->setColor(style->color());
101     return paint;
102 }
103
104 RefPtr<CSSValue> ComputedStyleExtractor::svgPropertyValue(CSSPropertyID propertyID, EUpdateLayout updateLayout) const
105 {
106     Node* node = m_node.get();
107     if (!node)
108         return nullptr;
109
110     // Make sure our layout is up to date before we allow a query on these attributes.
111     if (updateLayout)
112         node->document().updateLayout();
113
114     RenderStyle* style = node->computedStyle();
115     if (!style)
116         return nullptr;
117
118     const SVGRenderStyle& svgStyle = style->svgStyle();
119
120     switch (propertyID) {
121         case CSSPropertyClipRule:
122             return CSSPrimitiveValue::create(svgStyle.clipRule());
123         case CSSPropertyFloodOpacity:
124             return CSSPrimitiveValue::create(svgStyle.floodOpacity(), CSSPrimitiveValue::CSS_NUMBER);
125         case CSSPropertyStopOpacity:
126             return CSSPrimitiveValue::create(svgStyle.stopOpacity(), CSSPrimitiveValue::CSS_NUMBER);
127         case CSSPropertyColorInterpolation:
128             return CSSPrimitiveValue::create(svgStyle.colorInterpolation());
129         case CSSPropertyColorInterpolationFilters:
130             return CSSPrimitiveValue::create(svgStyle.colorInterpolationFilters());
131         case CSSPropertyFillOpacity:
132             return CSSPrimitiveValue::create(svgStyle.fillOpacity(), CSSPrimitiveValue::CSS_NUMBER);
133         case CSSPropertyFillRule:
134             return CSSPrimitiveValue::create(svgStyle.fillRule());
135         case CSSPropertyColorRendering:
136             return CSSPrimitiveValue::create(svgStyle.colorRendering());
137         case CSSPropertyShapeRendering:
138             return CSSPrimitiveValue::create(svgStyle.shapeRendering());
139         case CSSPropertyStrokeLinecap:
140             return CSSPrimitiveValue::create(svgStyle.capStyle());
141         case CSSPropertyStrokeLinejoin:
142             return CSSPrimitiveValue::create(svgStyle.joinStyle());
143         case CSSPropertyStrokeMiterlimit:
144             return CSSPrimitiveValue::create(svgStyle.strokeMiterLimit(), CSSPrimitiveValue::CSS_NUMBER);
145         case CSSPropertyStrokeOpacity:
146             return CSSPrimitiveValue::create(svgStyle.strokeOpacity(), CSSPrimitiveValue::CSS_NUMBER);
147         case CSSPropertyAlignmentBaseline:
148             return CSSPrimitiveValue::create(svgStyle.alignmentBaseline());
149         case CSSPropertyDominantBaseline:
150             return CSSPrimitiveValue::create(svgStyle.dominantBaseline());
151         case CSSPropertyTextAnchor:
152             return CSSPrimitiveValue::create(svgStyle.textAnchor());
153         case CSSPropertyWritingMode:
154             return CSSPrimitiveValue::create(svgStyle.writingMode());
155         case CSSPropertyClipPath:
156             if (!svgStyle.clipperResource().isEmpty())
157                 return CSSPrimitiveValue::create(svgStyle.clipperResource(), CSSPrimitiveValue::CSS_URI);
158             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
159         case CSSPropertyMask:
160             if (!svgStyle.maskerResource().isEmpty())
161                 return CSSPrimitiveValue::create(svgStyle.maskerResource(), CSSPrimitiveValue::CSS_URI);
162             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
163         case CSSPropertyFloodColor:
164             return currentColorOrValidColor(style, svgStyle.floodColor());
165         case CSSPropertyLightingColor:
166             return currentColorOrValidColor(style, svgStyle.lightingColor());
167         case CSSPropertyStopColor:
168             return currentColorOrValidColor(style, svgStyle.stopColor());
169         case CSSPropertyFill:
170             return adjustSVGPaintForCurrentColor(SVGPaint::create(svgStyle.fillPaintType(), svgStyle.fillPaintUri(), svgStyle.fillPaintColor()), style);
171         case CSSPropertyKerning:
172             return SVGLength::toCSSPrimitiveValue(svgStyle.kerning());
173         case CSSPropertyMarkerEnd:
174             if (!svgStyle.markerEndResource().isEmpty())
175                 return CSSPrimitiveValue::create(svgStyle.markerEndResource(), CSSPrimitiveValue::CSS_URI);
176             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
177         case CSSPropertyMarkerMid:
178             if (!svgStyle.markerMidResource().isEmpty())
179                 return CSSPrimitiveValue::create(svgStyle.markerMidResource(), CSSPrimitiveValue::CSS_URI);
180             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
181         case CSSPropertyMarkerStart:
182             if (!svgStyle.markerStartResource().isEmpty())
183                 return CSSPrimitiveValue::create(svgStyle.markerStartResource(), CSSPrimitiveValue::CSS_URI);
184             return CSSPrimitiveValue::createIdentifier(CSSValueNone);
185         case CSSPropertyStroke:
186             return adjustSVGPaintForCurrentColor(SVGPaint::create(svgStyle.strokePaintType(), svgStyle.strokePaintUri(), svgStyle.strokePaintColor()), style);
187         case CSSPropertyStrokeDasharray:
188             return strokeDashArrayToCSSValueList(svgStyle.strokeDashArray());
189         case CSSPropertyBaselineShift: {
190             switch (svgStyle.baselineShift()) {
191                 case BS_BASELINE:
192                     return CSSPrimitiveValue::createIdentifier(CSSValueBaseline);
193                 case BS_SUPER:
194                     return CSSPrimitiveValue::createIdentifier(CSSValueSuper);
195                 case BS_SUB:
196                     return CSSPrimitiveValue::createIdentifier(CSSValueSub);
197                 case BS_LENGTH:
198                     return SVGLength::toCSSPrimitiveValue(svgStyle.baselineShiftValue());
199             }
200             ASSERT_NOT_REACHED();
201             return nullptr;
202         }
203         case CSSPropertyBufferedRendering:
204             return CSSPrimitiveValue::create(svgStyle.bufferedRendering());
205         case CSSPropertyGlyphOrientationHorizontal:
206             return glyphOrientationToCSSPrimitiveValue(svgStyle.glyphOrientationHorizontal());
207         case CSSPropertyGlyphOrientationVertical: {
208             if (RefPtr<CSSPrimitiveValue> value = glyphOrientationToCSSPrimitiveValue(svgStyle.glyphOrientationVertical()))
209                 return value.release();
210
211             if (svgStyle.glyphOrientationVertical() == GO_AUTO)
212                 return CSSPrimitiveValue::createIdentifier(CSSValueAuto);
213
214             return nullptr;
215         }
216         case CSSPropertyWebkitSvgShadow:
217             return valueForShadow(svgStyle.shadow(), propertyID, *style);
218         case CSSPropertyVectorEffect:
219             return CSSPrimitiveValue::create(svgStyle.vectorEffect());
220         case CSSPropertyMaskType:
221             return CSSPrimitiveValue::create(svgStyle.maskType());
222         case CSSPropertyPaintOrder:
223             return paintOrder(svgStyle.paintOrder());
224         case CSSPropertyMarker:
225         case CSSPropertyEnableBackground:
226         case CSSPropertyColorProfile:
227             // the above properties are not yet implemented in the engine
228             break;
229     default:
230         // If you crash here, it's because you added a css property and are not handling it
231         // in either this switch statement or the one in CSSComputedStyleDelcaration::getPropertyCSSValue
232         ASSERT_WITH_MESSAGE(0, "unimplemented propertyID: %d", propertyID);
233     }
234     LOG_ERROR("unimplemented propertyID: %d", propertyID);
235     return nullptr;
236 }
237
238 }