1/* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * Copyright (C) 2014 Apple Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include "config.h" 33#include "CalculationValue.h" 34 35#include <limits> 36 37namespace WebCore { 38 39PassRef<CalculationValue> CalculationValue::create(std::unique_ptr<CalcExpressionNode> value, CalculationPermittedValueRange range) 40{ 41 return adoptRef(*new CalculationValue(WTF::move(value), range)); 42} 43 44float CalcExpressionNumber::evaluate(float) const 45{ 46 return m_value; 47} 48 49bool CalcExpressionNumber::operator==(const CalcExpressionNode& other) const 50{ 51 return other.type() == CalcExpressionNodeNumber && *this == toCalcExpressionNumber(other); 52} 53 54float CalculationValue::evaluate(float maxValue) const 55{ 56 float result = m_expression->evaluate(maxValue); 57 // FIXME: This test was originally needed when we did not detect division by zero at parse time. 58 // It's possible that this is now unneeded code and can be removed. 59 if (std::isnan(result)) 60 return 0; 61 return m_shouldClampToNonNegative && result < 0 ? 0 : result; 62} 63 64float CalcExpressionBinaryOperation::evaluate(float maxValue) const 65{ 66 float left = m_leftSide->evaluate(maxValue); 67 float right = m_rightSide->evaluate(maxValue); 68 switch (m_operator) { 69 case CalcAdd: 70 return left + right; 71 case CalcSubtract: 72 return left - right; 73 case CalcMultiply: 74 return left * right; 75 case CalcDivide: 76 if (!right) 77 return std::numeric_limits<float>::quiet_NaN(); 78 return left / right; 79 } 80 ASSERT_NOT_REACHED(); 81 return std::numeric_limits<float>::quiet_NaN(); 82} 83 84bool CalcExpressionBinaryOperation::operator==(const CalcExpressionNode& other) const 85{ 86 return other.type() == CalcExpressionNodeBinaryOperation && *this == toCalcExpressionBinaryOperation(other); 87} 88 89float CalcExpressionLength::evaluate(float maxValue) const 90{ 91 return floatValueForLength(m_length, maxValue); 92} 93 94bool CalcExpressionLength::operator==(const CalcExpressionNode& other) const 95{ 96 return other.type() == CalcExpressionNodeLength && *this == toCalcExpressionLength(other); 97} 98 99float CalcExpressionBlendLength::evaluate(float maxValue) const 100{ 101 return (1.0f - m_progress) * floatValueForLength(m_from, maxValue) + m_progress * floatValueForLength(m_to, maxValue); 102} 103 104bool CalcExpressionBlendLength::operator==(const CalcExpressionNode& other) const 105{ 106 return other.type() == CalcExpressionNodeBlendLength && *this == toCalcExpressionBlendLength(other); 107} 108 109} // namespace WebCore 110