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