2
Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3
2004, 2005, 2007 Rob Buis <buis@kde.org>
5
This file is part of the KDE project
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.
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.
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., 59 Temple Place - Suite 330,
20
Boston, MA 02111-1307, USA.
26
#include "SVGStyledElement.h"
29
#include "CSSStyleSelector.h"
31
#include "HTMLNames.h"
32
#include "PlatformString.h"
33
#include "RenderPath.h"
34
#include "SVGElement.h"
35
#include "SVGElementInstance.h"
37
#include "SVGRenderStyle.h"
38
#include "SVGSVGElement.h"
39
#include "ksvgcssproperties.h"
40
#include <wtf/Assertions.h>
44
// Defined in CSSGrammar.y, but not in any header, so just declare it here for now.
45
int getPropertyID(const char* str, int len);
47
using namespace SVGNames;
49
SVGStyledElement::SVGStyledElement(const QualifiedName& tagName, Document* doc)
50
: SVGElement(tagName, doc)
54
SVGStyledElement::~SVGStyledElement()
58
ANIMATED_PROPERTY_DEFINITIONS(SVGStyledElement, String, String, string, ClassName, className, HTMLNames::classAttr.localName(), m_className)
60
bool SVGStyledElement::rendererIsNeeded(RenderStyle* style)
62
if (!parentNode() || parentNode()->isSVGElement())
63
return StyledElement::rendererIsNeeded(style);
68
RenderObject* SVGStyledElement::createRenderer(RenderArena* arena, RenderStyle* style)
70
// The path data is set upon the first layout() call.
71
return new (arena) RenderPath(style, this);
74
static inline int cssPropertyIdForName(const char* propertyName, int propertyLength)
76
int propertyId = getPropertyID(propertyName, propertyLength);
78
propertyId = SVG::getSVGCSSPropertyID(propertyName, propertyLength);
82
static inline void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, int>* propertyNameToIdMap, const QualifiedName& attrName, const char* cssPropertyName = 0)
86
propertyId = cssPropertyIdForName(cssPropertyName, strlen(cssPropertyName));
88
DeprecatedString propertyName = attrName.localName().deprecatedString();
89
propertyId = cssPropertyIdForName(propertyName.ascii(), propertyName.length());
91
ASSERT(propertyId > 0);
92
propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
95
int SVGStyledElement::cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
97
if (!attrName.namespaceURI().isNull())
100
static HashMap<AtomicStringImpl*, int>* propertyNameToIdMap = 0;
101
if (!propertyNameToIdMap) {
102
propertyNameToIdMap = new HashMap<AtomicStringImpl*, int>;
103
// This is a list of all base CSS and SVG CSS properties which are exposed as SVG XML attributes
104
mapAttributeToCSSProperty(propertyNameToIdMap, alignment_baselineAttr);
105
mapAttributeToCSSProperty(propertyNameToIdMap, baseline_shiftAttr);
106
mapAttributeToCSSProperty(propertyNameToIdMap, clipAttr);
107
mapAttributeToCSSProperty(propertyNameToIdMap, clip_pathAttr);
108
mapAttributeToCSSProperty(propertyNameToIdMap, clip_ruleAttr);
109
mapAttributeToCSSProperty(propertyNameToIdMap, colorAttr);
110
mapAttributeToCSSProperty(propertyNameToIdMap, color_interpolationAttr);
111
mapAttributeToCSSProperty(propertyNameToIdMap, color_interpolation_filtersAttr);
112
mapAttributeToCSSProperty(propertyNameToIdMap, color_profileAttr);
113
mapAttributeToCSSProperty(propertyNameToIdMap, color_renderingAttr);
114
mapAttributeToCSSProperty(propertyNameToIdMap, cursorAttr);
115
mapAttributeToCSSProperty(propertyNameToIdMap, directionAttr);
116
mapAttributeToCSSProperty(propertyNameToIdMap, displayAttr);
117
mapAttributeToCSSProperty(propertyNameToIdMap, dominant_baselineAttr);
118
mapAttributeToCSSProperty(propertyNameToIdMap, enable_backgroundAttr);
119
mapAttributeToCSSProperty(propertyNameToIdMap, fillAttr);
120
mapAttributeToCSSProperty(propertyNameToIdMap, fill_opacityAttr);
121
mapAttributeToCSSProperty(propertyNameToIdMap, fill_ruleAttr);
122
mapAttributeToCSSProperty(propertyNameToIdMap, filterAttr);
123
mapAttributeToCSSProperty(propertyNameToIdMap, flood_colorAttr);
124
mapAttributeToCSSProperty(propertyNameToIdMap, flood_opacityAttr);
125
mapAttributeToCSSProperty(propertyNameToIdMap, font_familyAttr);
126
mapAttributeToCSSProperty(propertyNameToIdMap, font_sizeAttr);
127
mapAttributeToCSSProperty(propertyNameToIdMap, font_size_adjustAttr);
128
mapAttributeToCSSProperty(propertyNameToIdMap, font_stretchAttr);
129
mapAttributeToCSSProperty(propertyNameToIdMap, font_styleAttr);
130
mapAttributeToCSSProperty(propertyNameToIdMap, font_variantAttr);
131
mapAttributeToCSSProperty(propertyNameToIdMap, font_weightAttr);
132
mapAttributeToCSSProperty(propertyNameToIdMap, glyph_orientation_horizontalAttr);
133
mapAttributeToCSSProperty(propertyNameToIdMap, glyph_orientation_verticalAttr);
134
mapAttributeToCSSProperty(propertyNameToIdMap, image_renderingAttr);
135
mapAttributeToCSSProperty(propertyNameToIdMap, kerningAttr);
136
mapAttributeToCSSProperty(propertyNameToIdMap, letter_spacingAttr);
137
mapAttributeToCSSProperty(propertyNameToIdMap, lighting_colorAttr);
138
mapAttributeToCSSProperty(propertyNameToIdMap, marker_endAttr);
139
mapAttributeToCSSProperty(propertyNameToIdMap, marker_midAttr);
140
mapAttributeToCSSProperty(propertyNameToIdMap, marker_startAttr);
141
mapAttributeToCSSProperty(propertyNameToIdMap, maskAttr);
142
mapAttributeToCSSProperty(propertyNameToIdMap, opacityAttr);
143
mapAttributeToCSSProperty(propertyNameToIdMap, overflowAttr);
144
mapAttributeToCSSProperty(propertyNameToIdMap, pointer_eventsAttr);
145
mapAttributeToCSSProperty(propertyNameToIdMap, shape_renderingAttr);
146
mapAttributeToCSSProperty(propertyNameToIdMap, stop_colorAttr);
147
mapAttributeToCSSProperty(propertyNameToIdMap, stop_opacityAttr);
148
mapAttributeToCSSProperty(propertyNameToIdMap, strokeAttr);
149
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_dasharrayAttr);
150
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_dashoffsetAttr);
151
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_linecapAttr);
152
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_linejoinAttr);
153
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_miterlimitAttr);
154
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_opacityAttr);
155
mapAttributeToCSSProperty(propertyNameToIdMap, stroke_widthAttr);
156
mapAttributeToCSSProperty(propertyNameToIdMap, text_anchorAttr);
157
mapAttributeToCSSProperty(propertyNameToIdMap, text_decorationAttr);
158
mapAttributeToCSSProperty(propertyNameToIdMap, text_renderingAttr);
159
mapAttributeToCSSProperty(propertyNameToIdMap, unicode_bidiAttr);
160
mapAttributeToCSSProperty(propertyNameToIdMap, visibilityAttr);
161
mapAttributeToCSSProperty(propertyNameToIdMap, word_spacingAttr);
162
mapAttributeToCSSProperty(propertyNameToIdMap, writing_modeAttr);
165
return propertyNameToIdMap->get(attrName.localName().impl());
168
bool SVGStyledElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
170
if (SVGStyledElement::cssPropertyIdForSVGAttributeName(attrName) > 0) {
174
return SVGElement::mapToEntry(attrName, result);
177
void SVGStyledElement::parseMappedAttribute(MappedAttribute* attr)
179
// NOTE: Any subclass which overrides parseMappedAttribute for a property handled by
180
// cssPropertyIdForSVGAttributeName will also have to override mapToEntry to disable the default eSVG mapping
181
int propId = SVGStyledElement::cssPropertyIdForSVGAttributeName(attr->name());
183
addCSSProperty(attr, propId, attr->value());
188
// id and class are handled by StyledElement
189
SVGElement::parseMappedAttribute(attr);
192
void SVGStyledElement::notifyAttributeChange() const
194
SVGDocumentExtensions* extensions = document()->accessSVGExtensions();
198
// In case we're referenced by a <use> element, we have element instances registered
199
// to us in the SVGDocumentExtensions. If notifyAttributeChange() is called, we need
200
// to recursively update all children including ourselves.
201
updateElementInstance(extensions);
203
// If we're a child of an element creating a "resource" (ie. <pattern> child)
204
// then we have to notify our parent resource that we changed.
205
notifyResourceParentIfExistant();
208
void SVGStyledElement::notifyResourceParentIfExistant() const
210
Node* node = parentNode();
212
if (node->hasTagName(SVGNames::linearGradientTag) || node->hasTagName(SVGNames::radialGradientTag) ||
213
node->hasTagName(SVGNames::patternTag) || node->hasTagName(SVGNames::clipPathTag) ||
214
node->hasTagName(SVGNames::markerTag) || node->hasTagName(SVGNames::maskTag)) {
215
SVGElement* element = svg_dynamic_cast(node);
218
element->notifyAttributeChange();
221
node = node->parentNode();
225
void SVGStyledElement::updateElementInstance(SVGDocumentExtensions* extensions) const
227
SVGStyledElement* nonConstThis = const_cast<SVGStyledElement*>(this);
228
HashSet<SVGElementInstance*>* set = extensions->instancesForElement(nonConstThis);
229
if (!set || set->isEmpty())
232
// We need to be careful here, as the instancesForElement
233
// hash set may be modified after we call updateInstance!
234
HashSet<SVGElementInstance*> localCopy;
236
// First create a local copy of the hashset
237
HashSet<SVGElementInstance*>::const_iterator it1 = set->begin();
238
const HashSet<SVGElementInstance*>::const_iterator end1 = set->end();
240
for (; it1 != end1; ++it1)
243
// Actually nofify instances to update
244
HashSet<SVGElementInstance*>::const_iterator it2 = localCopy.begin();
245
const HashSet<SVGElementInstance*>::const_iterator end2 = localCopy.end();
247
for (; it2 != end2; ++it2)
248
(*it2)->updateInstance(nonConstThis);
251
void SVGStyledElement::attributeChanged(Attribute* attr, bool preserveDecls)
253
// FIXME: Eventually subclasses from SVGElement should implement
254
// attributeChanged() instead of notifyAttributeChange()
255
// This is a quick fix to allow dynamic updates of SVG elements
256
// but will result in slower dynamic-update performance than necessary.
257
SVGElement::attributeChanged(attr, preserveDecls);
258
notifyAttributeChange();
261
void SVGStyledElement::rebuildRenderer() const
263
if (!renderer() || !renderer()->isRenderPath())
266
RenderPath* renderPath = static_cast<RenderPath*>(renderer());
267
SVGElement* parentElement = svg_dynamic_cast(parentNode());
268
if (parentElement && parentElement->renderer() && parentElement->isStyled() &&
269
parentElement->childShouldCreateRenderer(const_cast<SVGStyledElement*>(this)))
270
renderPath->setNeedsLayout(true);
275
#endif // ENABLE(SVG)