Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  * Copyright (C) 2004, 2005, 2006 Rob Buis <buis (at) 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 #include "config.h"
     22 
     23 #if ENABLE(SVG) && ENABLE(FILTERS)
     24 #include "SVGFEColorMatrixElement.h"
     25 
     26 #include "Attribute.h"
     27 #include "FilterEffect.h"
     28 #include "SVGFilterBuilder.h"
     29 #include "SVGNames.h"
     30 
     31 namespace WebCore {
     32 
     33 // Animated property definitions
     34 DEFINE_ANIMATED_STRING(SVGFEColorMatrixElement, SVGNames::inAttr, In1, in1)
     35 DEFINE_ANIMATED_ENUMERATION(SVGFEColorMatrixElement, SVGNames::typeAttr, Type, type)
     36 DEFINE_ANIMATED_NUMBER_LIST(SVGFEColorMatrixElement, SVGNames::valuesAttr, Values, values)
     37 
     38 inline SVGFEColorMatrixElement::SVGFEColorMatrixElement(const QualifiedName& tagName, Document* document)
     39     : SVGFilterPrimitiveStandardAttributes(tagName, document)
     40     , m_type(FECOLORMATRIX_TYPE_UNKNOWN)
     41 {
     42 }
     43 
     44 PassRefPtr<SVGFEColorMatrixElement> SVGFEColorMatrixElement::create(const QualifiedName& tagName, Document* document)
     45 {
     46     return adoptRef(new SVGFEColorMatrixElement(tagName, document));
     47 }
     48 
     49 void SVGFEColorMatrixElement::parseMappedAttribute(Attribute* attr)
     50 {
     51     const String& value = attr->value();
     52     if (attr->name() == SVGNames::typeAttr) {
     53         if (value == "matrix")
     54             setTypeBaseValue(FECOLORMATRIX_TYPE_MATRIX);
     55         else if (value == "saturate")
     56             setTypeBaseValue(FECOLORMATRIX_TYPE_SATURATE);
     57         else if (value == "hueRotate")
     58             setTypeBaseValue(FECOLORMATRIX_TYPE_HUEROTATE);
     59         else if (value == "luminanceToAlpha")
     60             setTypeBaseValue(FECOLORMATRIX_TYPE_LUMINANCETOALPHA);
     61     } else if (attr->name() == SVGNames::inAttr)
     62         setIn1BaseValue(value);
     63     else if (attr->name() == SVGNames::valuesAttr) {
     64         SVGNumberList newList;
     65         newList.parse(value);
     66         detachAnimatedValuesListWrappers(newList.size());
     67         setValuesBaseValue(newList);
     68     } else
     69         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
     70 }
     71 
     72 bool SVGFEColorMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
     73 {
     74     FEColorMatrix* colorMatrix = static_cast<FEColorMatrix*>(effect);
     75     if (attrName == SVGNames::typeAttr)
     76         return colorMatrix->setType(static_cast<ColorMatrixType>(type()));
     77     if (attrName == SVGNames::valuesAttr)
     78         return colorMatrix->setValues(values());
     79 
     80     ASSERT_NOT_REACHED();
     81     return false;
     82 }
     83 
     84 void SVGFEColorMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
     85 {
     86     SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
     87 
     88     if (attrName == SVGNames::typeAttr
     89         || attrName == SVGNames::valuesAttr)
     90         primitiveAttributeChanged(attrName);
     91     if (attrName == SVGNames::inAttr)
     92         invalidate();
     93 }
     94 
     95 void SVGFEColorMatrixElement::synchronizeProperty(const QualifiedName& attrName)
     96 {
     97     SVGFilterPrimitiveStandardAttributes::synchronizeProperty(attrName);
     98 
     99     if (attrName == anyQName()) {
    100         synchronizeType();
    101         synchronizeIn1();
    102         synchronizeValues();
    103         return;
    104     }
    105 
    106     if (attrName == SVGNames::typeAttr)
    107         synchronizeType();
    108     else if (attrName == SVGNames::inAttr)
    109         synchronizeIn1();
    110     else if (attrName == SVGNames::valuesAttr)
    111         synchronizeValues();
    112 }
    113 
    114 AttributeToPropertyTypeMap& SVGFEColorMatrixElement::attributeToPropertyTypeMap()
    115 {
    116     DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
    117     return s_attributeToPropertyTypeMap;
    118 }
    119 
    120 void SVGFEColorMatrixElement::fillAttributeToPropertyTypeMap()
    121 {
    122     AttributeToPropertyTypeMap& attributeToPropertyTypeMap = this->attributeToPropertyTypeMap();
    123 
    124     SVGFilterPrimitiveStandardAttributes::fillPassedAttributeToPropertyTypeMap(attributeToPropertyTypeMap);
    125     attributeToPropertyTypeMap.set(SVGNames::inAttr, AnimatedString);
    126     attributeToPropertyTypeMap.set(SVGNames::typeAttr, AnimatedEnumeration);
    127     attributeToPropertyTypeMap.set(SVGNames::valuesAttr, AnimatedNumberList);
    128 }
    129 
    130 PassRefPtr<FilterEffect> SVGFEColorMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
    131 {
    132     FilterEffect* input1 = filterBuilder->getEffectById(in1());
    133 
    134     if (!input1)
    135         return 0;
    136 
    137     Vector<float> filterValues;
    138     const ColorMatrixType filterType(static_cast<ColorMatrixType>(type()));
    139 
    140     // Use defaults if values is empty (SVG 1.1 15.10).
    141     if (!hasAttribute(SVGNames::valuesAttr)) {
    142         switch (filterType) {
    143         case FECOLORMATRIX_TYPE_MATRIX:
    144             for (size_t i = 0; i < 20; i++)
    145                 filterValues.append((i % 6) ? 0.0f : 1.0f);
    146             break;
    147         case FECOLORMATRIX_TYPE_HUEROTATE:
    148             filterValues.append(0.0f);
    149             break;
    150         case FECOLORMATRIX_TYPE_SATURATE:
    151             filterValues.append(1.0f);
    152             break;
    153         default:
    154             break;
    155         }
    156     } else {
    157         filterValues = values();
    158         unsigned size = filterValues.size();
    159 
    160         if ((filterType == FECOLORMATRIX_TYPE_MATRIX && size != 20)
    161             || (filterType == FECOLORMATRIX_TYPE_HUEROTATE && size != 1)
    162             || (filterType == FECOLORMATRIX_TYPE_SATURATE && (size != 1
    163                 || filterValues[0] < 0.0f || filterValues[0] > 1.0f)))
    164             return 0;
    165     }
    166 
    167     RefPtr<FilterEffect> effect = FEColorMatrix::create(filter, filterType, filterValues);
    168     effect->inputEffects().append(input1);
    169     return effect.release();
    170 }
    171 
    172 } // namespace WebCore
    173 
    174 #endif // ENABLE(SVG)
    175