• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • khtml
  • svg
SVGRadialGradientElement.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4
5 This file is part of the KDE project
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include "config.h"
24#include "wtf/Platform.h"
25
26#if ENABLE(SVG)
27#include "SVGRadialGradientElement.h"
28
29#include "FloatConversion.h"
30#include "FloatPoint.h"
31#include "RadialGradientAttributes.h"
32#include "RenderObject.h"
33#include "SVGLength.h"
34#include "SVGNames.h"
35#include "SVGPaintServerRadialGradient.h"
36#include "SVGStopElement.h"
37#include "SVGTransform.h"
38#include "SVGTransformList.h"
39#include "SVGUnitTypes.h"
40
41namespace WebCore {
42
43SVGRadialGradientElement::SVGRadialGradientElement(const QualifiedName& tagName, Document* doc)
44 : SVGGradientElement(tagName, doc)
45 , m_cx(this, LengthModeWidth)
46 , m_cy(this, LengthModeHeight)
47 , m_r(this, LengthModeOther)
48 , m_fx(this, LengthModeWidth)
49 , m_fy(this, LengthModeHeight)
50{
51 // Spec: If the attribute is not specified, the effect is as if a value of "50%" were specified.
52 setCxBaseValue(SVGLength(this, LengthModeWidth, "50%"));
53 setCyBaseValue(SVGLength(this, LengthModeHeight, "50%"));
54 setRBaseValue(SVGLength(this, LengthModeOther, "50%"));
55}
56
57SVGRadialGradientElement::~SVGRadialGradientElement()
58{
59}
60
61ANIMATED_PROPERTY_DEFINITIONS(SVGRadialGradientElement, SVGLength, Length, length, Cx, cx, SVGNames::cxAttr, m_cx)
62ANIMATED_PROPERTY_DEFINITIONS(SVGRadialGradientElement, SVGLength, Length, length, Cy, cy, SVGNames::cyAttr, m_cy)
63ANIMATED_PROPERTY_DEFINITIONS(SVGRadialGradientElement, SVGLength, Length, length, Fx, fx, SVGNames::fxAttr, m_fx)
64ANIMATED_PROPERTY_DEFINITIONS(SVGRadialGradientElement, SVGLength, Length, length, Fy, fy, SVGNames::fyAttr, m_fy)
65ANIMATED_PROPERTY_DEFINITIONS(SVGRadialGradientElement, SVGLength, Length, length, R, r, SVGNames::rAttr, m_r)
66
67void SVGRadialGradientElement::parseMappedAttribute(MappedAttribute* attr)
68{
69 if (attr->name() == SVGNames::cxAttr)
70 setCxBaseValue(SVGLength(this, LengthModeWidth, attr->value()));
71 else if (attr->name() == SVGNames::cyAttr)
72 setCyBaseValue(SVGLength(this, LengthModeHeight, attr->value()));
73 else if (attr->name() == SVGNames::rAttr) {
74 setRBaseValue(SVGLength(this, LengthModeOther, attr->value()));
75 if (r().value() < 0.0)
76 document()->accessSVGExtensions()->reportError("A negative value for radial gradient radius <r> is not allowed");
77 } else if (attr->name() == SVGNames::fxAttr)
78 setFxBaseValue(SVGLength(this, LengthModeWidth, attr->value()));
79 else if (attr->name() == SVGNames::fyAttr)
80 setFyBaseValue(SVGLength(this, LengthModeHeight, attr->value()));
81 else
82 SVGGradientElement::parseMappedAttribute(attr);
83}
84
85void SVGRadialGradientElement::svgAttributeChanged(const QualifiedName& attrName)
86{
87 SVGGradientElement::svgAttributeChanged(attrName);
88
89 if (!m_resource)
90 return;
91
92 if (attrName == SVGNames::cxAttr || attrName == SVGNames::cyAttr ||
93 attrName == SVGNames::fxAttr || attrName == SVGNames::fyAttr ||
94 attrName == SVGNames::rAttr)
95 m_resource->invalidate();
96}
97
98void SVGRadialGradientElement::buildGradient() const
99{
100 RadialGradientAttributes attributes = collectGradientProperties();
101
102 // If we didn't find any gradient containing stop elements, ignore the request.
103 if (attributes.stops().isEmpty())
104 return;
105
106 RefPtr<SVGPaintServerRadialGradient> radialGradient = WTF::static_pointer_cast<SVGPaintServerRadialGradient>(m_resource);
107
108 radialGradient->setGradientStops(attributes.stops());
109 radialGradient->setBoundingBoxMode(attributes.boundingBoxMode());
110 radialGradient->setGradientSpreadMethod(attributes.spreadMethod());
111 radialGradient->setGradientTransform(attributes.gradientTransform());
112 radialGradient->setGradientCenter(FloatPoint::narrowPrecision(attributes.cx(), attributes.cy()));
113 radialGradient->setGradientFocal(FloatPoint::narrowPrecision(attributes.fx(), attributes.fy()));
114 radialGradient->setGradientRadius(narrowPrecisionToFloat(attributes.r()));
115}
116
117RadialGradientAttributes SVGRadialGradientElement::collectGradientProperties() const
118{
119 RadialGradientAttributes attributes;
120 HashSet<const SVGGradientElement*> processedGradients;
121
122 bool isRadial = true;
123 const SVGGradientElement* current = this;
124
125 while (current) {
126 if (!attributes.hasSpreadMethod() && current->hasAttribute(SVGNames::spreadMethodAttr))
127 attributes.setSpreadMethod((SVGGradientSpreadMethod) current->spreadMethod());
128
129 if (!attributes.hasBoundingBoxMode() && current->hasAttribute(SVGNames::gradientUnitsAttr))
130 attributes.setBoundingBoxMode(current->getAttribute(SVGNames::gradientUnitsAttr) == "objectBoundingBox");
131
132 if (!attributes.hasGradientTransform() && current->hasAttribute(SVGNames::gradientTransformAttr))
133 attributes.setGradientTransform(current->gradientTransform()->consolidate().matrix());
134
135 if (!attributes.hasStops()) {
136 const Vector<SVGGradientStop>& stops(current->buildStops());
137 if (!stops.isEmpty())
138 attributes.setStops(stops);
139 }
140
141 if (isRadial) {
142 const SVGRadialGradientElement* radial = static_cast<const SVGRadialGradientElement*>(current);
143
144 if (!attributes.hasCx() && current->hasAttribute(SVGNames::cxAttr))
145 attributes.setCx(radial->cx().valueAsPercentage());
146
147 if (!attributes.hasCy() && current->hasAttribute(SVGNames::cyAttr))
148 attributes.setCy(radial->cy().valueAsPercentage());
149
150 if (!attributes.hasR() && current->hasAttribute(SVGNames::rAttr))
151 attributes.setR(radial->r().valueAsPercentage());
152
153 if (!attributes.hasFx() && current->hasAttribute(SVGNames::fxAttr))
154 attributes.setFx(radial->fx().valueAsPercentage());
155
156 if (!attributes.hasFy() && current->hasAttribute(SVGNames::fyAttr))
157 attributes.setFy(radial->fy().valueAsPercentage());
158 }
159
160 processedGradients.add(current);
161
162 // Respect xlink:href, take attributes from referenced element
163 Node* refNode = ownerDocument()->getElementById(SVGURIReference::getTarget(current->href()));
164 if (refNode && (refNode->hasTagName(SVGNames::radialGradientTag) || refNode->hasTagName(SVGNames::linearGradientTag))) {
165 current = static_cast<const SVGGradientElement*>(const_cast<const Node*>(refNode));
166
167 // Cycle detection
168 if (processedGradients.contains(current))
169 return RadialGradientAttributes();
170
171 isRadial = current->gradientType() == RadialGradientPaintServer;
172 } else
173 current = 0;
174 }
175
176 // Handle default values for fx/fy
177 if (!attributes.hasFx())
178 attributes.setFx(attributes.cx());
179
180 if (!attributes.hasFy())
181 attributes.setFy(attributes.cy());
182
183 return attributes;
184}
185
186// KHTML ElementImpl pure virtual method
187quint32 SVGRadialGradientElement::id() const
188{
189 return SVGNames::radialGradientTag.id();
190}
191
192}
193
194#endif // ENABLE(SVG)
FloatConversion.h
FloatPoint.h
RadialGradientAttributes.h
RenderObject.h
SVGLength.h
SVGNames.h
SVGPaintServerRadialGradient.h
SVGRadialGradientElement.h
SVGStopElement.h
SVGTransformList.h
SVGTransform.h
SVGUnitTypes.h
WebCore
Definition: CSSHelper.h:7
WebCore::narrowPrecisionToFloat
float narrowPrecisionToFloat(T)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal