tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / svg / SVGTextElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2008 Rob Buis <buis@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #if ENABLE(SVG)
24 #include "SVGTextElement.h"
25
26 #include "AffineTransform.h"
27 #include "Attribute.h"
28 #include "FloatRect.h"
29 #include "RenderSVGResource.h"
30 #include "RenderSVGText.h"
31 #include "SVGElementInstance.h"
32 #include "SVGNames.h"
33 #include "SVGRenderStyle.h"
34 #include "SVGTSpanElement.h"
35
36 namespace WebCore {
37
38 // Animated property definitions
39 DEFINE_ANIMATED_TRANSFORM_LIST(SVGTextElement, SVGNames::transformAttr, Transform, transform)
40
41 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGTextElement)
42     REGISTER_LOCAL_ANIMATED_PROPERTY(transform)
43     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGTextPositioningElement)
44 END_REGISTER_ANIMATED_PROPERTIES
45
46 inline SVGTextElement::SVGTextElement(const QualifiedName& tagName, Document* doc)
47     : SVGTextPositioningElement(tagName, doc)
48 {
49     ASSERT(hasTagName(SVGNames::textTag));
50     registerAnimatedPropertiesForSVGTextElement();
51 }
52
53 PassRefPtr<SVGTextElement> SVGTextElement::create(const QualifiedName& tagName, Document* document)
54 {
55     return adoptRef(new SVGTextElement(tagName, document));
56 }
57
58 bool SVGTextElement::isSupportedAttribute(const QualifiedName& attrName)
59 {
60     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
61     if (supportedAttributes.isEmpty())
62         supportedAttributes.add(SVGNames::transformAttr);
63     return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
64 }
65
66 void SVGTextElement::parseMappedAttribute(Attribute* attr)
67 {
68     if (!isSupportedAttribute(attr->name())) {
69         SVGTextPositioningElement::parseMappedAttribute(attr);
70         return;
71     }
72
73     if (attr->name() == SVGNames::transformAttr) {
74         SVGTransformList newList;
75         if (!SVGTransformable::parseTransformAttribute(newList, attr->value()))
76             newList.clear();
77
78         detachAnimatedTransformListWrappers(newList.size());
79         setTransformBaseValue(newList);
80         return;
81     }
82
83     ASSERT_NOT_REACHED();
84 }
85
86 SVGElement* SVGTextElement::nearestViewportElement() const
87 {
88     return SVGTransformable::nearestViewportElement(this);
89 }
90
91 SVGElement* SVGTextElement::farthestViewportElement() const
92 {
93     return SVGTransformable::farthestViewportElement(this);
94 }
95
96 FloatRect SVGTextElement::getBBox(StyleUpdateStrategy styleUpdateStrategy)
97 {
98     return SVGTransformable::getBBox(this, styleUpdateStrategy);
99 }
100
101 AffineTransform SVGTextElement::getCTM(StyleUpdateStrategy styleUpdateStrategy)
102 {
103     return SVGLocatable::computeCTM(this, SVGLocatable::NearestViewportScope, styleUpdateStrategy);
104 }
105
106 AffineTransform SVGTextElement::getScreenCTM(StyleUpdateStrategy styleUpdateStrategy)
107 {
108     return SVGLocatable::computeCTM(this, SVGLocatable::ScreenScope, styleUpdateStrategy);
109 }
110
111 AffineTransform SVGTextElement::animatedLocalTransform() const
112 {
113     AffineTransform matrix;
114     RenderStyle* style = renderer()->style();
115
116     // if CSS property was set, use that, otherwise fallback to attribute (if set)
117     if (style->hasTransform()) {
118         TransformationMatrix t;
119         // For now, the transform-origin is not taken into account
120         // Also, any percentage values will not be taken into account
121         style->applyTransform(t, IntSize(0, 0), RenderStyle::ExcludeTransformOrigin);
122         // Flatten any 3D transform
123         matrix = t.toAffineTransform();
124     } else
125         transform().concatenate(matrix);
126
127     if (m_supplementalTransform)
128         return *m_supplementalTransform * matrix;
129     return matrix;
130 }
131
132 AffineTransform* SVGTextElement::supplementalTransform()
133 {
134     if (!m_supplementalTransform)
135         m_supplementalTransform = adoptPtr(new AffineTransform);
136     return m_supplementalTransform.get();
137 }
138
139 RenderObject* SVGTextElement::createRenderer(RenderArena* arena, RenderStyle*)
140 {
141     return new (arena) RenderSVGText(this);
142 }
143
144 bool SVGTextElement::childShouldCreateRenderer(Node* child) const
145 {
146     if (child->isTextNode()
147         || child->hasTagName(SVGNames::aTag)
148 #if ENABLE(SVG_FONTS)
149         || child->hasTagName(SVGNames::altGlyphTag)
150 #endif
151         || child->hasTagName(SVGNames::textPathTag)
152         || child->hasTagName(SVGNames::trefTag)
153         || child->hasTagName(SVGNames::tspanTag))
154         return true;
155
156     return false;
157 }
158
159 void SVGTextElement::svgAttributeChanged(const QualifiedName& attrName)
160 {
161     if (!isSupportedAttribute(attrName)) {
162         SVGTextPositioningElement::svgAttributeChanged(attrName);
163         return;
164     }
165
166     SVGElementInstance::InvalidationGuard invalidationGuard(this);
167
168     RenderObject* renderer = this->renderer();
169     if (!renderer)
170         return;
171
172     if (attrName == SVGNames::transformAttr) {
173         renderer->setNeedsTransformUpdate();
174         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
175         return;
176     }
177
178     ASSERT_NOT_REACHED();
179 }
180
181 }
182
183 #endif // ENABLE(SVG)