| 1 | /* |
| 2 | * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org> |
| 3 | * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.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 | #pragma once |
| 22 | |
| 23 | #include "AnimationUtilities.h" |
| 24 | #include "SVGLengthContext.h" |
| 25 | #include "SVGParsingError.h" |
| 26 | #include "SVGPropertyTraits.h" |
| 27 | |
| 28 | namespace WebCore { |
| 29 | |
| 30 | class CSSPrimitiveValue; |
| 31 | class QualifiedName; |
| 32 | |
| 33 | enum SVGLengthNegativeValuesMode { |
| 34 | AllowNegativeLengths, |
| 35 | ForbidNegativeLengths |
| 36 | }; |
| 37 | |
| 38 | class SVGLengthValue { |
| 39 | WTF_MAKE_FAST_ALLOCATED; |
| 40 | public: |
| 41 | // FIXME: Once all SVGLengthValue users use Length internally, we make this a wrapper for Length. |
| 42 | SVGLengthValue(SVGLengthMode = LengthModeOther, const String& valueAsString = String()); |
| 43 | SVGLengthValue(const SVGLengthContext&, float, SVGLengthMode = LengthModeOther, SVGLengthType = LengthTypeNumber); |
| 44 | |
| 45 | SVGLengthType unitType() const; |
| 46 | SVGLengthMode unitMode() const; |
| 47 | |
| 48 | bool operator==(const SVGLengthValue&) const; |
| 49 | bool operator!=(const SVGLengthValue&) const; |
| 50 | |
| 51 | static SVGLengthValue construct(SVGLengthMode, const String&, SVGParsingError&, SVGLengthNegativeValuesMode = AllowNegativeLengths); |
| 52 | |
| 53 | float value(const SVGLengthContext&) const; |
| 54 | ExceptionOr<float> valueForBindings(const SVGLengthContext&) const; |
| 55 | ExceptionOr<void> setValue(float, const SVGLengthContext&); |
| 56 | ExceptionOr<void> setValue(const SVGLengthContext&, float, SVGLengthMode, SVGLengthType); |
| 57 | |
| 58 | float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; } |
| 59 | void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value; } |
| 60 | |
| 61 | float valueAsPercentage() const; |
| 62 | |
| 63 | String valueAsString() const; |
| 64 | ExceptionOr<void> setValueAsString(const String&); |
| 65 | ExceptionOr<void> setValueAsString(const String&, SVGLengthMode); |
| 66 | |
| 67 | ExceptionOr<void> newValueSpecifiedUnits(unsigned short, float valueInSpecifiedUnits); |
| 68 | ExceptionOr<void> convertToSpecifiedUnits(unsigned short, const SVGLengthContext&); |
| 69 | |
| 70 | // Helper functions |
| 71 | bool isRelative() const |
| 72 | { |
| 73 | auto type = unitType(); |
| 74 | return type == LengthTypePercentage || type == LengthTypeEMS || type == LengthTypeEXS; |
| 75 | } |
| 76 | |
| 77 | bool isZero() const |
| 78 | { |
| 79 | return !m_valueInSpecifiedUnits; |
| 80 | } |
| 81 | |
| 82 | static SVGLengthValue fromCSSPrimitiveValue(const CSSPrimitiveValue&); |
| 83 | static Ref<CSSPrimitiveValue> toCSSPrimitiveValue(const SVGLengthValue&); |
| 84 | static SVGLengthMode lengthModeForAnimatedLengthAttribute(const QualifiedName&); |
| 85 | |
| 86 | SVGLengthValue blend(const SVGLengthValue& from, float progress) const |
| 87 | { |
| 88 | auto toType = unitType(); |
| 89 | auto fromType = from.unitType(); |
| 90 | if ((from.isZero() && isZero()) |
| 91 | || fromType == LengthTypeUnknown |
| 92 | || toType == LengthTypeUnknown |
| 93 | || (!from.isZero() && fromType != LengthTypePercentage && toType == LengthTypePercentage) |
| 94 | || (!isZero() && fromType == LengthTypePercentage && toType != LengthTypePercentage) |
| 95 | || (!from.isZero() && !isZero() && (fromType == LengthTypeEMS || fromType == LengthTypeEXS) && fromType != toType)) |
| 96 | return *this; |
| 97 | |
| 98 | SVGLengthValue length; |
| 99 | |
| 100 | if (fromType == LengthTypePercentage || toType == LengthTypePercentage) { |
| 101 | auto fromPercent = from.valueAsPercentage() * 100; |
| 102 | auto toPercent = valueAsPercentage() * 100; |
| 103 | auto result = length.newValueSpecifiedUnits(LengthTypePercentage, WebCore::blend(fromPercent, toPercent, progress)); |
| 104 | if (result.hasException()) |
| 105 | return { }; |
| 106 | return length; |
| 107 | } |
| 108 | |
| 109 | if (fromType == toType || from.isZero() || isZero() || fromType == LengthTypeEMS || fromType == LengthTypeEXS) { |
| 110 | auto fromValue = from.valueInSpecifiedUnits(); |
| 111 | auto toValue = valueInSpecifiedUnits(); |
| 112 | if (isZero()) { |
| 113 | auto result = length.newValueSpecifiedUnits(fromType, WebCore::blend(fromValue, toValue, progress)); |
| 114 | if (result.hasException()) |
| 115 | return { }; |
| 116 | } else { |
| 117 | auto result = length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue, progress)); |
| 118 | if (result.hasException()) |
| 119 | return { }; |
| 120 | } |
| 121 | return length; |
| 122 | } |
| 123 | |
| 124 | ASSERT(!isRelative()); |
| 125 | ASSERT(!from.isRelative()); |
| 126 | |
| 127 | SVGLengthContext nonRelativeLengthContext(nullptr); |
| 128 | auto fromValueInUserUnits = nonRelativeLengthContext.convertValueToUserUnits(from.valueInSpecifiedUnits(), from.unitMode(), fromType); |
| 129 | if (fromValueInUserUnits.hasException()) |
| 130 | return { }; |
| 131 | |
| 132 | auto fromValue = nonRelativeLengthContext.convertValueFromUserUnits(fromValueInUserUnits.releaseReturnValue(), unitMode(), toType); |
| 133 | if (fromValue.hasException()) |
| 134 | return { }; |
| 135 | |
| 136 | float toValue = valueInSpecifiedUnits(); |
| 137 | auto result = length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue.releaseReturnValue(), toValue, progress)); |
| 138 | if (result.hasException()) |
| 139 | return { }; |
| 140 | return length; |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | float m_valueInSpecifiedUnits { 0 }; |
| 145 | unsigned m_unit; |
| 146 | }; |
| 147 | |
| 148 | template<> struct SVGPropertyTraits<SVGLengthValue> { |
| 149 | static SVGLengthValue initialValue() { return { }; } |
| 150 | static Optional<SVGLengthValue> parse(const QualifiedName& attrName, const String& string) |
| 151 | { |
| 152 | SVGLengthValue length; |
| 153 | length.setValueAsString(string, SVGLengthValue::lengthModeForAnimatedLengthAttribute(attrName)).hasException(); |
| 154 | return length; |
| 155 | } |
| 156 | static String toString(const SVGLengthValue& length) { return length.valueAsString(); } |
| 157 | }; |
| 158 | |
| 159 | WTF::TextStream& operator<<(WTF::TextStream&, const SVGLengthValue&); |
| 160 | |
| 161 | } // namespace WebCore |
| 162 | |