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 // Animated property definitions
33 DEFINE_ANIMATED_ENUMERATION(SVGFEColorMatrixElement, SVGNames::typeAttr, Type, type, ColorMatrixType)
35 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEColorMatrixElement)
36 REGISTER_LOCAL_ANIMATED_PROPERTY(type)
37 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
38 END_REGISTER_ANIMATED_PROPERTIES
40 inline SVGFEColorMatrixElement::SVGFEColorMatrixElement(Document& document)
41 : SVGFilterPrimitiveStandardAttributes(SVGNames::feColorMatrixTag, document)
42 , m_values(SVGAnimatedNumberList::create(this, SVGNames::valuesAttr, SVGNumberList::create()))
43 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
44 , m_type(FECOLORMATRIX_TYPE_MATRIX)
46 ScriptWrappable::init(this);
48 addToPropertyMap(m_values);
49 addToPropertyMap(m_in1);
50 registerAnimatedPropertiesForSVGFEColorMatrixElement();
53 PassRefPtr<SVGFEColorMatrixElement> SVGFEColorMatrixElement::create(Document& document)
55 return adoptRef(new SVGFEColorMatrixElement(document));
58 bool SVGFEColorMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
60 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
61 if (supportedAttributes.isEmpty()) {
62 supportedAttributes.add(SVGNames::typeAttr);
63 supportedAttributes.add(SVGNames::valuesAttr);
64 supportedAttributes.add(SVGNames::inAttr);
66 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
69 void SVGFEColorMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
71 if (!isSupportedAttribute(name)) {
72 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
76 if (name == SVGNames::typeAttr) {
77 ColorMatrixType propertyValue = SVGPropertyTraits<ColorMatrixType>::fromString(value);
78 if (propertyValue > 0)
79 setTypeBaseValue(propertyValue);
83 SVGParsingError parseError = NoError;
85 if (name == SVGNames::inAttr)
86 m_in1->setBaseValueAsString(value, parseError);
87 else if (name == SVGNames::valuesAttr)
88 m_values->setBaseValueAsString(value, parseError);
92 reportAttributeParsingError(parseError, name, value);
95 bool SVGFEColorMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
97 FEColorMatrix* colorMatrix = static_cast<FEColorMatrix*>(effect);
98 if (attrName == SVGNames::typeAttr)
99 return colorMatrix->setType(typeCurrentValue());
100 if (attrName == SVGNames::valuesAttr)
101 return colorMatrix->setValues(m_values->currentValue()->toFloatVector());
103 ASSERT_NOT_REACHED();
107 void SVGFEColorMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
109 if (!isSupportedAttribute(attrName)) {
110 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
114 SVGElementInstance::InvalidationGuard invalidationGuard(this);
116 if (attrName == SVGNames::typeAttr || attrName == SVGNames::valuesAttr) {
117 primitiveAttributeChanged(attrName);
121 if (attrName == SVGNames::inAttr) {
126 ASSERT_NOT_REACHED();
129 PassRefPtr<FilterEffect> SVGFEColorMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
131 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
136 Vector<float> filterValues;
137 ColorMatrixType filterType = typeCurrentValue();
139 // Use defaults if values is empty (SVG 1.1 15.10).
140 if (!hasAttribute(SVGNames::valuesAttr)) {
141 switch (filterType) {
142 case FECOLORMATRIX_TYPE_MATRIX:
143 for (size_t i = 0; i < 20; i++)
144 filterValues.append((i % 6) ? 0 : 1);
146 case FECOLORMATRIX_TYPE_HUEROTATE:
147 filterValues.append(0);
149 case FECOLORMATRIX_TYPE_SATURATE:
150 filterValues.append(1);
156 RefPtr<SVGNumberList> values = m_values->currentValue();
157 size_t size = values->numberOfItems();
159 if ((filterType == FECOLORMATRIX_TYPE_MATRIX && size != 20)
160 || (filterType == FECOLORMATRIX_TYPE_HUEROTATE && size != 1)
161 || (filterType == FECOLORMATRIX_TYPE_SATURATE && size != 1))
164 filterValues = values->toFloatVector();
167 RefPtr<FilterEffect> effect = FEColorMatrix::create(filter, filterType, filterValues);
168 effect->inputEffects().append(input1);
169 return effect.release();
172 } // namespace WebCore