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

KHTML

  • khtml
  • svg
SVGLinearGradientElement.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 "SVGLinearGradientElement.h"
28
29#include "FloatPoint.h"
30#include "LinearGradientAttributes.h"
31#include "SVGLength.h"
32#include "SVGNames.h"
33#include "SVGPaintServerLinearGradient.h"
34#include "SVGTransform.h"
35#include "SVGTransformList.h"
36#include "SVGUnitTypes.h"
37
38namespace WebCore {
39
40SVGLinearGradientElement::SVGLinearGradientElement(const QualifiedName& tagName, Document* doc)
41 : SVGGradientElement(tagName, doc)
42 , m_x1(this, LengthModeWidth)
43 , m_y1(this, LengthModeHeight)
44 , m_x2(this, LengthModeWidth)
45 , m_y2(this, LengthModeHeight)
46{
47 // Spec: If the attribute is not specified, the effect is as if a value of "100%" were specified.
48 setX2BaseValue(SVGLength(this, LengthModeWidth, "100%"));
49}
50
51SVGLinearGradientElement::~SVGLinearGradientElement()
52{
53}
54
55ANIMATED_PROPERTY_DEFINITIONS(SVGLinearGradientElement, SVGLength, Length, length, X1, x1, SVGNames::x1Attr, m_x1)
56ANIMATED_PROPERTY_DEFINITIONS(SVGLinearGradientElement, SVGLength, Length, length, Y1, y1, SVGNames::y1Attr, m_y1)
57ANIMATED_PROPERTY_DEFINITIONS(SVGLinearGradientElement, SVGLength, Length, length, X2, x2, SVGNames::x2Attr, m_x2)
58ANIMATED_PROPERTY_DEFINITIONS(SVGLinearGradientElement, SVGLength, Length, length, Y2, y2, SVGNames::y2Attr, m_y2)
59
60void SVGLinearGradientElement::parseMappedAttribute(MappedAttribute* attr)
61{
62 if (attr->name() == SVGNames::x1Attr)
63 setX1BaseValue(SVGLength(this, LengthModeWidth, attr->value()));
64 else if (attr->name() == SVGNames::y1Attr)
65 setY1BaseValue(SVGLength(this, LengthModeHeight, attr->value()));
66 else if (attr->name() == SVGNames::x2Attr)
67 setX2BaseValue(SVGLength(this, LengthModeWidth, attr->value()));
68 else if (attr->name() == SVGNames::y2Attr)
69 setY2BaseValue(SVGLength(this, LengthModeHeight, attr->value()));
70 else
71 SVGGradientElement::parseMappedAttribute(attr);
72}
73
74void SVGLinearGradientElement::svgAttributeChanged(const QualifiedName& attrName)
75{
76 SVGGradientElement::svgAttributeChanged(attrName);
77
78 if (!m_resource)
79 return;
80
81 if (attrName == SVGNames::x1Attr || attrName == SVGNames::y1Attr ||
82 attrName == SVGNames::x2Attr || attrName == SVGNames::y2Attr)
83 m_resource->invalidate();
84}
85
86void SVGLinearGradientElement::buildGradient() const
87{
88 LinearGradientAttributes attributes = collectGradientProperties();
89
90 // If we didn't find any gradient containing stop elements, ignore the request.
91 if (attributes.stops().isEmpty())
92 return;
93
94 RefPtr<SVGPaintServerLinearGradient> linearGradient = WTF::static_pointer_cast<SVGPaintServerLinearGradient>(m_resource);
95
96 linearGradient->setGradientStops(attributes.stops());
97 linearGradient->setBoundingBoxMode(attributes.boundingBoxMode());
98 linearGradient->setGradientSpreadMethod(attributes.spreadMethod());
99 linearGradient->setGradientTransform(attributes.gradientTransform());
100 linearGradient->setGradientStart(FloatPoint::narrowPrecision(attributes.x1(), attributes.y1()));
101 linearGradient->setGradientEnd(FloatPoint::narrowPrecision(attributes.x2(), attributes.y2()));
102}
103
104LinearGradientAttributes SVGLinearGradientElement::collectGradientProperties() const
105{
106 LinearGradientAttributes attributes;
107 HashSet<const SVGGradientElement*> processedGradients;
108
109 bool isLinear = true;
110 const SVGGradientElement* current = this;
111
112 while (current) {
113 if (!attributes.hasSpreadMethod() && current->hasAttribute(SVGNames::spreadMethodAttr))
114 attributes.setSpreadMethod((SVGGradientSpreadMethod) current->spreadMethod());
115
116 if (!attributes.hasBoundingBoxMode() && current->hasAttribute(SVGNames::gradientUnitsAttr))
117 attributes.setBoundingBoxMode(current->getAttribute(SVGNames::gradientUnitsAttr) == "objectBoundingBox");
118
119 if (!attributes.hasGradientTransform() && current->hasAttribute(SVGNames::gradientTransformAttr))
120 attributes.setGradientTransform(current->gradientTransform()->consolidate().matrix());
121
122 if (!attributes.hasStops()) {
123 const Vector<SVGGradientStop>& stops(current->buildStops());
124 kDebug() << "stops.isEmpty()" << stops.isEmpty() << endl;
125 if (!stops.isEmpty())
126 attributes.setStops(stops);
127 }
128
129 if (isLinear) {
130 const SVGLinearGradientElement* linear = static_cast<const SVGLinearGradientElement*>(current);
131
132 if (!attributes.hasX1() && current->hasAttribute(SVGNames::x1Attr))
133 attributes.setX1(linear->x1().valueAsPercentage());
134
135 if (!attributes.hasY1() && current->hasAttribute(SVGNames::y1Attr))
136 attributes.setY1(linear->y1().valueAsPercentage());
137
138 if (!attributes.hasX2() && current->hasAttribute(SVGNames::x2Attr))
139 attributes.setX2(linear->x2().valueAsPercentage());
140
141 if (!attributes.hasY2() && current->hasAttribute(SVGNames::y2Attr))
142 attributes.setY2(linear->y2().valueAsPercentage());
143 }
144
145 processedGradients.add(current);
146
147 // Respect xlink:href, take attributes from referenced element
148 Node* refNode = ownerDocument()->getElementById(SVGURIReference::getTarget(current->href()));
149 if (refNode && (refNode->hasTagName(SVGNames::linearGradientTag) || refNode->hasTagName(SVGNames::radialGradientTag))) {
150 current = static_cast<const SVGGradientElement*>(const_cast<const Node*>(refNode));
151 int ec;
152 kDebug() << "take attributes from" << current->getAttributeNS("", "id", ec) << endl;
153
154 // Cycle detection
155 if (processedGradients.contains(current))
156 return LinearGradientAttributes();
157
158 isLinear = current->gradientType() == LinearGradientPaintServer;
159 } else
160 current = 0;
161 }
162
163 return attributes;
164}
165
166// KHTML ElementImpl pure virtual method
167quint32 SVGLinearGradientElement::id() const
168{
169 return SVGNames::linearGradientTag.id();
170}
171
172}
173
174#endif // ENABLE(SVG)
FloatPoint.h
LinearGradientAttributes.h
SVGLength.h
SVGLinearGradientElement.h
SVGNames.h
SVGPaintServerLinearGradient.h
SVGTransformList.h
SVGTransform.h
SVGUnitTypes.h
kDebug
#define kDebug
WebCore
Definition: CSSHelper.h:7
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