Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2009 Dirk Schulze <krit (at) webkit.org>
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  */
     19 
     20 #include "config.h"
     21 
     22 #include "core/svg/SVGFEMorphologyElement.h"
     23 
     24 #include "core/SVGNames.h"
     25 #include "platform/graphics/filters/FilterEffect.h"
     26 #include "core/svg/SVGParserUtilities.h"
     27 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
     28 
     29 namespace blink {
     30 
     31 template<> const SVGEnumerationStringEntries& getStaticStringEntries<MorphologyOperatorType>()
     32 {
     33     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
     34     if (entries.isEmpty()) {
     35         entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_ERODE, "erode"));
     36         entries.append(std::make_pair(FEMORPHOLOGY_OPERATOR_DILATE, "dilate"));
     37     }
     38     return entries;
     39 }
     40 
     41 inline SVGFEMorphologyElement::SVGFEMorphologyElement(Document& document)
     42     : SVGFilterPrimitiveStandardAttributes(SVGNames::feMorphologyTag, document)
     43     , m_radius(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::radiusAttr))
     44     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
     45     , m_svgOperator(SVGAnimatedEnumeration<MorphologyOperatorType>::create(this, SVGNames::operatorAttr, FEMORPHOLOGY_OPERATOR_ERODE))
     46 {
     47     addToPropertyMap(m_radius);
     48     addToPropertyMap(m_in1);
     49     addToPropertyMap(m_svgOperator);
     50 }
     51 
     52 DEFINE_NODE_FACTORY(SVGFEMorphologyElement)
     53 
     54 bool SVGFEMorphologyElement::isSupportedAttribute(const QualifiedName& attrName)
     55 {
     56     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
     57     if (supportedAttributes.isEmpty()) {
     58         supportedAttributes.add(SVGNames::inAttr);
     59         supportedAttributes.add(SVGNames::operatorAttr);
     60         supportedAttributes.add(SVGNames::radiusAttr);
     61     }
     62     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
     63 }
     64 
     65 void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
     66 {
     67     if (!isSupportedAttribute(name)) {
     68         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
     69         return;
     70     }
     71 
     72     SVGParsingError parseError = NoError;
     73 
     74     if (name == SVGNames::inAttr)
     75         m_in1->setBaseValueAsString(value, parseError);
     76     else if (name == SVGNames::radiusAttr)
     77         m_radius->setBaseValueAsString(value, parseError);
     78     else if (name == SVGNames::operatorAttr)
     79         m_svgOperator->setBaseValueAsString(value, parseError);
     80     else
     81         ASSERT_NOT_REACHED();
     82 
     83     reportAttributeParsingError(parseError, name, value);
     84 }
     85 
     86 bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
     87 {
     88     FEMorphology* morphology = static_cast<FEMorphology*>(effect);
     89     if (attrName == SVGNames::operatorAttr)
     90         return morphology->setMorphologyOperator(m_svgOperator->currentValue()->enumValue());
     91     if (attrName == SVGNames::radiusAttr) {
     92         // Both setRadius functions should be evaluated separately.
     93         bool isRadiusXChanged = morphology->setRadiusX(radiusX()->currentValue()->value());
     94         bool isRadiusYChanged = morphology->setRadiusY(radiusY()->currentValue()->value());
     95         return isRadiusXChanged || isRadiusYChanged;
     96     }
     97 
     98     ASSERT_NOT_REACHED();
     99     return false;
    100 }
    101 
    102 void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
    103 {
    104     if (!isSupportedAttribute(attrName)) {
    105         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
    106         return;
    107     }
    108 
    109     SVGElement::InvalidationGuard invalidationGuard(this);
    110 
    111     if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
    112         primitiveAttributeChanged(attrName);
    113         return;
    114     }
    115 
    116     if (attrName == SVGNames::inAttr) {
    117         invalidate();
    118         return;
    119     }
    120 
    121     ASSERT_NOT_REACHED();
    122 }
    123 
    124 PassRefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
    125 {
    126     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
    127     float xRadius = radiusX()->currentValue()->value();
    128     float yRadius = radiusY()->currentValue()->value();
    129 
    130     if (!input1)
    131         return nullptr;
    132 
    133     if (xRadius < 0 || yRadius < 0)
    134         return nullptr;
    135 
    136     RefPtr<FilterEffect> effect = FEMorphology::create(filter, m_svgOperator->currentValue()->enumValue(), xRadius, yRadius);
    137     effect->inputEffects().append(input1);
    138     return effect.release();
    139 }
    140 
    141 } // namespace blink
    142