2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/animation/css/CSSAnimatableValueFactory.h"
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"
64 static PassRefPtr<AnimatableValue> createFromLength(const Length& length, const RenderStyle& style)
66 switch (length.type()) {
68 return AnimatableLength::create(adjustFloatForAbsoluteZoom(length.value(), style), AnimatableLength::UnitTypePixels);
70 return AnimatableLength::create(length.value(), AnimatableLength::UnitTypePercentage);
72 return AnimatableLength::create(CSSCalcValue::createExpressionNode(length.calculationValue()->expression(), style.effectiveZoom()));
80 return AnimatableUnknown::create(CSSPrimitiveValue::create(length));
82 return AnimatableUnknown::create(CSSValueNone);
83 case ExtendToZoom: // Does not apply to elements.
93 static PassRefPtr<AnimatableValue> createFromLineHeight(const Length& length, const RenderStyle& style)
95 if (length.type() == Percent) {
96 double value = length.value();
97 // -100% is used to represent "normal" line height.
99 return AnimatableUnknown::create(CSSValueNormal);
100 return AnimatableDouble::create(value);
102 return createFromLength(length, style);
105 inline static PassRefPtr<AnimatableValue> createFromDouble(double value, AnimatableDouble::Constraint constraint = AnimatableDouble::Unconstrained)
107 return AnimatableDouble::create(value, constraint);
110 inline static PassRefPtr<AnimatableValue> createFromLengthBox(const LengthBox& lengthBox, const RenderStyle& style)
112 return AnimatableLengthBox::create(
113 createFromLength(lengthBox.left(), style),
114 createFromLength(lengthBox.right(), style),
115 createFromLength(lengthBox.top(), style),
116 createFromLength(lengthBox.bottom(), style));
119 static PassRefPtr<AnimatableValue> createFromBorderImageLength(const BorderImageLength& borderImageLength, const RenderStyle& style)
121 if (borderImageLength.isNumber())
122 return createFromDouble(borderImageLength.number());
123 return createFromLength(borderImageLength.length(), style);
126 inline static PassRefPtr<AnimatableValue> createFromBorderImageLengthBox(const BorderImageLengthBox& borderImageLengthBox, const RenderStyle& style)
128 return AnimatableLengthBox::create(
129 createFromBorderImageLength(borderImageLengthBox.left(), style),
130 createFromBorderImageLength(borderImageLengthBox.right(), style),
131 createFromBorderImageLength(borderImageLengthBox.top(), style),
132 createFromBorderImageLength(borderImageLengthBox.bottom(), style));
135 inline static PassRefPtr<AnimatableValue> createFromLengthBoxAndBool(const LengthBox lengthBox, const bool flag, const RenderStyle& style)
137 return AnimatableLengthBoxAndBool::create(
138 createFromLengthBox(lengthBox, style),
142 inline static PassRefPtr<AnimatableValue> createFromLengthPoint(const LengthPoint& lengthPoint, const RenderStyle& style)
144 return AnimatableLengthPoint::create(
145 createFromLength(lengthPoint.x(), style),
146 createFromLength(lengthPoint.y(), style));
149 inline static PassRefPtr<AnimatableValue> createFromLengthSize(const LengthSize& lengthSize, const RenderStyle& style)
151 return AnimatableLengthSize::create(
152 createFromLength(lengthSize.width(), style),
153 createFromLength(lengthSize.height(), style));
156 inline static PassRefPtr<AnimatableValue> createFromStyleImage(StyleImage* image)
159 return AnimatableImage::create(image);
160 return AnimatableUnknown::create(CSSValueNone);
163 inline static PassRefPtr<AnimatableValue> createFromFillSize(const FillSize& fillSize, const RenderStyle& style)
165 switch (fillSize.type) {
167 return createFromLengthSize(fillSize.size, style);
171 return AnimatableUnknown::create(CSSPrimitiveValue::create(fillSize.type));
173 ASSERT_NOT_REACHED();
178 template<CSSPropertyID property>
179 inline static PassRefPtr<AnimatableValue> createFromFillLayers(const FillLayer* fillLayer, const RenderStyle& style)
182 Vector<RefPtr<AnimatableValue> > values;
184 if (property == CSSPropertyBackgroundImage || property == CSSPropertyWebkitMaskImage) {
185 if (!fillLayer->isImageSet())
187 values.append(createFromStyleImage(fillLayer->image()));
188 } else if (property == CSSPropertyBackgroundPositionX || property == CSSPropertyWebkitMaskPositionX) {
189 if (!fillLayer->isXPositionSet())
191 values.append(createFromLength(fillLayer->xPosition(), style));
192 } else if (property == CSSPropertyBackgroundPositionY || property == CSSPropertyWebkitMaskPositionY) {
193 if (!fillLayer->isYPositionSet())
195 values.append(createFromLength(fillLayer->yPosition(), style));
196 } else if (property == CSSPropertyBackgroundSize || property == CSSPropertyWebkitMaskSize) {
197 if (!fillLayer->isSizeSet())
199 values.append(createFromFillSize(fillLayer->size(), style));
201 ASSERT_NOT_REACHED();
203 fillLayer = fillLayer->next();
205 return AnimatableRepeatable::create(values);
208 PassRefPtr<AnimatableValue> CSSAnimatableValueFactory::createFromColor(CSSPropertyID property, const RenderStyle& style)
210 Color color = style.colorIncludingFallback(property, false);
211 Color visitedLinkColor = style.colorIncludingFallback(property, true);
212 return AnimatableColor::create(color, visitedLinkColor);
215 inline static PassRefPtr<AnimatableValue> createFromShapeValue(ShapeValue* value)
218 return AnimatableShapeValue::create(value);
219 return AnimatableUnknown::create(CSSValueAuto);
222 // FIXME: Generate this function.
223 PassRefPtr<AnimatableValue> CSSAnimatableValueFactory::create(CSSPropertyID property, const RenderStyle& style)
225 ASSERT(CSSAnimations::isAnimatableProperty(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:
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());
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());
452 ASSERT_NOT_REACHED();
453 // This return value is to avoid a release crash if possible.
454 return AnimatableUnknown::create(0);
458 } // namespace WebCore