KHtml

SVGGradientElement.cpp
1 /*
2  Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <[email protected]>
3  2004, 2005, 2006, 2007 Rob Buis <[email protected]>
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 "wtf/Platform.h"
24 
25 #if ENABLE(SVG)
26 #include "SVGGradientElement.h"
27 
28 #include "css/cssstyleselector.h"
29 #include "RenderPath.h"
30 #include "RenderSVGHiddenContainer.h"
31 #include "SVGNames.h"
32 #include "SVGPaintServerLinearGradient.h"
33 #include "SVGPaintServerRadialGradient.h"
34 #include "SVGStopElement.h"
35 #include "SVGTransformList.h"
36 #include "SVGTransformable.h"
37 #include "SVGUnitTypes.h"
38 
39 namespace WebCore
40 {
41 
42 SVGGradientElement::SVGGradientElement(const QualifiedName &tagName, Document *doc)
43  : SVGStyledElement(tagName, doc)
44  , SVGURIReference()
45  , SVGExternalResourcesRequired()
46  , m_spreadMethod(0)
47  , m_gradientUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
48  , m_gradientTransform(SVGTransformList::create(SVGNames::gradientTransformAttr))
49 {
50 }
51 
52 SVGGradientElement::~SVGGradientElement()
53 {
54 }
55 
56 ANIMATED_PROPERTY_DEFINITIONS(SVGGradientElement, int, Enumeration, enumeration, GradientUnits, gradientUnits, SVGNames::gradientUnitsAttr, m_gradientUnits)
57 ANIMATED_PROPERTY_DEFINITIONS(SVGGradientElement, SVGTransformList *, TransformList, transformList, GradientTransform, gradientTransform, SVGNames::gradientTransformAttr, m_gradientTransform.get())
58 ANIMATED_PROPERTY_DEFINITIONS(SVGGradientElement, int, Enumeration, enumeration, SpreadMethod, spreadMethod, SVGNames::spreadMethodAttr, m_spreadMethod)
59 
60 void SVGGradientElement::parseMappedAttribute(MappedAttribute *attr)
61 {
62  if (attr->name() == SVGNames::gradientUnitsAttr) {
63  if (attr->value() == "userSpaceOnUse") {
64  setGradientUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
65  } else if (attr->value() == "objectBoundingBox") {
66  setGradientUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
67  }
68  } else if (attr->name() == SVGNames::gradientTransformAttr) {
69  SVGTransformList *gradientTransforms = gradientTransformBaseValue();
70  if (!SVGTransformable::parseTransformAttribute(gradientTransforms, attr->value())) {
71  ExceptionCode ec = 0;
72  gradientTransforms->clear(ec);
73  }
74  } else if (attr->name() == SVGNames::spreadMethodAttr) {
75  if (attr->value() == "reflect") {
76  setSpreadMethodBaseValue(SVG_SPREADMETHOD_REFLECT);
77  } else if (attr->value() == "repeat") {
78  setSpreadMethodBaseValue(SVG_SPREADMETHOD_REPEAT);
79  } else if (attr->value() == "pad") {
80  setSpreadMethodBaseValue(SVG_SPREADMETHOD_PAD);
81  }
82  } else {
83  if (SVGURIReference::parseMappedAttribute(attr)) {
84  return;
85  }
86  if (SVGExternalResourcesRequired::parseMappedAttribute(attr)) {
87  return;
88  }
89 
90  SVGStyledElement::parseMappedAttribute(attr);
91  }
92 }
93 
94 void SVGGradientElement::svgAttributeChanged(const QualifiedName &attrName)
95 {
96  SVGStyledElement::svgAttributeChanged(attrName);
97 
98  if (!m_resource) {
99  return;
100  }
101 
102  if (attrName == SVGNames::gradientUnitsAttr ||
103  attrName == SVGNames::gradientTransformAttr ||
104  attrName == SVGNames::spreadMethodAttr ||
105  SVGURIReference::isKnownAttribute(attrName) ||
106  SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
107  SVGStyledElement::isKnownAttribute(attrName)) {
108  m_resource->invalidate();
109  }
110 }
111 
112 void SVGGradientElement::childrenChanged(bool changedByParser, Node *beforeChange, Node *afterChange, int childCountDelta)
113 {
114  SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
115 
116  if (m_resource) {
117  m_resource->invalidate();
118  }
119 }
120 
121 RenderObject *SVGGradientElement::createRenderer(RenderArena *arena, RenderStyle *)
122 {
123  return new(arena) RenderSVGHiddenContainer(this);
124 }
125 
126 SVGResource *SVGGradientElement::canvasResource()
127 {
128  // qCDebug(KHTML_LOG) << "request gradient paint server";
129  if (!m_resource) {
130  if (gradientType() == LinearGradientPaintServer) {
131  m_resource = SVGPaintServerLinearGradient::create(this);
132  } else {
133  m_resource = SVGPaintServerRadialGradient::create(this);
134  }
135  }
136 
137  return m_resource.get();
138 }
139 
140 Vector<SVGGradientStop> SVGGradientElement::buildStops() const
141 {
142  Vector<SVGGradientStop> stops;
143 
144  for (Node *n = firstChild(); n; n = n->nextSibling()) {
145  SVGElement *element = n->isSVGElement() ? static_cast<SVGElement *>(n) : nullptr;
146 
147  if (element && element->isGradientStop()) {
148  SVGStopElement *stop = static_cast<SVGStopElement *>(element);
149  float stopOffset = stop->offset();
150 
151  RenderStyle *stopStyle = stop->computedStyle();
152  QColor color = stopStyle->svgStyle()->stopColor();
153  float opacity = stopStyle->svgStyle()->stopOpacity();
154 
155  stops.append(makeGradientStop(stopOffset, QColor(color.red(), color.green(), color.blue(), int(opacity * 255.))));
156  }
157  }
158 
159  return stops;
160 }
161 
162 }
163 
164 #endif // ENABLE(SVG)
QAction * create(StandardAction id, const QObject *recvr, Func slot, QObject *parent)
KGuiItem stop()
int red() const const
MESSAGECORE_EXPORT KMime::Content * firstChild(const KMime::Content *node)
int green() const const
int blue() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.