2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
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.
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.
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.
23 #include "core/svg/SVGFEColorMatrixElement.h"
26 #include "platform/graphics/filters/FilterEffect.h"
27 #include "core/svg/SVGElementInstance.h"
28 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
32 template<> const SVGEnumerationStringEntries& getStaticStringEntries<ColorMatrixType>()
34 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
35 if (entries.isEmpty()) {
36 entries.append(std::make_pair(FECOLORMATRIX_TYPE_MATRIX, "matrix"));
37 entries.append(std::make_pair(FECOLORMATRIX_TYPE_SATURATE, "saturate"));
38 entries.append(std::make_pair(FECOLORMATRIX_TYPE_HUEROTATE, "hueRotate"));
39 entries.append(std::make_pair(FECOLORMATRIX_TYPE_LUMINANCETOALPHA, "luminanceToAlpha"));
44 inline SVGFEColorMatrixElement::SVGFEColorMatrixElement(Document& document)
45 : SVGFilterPrimitiveStandardAttributes(SVGNames::feColorMatrixTag, document)
46 , m_values(SVGAnimatedNumberList::create(this, SVGNames::valuesAttr, SVGNumberList::create()))
47 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
48 , m_type(SVGAnimatedEnumeration<ColorMatrixType>::create(this, SVGNames::typeAttr, FECOLORMATRIX_TYPE_MATRIX))
50 ScriptWrappable::init(this);
52 addToPropertyMap(m_values);
53 addToPropertyMap(m_in1);
54 addToPropertyMap(m_type);
57 PassRefPtr<SVGFEColorMatrixElement> SVGFEColorMatrixElement::create(Document& document)
59 return adoptRef(new SVGFEColorMatrixElement(document));
62 bool SVGFEColorMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
64 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
65 if (supportedAttributes.isEmpty()) {
66 supportedAttributes.add(SVGNames::typeAttr);
67 supportedAttributes.add(SVGNames::valuesAttr);
68 supportedAttributes.add(SVGNames::inAttr);
70 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
73 void SVGFEColorMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
75 if (!isSupportedAttribute(name)) {
76 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
80 SVGParsingError parseError = NoError;
82 if (name == SVGNames::inAttr)
83 m_in1->setBaseValueAsString(value, parseError);
84 else if (name == SVGNames::valuesAttr)
85 m_values->setBaseValueAsString(value, parseError);
86 else if (name == SVGNames::typeAttr)
87 m_type->setBaseValueAsString(value, parseError);
91 reportAttributeParsingError(parseError, name, value);
94 bool SVGFEColorMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
96 FEColorMatrix* colorMatrix = static_cast<FEColorMatrix*>(effect);
97 if (attrName == SVGNames::typeAttr)
98 return colorMatrix->setType(m_type->currentValue()->enumValue());
99 if (attrName == SVGNames::valuesAttr)
100 return colorMatrix->setValues(m_values->currentValue()->toFloatVector());
102 ASSERT_NOT_REACHED();
106 void SVGFEColorMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
108 if (!isSupportedAttribute(attrName)) {
109 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
113 SVGElementInstance::InvalidationGuard invalidationGuard(this);
115 if (attrName == SVGNames::typeAttr || attrName == SVGNames::valuesAttr) {
116 primitiveAttributeChanged(attrName);
120 if (attrName == SVGNames::inAttr) {
125 ASSERT_NOT_REACHED();
128 PassRefPtr<FilterEffect> SVGFEColorMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
130 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
135 Vector<float> filterValues;
136 ColorMatrixType filterType = m_type->currentValue()->enumValue();
138 // Use defaults if values is empty (SVG 1.1 15.10).
139 if (!hasAttribute(SVGNames::valuesAttr)) {
140 switch (filterType) {
141 case FECOLORMATRIX_TYPE_MATRIX:
142 for (size_t i = 0; i < 20; i++)
143 filterValues.append((i % 6) ? 0 : 1);
145 case FECOLORMATRIX_TYPE_HUEROTATE:
146 filterValues.append(0);
148 case FECOLORMATRIX_TYPE_SATURATE:
149 filterValues.append(1);
155 RefPtr<SVGNumberList> values = m_values->currentValue();
156 size_t size = values->length();
158 if ((filterType == FECOLORMATRIX_TYPE_MATRIX && size != 20)
159 || (filterType == FECOLORMATRIX_TYPE_HUEROTATE && size != 1)
160 || (filterType == FECOLORMATRIX_TYPE_SATURATE && size != 1))
163 filterValues = values->toFloatVector();
166 RefPtr<FilterEffect> effect = FEColorMatrix::create(filter, filterType, filterValues);
167 effect->inputEffects().append(input1);
168 return effect.release();
171 } // namespace WebCore