1/*
2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) 2018-2019 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23#include "SVGFEColorMatrixElement.h"
24
25#include "FilterEffect.h"
26#include "SVGFilterBuilder.h"
27#include "SVGNames.h"
28#include <wtf/IsoMallocInlines.h>
29
30namespace WebCore {
31
32WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFEColorMatrixElement);
33
34inline SVGFEColorMatrixElement::SVGFEColorMatrixElement(const QualifiedName& tagName, Document& document)
35 : SVGFilterPrimitiveStandardAttributes(tagName, document)
36{
37 ASSERT(hasTagName(SVGNames::feColorMatrixTag));
38
39 static std::once_flag onceFlag;
40 std::call_once(onceFlag, [] {
41 PropertyRegistry::registerProperty<SVGNames::inAttr, &SVGFEColorMatrixElement::m_in1>();
42 PropertyRegistry::registerProperty<SVGNames::typeAttr, ColorMatrixType, &SVGFEColorMatrixElement::m_type>();
43 PropertyRegistry::registerProperty<SVGNames::valuesAttr, &SVGFEColorMatrixElement::m_values>();
44 });
45}
46
47Ref<SVGFEColorMatrixElement> SVGFEColorMatrixElement::create(const QualifiedName& tagName, Document& document)
48{
49 return adoptRef(*new SVGFEColorMatrixElement(tagName, document));
50}
51
52void SVGFEColorMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
53{
54 if (name == SVGNames::typeAttr) {
55 auto propertyValue = SVGPropertyTraits<ColorMatrixType>::fromString(value);
56 if (propertyValue > 0)
57 m_type->setBaseValInternal<ColorMatrixType>(propertyValue);
58 return;
59 }
60
61 if (name == SVGNames::inAttr) {
62 m_in1->setBaseValInternal(value);
63 return;
64 }
65
66 if (name == SVGNames::valuesAttr) {
67 m_values->baseVal()->parse(value);
68 return;
69 }
70
71 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
72}
73
74bool SVGFEColorMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
75{
76 FEColorMatrix* colorMatrix = static_cast<FEColorMatrix*>(effect);
77 if (attrName == SVGNames::typeAttr)
78 return colorMatrix->setType(type());
79 if (attrName == SVGNames::valuesAttr)
80 return colorMatrix->setValues(values());
81
82 ASSERT_NOT_REACHED();
83 return false;
84}
85
86void SVGFEColorMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
87{
88 if (attrName == SVGNames::typeAttr || attrName == SVGNames::valuesAttr) {
89 InstanceInvalidationGuard guard(*this);
90 primitiveAttributeChanged(attrName);
91 return;
92 }
93
94 if (attrName == SVGNames::inAttr) {
95 InstanceInvalidationGuard guard(*this);
96 invalidate();
97 return;
98 }
99
100 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
101}
102
103RefPtr<FilterEffect> SVGFEColorMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter& filter) const
104{
105 auto input1 = filterBuilder->getEffectById(in1());
106
107 if (!input1)
108 return nullptr;
109
110 Vector<float> filterValues;
111 ColorMatrixType filterType = type();
112
113 // Use defaults if values is empty (SVG 1.1 15.10).
114 if (!hasAttribute(SVGNames::valuesAttr)) {
115 switch (filterType) {
116 case FECOLORMATRIX_TYPE_MATRIX:
117 for (size_t i = 0; i < 20; i++)
118 filterValues.append((i % 6) ? 0 : 1);
119 break;
120 case FECOLORMATRIX_TYPE_HUEROTATE:
121 filterValues.append(0);
122 break;
123 case FECOLORMATRIX_TYPE_SATURATE:
124 filterValues.append(1);
125 break;
126 default:
127 break;
128 }
129 } else {
130 unsigned size = values().size();
131
132 if ((filterType == FECOLORMATRIX_TYPE_MATRIX && size != 20)
133 || (filterType == FECOLORMATRIX_TYPE_HUEROTATE && size != 1)
134 || (filterType == FECOLORMATRIX_TYPE_SATURATE && size != 1))
135 return nullptr;
136
137 filterValues = values();
138 }
139
140 auto effect = FEColorMatrix::create(filter, filterType, filterValues);
141 effect->inputEffects().append(input1);
142 return effect;
143}
144
145} // namespace WebCore
146