1/*
2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
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
24#if ENABLE(FILTERS)
25#include "SVGFilterPrimitiveStandardAttributes.h"
26
27#include "Attribute.h"
28#include "FilterEffect.h"
29#include "RenderSVGResourceFilterPrimitive.h"
30#include "SVGElement.h"
31#include "SVGElementInstance.h"
32#include "SVGFilterBuilder.h"
33#include "SVGLength.h"
34#include "SVGNames.h"
35#include "SVGUnitTypes.h"
36#include <wtf/NeverDestroyed.h>
37
38namespace WebCore {
39
40// Animated property definitions
41DEFINE_ANIMATED_LENGTH(SVGFilterPrimitiveStandardAttributes, SVGNames::xAttr, X, x)
42DEFINE_ANIMATED_LENGTH(SVGFilterPrimitiveStandardAttributes, SVGNames::yAttr, Y, y)
43DEFINE_ANIMATED_LENGTH(SVGFilterPrimitiveStandardAttributes, SVGNames::widthAttr, Width, width)
44DEFINE_ANIMATED_LENGTH(SVGFilterPrimitiveStandardAttributes, SVGNames::heightAttr, Height, height)
45DEFINE_ANIMATED_STRING(SVGFilterPrimitiveStandardAttributes, SVGNames::resultAttr, Result, result)
46
47BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
48    REGISTER_LOCAL_ANIMATED_PROPERTY(x)
49    REGISTER_LOCAL_ANIMATED_PROPERTY(y)
50    REGISTER_LOCAL_ANIMATED_PROPERTY(width)
51    REGISTER_LOCAL_ANIMATED_PROPERTY(height)
52    REGISTER_LOCAL_ANIMATED_PROPERTY(result)
53    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGElement)
54END_REGISTER_ANIMATED_PROPERTIES
55
56SVGFilterPrimitiveStandardAttributes::SVGFilterPrimitiveStandardAttributes(const QualifiedName& tagName, Document& document)
57    : SVGElement(tagName, document)
58    , m_x(LengthModeWidth, "0%")
59    , m_y(LengthModeHeight, "0%")
60    , m_width(LengthModeWidth, "100%")
61    , m_height(LengthModeHeight, "100%")
62{
63    // Spec: If the x/y attribute is not specified, the effect is as if a value of "0%" were specified.
64    // Spec: If the width/height attribute is not specified, the effect is as if a value of "100%" were specified.
65    registerAnimatedPropertiesForSVGFilterPrimitiveStandardAttributes();
66}
67
68bool SVGFilterPrimitiveStandardAttributes::isSupportedAttribute(const QualifiedName& attrName)
69{
70    static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
71    if (supportedAttributes.get().isEmpty()) {
72        supportedAttributes.get().add(SVGNames::xAttr);
73        supportedAttributes.get().add(SVGNames::yAttr);
74        supportedAttributes.get().add(SVGNames::widthAttr);
75        supportedAttributes.get().add(SVGNames::heightAttr);
76        supportedAttributes.get().add(SVGNames::resultAttr);
77    }
78    return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
79}
80
81void SVGFilterPrimitiveStandardAttributes::parseAttribute(const QualifiedName& name, const AtomicString& value)
82{
83    SVGParsingError parseError = NoError;
84
85    if (!isSupportedAttribute(name))
86        SVGElement::parseAttribute(name, value);
87    else if (name == SVGNames::xAttr)
88        setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
89    else if (name == SVGNames::yAttr)
90        setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
91    else if (name == SVGNames::widthAttr)
92        setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
93    else if (name == SVGNames::heightAttr)
94        setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
95    else if (name == SVGNames::resultAttr)
96        setResultBaseValue(value);
97    else
98        ASSERT_NOT_REACHED();
99
100    reportAttributeParsingError(parseError, name, value);
101}
102
103bool SVGFilterPrimitiveStandardAttributes::setFilterEffectAttribute(FilterEffect*, const QualifiedName&)
104{
105    // When all filters support this method, it will be changed to a pure virtual method.
106    ASSERT_NOT_REACHED();
107    return false;
108}
109
110void SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(const QualifiedName& attrName)
111{
112    if (!isSupportedAttribute(attrName)) {
113        SVGElement::svgAttributeChanged(attrName);
114        return;
115    }
116
117    SVGElementInstance::InvalidationGuard invalidationGuard(this);
118    invalidate();
119}
120
121void SVGFilterPrimitiveStandardAttributes::childrenChanged(const ChildChange& change)
122{
123    SVGElement::childrenChanged(change);
124
125    if (change.source == ChildChangeSourceParser)
126        return;
127    invalidate();
128}
129
130void SVGFilterPrimitiveStandardAttributes::setStandardAttributes(FilterEffect* filterEffect) const
131{
132    ASSERT(filterEffect);
133    if (!filterEffect)
134        return;
135
136    if (hasAttribute(SVGNames::xAttr))
137        filterEffect->setHasX(true);
138    if (hasAttribute(SVGNames::yAttr))
139        filterEffect->setHasY(true);
140    if (hasAttribute(SVGNames::widthAttr))
141        filterEffect->setHasWidth(true);
142    if (hasAttribute(SVGNames::heightAttr))
143        filterEffect->setHasHeight(true);
144}
145
146RenderPtr<RenderElement> SVGFilterPrimitiveStandardAttributes::createElementRenderer(PassRef<RenderStyle> style)
147{
148    return createRenderer<RenderSVGResourceFilterPrimitive>(*this, WTF::move(style));
149}
150
151bool SVGFilterPrimitiveStandardAttributes::rendererIsNeeded(const RenderStyle& style)
152{
153    if (parentNode() && (parentNode()->hasTagName(SVGNames::filterTag)))
154        return SVGElement::rendererIsNeeded(style);
155
156    return false;
157}
158
159void invalidateFilterPrimitiveParent(SVGElement* element)
160{
161    if (!element)
162        return;
163
164    ContainerNode* parent = element->parentNode();
165
166    if (!parent)
167        return;
168
169    RenderElement* renderer = parent->renderer();
170    if (!renderer || !renderer->isSVGResourceFilterPrimitive())
171        return;
172
173    RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer, false);
174}
175
176}
177
178#endif
179