6c396e822d4d8991b49918c8caa2d33e9a5a3d2f
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEColorMatrixElement.cpp
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 template<> const SVGEnumerationStringEntries& getStaticStringEntries<ColorMatrixType>()
33 {
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"));
41     }
42     return entries;
43 }
44
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))
50 {
51     ScriptWrappable::init(this);
52
53     addToPropertyMap(m_values);
54     addToPropertyMap(m_in1);
55     addToPropertyMap(m_type);
56 }
57
58 PassRefPtr<SVGFEColorMatrixElement> SVGFEColorMatrixElement::create(Document& document)
59 {
60     return adoptRef(new SVGFEColorMatrixElement(document));
61 }
62
63 bool SVGFEColorMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
64 {
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);
70     }
71     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
72 }
73
74 void SVGFEColorMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
75 {
76     if (!isSupportedAttribute(name)) {
77         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
78         return;
79     }
80
81     SVGParsingError parseError = NoError;
82
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);
89     else
90         ASSERT_NOT_REACHED();
91
92     reportAttributeParsingError(parseError, name, value);
93 }
94
95 bool SVGFEColorMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
96 {
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());
102
103     ASSERT_NOT_REACHED();
104     return false;
105 }
106
107 void SVGFEColorMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
108 {
109     if (!isSupportedAttribute(attrName)) {
110         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
111         return;
112     }
113
114     SVGElementInstance::InvalidationGuard invalidationGuard(this);
115
116     if (attrName == SVGNames::typeAttr || attrName == SVGNames::valuesAttr) {
117         primitiveAttributeChanged(attrName);
118         return;
119     }
120
121     if (attrName == SVGNames::inAttr) {
122         invalidate();
123         return;
124     }
125
126     ASSERT_NOT_REACHED();
127 }
128
129 PassRefPtr<FilterEffect> SVGFEColorMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
130 {
131     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
132
133     if (!input1)
134         return nullptr;
135
136     Vector<float> filterValues;
137     ColorMatrixType filterType = m_type->currentValue()->enumValue();
138
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);
145             break;
146         case FECOLORMATRIX_TYPE_HUEROTATE:
147             filterValues.append(0);
148             break;
149         case FECOLORMATRIX_TYPE_SATURATE:
150             filterValues.append(1);
151             break;
152         default:
153             break;
154         }
155     } else {
156         RefPtr<SVGNumberList> values = m_values->currentValue();
157         size_t size = values->length();
158
159         if ((filterType == FECOLORMATRIX_TYPE_MATRIX && size != 20)
160             || (filterType == FECOLORMATRIX_TYPE_HUEROTATE && size != 1)
161             || (filterType == FECOLORMATRIX_TYPE_SATURATE && size != 1))
162             return nullptr;
163
164         filterValues = values->toFloatVector();
165     }
166
167     RefPtr<FilterEffect> effect = FEColorMatrix::create(filter, filterType, filterValues);
168     effect->inputEffects().append(input1);
169     return effect.release();
170 }
171
172 } // namespace WebCore