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 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/SVGFEComponentTransferElement.h"
     24 
     25 #include "core/SVGNames.h"
     26 #include "core/dom/ElementTraversal.h"
     27 #include "core/svg/SVGFEFuncAElement.h"
     28 #include "core/svg/SVGFEFuncBElement.h"
     29 #include "core/svg/SVGFEFuncGElement.h"
     30 #include "core/svg/SVGFEFuncRElement.h"
     31 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
     32 #include "platform/graphics/filters/FilterEffect.h"
     33 
     34 namespace blink {
     35 
     36 inline SVGFEComponentTransferElement::SVGFEComponentTransferElement(Document& document)
     37     : SVGFilterPrimitiveStandardAttributes(SVGNames::feComponentTransferTag, document)
     38     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
     39 {
     40     addToPropertyMap(m_in1);
     41 }
     42 
     43 DEFINE_NODE_FACTORY(SVGFEComponentTransferElement)
     44 
     45 bool SVGFEComponentTransferElement::isSupportedAttribute(const QualifiedName& attrName)
     46 {
     47     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
     48     if (supportedAttributes.isEmpty())
     49         supportedAttributes.add(SVGNames::inAttr);
     50     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
     51 }
     52 
     53 void SVGFEComponentTransferElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
     54 {
     55     if (!isSupportedAttribute(name)) {
     56         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
     57         return;
     58     }
     59 
     60     SVGParsingError parseError = NoError;
     61 
     62     if (name == SVGNames::inAttr)
     63         m_in1->setBaseValueAsString(value, parseError);
     64     else
     65         ASSERT_NOT_REACHED();
     66 
     67     reportAttributeParsingError(parseError, name, value);
     68 }
     69 
     70 PassRefPtr<FilterEffect> SVGFEComponentTransferElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
     71 {
     72     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
     73 
     74     if (!input1)
     75         return nullptr;
     76 
     77     ComponentTransferFunction red;
     78     ComponentTransferFunction green;
     79     ComponentTransferFunction blue;
     80     ComponentTransferFunction alpha;
     81 
     82     for (SVGElement* element = Traversal<SVGElement>::firstChild(*this); element; element = Traversal<SVGElement>::nextSibling(*element)) {
     83         if (isSVGFEFuncRElement(*element))
     84             red = toSVGFEFuncRElement(*element).transferFunction();
     85         else if (isSVGFEFuncGElement(*element))
     86             green = toSVGFEFuncGElement(*element).transferFunction();
     87         else if (isSVGFEFuncBElement(*element))
     88             blue = toSVGFEFuncBElement(*element).transferFunction();
     89         else if (isSVGFEFuncAElement(*element))
     90             alpha = toSVGFEFuncAElement(*element).transferFunction();
     91     }
     92 
     93     RefPtr<FilterEffect> effect = FEComponentTransfer::create(filter, red, green, blue, alpha);
     94     effect->inputEffects().append(input1);
     95     return effect.release();
     96 }
     97 
     98 }
     99