~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/ksvg2/svg/SVGStyledElement.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
 
3
                  2004, 2005, 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., 59 Temple Place - Suite 330,
 
20
    Boston, MA 02111-1307, USA.
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#if ENABLE(SVG)
 
26
#include "SVGStyledElement.h"
 
27
 
 
28
#include "Attr.h"
 
29
#include "CSSStyleSelector.h"
 
30
#include "Document.h"
 
31
#include "HTMLNames.h"
 
32
#include "PlatformString.h"
 
33
#include "RenderPath.h"
 
34
#include "SVGElement.h"
 
35
#include "SVGElementInstance.h"
 
36
#include "SVGNames.h"
 
37
#include "SVGRenderStyle.h"
 
38
#include "SVGSVGElement.h"
 
39
#include "ksvgcssproperties.h"
 
40
#include <wtf/Assertions.h>
 
41
 
 
42
namespace WebCore {
 
43
 
 
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);
 
46
 
 
47
using namespace SVGNames;
 
48
 
 
49
SVGStyledElement::SVGStyledElement(const QualifiedName& tagName, Document* doc)
 
50
    : SVGElement(tagName, doc)
 
51
{
 
52
}
 
53
 
 
54
SVGStyledElement::~SVGStyledElement()
 
55
{
 
56
}
 
57
 
 
58
ANIMATED_PROPERTY_DEFINITIONS(SVGStyledElement, String, String, string, ClassName, className, HTMLNames::classAttr.localName(), m_className)
 
59
 
 
60
bool SVGStyledElement::rendererIsNeeded(RenderStyle* style)
 
61
{
 
62
    if (!parentNode() || parentNode()->isSVGElement())
 
63
        return StyledElement::rendererIsNeeded(style);
 
64
 
 
65
    return false;
 
66
}
 
67
 
 
68
RenderObject* SVGStyledElement::createRenderer(RenderArena* arena, RenderStyle* style)
 
69
{
 
70
    // The path data is set upon the first layout() call.
 
71
    return new (arena) RenderPath(style, this);
 
72
}
 
73
 
 
74
static inline int cssPropertyIdForName(const char* propertyName, int propertyLength)
 
75
{
 
76
    int propertyId = getPropertyID(propertyName, propertyLength);
 
77
    if (propertyId == 0)
 
78
        propertyId = SVG::getSVGCSSPropertyID(propertyName, propertyLength);
 
79
    return propertyId;
 
80
}
 
81
 
 
82
static inline void mapAttributeToCSSProperty(HashMap<AtomicStringImpl*, int>* propertyNameToIdMap, const QualifiedName& attrName, const char* cssPropertyName = 0)
 
83
{
 
84
    int propertyId = 0;
 
85
    if (cssPropertyName)
 
86
        propertyId = cssPropertyIdForName(cssPropertyName, strlen(cssPropertyName));
 
87
    else {
 
88
        DeprecatedString propertyName = attrName.localName().deprecatedString();
 
89
        propertyId = cssPropertyIdForName(propertyName.ascii(), propertyName.length());
 
90
    }
 
91
    ASSERT(propertyId > 0);
 
92
    propertyNameToIdMap->set(attrName.localName().impl(), propertyId);
 
93
}
 
94
 
 
95
int SVGStyledElement::cssPropertyIdForSVGAttributeName(const QualifiedName& attrName)
 
96
{
 
97
    if (!attrName.namespaceURI().isNull())
 
98
        return 0;
 
99
    
 
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);
 
163
    }
 
164
    
 
165
    return propertyNameToIdMap->get(attrName.localName().impl());
 
166
}
 
167
 
 
168
bool SVGStyledElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
 
169
{
 
170
    if (SVGStyledElement::cssPropertyIdForSVGAttributeName(attrName) > 0) {
 
171
        result = eSVG;
 
172
        return false;
 
173
    }
 
174
    return SVGElement::mapToEntry(attrName, result);
 
175
}
 
176
 
 
177
void SVGStyledElement::parseMappedAttribute(MappedAttribute* attr)
 
178
{
 
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());
 
182
    if (propId > 0) {
 
183
        addCSSProperty(attr, propId, attr->value());
 
184
        setChanged();
 
185
        return;
 
186
    }
 
187
    
 
188
    // id and class are handled by StyledElement
 
189
    SVGElement::parseMappedAttribute(attr);
 
190
}
 
191
 
 
192
void SVGStyledElement::notifyAttributeChange() const
 
193
{
 
194
    SVGDocumentExtensions* extensions = document()->accessSVGExtensions();
 
195
    if (!extensions)
 
196
        return;
 
197
 
 
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);
 
202
 
 
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();
 
206
}
 
207
 
 
208
void SVGStyledElement::notifyResourceParentIfExistant() const
 
209
{
 
210
    Node* node = parentNode();
 
211
    while (node) {
 
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);
 
216
            ASSERT(element);
 
217
 
 
218
            element->notifyAttributeChange();
 
219
        }
 
220
 
 
221
        node = node->parentNode();
 
222
    }
 
223
}
 
224
 
 
225
void SVGStyledElement::updateElementInstance(SVGDocumentExtensions* extensions) const
 
226
{
 
227
    SVGStyledElement* nonConstThis = const_cast<SVGStyledElement*>(this);
 
228
    HashSet<SVGElementInstance*>* set = extensions->instancesForElement(nonConstThis);
 
229
    if (!set || set->isEmpty())
 
230
        return;
 
231
 
 
232
    // We need to be careful here, as the instancesForElement
 
233
    // hash set may be modified after we call updateInstance! 
 
234
    HashSet<SVGElementInstance*> localCopy;
 
235
 
 
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();
 
239
 
 
240
    for (; it1 != end1; ++it1)
 
241
        localCopy.add(*it1);
 
242
 
 
243
    // Actually nofify instances to update
 
244
    HashSet<SVGElementInstance*>::const_iterator it2 = localCopy.begin();
 
245
    const HashSet<SVGElementInstance*>::const_iterator end2 = localCopy.end();
 
246
 
 
247
    for (; it2 != end2; ++it2)
 
248
        (*it2)->updateInstance(nonConstThis);
 
249
}
 
250
 
 
251
void SVGStyledElement::attributeChanged(Attribute* attr, bool preserveDecls)
 
252
{
 
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();
 
259
}
 
260
 
 
261
void SVGStyledElement::rebuildRenderer() const
 
262
{
 
263
    if (!renderer() || !renderer()->isRenderPath())
 
264
        return;
 
265
 
 
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);
 
271
}
 
272
 
 
273
}
 
274
 
 
275
#endif // ENABLE(SVG)
 
276
 
 
277
// vim:ts=4:noet