1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include "AnimationUtilities.h"
#include "SVGLengthContext.h"
#include "SVGParsingError.h"
#include "SVGPropertyTraits.h"
namespace WebCore {
class CSSPrimitiveValue;
class QualifiedName;
enum SVGLengthNegativeValuesMode {
AllowNegativeLengths,
ForbidNegativeLengths
};
class SVGLengthValue {
WTF_MAKE_FAST_ALLOCATED;
public:
// FIXME: Once all SVGLengthValue users use Length internally, we make this a wrapper for Length.
SVGLengthValue(SVGLengthMode = LengthModeOther, const String& valueAsString = String());
SVGLengthValue(const SVGLengthContext&, float, SVGLengthMode = LengthModeOther, SVGLengthType = LengthTypeNumber);
SVGLengthType unitType() const;
SVGLengthMode unitMode() const;
bool operator==(const SVGLengthValue&) const;
bool operator!=(const SVGLengthValue&) const;
static SVGLengthValue construct(SVGLengthMode, const String&, SVGParsingError&, SVGLengthNegativeValuesMode = AllowNegativeLengths);
float value(const SVGLengthContext&) const;
ExceptionOr<float> valueForBindings(const SVGLengthContext&) const;
ExceptionOr<void> setValue(float, const SVGLengthContext&);
ExceptionOr<void> setValue(const SVGLengthContext&, float, SVGLengthMode, SVGLengthType);
float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; }
void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value; }
float valueAsPercentage() const;
String valueAsString() const;
ExceptionOr<void> setValueAsString(const String&);
ExceptionOr<void> setValueAsString(const String&, SVGLengthMode);
ExceptionOr<void> newValueSpecifiedUnits(unsigned short, float valueInSpecifiedUnits);
ExceptionOr<void> convertToSpecifiedUnits(unsigned short, const SVGLengthContext&);
// Helper functions
bool isRelative() const
{
auto type = unitType();
return type == LengthTypePercentage || type == LengthTypeEMS || type == LengthTypeEXS;
}
bool isZero() const
{
return !m_valueInSpecifiedUnits;
}
static SVGLengthValue fromCSSPrimitiveValue(const CSSPrimitiveValue&);
static Ref<CSSPrimitiveValue> toCSSPrimitiveValue(const SVGLengthValue&);
static SVGLengthMode lengthModeForAnimatedLengthAttribute(const QualifiedName&);
SVGLengthValue blend(const SVGLengthValue& from, float progress) const
{
auto toType = unitType();
auto fromType = from.unitType();
if ((from.isZero() && isZero())
|| fromType == LengthTypeUnknown
|| toType == LengthTypeUnknown
|| (!from.isZero() && fromType != LengthTypePercentage && toType == LengthTypePercentage)
|| (!isZero() && fromType == LengthTypePercentage && toType != LengthTypePercentage)
|| (!from.isZero() && !isZero() && (fromType == LengthTypeEMS || fromType == LengthTypeEXS) && fromType != toType))
return *this;
SVGLengthValue length;
if (fromType == LengthTypePercentage || toType == LengthTypePercentage) {
auto fromPercent = from.valueAsPercentage() * 100;
auto toPercent = valueAsPercentage() * 100;
auto result = length.newValueSpecifiedUnits(LengthTypePercentage, WebCore::blend(fromPercent, toPercent, progress));
if (result.hasException())
return { };
return length;
}
if (fromType == toType || from.isZero() || isZero() || fromType == LengthTypeEMS || fromType == LengthTypeEXS) {
auto fromValue = from.valueInSpecifiedUnits();
auto toValue = valueInSpecifiedUnits();
if (isZero()) {
auto result = length.newValueSpecifiedUnits(fromType, WebCore::blend(fromValue, toValue, progress));
if (result.hasException())
return { };
} else {
auto result = length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue, progress));
if (result.hasException())
return { };
}
return length;
}
ASSERT(!isRelative());
ASSERT(!from.isRelative());
SVGLengthContext nonRelativeLengthContext(nullptr);
auto fromValueInUserUnits = nonRelativeLengthContext.convertValueToUserUnits(from.valueInSpecifiedUnits(), from.unitMode(), fromType);
if (fromValueInUserUnits.hasException())
return { };
auto fromValue = nonRelativeLengthContext.convertValueFromUserUnits(fromValueInUserUnits.releaseReturnValue(), unitMode(), toType);
if (fromValue.hasException())
return { };
float toValue = valueInSpecifiedUnits();
auto result = length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue.releaseReturnValue(), toValue, progress));
if (result.hasException())
return { };
return length;
}
private:
float m_valueInSpecifiedUnits { 0 };
unsigned m_unit;
};
template<> struct SVGPropertyTraits<SVGLengthValue> {
static SVGLengthValue initialValue() { return { }; }
static String toString(const SVGLengthValue& type) { return type.valueAsString(); }
};
WTF::TextStream& operator<<(WTF::TextStream&, const SVGLengthValue&);
} // namespace WebCore
|