Upstream version 5.34.104.0
[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 inline static PassRefPtr<AnimatableValue> createFromBackgroundPosition(const Length& length, bool originIsSet, BackgroundEdgeOrigin origin, const RenderStyle& style)
179 {
180     if (!originIsSet || origin == LeftEdge || origin == TopEdge)
181         return createFromLength(length, style);
182
183     return AnimatableLength::create(CSSCalcValue::createExpressionNode(
184         CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(100, CSSPrimitiveValue::CSS_PERCENTAGE), true),
185         CSSCalcValue::createExpressionNode(length, style.effectiveZoom()),
186         CalcSubtract));
187 }
188
189 template<CSSPropertyID property>
190 inline static PassRefPtr<AnimatableValue> createFromFillLayers(const FillLayer* fillLayer, const RenderStyle& style)
191 {
192     ASSERT(fillLayer);
193     Vector<RefPtr<AnimatableValue> > values;
194     while (fillLayer) {
195         if (property == CSSPropertyBackgroundImage || property == CSSPropertyWebkitMaskImage) {
196             if (!fillLayer->isImageSet())
197                 break;
198             values.append(createFromStyleImage(fillLayer->image()));
199         } else if (property == CSSPropertyBackgroundPositionX || property == CSSPropertyWebkitMaskPositionX) {
200             if (!fillLayer->isXPositionSet())
201                 break;
202             values.append(createFromBackgroundPosition(fillLayer->xPosition(), fillLayer->isBackgroundXOriginSet(), fillLayer->backgroundXOrigin(), style));
203         } else if (property == CSSPropertyBackgroundPositionY || property == CSSPropertyWebkitMaskPositionY) {
204             if (!fillLayer->isYPositionSet())
205                 break;
206             values.append(createFromBackgroundPosition(fillLayer->yPosition(), fillLayer->isBackgroundYOriginSet(), fillLayer->backgroundYOrigin(), style));
207         } else if (property == CSSPropertyBackgroundSize || property == CSSPropertyWebkitMaskSize) {
208             if (!fillLayer->isSizeSet())
209                 break;
210             values.append(createFromFillSize(fillLayer->size(), style));
211         } else {
212             ASSERT_NOT_REACHED();
213         }
214         fillLayer = fillLayer->next();
215     }
216     return AnimatableRepeatable::create(values);
217 }
218
219 PassRefPtr<AnimatableValue> CSSAnimatableValueFactory::createFromColor(CSSPropertyID property, const RenderStyle& style)
220 {
221     Color color = style.colorIncludingFallback(property, false);
222     Color visitedLinkColor = style.colorIncludingFallback(property, true);
223     return AnimatableColor::create(color, visitedLinkColor);
224 }
225
226 inline static PassRefPtr<AnimatableValue> createFromShapeValue(ShapeValue* value)
227 {
228     if (value)
229         return AnimatableShapeValue::create(value);
230     return AnimatableUnknown::create(CSSValueAuto);
231 }
232
233 // FIXME: Generate this function.
234 PassRefPtr<AnimatableValue> CSSAnimatableValueFactory::create(CSSPropertyID property, const RenderStyle& style)
235 {
236     ASSERT(CSSAnimations::isAnimatableProperty(property));
237     switch (property) {
238     case CSSPropertyBackgroundColor:
239         return createFromColor(property, style);
240     case CSSPropertyBackgroundImage:
241         return createFromFillLayers<CSSPropertyBackgroundImage>(style.backgroundLayers(), style);
242     case CSSPropertyBackgroundPositionX:
243         return createFromFillLayers<CSSPropertyBackgroundPositionX>(style.backgroundLayers(), style);
244     case CSSPropertyBackgroundPositionY:
245         return createFromFillLayers<CSSPropertyBackgroundPositionY>(style.backgroundLayers(), style);
246     case CSSPropertyBackgroundSize:
247     case CSSPropertyWebkitBackgroundSize:
248         return createFromFillLayers<CSSPropertyBackgroundSize>(style.backgroundLayers(), style);
249     case CSSPropertyBaselineShift:
250         return AnimatableSVGLength::create(style.baselineShiftValue());
251     case CSSPropertyBorderBottomColor:
252         return createFromColor(property, style);
253     case CSSPropertyBorderBottomLeftRadius:
254         return createFromLengthSize(style.borderBottomLeftRadius(), style);
255     case CSSPropertyBorderBottomRightRadius:
256         return createFromLengthSize(style.borderBottomRightRadius(), style);
257     case CSSPropertyBorderBottomWidth:
258         return createFromDouble(style.borderBottomWidth());
259     case CSSPropertyBorderImageOutset:
260         return createFromBorderImageLengthBox(style.borderImageOutset(), style);
261     case CSSPropertyBorderImageSlice:
262         return createFromLengthBox(style.borderImageSlices(), style);
263     case CSSPropertyBorderImageSource:
264         return createFromStyleImage(style.borderImageSource());
265     case CSSPropertyBorderImageWidth:
266         return createFromBorderImageLengthBox(style.borderImageWidth(), style);
267     case CSSPropertyBorderLeftColor:
268         return createFromColor(property, style);
269     case CSSPropertyBorderLeftWidth:
270         return createFromDouble(style.borderLeftWidth());
271     case CSSPropertyBorderRightColor:
272         return createFromColor(property, style);
273     case CSSPropertyBorderRightWidth:
274         return createFromDouble(style.borderRightWidth());
275     case CSSPropertyBorderTopColor:
276         return createFromColor(property, style);
277     case CSSPropertyBorderTopLeftRadius:
278         return createFromLengthSize(style.borderTopLeftRadius(), style);
279     case CSSPropertyBorderTopRightRadius:
280         return createFromLengthSize(style.borderTopRightRadius(), style);
281     case CSSPropertyBorderTopWidth:
282         return createFromDouble(style.borderTopWidth());
283     case CSSPropertyBottom:
284         return createFromLength(style.bottom(), style);
285     case CSSPropertyBoxShadow:
286     case CSSPropertyWebkitBoxShadow:
287         return AnimatableShadow::create(style.boxShadow());
288     case CSSPropertyClip:
289         if (style.hasClip())
290             return createFromLengthBox(style.clip(), style);
291         return AnimatableUnknown::create(CSSPrimitiveValue::create(CSSValueAuto));
292     case CSSPropertyColor:
293         return createFromColor(property, style);
294     case CSSPropertyFillOpacity:
295         return createFromDouble(style.fillOpacity());
296     case CSSPropertyFill:
297         return AnimatableSVGPaint::create(style.svgStyle()->fillPaintType(), style.svgStyle()->fillPaintColor(), style.svgStyle()->fillPaintUri());
298     case CSSPropertyFlexGrow:
299         return createFromDouble(style.flexGrow(), AnimatableDouble::InterpolationIsNonContinuousWithZero);
300     case CSSPropertyFlexShrink:
301         return createFromDouble(style.flexShrink(), AnimatableDouble::InterpolationIsNonContinuousWithZero);
302     case CSSPropertyFlexBasis:
303         return createFromLength(style.flexBasis(), style);
304     case CSSPropertyFloodColor:
305         return createFromColor(property, style);
306     case CSSPropertyFloodOpacity:
307         return createFromDouble(style.floodOpacity());
308     case CSSPropertyFontSize:
309         // Must pass a specified size to setFontSize if Text Autosizing is enabled, but a computed size
310         // if text zoom is enabled (if neither is enabled it's irrelevant as they're probably the same).
311         // FIXME: Should we introduce an option to pass the computed font size here, allowing consumers to
312         // enable text zoom rather than Text Autosizing? See http://crbug.com/227545.
313         return createFromDouble(style.specifiedFontSize());
314     case CSSPropertyHeight:
315         return createFromLength(style.height(), style);
316     case CSSPropertyKerning:
317         return AnimatableSVGLength::create(style.kerning());
318     case CSSPropertyLightingColor:
319         return createFromColor(property, style);
320     case CSSPropertyListStyleImage:
321         return createFromStyleImage(style.listStyleImage());
322     case CSSPropertyLeft:
323         return createFromLength(style.left(), style);
324     case CSSPropertyLetterSpacing:
325         return createFromDouble(style.letterSpacing());
326     case CSSPropertyLineHeight:
327         return createFromLineHeight(style.specifiedLineHeight(), style);
328     case CSSPropertyMarginBottom:
329         return createFromLength(style.marginBottom(), style);
330     case CSSPropertyMarginLeft:
331         return createFromLength(style.marginLeft(), style);
332     case CSSPropertyMarginRight:
333         return createFromLength(style.marginRight(), style);
334     case CSSPropertyMarginTop:
335         return createFromLength(style.marginTop(), style);
336     case CSSPropertyMaxHeight:
337         return createFromLength(style.maxHeight(), style);
338     case CSSPropertyMaxWidth:
339         return createFromLength(style.maxWidth(), style);
340     case CSSPropertyMinHeight:
341         return createFromLength(style.minHeight(), style);
342     case CSSPropertyMinWidth:
343         return createFromLength(style.minWidth(), style);
344     case CSSPropertyObjectPosition:
345         return createFromLengthPoint(style.objectPosition(), style);
346     case CSSPropertyOpacity:
347         return createFromDouble(style.opacity());
348     case CSSPropertyOrphans:
349         return createFromDouble(style.orphans());
350     case CSSPropertyOutlineColor:
351         return createFromColor(property, style);
352     case CSSPropertyOutlineOffset:
353         return createFromDouble(style.outlineOffset());
354     case CSSPropertyOutlineWidth:
355         return createFromDouble(style.outlineWidth());
356     case CSSPropertyPaddingBottom:
357         return createFromLength(style.paddingBottom(), style);
358     case CSSPropertyPaddingLeft:
359         return createFromLength(style.paddingLeft(), style);
360     case CSSPropertyPaddingRight:
361         return createFromLength(style.paddingRight(), style);
362     case CSSPropertyPaddingTop:
363         return createFromLength(style.paddingTop(), style);
364     case CSSPropertyRight:
365         return createFromLength(style.right(), style);
366     case CSSPropertyStrokeWidth:
367         return AnimatableSVGLength::create(style.strokeWidth());
368     case CSSPropertyStopColor:
369         return createFromColor(property, style);
370     case CSSPropertyStopOpacity:
371         return createFromDouble(style.stopOpacity());
372     case CSSPropertyStrokeDasharray:
373         return AnimatableStrokeDasharrayList::create(style.strokeDashArray());
374     case CSSPropertyStrokeDashoffset:
375         return AnimatableSVGLength::create(style.strokeDashOffset());
376     case CSSPropertyStrokeMiterlimit:
377         return createFromDouble(style.strokeMiterLimit());
378     case CSSPropertyStrokeOpacity:
379         return createFromDouble(style.strokeOpacity());
380     case CSSPropertyStroke:
381         return AnimatableSVGPaint::create(style.svgStyle()->strokePaintType(), style.svgStyle()->strokePaintColor(), style.svgStyle()->strokePaintUri());
382     case CSSPropertyTextDecorationColor:
383         return AnimatableColor::create(style.textDecorationColor().resolve(style.color()), style.visitedLinkTextDecorationColor().resolve(style.visitedLinkColor()));
384     case CSSPropertyTextIndent:
385         return createFromLength(style.textIndent(), style);
386     case CSSPropertyTextShadow:
387         return AnimatableShadow::create(style.textShadow());
388     case CSSPropertyTop:
389         return createFromLength(style.top(), style);
390     case CSSPropertyWebkitBorderHorizontalSpacing:
391         return createFromDouble(style.horizontalBorderSpacing());
392     case CSSPropertyWebkitBorderVerticalSpacing:
393         return createFromDouble(style.verticalBorderSpacing());
394     case CSSPropertyWebkitClipPath:
395         if (ClipPathOperation* operation = style.clipPath())
396             return AnimatableClipPathOperation::create(operation);
397         return AnimatableUnknown::create(CSSValueNone);
398     case CSSPropertyWebkitColumnCount:
399         return createFromDouble(style.columnCount());
400     case CSSPropertyWebkitColumnGap:
401         return createFromDouble(style.columnGap());
402     case CSSPropertyWebkitColumnRuleColor:
403         return createFromColor(property, style);
404     case CSSPropertyWebkitColumnRuleWidth:
405         return createFromDouble(style.columnRuleWidth());
406     case CSSPropertyWebkitColumnWidth:
407         return createFromDouble(style.columnWidth());
408     case CSSPropertyWebkitFilter:
409         return AnimatableFilterOperations::create(style.filter());
410     case CSSPropertyWebkitMaskBoxImageOutset:
411         return createFromBorderImageLengthBox(style.maskBoxImageOutset(), style);
412     case CSSPropertyWebkitMaskBoxImageSlice:
413         return createFromLengthBoxAndBool(style.maskBoxImageSlices(), style.maskBoxImageSlicesFill(), style);
414     case CSSPropertyWebkitMaskBoxImageSource:
415         return createFromStyleImage(style.maskBoxImageSource());
416     case CSSPropertyWebkitMaskBoxImageWidth:
417         return createFromBorderImageLengthBox(style.maskBoxImageWidth(), style);
418     case CSSPropertyWebkitMaskImage:
419         return createFromFillLayers<CSSPropertyWebkitMaskImage>(style.maskLayers(), style);
420     case CSSPropertyWebkitMaskPositionX:
421         return createFromFillLayers<CSSPropertyWebkitMaskPositionX>(style.maskLayers(), style);
422     case CSSPropertyWebkitMaskPositionY:
423         return createFromFillLayers<CSSPropertyWebkitMaskPositionY>(style.maskLayers(), style);
424     case CSSPropertyWebkitMaskSize:
425         return createFromFillLayers<CSSPropertyWebkitMaskSize>(style.maskLayers(), style);
426     case CSSPropertyWebkitPerspective:
427         return createFromDouble(style.perspective());
428     case CSSPropertyWebkitPerspectiveOriginX:
429         return createFromLength(style.perspectiveOriginX(), style);
430     case CSSPropertyWebkitPerspectiveOriginY:
431         return createFromLength(style.perspectiveOriginY(), style);
432     case CSSPropertyShapeInside:
433         return createFromShapeValue(style.shapeInside());
434     case CSSPropertyShapeOutside:
435         return createFromShapeValue(style.shapeOutside());
436     case CSSPropertyShapeMargin:
437         return createFromLength(style.shapeMargin(), style);
438     case CSSPropertyShapeImageThreshold:
439         return createFromDouble(style.shapeImageThreshold());
440     case CSSPropertyWebkitTextStrokeColor:
441         return createFromColor(property, style);
442     case CSSPropertyWebkitTransform:
443         return AnimatableTransform::create(style.transform());
444     case CSSPropertyWebkitTransformOriginX:
445         return createFromLength(style.transformOriginX(), style);
446     case CSSPropertyWebkitTransformOriginY:
447         return createFromLength(style.transformOriginY(), style);
448     case CSSPropertyWebkitTransformOriginZ:
449         return createFromDouble(style.transformOriginZ());
450     case CSSPropertyWidows:
451         return createFromDouble(style.widows());
452     case CSSPropertyWidth:
453         return createFromLength(style.width(), style);
454     case CSSPropertyWordSpacing:
455         return createFromDouble(style.wordSpacing());
456     case CSSPropertyVisibility:
457         return AnimatableVisibility::create(style.visibility());
458     case CSSPropertyZIndex:
459         return createFromDouble(style.zIndex());
460     case CSSPropertyZoom:
461         return createFromDouble(style.zoom());
462     default:
463         ASSERT_NOT_REACHED();
464         // This return value is to avoid a release crash if possible.
465         return AnimatableUnknown::create(0);
466     }
467 }
468
469 } // namespace WebCore