diff options
Diffstat (limited to 'Source/WebCore/svg/SVGLength.h')
-rw-r--r-- | Source/WebCore/svg/SVGLength.h | 257 |
1 files changed, 127 insertions, 130 deletions
diff --git a/Source/WebCore/svg/SVGLength.h b/Source/WebCore/svg/SVGLength.h index 24f6907c2..352c2d1d7 100644 --- a/Source/WebCore/svg/SVGLength.h +++ b/Source/WebCore/svg/SVGLength.h @@ -1,46 +1,37 @@ /* - * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org> - * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> + * Copyright (C) 2016 Apple Inc. All rights reserved. * - * 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. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * 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. + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef SVGLength_h -#define SVGLength_h +#pragma once -#if ENABLE(SVG) -#include "AnimationUtilities.h" -#include "SVGLengthContext.h" -#include "SVGParsingError.h" -#include "SVGPropertyTraits.h" +#include "ExceptionCode.h" +#include "SVGLengthValue.h" +#include "SVGPropertyTearOff.h" namespace WebCore { -class CSSPrimitiveValue; -class QualifiedName; - -typedef int ExceptionCode; - -enum SVGLengthNegativeValuesMode { - AllowNegativeLengths, - ForbidNegativeLengths -}; - -class SVGLength { - WTF_MAKE_FAST_ALLOCATED; +class SVGLength : public SVGPropertyTearOff<SVGLengthValue> { public: // Forward declare these enums in the w3c naming scheme, for IDL generation enum { @@ -57,120 +48,126 @@ public: SVG_LENGTHTYPE_PC = LengthTypePC }; - SVGLength(SVGLengthMode = LengthModeOther, const String& valueAsString = String()); - SVGLength(const SVGLengthContext&, float, SVGLengthMode = LengthModeOther, SVGLengthType = LengthTypeNumber); - SVGLength(const SVGLength&); - - SVGLengthType unitType() const; - SVGLengthMode unitMode() const; - - bool operator==(const SVGLength&) const; - bool operator!=(const SVGLength&) const; - - static SVGLength construct(SVGLengthMode, const String&, SVGParsingError&, SVGLengthNegativeValuesMode = AllowNegativeLengths); - - float value(const SVGLengthContext&) const; - float value(const SVGLengthContext&, ExceptionCode&) const; - void setValue(float, const SVGLengthContext&, ExceptionCode&); - void setValue(const SVGLengthContext&, float, SVGLengthMode, SVGLengthType, ExceptionCode&); - - float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; } - void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value; } - - float valueAsPercentage() const; - - String valueAsString() const; - void setValueAsString(const String&, ExceptionCode&); - void setValueAsString(const String&, SVGLengthMode, ExceptionCode&); - - void newValueSpecifiedUnits(unsigned short, float valueInSpecifiedUnits, ExceptionCode&); - void convertToSpecifiedUnits(unsigned short, const SVGLengthContext&, ExceptionCode&); - - // Helper functions - inline bool isRelative() const + static Ref<SVGLength> create(SVGAnimatedProperty& animatedProperty, SVGPropertyRole role, SVGLengthValue& value) { - SVGLengthType type = unitType(); - return type == LengthTypePercentage || type == LengthTypeEMS || type == LengthTypeEXS; + return adoptRef(*new SVGLength(animatedProperty, role, value)); } - bool isZero() const - { - return !m_valueInSpecifiedUnits; + static Ref<SVGLength> create(const SVGLengthValue& initialValue = { }) + { + return adoptRef(*new SVGLength(initialValue)); } - static SVGLength fromCSSPrimitiveValue(CSSPrimitiveValue*); - static PassRefPtr<CSSPrimitiveValue> toCSSPrimitiveValue(const SVGLength&); - static SVGLengthMode lengthModeForAnimatedLengthAttribute(const QualifiedName&); - - SVGLength blend(const SVGLength& from, float progress) const + static Ref<SVGLength> create(const SVGLengthValue* initialValue) { - SVGLengthType toType = unitType(); - SVGLengthType 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; + return adoptRef(*new SVGLength(initialValue)); + } - SVGLength length; - ExceptionCode ec = 0; + template<typename T> static ExceptionOr<Ref<SVGLength>> create(ExceptionOr<T>&& initialValue) + { + if (initialValue.hasException()) + return initialValue.releaseException(); + return create(initialValue.releaseReturnValue()); + } - if (fromType == LengthTypePercentage || toType == LengthTypePercentage) { - float fromPercent = from.valueAsPercentage() * 100; - float toPercent = valueAsPercentage() * 100; - length.newValueSpecifiedUnits(LengthTypePercentage, WebCore::blend(fromPercent, toPercent, progress), ec); - if (ec) - return SVGLength(); - return length; - } + unsigned short unitType() + { + return propertyReference().unitType(); + } - if (fromType == toType || from.isZero() || isZero() || fromType == LengthTypeEMS || fromType == LengthTypeEXS) { - float fromValue = from.valueInSpecifiedUnits(); - float toValue = valueInSpecifiedUnits(); - if (isZero()) - length.newValueSpecifiedUnits(fromType, WebCore::blend(fromValue, toValue, progress), ec); - else - length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue, progress), ec); - if (ec) - return SVGLength(); - return length; - } + ExceptionOr<float> valueForBindings() + { + return propertyReference().valueForBindings(SVGLengthContext { contextElement() }); + } - ASSERT(!isRelative()); - ASSERT(!from.isRelative()); + ExceptionOr<void> setValueForBindings(float value) + { + if (isReadOnly()) + return Exception { NO_MODIFICATION_ALLOWED_ERR }; + + auto result = propertyReference().setValue(value, SVGLengthContext { contextElement() }); + if (result.hasException()) + return result; + + commitChange(); + return result; + } + + float valueInSpecifiedUnits() + { + return propertyReference().valueInSpecifiedUnits(); + } - SVGLengthContext nonRelativeLengthContext(0); - float fromValueInUserUnits = nonRelativeLengthContext.convertValueToUserUnits(from.valueInSpecifiedUnits(), from.unitMode(), fromType, ec); - if (ec) - return SVGLength(); + ExceptionOr<void> setValueInSpecifiedUnits(float valueInSpecifiedUnits) + { + if (isReadOnly()) + return Exception { NO_MODIFICATION_ALLOWED_ERR }; - float fromValue = nonRelativeLengthContext.convertValueFromUserUnits(fromValueInUserUnits, unitMode(), toType, ec); - if (ec) - return SVGLength(); + propertyReference().setValueInSpecifiedUnits(valueInSpecifiedUnits); + commitChange(); + + return { }; + } + + String valueAsString() + { + return propertyReference().valueAsString(); + } - float toValue = valueInSpecifiedUnits(); - length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue, progress), ec); + ExceptionOr<void> setValueAsString(const String& value) + { + if (isReadOnly()) + return Exception { NO_MODIFICATION_ALLOWED_ERR }; + + auto result = propertyReference().setValueAsString(value); + if (result.hasException()) + return result; + + commitChange(); + return result; + } - if (ec) - return SVGLength(); - return length; + ExceptionOr<void> newValueSpecifiedUnits(unsigned short unitType, float valueInSpecifiedUnits) + { + if (isReadOnly()) + return Exception { NO_MODIFICATION_ALLOWED_ERR }; + + auto result = propertyReference().newValueSpecifiedUnits(unitType, valueInSpecifiedUnits); + if (result.hasException()) + return result; + + commitChange(); + return result; + } + + ExceptionOr<void> convertToSpecifiedUnits(unsigned short unitType) + { + if (isReadOnly()) + return Exception { NO_MODIFICATION_ALLOWED_ERR }; + + auto result = propertyReference().convertToSpecifiedUnits(unitType, SVGLengthContext { contextElement() }); + if (result.hasException()) + return result; + + commitChange(); + return result; } private: - float m_valueInSpecifiedUnits; - unsigned int m_unit; -}; + SVGLength(SVGAnimatedProperty& animatedProperty, SVGPropertyRole role, SVGLengthValue& value) + : SVGPropertyTearOff<SVGLengthValue>(&animatedProperty, role, value) + { + } -template<> -struct SVGPropertyTraits<SVGLength> { - static SVGLength initialValue() { return SVGLength(); } - static String toString(const SVGLength& type) { return type.valueAsString(); } -}; + explicit SVGLength(const SVGLengthValue& initialValue) + : SVGPropertyTearOff<SVGLengthValue>(initialValue) + { + } + explicit SVGLength(const SVGLengthValue* initialValue) + : SVGPropertyTearOff<SVGLengthValue>(initialValue) + { + } +}; } // namespace WebCore - -#endif // ENABLE(SVG) -#endif // SVGLength_h |