OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebCore / svg / SVGImageElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Rob Buis <buis@kde.org>
4  * Copyright (C) 2006 Alexander Kellett <lypanov@kde.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23
24 #if ENABLE(SVG)
25 #include "SVGImageElement.h"
26
27 #include "Attribute.h"
28 #include "CSSPropertyNames.h"
29 #include "RenderImageResource.h"
30 #include "RenderSVGImage.h"
31 #include "RenderSVGResource.h"
32 #include "SVGNames.h"
33 #include "SVGSVGElement.h"
34 #include "XLinkNames.h"
35
36 namespace WebCore {
37
38 // Animated property definitions
39 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::xAttr, X, x)
40 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::yAttr, Y, y)
41 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::widthAttr, Width, width)
42 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::heightAttr, Height, height)
43 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
44 DEFINE_ANIMATED_STRING(SVGImageElement, XLinkNames::hrefAttr, Href, href)
45 DEFINE_ANIMATED_BOOLEAN(SVGImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
46
47 inline SVGImageElement::SVGImageElement(const QualifiedName& tagName, Document* document)
48     : SVGStyledTransformableElement(tagName, document)
49     , m_x(LengthModeWidth)
50     , m_y(LengthModeHeight)
51     , m_width(LengthModeWidth)
52     , m_height(LengthModeHeight)
53     , m_imageLoader(this)
54 {
55 }
56
57 PassRefPtr<SVGImageElement> SVGImageElement::create(const QualifiedName& tagName, Document* document)
58 {
59     return adoptRef(new SVGImageElement(tagName, document));
60 }
61
62 void SVGImageElement::parseMappedAttribute(Attribute* attr)
63 {
64     if (attr->name() == SVGNames::xAttr)
65         setXBaseValue(SVGLength(LengthModeWidth, attr->value()));
66     else if (attr->name() == SVGNames::yAttr)
67         setYBaseValue(SVGLength(LengthModeHeight, attr->value()));
68     else if (attr->name() == SVGNames::preserveAspectRatioAttr)
69         SVGPreserveAspectRatio::parsePreserveAspectRatio(this, attr->value());
70     else if (attr->name() == SVGNames::widthAttr) {
71         setWidthBaseValue(SVGLength(LengthModeWidth, attr->value()));
72         addCSSProperty(attr, CSSPropertyWidth, attr->value());
73         if (widthBaseValue().value(this) < 0.0)
74             document()->accessSVGExtensions()->reportError("A negative value for image attribute <width> is not allowed");
75     } else if (attr->name() == SVGNames::heightAttr) {
76         setHeightBaseValue(SVGLength(LengthModeHeight, attr->value()));
77         addCSSProperty(attr, CSSPropertyHeight, attr->value());
78         if (heightBaseValue().value(this) < 0.0)
79             document()->accessSVGExtensions()->reportError("A negative value for image attribute <height> is not allowed");
80     } else {
81         if (SVGTests::parseMappedAttribute(attr))
82             return;
83         if (SVGLangSpace::parseMappedAttribute(attr))
84             return;
85         if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
86             return;
87         if (SVGURIReference::parseMappedAttribute(attr))
88             return;
89         SVGStyledTransformableElement::parseMappedAttribute(attr);
90     }
91 }
92
93 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
94 {
95     SVGStyledTransformableElement::svgAttributeChanged(attrName);
96
97     if (SVGURIReference::isKnownAttribute(attrName))
98         m_imageLoader.updateFromElementIgnoringPreviousError();
99
100     bool isLengthAttribute = attrName == SVGNames::xAttr
101                           || attrName == SVGNames::yAttr
102                           || attrName == SVGNames::widthAttr
103                           || attrName == SVGNames::heightAttr;
104
105     if (isLengthAttribute)
106         updateRelativeLengthsInformation();
107
108     if (SVGTests::handleAttributeChange(this, attrName))
109         return;
110
111     RenderObject* renderer = this->renderer();
112     if (!renderer)
113         return;
114
115     if (SVGStyledTransformableElement::isKnownAttribute(attrName)) {
116         renderer->setNeedsTransformUpdate();
117         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
118         return;
119     }
120
121     if (isLengthAttribute) {
122         renderer->updateFromElement();
123         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer, false);
124         return;
125     }
126
127     if (attrName == SVGNames::preserveAspectRatioAttr
128         || SVGLangSpace::isKnownAttribute(attrName)
129         || SVGExternalResourcesRequired::isKnownAttribute(attrName))
130         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
131 }
132
133 void SVGImageElement::synchronizeProperty(const QualifiedName& attrName)
134 {
135     SVGStyledTransformableElement::synchronizeProperty(attrName);
136
137     if (attrName == anyQName()) {
138         synchronizeX();
139         synchronizeY();
140         synchronizeWidth();
141         synchronizeHeight();
142         synchronizePreserveAspectRatio();
143         synchronizeExternalResourcesRequired();
144         synchronizeHref();
145         SVGTests::synchronizeProperties(this, attrName);
146         return;
147     }
148
149     if (attrName == SVGNames::xAttr)
150         synchronizeX();
151     else if (attrName == SVGNames::yAttr)
152         synchronizeY();
153     else if (attrName == SVGNames::widthAttr)
154         synchronizeWidth();
155     else if (attrName == SVGNames::heightAttr)
156         synchronizeHeight();
157     else if (attrName == SVGNames::preserveAspectRatioAttr)
158         synchronizePreserveAspectRatio();
159     else if (SVGExternalResourcesRequired::isKnownAttribute(attrName))
160         synchronizeExternalResourcesRequired();
161     else if (SVGURIReference::isKnownAttribute(attrName))
162         synchronizeHref();
163     else if (SVGTests::isKnownAttribute(attrName))
164         SVGTests::synchronizeProperties(this, attrName);
165 }
166
167 AttributeToPropertyTypeMap& SVGImageElement::attributeToPropertyTypeMap()
168 {
169     DEFINE_STATIC_LOCAL(AttributeToPropertyTypeMap, s_attributeToPropertyTypeMap, ());
170     return s_attributeToPropertyTypeMap;
171 }
172
173 void SVGImageElement::fillAttributeToPropertyTypeMap()
174 {
175     AttributeToPropertyTypeMap& attributeToPropertyTypeMap = this->attributeToPropertyTypeMap();
176
177     SVGStyledTransformableElement::fillPassedAttributeToPropertyTypeMap(attributeToPropertyTypeMap);
178     attributeToPropertyTypeMap.set(SVGNames::xAttr, AnimatedLength);
179     attributeToPropertyTypeMap.set(SVGNames::yAttr, AnimatedLength);
180     attributeToPropertyTypeMap.set(SVGNames::widthAttr, AnimatedLength);
181     attributeToPropertyTypeMap.set(SVGNames::heightAttr, AnimatedLength);
182     attributeToPropertyTypeMap.set(SVGNames::preserveAspectRatioAttr, AnimatedPreserveAspectRatio);
183     attributeToPropertyTypeMap.set(XLinkNames::hrefAttr, AnimatedString);
184 }
185
186 bool SVGImageElement::selfHasRelativeLengths() const
187 {
188     return x().isRelative()
189         || y().isRelative()
190         || width().isRelative()
191         || height().isRelative();
192 }
193
194 RenderObject* SVGImageElement::createRenderer(RenderArena* arena, RenderStyle*)
195 {
196     return new (arena) RenderSVGImage(this);
197 }
198
199 bool SVGImageElement::haveLoadedRequiredResources()
200 {
201     return !externalResourcesRequiredBaseValue() || m_imageLoader.haveFiredLoadEvent();
202 }
203
204 void SVGImageElement::attach()
205 {
206     SVGStyledTransformableElement::attach();
207
208     if (RenderSVGImage* imageObj = toRenderSVGImage(renderer())) {
209         if (imageObj->imageResource()->hasImage())
210             return;
211
212         imageObj->imageResource()->setCachedImage(m_imageLoader.image());
213     }
214 }
215
216 void SVGImageElement::insertedIntoDocument()
217 {
218     SVGStyledTransformableElement::insertedIntoDocument();
219
220     // Update image loader, as soon as we're living in the tree.
221     // We can only resolve base URIs properly, after that!
222     m_imageLoader.updateFromElement();
223 }
224
225 const QualifiedName& SVGImageElement::imageSourceAttributeName() const
226 {
227     return XLinkNames::hrefAttr;
228 }
229
230 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
231 {
232     SVGStyledTransformableElement::addSubresourceAttributeURLs(urls);
233
234     addSubresourceURL(urls, document()->completeURL(href()));
235 }
236
237 void SVGImageElement::willMoveToNewOwnerDocument()
238 {
239     m_imageLoader.elementWillMoveToNewOwnerDocument();
240     SVGStyledTransformableElement::willMoveToNewOwnerDocument();
241 }
242
243 }
244
245 #endif // ENABLE(SVG)