1/*
2 * Copyright (C) 2006 Oliver Hunt <oliver@nerget.com>
3 * Copyright (C) 2018-2019 Apple Inc. All rights reserved.
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#pragma once
22
23#include "FEDisplacementMap.h"
24#include "SVGFilterPrimitiveStandardAttributes.h"
25
26namespace WebCore {
27
28template<>
29struct SVGPropertyTraits<ChannelSelectorType> {
30 static unsigned highestEnumValue() { return CHANNEL_A; }
31
32 static String toString(ChannelSelectorType type)
33 {
34 switch (type) {
35 case CHANNEL_UNKNOWN:
36 return emptyString();
37 case CHANNEL_R:
38 return "R"_s;
39 case CHANNEL_G:
40 return "G"_s;
41 case CHANNEL_B:
42 return "B"_s;
43 case CHANNEL_A:
44 return "A"_s;
45 }
46
47 ASSERT_NOT_REACHED();
48 return emptyString();
49 }
50
51 static ChannelSelectorType fromString(const String& value)
52 {
53 if (value == "R")
54 return CHANNEL_R;
55 if (value == "G")
56 return CHANNEL_G;
57 if (value == "B")
58 return CHANNEL_B;
59 if (value == "A")
60 return CHANNEL_A;
61 return CHANNEL_UNKNOWN;
62 }
63};
64
65class SVGFEDisplacementMapElement final : public SVGFilterPrimitiveStandardAttributes {
66 WTF_MAKE_ISO_ALLOCATED(SVGFEDisplacementMapElement);
67public:
68 static Ref<SVGFEDisplacementMapElement> create(const QualifiedName&, Document&);
69
70 static ChannelSelectorType stringToChannel(const String&);
71
72 String in1() const { return m_in1->currentValue(); }
73 String in2() const { return m_in2->currentValue(); }
74 ChannelSelectorType xChannelSelector() const { return m_xChannelSelector->currentValue<ChannelSelectorType>(); }
75 ChannelSelectorType yChannelSelector() const { return m_yChannelSelector->currentValue<ChannelSelectorType>(); }
76 float scale() const { return m_scale->currentValue(); }
77
78 SVGAnimatedString& in1Animated() { return m_in1; }
79 SVGAnimatedString& in2Animated() { return m_in2; }
80 SVGAnimatedEnumeration& xChannelSelectorAnimated() { return m_xChannelSelector; }
81 SVGAnimatedEnumeration& yChannelSelectorAnimated() { return m_yChannelSelector; }
82 SVGAnimatedNumber& scaleAnimated() { return m_scale; }
83
84private:
85 SVGFEDisplacementMapElement(const QualifiedName& tagName, Document&);
86
87 using PropertyRegistry = SVGPropertyOwnerRegistry<SVGFEDisplacementMapElement, SVGFilterPrimitiveStandardAttributes>;
88 const SVGPropertyRegistry& propertyRegistry() const final { return m_propertyRegistry; }
89
90 void parseAttribute(const QualifiedName&, const AtomicString&) override;
91 void svgAttributeChanged(const QualifiedName&) override;
92
93 bool setFilterEffectAttribute(FilterEffect*, const QualifiedName& attrName) override;
94 RefPtr<FilterEffect> build(SVGFilterBuilder*, Filter&) const override;
95
96 PropertyRegistry m_propertyRegistry { *this };
97 Ref<SVGAnimatedString> m_in1 { SVGAnimatedString::create(this) };
98 Ref<SVGAnimatedString> m_in2 { SVGAnimatedString::create(this) };
99 Ref<SVGAnimatedEnumeration> m_xChannelSelector { SVGAnimatedEnumeration::create(this, CHANNEL_A) };
100 Ref<SVGAnimatedEnumeration> m_yChannelSelector { SVGAnimatedEnumeration::create(this, CHANNEL_A) };
101 Ref<SVGAnimatedNumber> m_scale { SVGAnimatedNumber::create(this) };
102};
103
104} // namespace WebCore
105