05a8e55934cad427e35f9d4755e0f7677a51ca16
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / css / CSSAnimatableValueFactory.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "core/animation/css/CSSAnimatableValueFactory.h"
33
34 #include "CSSValueKeywords.h"
35 #include "core/animation/AnimatableClipPathOperation.h"
36 #include "core/animation/AnimatableColor.h"
37 #include "core/animation/AnimatableDouble.h"
38 #include "core/animation/AnimatableFilterOperations.h"
39 #include "core/animation/AnimatableImage.h"
40 #include "core/animation/AnimatableLength.h"
41 #include "core/animation/AnimatableLengthBox.h"
42 #include "core/animation/AnimatableLengthBoxAndBool.h"
43 #include "core/animation/AnimatableLengthPoint.h"
44 #include "core/animation/AnimatableLengthSize.h"
45 #include "core/animation/AnimatableRepeatable.h"
46 #include "core/animation/AnimatableSVGLength.h"
47 #include "core/animation/AnimatableSVGPaint.h"
48 #include "core/animation/AnimatableShadow.h"
49 #include "core/animation/AnimatableShapeValue.h"
50 #include "core/animation/AnimatableStrokeDasharrayList.h"
51 #include "core/animation/AnimatableTransform.h"
52 #include "core/animation/AnimatableUnknown.h"
53 #include "core/animation/AnimatableVisibility.h"
54 #include "core/animation/css/CSSAnimations.h"
55 #include "core/css/CSSCalculationValue.h"
56 #include "core/css/CSSPrimitiveValue.h"
57 #include "core/css/CSSPrimitiveValueMappings.h"
58 #include "core/rendering/style/RenderStyle.h"
59 #include "platform/Length.h"
60 #include "platform/LengthBox.h"
61
62 namespace WebCore {
63
64 static PassRefPtr<AnimatableValue> createFromLength(const Length& length, const RenderStyle& style)
65 {
66     switch (length.type()) {
67     case Fixed:
68         return AnimatableLength::create(adjustFloatForAbsoluteZoom(length.value(), style), AnimatableLength::UnitTypePixels);
69     case Percent:
70         return AnimatableLength::create(length.value(), AnimatableLength::UnitTypePercentage);
71     case Calculated:
72         return AnimatableLength::create(CSSCalcValue::createExpressionNode(length.calculationValue()->expression(), style.effectiveZoom()));
73     case Auto:
74     case Intrinsic:
75     case MinIntrinsic:
76     case MinContent:
77     case MaxContent:
78     case FillAvailable:
79     case FitContent:
80         return AnimatableUnknown::create(CSSPrimitiveValue::create(length));
81     case Undefined:
82         return AnimatableUnknown::create(CSSValueNone);
83     case ExtendToZoom: // Does not apply to elements.
84     case DeviceWidth:
85     case DeviceHeight:
86         ASSERT_NOT_REACHED();
87         return 0;
88     }
89     ASSERT_NOT_REACHED();
90     return 0;
91 }
92
93 static PassRefPtr<AnimatableValue> createFromLineHeight(const Length& length, const RenderStyle& style)
94 {
95     if (length.type() == Percent) {
96         double value = length.value();
97         // -100% is used to represent "normal" line height.
98         if (value == -100)
99             return AnimatableUnknown::create(CSSValueNormal);
100         return AnimatableDouble::create(value);
101     }
102     return createFromLength(length, style);
103 }
104
105 inline static PassRefPtr<AnimatableValue> createFromDouble(double value, AnimatableDouble::Constraint constraint = AnimatableDouble::Unconstrained)
106 {
107     return AnimatableDouble::create(value, constraint);
108 }
109
110 inline static PassRefPtr<AnimatableValue> createFromLengthBox(const LengthBox& lengthBox, const RenderStyle& style)
111 {
112     return AnimatableLengthBox::create(
113         createFromLength(lengthBox.left(), style),
114         createFromLength(lengthBox.right(), style),
115         createFromLength(lengthBox.top(), style),
116         createFromLength(lengthBox.bottom(), style));
117 }
118
119 static PassRefPtr<AnimatableValue> createFromBorderImageLength(const BorderImageLength& borderImageLength, const RenderStyle& style)
120 {
121     if (borderImageLength.isNumber())
122         return createFromDouble(borderImageLength.number());
123     return createFromLength(borderImageLength.length(), style);
124 }
125
126 inline static PassRefPtr<AnimatableValue> createFromBorderImageLengthBox(const BorderImageLengthBox& borderImageLengthBox, const RenderStyle& style)
127 {
128     return AnimatableLengthBox::create(
129         createFromBorderImageLength(borderImageLengthBox.left(), style),
130         createFromBorderImageLength(borderImageLengthBox.right(), style),
131         createFromBorderImageLength(borderImageLengthBox.top(), style),
132         createFromBorderImageLength(borderImageLengthBox.bottom(), style));
133 }
134
135 inline static PassRefPtr<AnimatableValue> createFromLengthBoxAndBool(const LengthBox lengthBox, const bool flag, const RenderStyle& style)
136 {
137     return AnimatableLengthBoxAndBool::create(
138         createFromLengthBox(lengthBox, style),
139         flag);
140 }
141
142 inline static PassRefPtr<AnimatableValue> createFromLengthPoint(const LengthPoint& lengthPoint, const RenderStyle& style)
143 {
144     return AnimatableLengthPoint::create(
145         createFromLength(lengthPoint.x(), style),
146         createFromLength(lengthPoint.y(), style));
147 }
148
149 inline static PassRefPtr<AnimatableValue> createFromLengthSize(const LengthSize& lengthSize, const RenderStyle& style)
150 {
151     return AnimatableLengthSize::create(
152         createFromLength(lengthSize.width(), style),
153         createFromLength(lengthSize.height(), style));
154 }
155
156 inline static PassRefPtr<AnimatableValue> createFromStyleImage(StyleImage* image)
157 {
158     if (image)
159         return AnimatableImage::create(image);
160     return AnimatableUnknown::create(CSSValueNone);
161 }
162
163 inline static PassRefPtr<AnimatableValue> createFromFillSize(const FillSize& fillSize, const RenderStyle& style)
164 {
165     switch (fillSize.type) {
166     case SizeLength:
167         return createFromLengthSize(fillSize.size, style);
168     case Contain:
169     case Cover:
170     case SizeNone:
171         return AnimatableUnknown::create(CSSPrimitiveValue::create(fillSize.type));
172     default:
173         ASSERT_NOT_REACHED();
174         return 0;
175     }
176 }
177
178 template<CSSPropertyID property>
179 inline static PassRefPtr<AnimatableValue> createFromFillLayers(const FillLayer* fillLayer, const RenderStyle& style)
180 {
181     ASSERT(fillLayer);
182     Vector<RefPtr<AnimatableValue> > values;
183     while (fillLayer) {
184         if (property == CSSPropertyBackgroundImage || property == CSSPropertyWebkitMaskImage) {
185             if (!fillLayer->isImageSet())
186                 break;
187             values.append(createFromStyleImage(fillLayer->image()));
188         } else if (property == CSSPropertyBackgroundPositionX || property == CSSPropertyWebkitMaskPositionX) {
189             if (!fillLayer->isXPositionSet())
190                 break;
191             values.append(createFromLength(fillLayer->xPosition(), style));
192         } else if (property == CSSPropertyBackgroundPositionY || property == CSSPropertyWebkitMaskPositionY) {
193             if (!fillLayer->isYPositionSet())
194                 break;
195             values.append(createFromLength(fillLayer->yPosition(), style));
196         } else if (property == CSSPropertyBackgroundSize || property == CSSPropertyWebkitMaskSize) {
197             if (!fillLayer->isSizeSet())
198                 break;
199             values.append(createFromFillSize(fillLayer->size(), style));
200         } else {
201             ASSERT_NOT_REACHED();
202         }
203         fillLayer = fillLayer->next();
204     }
205     return AnimatableRepeatable::create(values);
206 }
207
208 PassRefPtr<AnimatableValue> CSSAnimatableValueFactory::createFromColor(CSSPropertyID property, const RenderStyle& style)
209 {
210     Color color = style.colorIncludingFallback(property, false);
211     Color visitedLinkColor = style.colorIncludingFallback(property, true);
212     return AnimatableColor::create(color, visitedLinkColor);
213 }
214
215 inline static PassRefPtr<AnimatableValue> createFromShapeValue(ShapeValue* value)
216 {
217     if (value)
218         return AnimatableShapeValue::create(value);
219     return AnimatableUnknown::create(CSSValueAuto);
220 }
221
222 // FIXME: Generate this function.
223 PassRefPtr<AnimatableValue> CSSAnimatableValueFactory::create(CSSPropertyID property, const RenderStyle& style)
224 {
225     ASSERT(CSSAnimations::isAnimatableProperty(property));
226     switch (property) {
227     case CSSPropertyBackgroundColor:
228         return createFromColor(property, style);
229     case CSSPropertyBackgroundImage:
230         return createFromFillLayers<CSSPropertyBackgroundImage>(style.backgroundLayers(), style);
231     case CSSPropertyBackgroundPositionX:
232         return createFromFillLayers<CSSPropertyBackgroundPositionX>(style.backgroundLayers(), style);
233     case CSSPropertyBackgroundPositionY:
234         return createFromFillLayers<CSSPropertyBackgroundPositionY>(style.backgroundLayers(), style);
235     case CSSPropertyBackgroundSize:
236     case CSSPropertyWebkitBackgroundSize:
237         return createFromFillLayers<CSSPropertyBackgroundSize>(style.backgroundLayers(), style);
238     case CSSPropertyBaselineShift:
239         return AnimatableSVGLength::create(style.baselineShiftValue());
240     case CSSPropertyBorderBottomColor:
241         return createFromColor(property, style);
242     case CSSPropertyBorderBottomLeftRadius:
243         return createFromLengthSize(style.borderBottomLeftRadius(), style);
244     case CSSPropertyBorderBottomRightRadius:
245         return createFromLengthSize(style.borderBottomRightRadius(), style);
246     case CSSPropertyBorderBottomWidth:
247         return createFromDouble(style.borderBottomWidth());
248     case CSSPropertyBorderImageOutset:
249         return createFromBorderImageLengthBox(style.borderImageOutset(), style);
250     case CSSPropertyBorderImageSlice:
251         return createFromLengthBox(style.borderImageSlices(), style);
252     case CSSPropertyBorderImageSource:
253         return createFromStyleImage(style.borderImageSource());
254     case CSSPropertyBorderImageWidth:
255         return createFromBorderImageLengthBox(style.borderImageWidth(), style);
256     case CSSPropertyBorderLeftColor:
257         return createFromColor(property, style);
258     case CSSPropertyBorderLeftWidth:
259         return createFromDouble(style.borderLeftWidth());
260     case CSSPropertyBorderRightColor:
261         return createFromColor(property, style);
262     case CSSPropertyBorderRightWidth:
263         return createFromDouble(style.borderRightWidth());
264     case CSSPropertyBorderTopColor:
265         return createFromColor(property, style);
266     case CSSPropertyBorderTopLeftRadius:
267         return createFromLengthSize(style.borderTopLeftRadius(), style);
268     case CSSPropertyBorderTopRightRadius:
269         return createFromLengthSize(style.borderTopRightRadius(), style);
270     case CSSPropertyBorderTopWidth:
271         return createFromDouble(style.borderTopWidth());
272     case CSSPropertyBottom:
273         return createFromLength(style.bottom(), style);
274     case CSSPropertyBoxShadow:
275     case CSSPropertyWebkitBoxShadow:
276         return AnimatableShadow::create(style.boxShadow());
277     case CSSPropertyClip:
278         if (style.hasClip())
279             return createFromLengthBox(style.clip(), style);
280         return AnimatableUnknown::create(CSSPrimitiveValue::create(CSSValueAuto));
281     case CSSPropertyColor:
282         return createFromColor(property, style);
283     case CSSPropertyFillOpacity:
284         return createFromDouble(style.fillOpacity());
285     case CSSPropertyFill:
286         return AnimatableSVGPaint::create(style.svgStyle()->fillPaintType(), style.svgStyle()->fillPaintColor(), style.svgStyle()->fillPaintUri());
287     case CSSPropertyFlexGrow:
288         return createFromDouble(style.flexGrow(), AnimatableDouble::InterpolationIsNonContinuousWithZero);
289     case CSSPropertyFlexShrink:
290         return createFromDouble(style.flexShrink(), AnimatableDouble::InterpolationIsNonContinuousWithZero);
291     case CSSPropertyFlexBasis:
292         return createFromLength(style.flexBasis(), style);
293     case CSSPropertyFloodColor:
294         return createFromColor(property, style);
295     case CSSPropertyFloodOpacity:
296         return createFromDouble(style.floodOpacity());
297     case CSSPropertyFontSize:
298         // Must pass a specified size to setFontSize if Text Autosizing is enabled, but a computed size
299         // if text zoom is enabled (if neither is enabled it's irrelevant as they're probably the same).
300         // FIXME: Should we introduce an option to pass the computed font size here, allowing consumers to
301         // enable text zoom rather than Text Autosizing? See http://crbug.com/227545.
302         return createFromDouble(style.specifiedFontSize());
303     case CSSPropertyHeight:
304         return createFromLength(style.height(), style);
305     case CSSPropertyKerning:
306         return AnimatableSVGLength::create(style.kerning());
307     case CSSPropertyLightingColor:
308         return createFromColor(property, style);
309     case CSSPropertyListStyleImage:
310         return createFromStyleImage(style.listStyleImage());
311     case CSSPropertyLeft:
312         return createFromLength(style.left(), style);
313     case CSSPropertyLetterSpacing:
314         return createFromDouble(style.letterSpacing());
315     case CSSPropertyLineHeight:
316         return createFromLineHeight(style.specifiedLineHeight(), style);
317     case CSSPropertyMarginBottom:
318         return createFromLength(style.marginBottom(), style);
319     case CSSPropertyMarginLeft:
320         return createFromLength(style.marginLeft(), style);
321     case CSSPropertyMarginRight:
322         return createFromLength(style.marginRight(), style);
323     case CSSPropertyMarginTop:
324         return createFromLength(style.marginTop(), style);
325     case CSSPropertyMaxHeight:
326         return createFromLength(style.maxHeight(), style);
327     case CSSPropertyMaxWidth:
328         return createFromLength(style.maxWidth(), style);
329     case CSSPropertyMinHeight:
330         return createFromLength(style.minHeight(), style);
331     case CSSPropertyMinWidth:
332         return createFromLength(style.minWidth(), style);
333     case CSSPropertyObjectPosition:
334         return createFromLengthPoint(style.objectPosition(), style);
335     case CSSPropertyOpacity:
336         return createFromDouble(style.opacity());
337     case CSSPropertyOrphans:
338         return createFromDouble(style.orphans());
339     case CSSPropertyOutlineColor:
340         return createFromColor(property, style);
341     case CSSPropertyOutlineOffset:
342         return createFromDouble(style.outlineOffset());
343     case CSSPropertyOutlineWidth:
344         return createFromDouble(style.outlineWidth());
345     case CSSPropertyPaddingBottom:
346         return createFromLength(style.paddingBottom(), style);
347     case CSSPropertyPaddingLeft:
348         return createFromLength(style.paddingLeft(), style);
349     case CSSPropertyPaddingRight:
350         return createFromLength(style.paddingRight(), style);
351     case CSSPropertyPaddingTop:
352         return createFromLength(style.paddingTop(), style);
353     case CSSPropertyRight:
354         return createFromLength(style.right(), style);
355     case CSSPropertyStrokeWidth:
356         return AnimatableSVGLength::create(style.strokeWidth());
357     case CSSPropertyStopColor:
358         return createFromColor(property, style);
359     case CSSPropertyStopOpacity:
360         return createFromDouble(style.stopOpacity());
361     case CSSPropertyStrokeDasharray:
362         return AnimatableStrokeDasharrayList::create(style.strokeDashArray());
363     case CSSPropertyStrokeDashoffset:
364         return AnimatableSVGLength::create(style.strokeDashOffset());
365     case CSSPropertyStrokeMiterlimit:
366         return createFromDouble(style.strokeMiterLimit());
367     case CSSPropertyStrokeOpacity:
368         return createFromDouble(style.strokeOpacity());
369     case CSSPropertyStroke:
370         return AnimatableSVGPaint::create(style.svgStyle()->strokePaintType(), style.svgStyle()->strokePaintColor(), style.svgStyle()->strokePaintUri());
371     case CSSPropertyTextDecorationColor:
372         return AnimatableColor::create(style.textDecorationColor().resolve(style.color()), style.visitedLinkTextDecorationColor().resolve(style.visitedLinkColor()));
373     case CSSPropertyTextIndent:
374         return createFromLength(style.textIndent(), style);
375     case CSSPropertyTextShadow:
376         return AnimatableShadow::create(style.textShadow());
377     case CSSPropertyTop:
378         return createFromLength(style.top(), style);
379     case CSSPropertyWebkitBorderHorizontalSpacing:
380         return createFromDouble(style.horizontalBorderSpacing());
381     case CSSPropertyWebkitBorderVerticalSpacing:
382         return createFromDouble(style.verticalBorderSpacing());
383     case CSSPropertyWebkitClipPath:
384         if (ClipPathOperation* operation = style.clipPath())
385             return AnimatableClipPathOperation::create(operation);
386         return AnimatableUnknown::create(CSSValueNone);
387     case CSSPropertyWebkitColumnCount:
388         return createFromDouble(style.columnCount());
389     case CSSPropertyWebkitColumnGap:
390         return createFromDouble(style.columnGap());
391     case CSSPropertyWebkitColumnRuleColor:
392         return createFromColor(property, style);
393     case CSSPropertyWebkitColumnRuleWidth:
394         return createFromDouble(style.columnRuleWidth());
395     case CSSPropertyWebkitColumnWidth:
396         return createFromDouble(style.columnWidth());
397     case CSSPropertyWebkitFilter:
398         return AnimatableFilterOperations::create(style.filter());
399     case CSSPropertyWebkitMaskBoxImageOutset:
400         return createFromBorderImageLengthBox(style.maskBoxImageOutset(), style);
401     case CSSPropertyWebkitMaskBoxImageSlice:
402         return createFromLengthBoxAndBool(style.maskBoxImageSlices(), style.maskBoxImageSlicesFill(), style);
403     case CSSPropertyWebkitMaskBoxImageSource:
404         return createFromStyleImage(style.maskBoxImageSource());
405     case CSSPropertyWebkitMaskBoxImageWidth:
406         return createFromBorderImageLengthBox(style.maskBoxImageWidth(), style);
407     case CSSPropertyWebkitMaskImage:
408         return createFromFillLayers<CSSPropertyWebkitMaskImage>(style.maskLayers(), style);
409     case CSSPropertyWebkitMaskPositionX:
410         return createFromFillLayers<CSSPropertyWebkitMaskPositionX>(style.maskLayers(), style);
411     case CSSPropertyWebkitMaskPositionY:
412         return createFromFillLayers<CSSPropertyWebkitMaskPositionY>(style.maskLayers(), style);
413     case CSSPropertyWebkitMaskSize:
414         return createFromFillLayers<CSSPropertyWebkitMaskSize>(style.maskLayers(), style);
415     case CSSPropertyWebkitPerspective:
416         return createFromDouble(style.perspective());
417     case CSSPropertyWebkitPerspectiveOriginX:
418         return createFromLength(style.perspectiveOriginX(), style);
419     case CSSPropertyWebkitPerspectiveOriginY:
420         return createFromLength(style.perspectiveOriginY(), style);
421     case CSSPropertyShapeInside:
422         return createFromShapeValue(style.shapeInside());
423     case CSSPropertyShapeOutside:
424         return createFromShapeValue(style.shapeOutside());
425     case CSSPropertyShapeMargin:
426         return createFromLength(style.shapeMargin(), style);
427     case CSSPropertyShapeImageThreshold:
428         return createFromDouble(style.shapeImageThreshold());
429     case CSSPropertyWebkitTextStrokeColor:
430         return createFromColor(property, style);
431     case CSSPropertyWebkitTransform:
432         return AnimatableTransform::create(style.transform());
433     case CSSPropertyWebkitTransformOriginX:
434         return createFromLength(style.transformOriginX(), style);
435     case CSSPropertyWebkitTransformOriginY:
436         return createFromLength(style.transformOriginY(), style);
437     case CSSPropertyWebkitTransformOriginZ:
438         return createFromDouble(style.transformOriginZ());
439     case CSSPropertyWidows:
440         return createFromDouble(style.widows());
441     case CSSPropertyWidth:
442         return createFromLength(style.width(), style);
443     case CSSPropertyWordSpacing:
444         return createFromDouble(style.wordSpacing());
445     case CSSPropertyVisibility:
446         return AnimatableVisibility::create(style.visibility());
447     case CSSPropertyZIndex:
448         return createFromDouble(style.zIndex());
449     case CSSPropertyZoom:
450         return createFromDouble(style.zoom());
451     default:
452         ASSERT_NOT_REACHED();
453         // This return value is to avoid a release crash if possible.
454         return AnimatableUnknown::create(0);
455     }
456 }
457
458 } // namespace WebCore