1/*
2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 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#include "SVGLineElement.h"
23
24#include "Attribute.h"
25#include "FloatPoint.h"
26#include "RenderSVGPath.h"
27#include "RenderSVGResource.h"
28#include "SVGElementInstance.h"
29#include "SVGLength.h"
30#include "SVGNames.h"
31#include <wtf/NeverDestroyed.h>
32
33namespace WebCore {
34
35// Animated property definitions
36DEFINE_ANIMATED_LENGTH(SVGLineElement, SVGNames::x1Attr, X1, x1)
37DEFINE_ANIMATED_LENGTH(SVGLineElement, SVGNames::y1Attr, Y1, y1)
38DEFINE_ANIMATED_LENGTH(SVGLineElement, SVGNames::x2Attr, X2, x2)
39DEFINE_ANIMATED_LENGTH(SVGLineElement, SVGNames::y2Attr, Y2, y2)
40DEFINE_ANIMATED_BOOLEAN(SVGLineElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
41
42BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGLineElement)
43    REGISTER_LOCAL_ANIMATED_PROPERTY(x1)
44    REGISTER_LOCAL_ANIMATED_PROPERTY(y1)
45    REGISTER_LOCAL_ANIMATED_PROPERTY(x2)
46    REGISTER_LOCAL_ANIMATED_PROPERTY(y2)
47    REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
48    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
49END_REGISTER_ANIMATED_PROPERTIES
50
51inline SVGLineElement::SVGLineElement(const QualifiedName& tagName, Document& document)
52    : SVGGraphicsElement(tagName, document)
53    , m_x1(LengthModeWidth)
54    , m_y1(LengthModeHeight)
55    , m_x2(LengthModeWidth)
56    , m_y2(LengthModeHeight)
57{
58    ASSERT(hasTagName(SVGNames::lineTag));
59    registerAnimatedPropertiesForSVGLineElement();
60}
61
62PassRefPtr<SVGLineElement> SVGLineElement::create(const QualifiedName& tagName, Document& document)
63{
64    return adoptRef(new SVGLineElement(tagName, document));
65}
66
67bool SVGLineElement::isSupportedAttribute(const QualifiedName& attrName)
68{
69    static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
70    if (supportedAttributes.get().isEmpty()) {
71        SVGLangSpace::addSupportedAttributes(supportedAttributes);
72        SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
73        supportedAttributes.get().add(SVGNames::x1Attr);
74        supportedAttributes.get().add(SVGNames::x2Attr);
75        supportedAttributes.get().add(SVGNames::y1Attr);
76        supportedAttributes.get().add(SVGNames::y2Attr);
77    }
78    return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
79}
80
81void SVGLineElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
82{
83    SVGParsingError parseError = NoError;
84
85    if (!isSupportedAttribute(name))
86        SVGGraphicsElement::parseAttribute(name, value);
87    else if (name == SVGNames::x1Attr)
88        setX1BaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
89    else if (name == SVGNames::y1Attr)
90        setY1BaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
91    else if (name == SVGNames::x2Attr)
92        setX2BaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
93    else if (name == SVGNames::y2Attr)
94        setY2BaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
95    else if (SVGLangSpace::parseAttribute(name, value)
96             || SVGExternalResourcesRequired::parseAttribute(name, value)) {
97    } else
98        ASSERT_NOT_REACHED();
99
100    reportAttributeParsingError(parseError, name, value);
101}
102
103void SVGLineElement::svgAttributeChanged(const QualifiedName& attrName)
104{
105    if (!isSupportedAttribute(attrName)) {
106        SVGGraphicsElement::svgAttributeChanged(attrName);
107        return;
108    }
109
110    SVGElementInstance::InvalidationGuard invalidationGuard(this);
111
112    bool isLengthAttribute = attrName == SVGNames::x1Attr
113                          || attrName == SVGNames::y1Attr
114                          || attrName == SVGNames::x2Attr
115                          || attrName == SVGNames::y2Attr;
116
117    if (isLengthAttribute)
118        updateRelativeLengthsInformation();
119
120    RenderSVGShape* renderer = toRenderSVGShape(this->renderer());
121    if (!renderer)
122        return;
123
124    if (isLengthAttribute) {
125        renderer->setNeedsShapeUpdate();
126        RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
127        return;
128    }
129
130    if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
131        RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
132        return;
133    }
134
135    ASSERT_NOT_REACHED();
136}
137
138bool SVGLineElement::selfHasRelativeLengths() const
139{
140    return x1().isRelative()
141        || y1().isRelative()
142        || x2().isRelative()
143        || y2().isRelative();
144}
145
146}
147