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 #include "core/svg/SVGFEColorMatrixElement.h"
     24 
     25 #include "core/SVGNames.h"
     26 #include "platform/graphics/filters/FilterEffect.h"
     27 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
     28 
     29 namespace blink {
     30 
     31 template<> const SVGEnumerationStringEntries& getStaticStringEntries<ColorMatrixType>()
     32 {
     33     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
     34     if (entries.isEmpty()) {
     35         entries.append(std::make_pair(FECOLORMATRIX_TYPE_MATRIX, "matrix"));
     36         entries.append(std::make_pair(FECOLORMATRIX_TYPE_SATURATE, "saturate"));
     37         entries.append(std::make_pair(FECOLORMATRIX_TYPE_HUEROTATE, "hueRotate"));
     38         entries.append(std::make_pair(FECOLORMATRIX_TYPE_LUMINANCETOALPHA, "luminanceToAlpha"));
     39     }
     40     return entries;
     41 }
     42 
     43 inline SVGFEColorMatrixElement::SVGFEColorMatrixElement(Document& document)
     44     : SVGFilterPrimitiveStandardAttributes(SVGNames::feColorMatrixTag, document)
     45     , m_values(SVGAnimatedNumberList::create(this, SVGNames::valuesAttr, SVGNumberList::create()))
     46     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
     47     , m_type(SVGAnimatedEnumeration<ColorMatrixType>::create(this, SVGNames::typeAttr, FECOLORMATRIX_TYPE_MATRIX))
     48 {
     49     addToPropertyMap(m_values);
     50     addToPropertyMap(m_in1);
     51     addToPropertyMap(m_type);
     52 }
     53 
     54 DEFINE_NODE_FACTORY(SVGFEColorMatrixElement)
     55 
     56 bool SVGFEColorMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
     57 {
     58     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
     59     if (supportedAttributes.isEmpty()) {
     60         supportedAttributes.add(SVGNames::typeAttr);
     61         supportedAttributes.add(SVGNames::valuesAttr);
     62         supportedAttributes.add(SVGNames::inAttr);
     63     }
     64     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
     65 }
     66 
     67 void SVGFEColorMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
     68 {
     69     parseAttributeNew(name, value);
     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(m_type->currentValue()->enumValue());
     77     if (attrName == SVGNames::valuesAttr)
     78         return colorMatrix->setValues(m_values->currentValue()->toFloatVector());
     79 
     80     ASSERT_NOT_REACHED();
     81     return false;
     82 }
     83 
     84 void SVGFEColorMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
     85 {
     86     if (!isSupportedAttribute(attrName)) {
     87         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
     88         return;
     89     }
     90 
     91     SVGElement::InvalidationGuard invalidationGuard(this);
     92 
     93     if (attrName == SVGNames::typeAttr || attrName == SVGNames::valuesAttr) {
     94         primitiveAttributeChanged(attrName);
     95         return;
     96     }
     97 
     98     if (attrName == SVGNames::inAttr) {
     99         invalidate();
    100         return;
    101     }
    102 
    103     ASSERT_NOT_REACHED();
    104 }
    105 
    106 PassRefPtr<FilterEffect> SVGFEColorMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
    107 {
    108     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
    109 
    110     if (!input1)
    111         return nullptr;
    112 
    113     Vector<float> filterValues;
    114     ColorMatrixType filterType = m_type->currentValue()->enumValue();
    115 
    116     // Use defaults if values is empty (SVG 1.1 15.10).
    117     if (!hasAttribute(SVGNames::valuesAttr)) {
    118         switch (filterType) {
    119         case FECOLORMATRIX_TYPE_MATRIX:
    120             for (size_t i = 0; i < 20; i++)
    121                 filterValues.append((i % 6) ? 0 : 1);
    122             break;
    123         case FECOLORMATRIX_TYPE_HUEROTATE:
    124             filterValues.append(0);
    125             break;
    126         case FECOLORMATRIX_TYPE_SATURATE:
    127             filterValues.append(1);
    128             break;
    129         default:
    130             break;
    131         }
    132     } else {
    133         RefPtr<SVGNumberList> values = m_values->currentValue();
    134         size_t size = values->length();
    135 
    136         if ((filterType == FECOLORMATRIX_TYPE_MATRIX && size != 20)
    137             || (filterType == FECOLORMATRIX_TYPE_HUEROTATE && size != 1)
    138             || (filterType == FECOLORMATRIX_TYPE_SATURATE && size != 1))
    139             return nullptr;
    140 
    141         filterValues = values->toFloatVector();
    142     }
    143 
    144     RefPtr<FilterEffect> effect = FEColorMatrix::create(filter, filterType, filterValues);
    145     effect->inputEffects().append(input1);
    146     return effect.release();
    147 }
    148 
    149 } // namespace blink
    150