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 "SVGFETurbulenceElement.h" |
24 | |
25 | #include "SVGNames.h" |
26 | #include "SVGParserUtilities.h" |
27 | #include <wtf/IsoMallocInlines.h> |
28 | |
29 | namespace WebCore { |
30 | |
31 | WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFETurbulenceElement); |
32 | |
33 | inline SVGFETurbulenceElement::SVGFETurbulenceElement(const QualifiedName& tagName, Document& document) |
34 | : SVGFilterPrimitiveStandardAttributes(tagName, document) |
35 | { |
36 | ASSERT(hasTagName(SVGNames::feTurbulenceTag)); |
37 | |
38 | static std::once_flag onceFlag; |
39 | std::call_once(onceFlag, [] { |
40 | PropertyRegistry::registerProperty<SVGNames::baseFrequencyAttr, &SVGFETurbulenceElement::m_baseFrequencyX, &SVGFETurbulenceElement::m_baseFrequencyY>(); |
41 | PropertyRegistry::registerProperty<SVGNames::numOctavesAttr, &SVGFETurbulenceElement::m_numOctaves>(); |
42 | PropertyRegistry::registerProperty<SVGNames::seedAttr, &SVGFETurbulenceElement::m_seed>(); |
43 | PropertyRegistry::registerProperty<SVGNames::stitchTilesAttr, SVGStitchOptions, &SVGFETurbulenceElement::m_stitchTiles>(); |
44 | PropertyRegistry::registerProperty<SVGNames::typeAttr, TurbulenceType, &SVGFETurbulenceElement::m_type>(); |
45 | }); |
46 | } |
47 | |
48 | Ref<SVGFETurbulenceElement> SVGFETurbulenceElement::create(const QualifiedName& tagName, Document& document) |
49 | { |
50 | return adoptRef(*new SVGFETurbulenceElement(tagName, document)); |
51 | } |
52 | |
53 | void SVGFETurbulenceElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
54 | { |
55 | if (name == SVGNames::typeAttr) { |
56 | TurbulenceType propertyValue = SVGPropertyTraits<TurbulenceType>::fromString(value); |
57 | if (propertyValue != TurbulenceType::Unknown) |
58 | m_type->setBaseValInternal<TurbulenceType>(propertyValue); |
59 | return; |
60 | } |
61 | |
62 | if (name == SVGNames::stitchTilesAttr) { |
63 | SVGStitchOptions propertyValue = SVGPropertyTraits<SVGStitchOptions>::fromString(value); |
64 | if (propertyValue > 0) |
65 | m_stitchTiles->setBaseValInternal<SVGStitchOptions>(propertyValue); |
66 | return; |
67 | } |
68 | |
69 | if (name == SVGNames::baseFrequencyAttr) { |
70 | float x, y; |
71 | if (parseNumberOptionalNumber(value, x, y)) { |
72 | m_baseFrequencyX->setBaseValInternal(x); |
73 | m_baseFrequencyY->setBaseValInternal(y); |
74 | } |
75 | return; |
76 | } |
77 | |
78 | if (name == SVGNames::seedAttr) { |
79 | m_seed->setBaseValInternal(value.toFloat()); |
80 | return; |
81 | } |
82 | |
83 | if (name == SVGNames::numOctavesAttr) { |
84 | m_numOctaves->setBaseValInternal(value.string().toUIntStrict()); |
85 | return; |
86 | } |
87 | |
88 | SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value); |
89 | } |
90 | |
91 | bool SVGFETurbulenceElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName) |
92 | { |
93 | FETurbulence* turbulence = static_cast<FETurbulence*>(effect); |
94 | if (attrName == SVGNames::typeAttr) |
95 | return turbulence->setType(type()); |
96 | if (attrName == SVGNames::stitchTilesAttr) |
97 | return turbulence->setStitchTiles(stitchTiles()); |
98 | if (attrName == SVGNames::baseFrequencyAttr) |
99 | return (turbulence->setBaseFrequencyX(baseFrequencyX()) || turbulence->setBaseFrequencyY(baseFrequencyY())); |
100 | if (attrName == SVGNames::seedAttr) |
101 | return turbulence->setSeed(seed()); |
102 | if (attrName == SVGNames::numOctavesAttr) |
103 | return turbulence->setNumOctaves(numOctaves()); |
104 | |
105 | ASSERT_NOT_REACHED(); |
106 | return false; |
107 | } |
108 | |
109 | void SVGFETurbulenceElement::svgAttributeChanged(const QualifiedName& attrName) |
110 | { |
111 | if (PropertyRegistry::isKnownAttribute(attrName)) { |
112 | InstanceInvalidationGuard guard(*this); |
113 | primitiveAttributeChanged(attrName); |
114 | return; |
115 | } |
116 | |
117 | SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName); |
118 | } |
119 | |
120 | RefPtr<FilterEffect> SVGFETurbulenceElement::build(SVGFilterBuilder*, Filter& filter) const |
121 | { |
122 | if (baseFrequencyX() < 0 || baseFrequencyY() < 0) |
123 | return nullptr; |
124 | return FETurbulence::create(filter, type(), baseFrequencyX(), baseFrequencyY(), numOctaves(), seed(), stitchTiles() == SVG_STITCHTYPE_STITCH); |
125 | } |
126 | |
127 | } |
128 | |