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_UNKNOWN, emptyString()));
37 entries.append(std::make_pair(FECOLORMATRIX_TYPE_MATRIX, "matrix"));
38 entries.append(std::make_pair(FECOLORMATRIX_TYPE_SATURATE, "saturate"));
39 entries.append(std::make_pair(FECOLORMATRIX_TYPE_HUEROTATE, "hueRotate"));
40 entries.append(std::make_pair(FECOLORMATRIX_TYPE_LUMINANCETOALPHA, "luminanceToAlpha"));
45 inline SVGFEColorMatrixElement::SVGFEColorMatrixElement(Document& document)
46 : SVGFilterPrimitiveStandardAttributes(SVGNames::feColorMatrixTag, document)
47 , m_values(SVGAnimatedNumberList::create(this, SVGNames::valuesAttr, SVGNumberList::create()))
48 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
49 , m_type(SVGAnimatedEnumeration<ColorMatrixType>::create(this, SVGNames::typeAttr, FECOLORMATRIX_TYPE_MATRIX))
51 ScriptWrappable::init(this);
53 addToPropertyMap(m_values);
54 addToPropertyMap(m_in1);
55 addToPropertyMap(m_type);
58 PassRefPtr<SVGFEColorMatrixElement> SVGFEColorMatrixElement::create(Document& document)
60 return adoptRef(new SVGFEColorMatrixElement(document));
63 bool SVGFEColorMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
65 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
66 if (supportedAttributes.isEmpty()) {
67 supportedAttributes.add(SVGNames::typeAttr);
68 supportedAttributes.add(SVGNames::valuesAttr);
69 supportedAttributes.add(SVGNames::inAttr);
71 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
74 void SVGFEColorMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
76 if (!isSupportedAttribute(name)) {
77 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
81 SVGParsingError parseError = NoError;
83 if (name == SVGNames::inAttr)
84 m_in1->setBaseValueAsString(value, parseError);
85 else if (name == SVGNames::valuesAttr)
86 m_values->setBaseValueAsString(value, parseError);
87 else if (name == SVGNames::typeAttr)
88 m_type->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(m_type->currentValue()->enumValue());
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 = m_type->currentValue()->enumValue();
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->length();
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