Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2014 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      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
     13  * distribution.
     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.
     17  *
     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.
     29  */
     30 
     31 #ifndef SVGEnumeration_h
     32 #define SVGEnumeration_h
     33 
     34 #include "core/svg/properties/SVGProperty.h"
     35 
     36 namespace blink {
     37 
     38 class ExceptionState;
     39 
     40 class SVGEnumerationBase : public SVGPropertyBase {
     41 public:
     42     typedef std::pair<unsigned short, String> StringEntry;
     43     typedef Vector<StringEntry> StringEntries;
     44 
     45     // SVGEnumeration does not have a tear-off type.
     46     typedef void TearOffType;
     47     typedef unsigned short PrimitiveType;
     48 
     49     virtual ~SVGEnumerationBase();
     50 
     51     unsigned short value() const { return m_value <= maxExposedEnumValue() ? m_value : 0; }
     52     void setValue(unsigned short, ExceptionState&);
     53 
     54     // SVGPropertyBase:
     55     virtual PassRefPtr<SVGEnumerationBase> clone() const = 0;
     56     virtual PassRefPtr<SVGPropertyBase> cloneForAnimation(const String&) const OVERRIDE;
     57 
     58     virtual String valueAsString() const OVERRIDE;
     59     void setValueAsString(const String&, ExceptionState&);
     60 
     61     virtual void add(PassRefPtrWillBeRawPtr<SVGPropertyBase>, SVGElement*) OVERRIDE;
     62     virtual void calculateAnimatedValue(SVGAnimationElement*, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement*) OVERRIDE;
     63     virtual float calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement*) OVERRIDE;
     64 
     65     static AnimatedPropertyType classType() { return AnimatedEnumeration; }
     66 
     67     // Ensure that |SVGAnimatedEnumerationBase::setBaseVal| is used instead of |SVGAnimatedProperty<SVGEnumerationBase>::setBaseVal|.
     68     void setValue(unsigned short) { ASSERT_NOT_REACHED(); }
     69 
     70     static unsigned short valueOfLastEnum(const StringEntries& entries) { return entries.last().first; }
     71 
     72 protected:
     73     SVGEnumerationBase(unsigned short value, const StringEntries& entries, unsigned short maxExposed)
     74         : SVGPropertyBase(classType())
     75         , m_value(value)
     76         , m_maxExposed(maxExposed)
     77         , m_entries(entries)
     78     {
     79     }
     80 
     81     // This is the maximum value of all the internal enumeration values.
     82     // This assumes that |m_entries| are sorted.
     83     unsigned short maxInternalEnumValue() const { return valueOfLastEnum(m_entries); }
     84 
     85     // This is the maximum value that is exposed as an IDL constant on the relevant interface.
     86     unsigned short maxExposedEnumValue() const { return m_maxExposed; }
     87 
     88     // Used by SVGMarkerOrientEnumeration.
     89     virtual void notifyChange() { }
     90 
     91     unsigned short m_value;
     92     const unsigned short m_maxExposed;
     93     const StringEntries& m_entries;
     94 };
     95 typedef SVGEnumerationBase::StringEntries SVGEnumerationStringEntries;
     96 
     97 template<typename Enum> const SVGEnumerationStringEntries& getStaticStringEntries();
     98 template<typename Enum> unsigned short getMaxExposedEnumValue()
     99 {
    100     return SVGEnumerationBase::valueOfLastEnum(getStaticStringEntries<Enum>());
    101 }
    102 
    103 template<typename Enum>
    104 class SVGEnumeration : public SVGEnumerationBase {
    105 public:
    106     static PassRefPtr<SVGEnumeration<Enum> > create(Enum newValue)
    107     {
    108         return adoptRef(new SVGEnumeration<Enum>(newValue));
    109     }
    110 
    111     virtual ~SVGEnumeration()
    112     {
    113     }
    114 
    115     virtual PassRefPtr<SVGEnumerationBase> clone() const OVERRIDE
    116     {
    117         return create(enumValue());
    118     }
    119 
    120     Enum enumValue() const
    121     {
    122         ASSERT(m_value <= maxInternalEnumValue());
    123         return static_cast<Enum>(m_value);
    124     }
    125 
    126     void setEnumValue(Enum value)
    127     {
    128         m_value = value;
    129         notifyChange();
    130     }
    131 
    132 protected:
    133     explicit SVGEnumeration(Enum newValue)
    134         : SVGEnumerationBase(newValue, getStaticStringEntries<Enum>(), getMaxExposedEnumValue<Enum>())
    135     {
    136     }
    137 };
    138 
    139 } // namespace blink
    140 
    141 #endif // SVGEnumeration_h
    142