1 /*
2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@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/SVGFEColorMatrixElement.h"
24
25 #include "SVGNames.h"
26 #include "platform/graphics/filters/FilterEffect.h"
27 #include "core/svg/SVGElementInstance.h"
28 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
29
30 namespace WebCore {
31
32 // Animated property definitions
DEFINE_ANIMATED_STRING(SVGFEColorMatrixElement,SVGNames::inAttr,In1,in1)33 DEFINE_ANIMATED_STRING(SVGFEColorMatrixElement, SVGNames::inAttr, In1, in1)
34 DEFINE_ANIMATED_ENUMERATION(SVGFEColorMatrixElement, SVGNames::typeAttr, Type, type, ColorMatrixType)
35 DEFINE_ANIMATED_NUMBER_LIST(SVGFEColorMatrixElement, SVGNames::valuesAttr, Values, values)
36
37 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEColorMatrixElement)
38 REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
39 REGISTER_LOCAL_ANIMATED_PROPERTY(type)
40 REGISTER_LOCAL_ANIMATED_PROPERTY(values)
41 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
42 END_REGISTER_ANIMATED_PROPERTIES
43
44 inline SVGFEColorMatrixElement::SVGFEColorMatrixElement(Document& document)
45 : SVGFilterPrimitiveStandardAttributes(SVGNames::feColorMatrixTag, document)
46 , m_type(FECOLORMATRIX_TYPE_MATRIX)
47 {
48 ScriptWrappable::init(this);
49 registerAnimatedPropertiesForSVGFEColorMatrixElement();
50 }
51
create(Document & document)52 PassRefPtr<SVGFEColorMatrixElement> SVGFEColorMatrixElement::create(Document& document)
53 {
54 return adoptRef(new SVGFEColorMatrixElement(document));
55 }
56
isSupportedAttribute(const QualifiedName & attrName)57 bool SVGFEColorMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
58 {
59 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
60 if (supportedAttributes.isEmpty()) {
61 supportedAttributes.add(SVGNames::typeAttr);
62 supportedAttributes.add(SVGNames::valuesAttr);
63 supportedAttributes.add(SVGNames::inAttr);
64 }
65 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
66 }
67
parseAttribute(const QualifiedName & name,const AtomicString & value)68 void SVGFEColorMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
69 {
70 if (!isSupportedAttribute(name)) {
71 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
72 return;
73 }
74
75 if (name == SVGNames::typeAttr) {
76 ColorMatrixType propertyValue = SVGPropertyTraits<ColorMatrixType>::fromString(value);
77 if (propertyValue > 0)
78 setTypeBaseValue(propertyValue);
79 return;
80 }
81
82 if (name == SVGNames::inAttr) {
83 setIn1BaseValue(value);
84 return;
85 }
86
87 if (name == SVGNames::valuesAttr) {
88 SVGNumberList newList;
89 newList.parse(value);
90 detachAnimatedValuesListWrappers(newList.size());
91 setValuesBaseValue(newList);
92 return;
93 }
94
95 ASSERT_NOT_REACHED();
96 }
97
setFilterEffectAttribute(FilterEffect * effect,const QualifiedName & attrName)98 bool SVGFEColorMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
99 {
100 FEColorMatrix* colorMatrix = static_cast<FEColorMatrix*>(effect);
101 if (attrName == SVGNames::typeAttr)
102 return colorMatrix->setType(typeCurrentValue());
103 if (attrName == SVGNames::valuesAttr)
104 return colorMatrix->setValues(valuesCurrentValue().toFloatVector());
105
106 ASSERT_NOT_REACHED();
107 return false;
108 }
109
svgAttributeChanged(const QualifiedName & attrName)110 void SVGFEColorMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
111 {
112 if (!isSupportedAttribute(attrName)) {
113 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
114 return;
115 }
116
117 SVGElementInstance::InvalidationGuard invalidationGuard(this);
118
119 if (attrName == SVGNames::typeAttr || attrName == SVGNames::valuesAttr) {
120 primitiveAttributeChanged(attrName);
121 return;
122 }
123
124 if (attrName == SVGNames::inAttr) {
125 invalidate();
126 return;
127 }
128
129 ASSERT_NOT_REACHED();
130 }
131
build(SVGFilterBuilder * filterBuilder,Filter * filter)132 PassRefPtr<FilterEffect> SVGFEColorMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
133 {
134 FilterEffect* input1 = filterBuilder->getEffectById(in1CurrentValue());
135
136 if (!input1)
137 return 0;
138
139 Vector<float> filterValues;
140 ColorMatrixType filterType = typeCurrentValue();
141
142 // Use defaults if values is empty (SVG 1.1 15.10).
143 if (!hasAttribute(SVGNames::valuesAttr)) {
144 switch (filterType) {
145 case FECOLORMATRIX_TYPE_MATRIX:
146 for (size_t i = 0; i < 20; i++)
147 filterValues.append((i % 6) ? 0 : 1);
148 break;
149 case FECOLORMATRIX_TYPE_HUEROTATE:
150 filterValues.append(0);
151 break;
152 case FECOLORMATRIX_TYPE_SATURATE:
153 filterValues.append(1);
154 break;
155 default:
156 break;
157 }
158 } else {
159 SVGNumberList& values = valuesCurrentValue();
160 unsigned size = values.size();
161
162 if ((filterType == FECOLORMATRIX_TYPE_MATRIX && size != 20)
163 || (filterType == FECOLORMATRIX_TYPE_HUEROTATE && size != 1)
164 || (filterType == FECOLORMATRIX_TYPE_SATURATE && size != 1))
165 return 0;
166
167 filterValues = values.toFloatVector();
168 }
169
170 RefPtr<FilterEffect> effect = FEColorMatrix::create(filter, filterType, filterValues);
171 effect->inputEffects().append(input1);
172 return effect.release();
173 }
174
175 } // namespace WebCore
176