1 /*
2 Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007, 2008, 2009 Rob Buis <buis@kde.org>
4 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 "CSSPropertyNames.h"
28 #include "MappedAttribute.h"
29 #include "RenderSVGImage.h"
30 #include "SVGDocument.h"
31 #include "SVGLength.h"
32 #include "SVGPreserveAspectRatio.h"
33 #include "SVGSVGElement.h"
34 #include "XLinkNames.h"
35
36 namespace WebCore {
37
SVGImageElement(const QualifiedName & tagName,Document * doc)38 SVGImageElement::SVGImageElement(const QualifiedName& tagName, Document* doc)
39 : SVGStyledTransformableElement(tagName, doc)
40 , SVGTests()
41 , SVGLangSpace()
42 , SVGExternalResourcesRequired()
43 , SVGURIReference()
44 , m_x(LengthModeWidth)
45 , m_y(LengthModeHeight)
46 , m_width(LengthModeWidth)
47 , m_height(LengthModeHeight)
48 , m_imageLoader(this)
49 {
50 }
51
~SVGImageElement()52 SVGImageElement::~SVGImageElement()
53 {
54 }
55
parseMappedAttribute(MappedAttribute * attr)56 void SVGImageElement::parseMappedAttribute(MappedAttribute *attr)
57 {
58 if (attr->name() == SVGNames::xAttr)
59 setXBaseValue(SVGLength(LengthModeWidth, attr->value()));
60 else if (attr->name() == SVGNames::yAttr)
61 setYBaseValue(SVGLength(LengthModeHeight, attr->value()));
62 else if (attr->name() == SVGNames::preserveAspectRatioAttr)
63 SVGPreserveAspectRatio::parsePreserveAspectRatio(this, attr->value());
64 else if (attr->name() == SVGNames::widthAttr) {
65 setWidthBaseValue(SVGLength(LengthModeWidth, attr->value()));
66 addCSSProperty(attr, CSSPropertyWidth, attr->value());
67 if (widthBaseValue().value(this) < 0.0)
68 document()->accessSVGExtensions()->reportError("A negative value for image attribute <width> is not allowed");
69 } else if (attr->name() == SVGNames::heightAttr) {
70 setHeightBaseValue(SVGLength(LengthModeHeight, attr->value()));
71 addCSSProperty(attr, CSSPropertyHeight, attr->value());
72 if (heightBaseValue().value(this) < 0.0)
73 document()->accessSVGExtensions()->reportError("A negative value for image attribute <height> is not allowed");
74 } else {
75 if (SVGTests::parseMappedAttribute(attr))
76 return;
77 if (SVGLangSpace::parseMappedAttribute(attr))
78 return;
79 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
80 return;
81 if (SVGURIReference::parseMappedAttribute(attr))
82 return;
83 SVGStyledTransformableElement::parseMappedAttribute(attr);
84 }
85 }
86
svgAttributeChanged(const QualifiedName & attrName)87 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
88 {
89 SVGStyledTransformableElement::svgAttributeChanged(attrName);
90
91 if (SVGURIReference::isKnownAttribute(attrName))
92 m_imageLoader.updateFromElementIgnoringPreviousError();
93
94 if (!renderer())
95 return;
96
97 if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr ||
98 attrName == SVGNames::widthAttr || attrName == SVGNames::heightAttr ||
99 attrName == SVGNames::preserveAspectRatioAttr ||
100 SVGTests::isKnownAttribute(attrName) ||
101 SVGLangSpace::isKnownAttribute(attrName) ||
102 SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
103 SVGStyledTransformableElement::isKnownAttribute(attrName)) {
104 renderer()->setNeedsLayout(true);
105 }
106 }
107
synchronizeProperty(const QualifiedName & attrName)108 void SVGImageElement::synchronizeProperty(const QualifiedName& attrName)
109 {
110 SVGStyledTransformableElement::synchronizeProperty(attrName);
111
112 if (attrName == anyQName()) {
113 synchronizeX();
114 synchronizeY();
115 synchronizeWidth();
116 synchronizeHeight();
117 synchronizePreserveAspectRatio();
118 synchronizeExternalResourcesRequired();
119 synchronizeHref();
120 return;
121 }
122
123 if (attrName == SVGNames::xAttr)
124 synchronizeX();
125 else if (attrName == SVGNames::yAttr)
126 synchronizeY();
127 else if (attrName == SVGNames::widthAttr)
128 synchronizeWidth();
129 else if (attrName == SVGNames::heightAttr)
130 synchronizeHeight();
131 else if (attrName == SVGNames::preserveAspectRatioAttr)
132 synchronizePreserveAspectRatio();
133 else if (SVGExternalResourcesRequired::isKnownAttribute(attrName))
134 synchronizeExternalResourcesRequired();
135 else if (SVGURIReference::isKnownAttribute(attrName))
136 synchronizeHref();
137 }
138
hasRelativeValues() const139 bool SVGImageElement::hasRelativeValues() const
140 {
141 return (x().isRelative() || width().isRelative() ||
142 y().isRelative() || height().isRelative());
143 }
144
createRenderer(RenderArena * arena,RenderStyle *)145 RenderObject* SVGImageElement::createRenderer(RenderArena* arena, RenderStyle*)
146 {
147 return new (arena) RenderSVGImage(this);
148 }
149
haveLoadedRequiredResources()150 bool SVGImageElement::haveLoadedRequiredResources()
151 {
152 return !externalResourcesRequiredBaseValue() || m_imageLoader.haveFiredLoadEvent();
153 }
154
attach()155 void SVGImageElement::attach()
156 {
157 SVGStyledTransformableElement::attach();
158
159 if (RenderImage* imageObj = toRenderImage(renderer())) {
160 if (imageObj->hasImage())
161 return;
162
163 imageObj->setCachedImage(m_imageLoader.image());
164 }
165 }
166
insertedIntoDocument()167 void SVGImageElement::insertedIntoDocument()
168 {
169 SVGStyledTransformableElement::insertedIntoDocument();
170
171 // Update image loader, as soon as we're living in the tree.
172 // We can only resolve base URIs properly, after that!
173 m_imageLoader.updateFromElement();
174 }
175
imageSourceAttributeName() const176 const QualifiedName& SVGImageElement::imageSourceAttributeName() const
177 {
178 return XLinkNames::hrefAttr;
179 }
180
addSubresourceAttributeURLs(ListHashSet<KURL> & urls) const181 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
182 {
183 SVGStyledTransformableElement::addSubresourceAttributeURLs(urls);
184
185 addSubresourceURL(urls, document()->completeURL(href()));
186 }
187
188 }
189
190 #endif // ENABLE(SVG)
191