- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 #include "core/svg/SVGImageElement.h"
25
26 #include "CSSPropertyNames.h"
27 #include "SVGNames.h"
28 #include "XLinkNames.h"
29 #include "core/rendering/RenderImageResource.h"
30 #include "core/rendering/svg/RenderSVGImage.h"
31 #include "core/rendering/svg/RenderSVGResource.h"
32 #include "core/svg/SVGElementInstance.h"
33
34 namespace WebCore {
35
36 // Animated property definitions
37 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::xAttr, X, x)
38 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::yAttr, Y, y)
39 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::widthAttr, Width, width)
40 DEFINE_ANIMATED_LENGTH(SVGImageElement, SVGNames::heightAttr, Height, height)
41 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
42 DEFINE_ANIMATED_STRING(SVGImageElement, XLinkNames::hrefAttr, Href, href)
43 DEFINE_ANIMATED_BOOLEAN(SVGImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
44
45 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGImageElement)
46     REGISTER_LOCAL_ANIMATED_PROPERTY(x)
47     REGISTER_LOCAL_ANIMATED_PROPERTY(y)
48     REGISTER_LOCAL_ANIMATED_PROPERTY(width)
49     REGISTER_LOCAL_ANIMATED_PROPERTY(height)
50     REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAspectRatio)
51     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
52     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
53     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGGraphicsElement)
54 END_REGISTER_ANIMATED_PROPERTIES
55
56 inline SVGImageElement::SVGImageElement(const QualifiedName& tagName, Document& document)
57     : SVGGraphicsElement(tagName, document)
58     , m_x(LengthModeWidth)
59     , m_y(LengthModeHeight)
60     , m_width(LengthModeWidth)
61     , m_height(LengthModeHeight)
62     , m_imageLoader(this)
63 {
64     ASSERT(hasTagName(SVGNames::imageTag));
65     ScriptWrappable::init(this);
66     registerAnimatedPropertiesForSVGImageElement();
67 }
68
69 PassRefPtr<SVGImageElement> SVGImageElement::create(const QualifiedName& tagName, Document& document)
70 {
71     return adoptRef(new SVGImageElement(tagName, document));
72 }
73
74 bool SVGImageElement::currentFrameHasSingleSecurityOrigin() const
75 {
76     if (RenderSVGImage* renderSVGImage = toRenderSVGImage(renderer())) {
77         if (renderSVGImage->imageResource()->hasImage()) {
78             if (Image* image = renderSVGImage->imageResource()->cachedImage()->image())
79                 return image->currentFrameHasSingleSecurityOrigin();
80         }
81     }
82
83     return true;
84 }
85
86 bool SVGImageElement::isSupportedAttribute(const QualifiedName& attrName)
87 {
88     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
89     if (supportedAttributes.isEmpty()) {
90         SVGLangSpace::addSupportedAttributes(supportedAttributes);
91         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
92         SVGURIReference::addSupportedAttributes(supportedAttributes);
93         supportedAttributes.add(SVGNames::xAttr);
94         supportedAttributes.add(SVGNames::yAttr);
95         supportedAttributes.add(SVGNames::widthAttr);
96         supportedAttributes.add(SVGNames::heightAttr);
97         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
98     }
99     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
100 }
101
102 bool SVGImageElement::isPresentationAttribute(const QualifiedName& name) const
103 {
104     if (name == SVGNames::widthAttr || name == SVGNames::heightAttr)
105         return true;
106     return SVGGraphicsElement::isPresentationAttribute(name);
107 }
108
109 void SVGImageElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
110 {
111     if (!isSupportedAttribute(name))
112         SVGGraphicsElement::collectStyleForPresentationAttribute(name, value, style);
113     else if (name == SVGNames::widthAttr)
114         addPropertyToPresentationAttributeStyle(style, CSSPropertyWidth, value);
115     else if (name == SVGNames::heightAttr)
116         addPropertyToPresentationAttributeStyle(style, CSSPropertyHeight, value);
117 }
118
119 void SVGImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
120 {
121     SVGParsingError parseError = NoError;
122
123     if (!isSupportedAttribute(name))
124         SVGGraphicsElement::parseAttribute(name, value);
125     else if (name == SVGNames::xAttr)
126         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
127     else if (name == SVGNames::yAttr)
128         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
129     else if (name == SVGNames::preserveAspectRatioAttr) {
130         SVGPreserveAspectRatio preserveAspectRatio;
131         preserveAspectRatio.parse(value);
132         setPreserveAspectRatioBaseValue(preserveAspectRatio);
133     } else if (name == SVGNames::widthAttr)
134         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError, ForbidNegativeLengths));
135     else if (name == SVGNames::heightAttr)
136         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError, ForbidNegativeLengths));
137     else if (SVGLangSpace::parseAttribute(name, value)
138              || SVGExternalResourcesRequired::parseAttribute(name, value)
139              || SVGURIReference::parseAttribute(name, value)) {
140     } else
141         ASSERT_NOT_REACHED();
142
143     reportAttributeParsingError(parseError, name, value);
144 }
145
146 void SVGImageElement::svgAttributeChanged(const QualifiedName& attrName)
147 {
148     if (!isSupportedAttribute(attrName)) {
149         SVGGraphicsElement::svgAttributeChanged(attrName);
150         return;
151     }
152
153     SVGElementInstance::InvalidationGuard invalidationGuard(this);
154
155     bool isLengthAttribute = attrName == SVGNames::xAttr
156                           || attrName == SVGNames::yAttr
157                           || attrName == SVGNames::widthAttr
158                           || attrName == SVGNames::heightAttr;
159
160     if (isLengthAttribute)
161         updateRelativeLengthsInformation();
162
163     if (SVGURIReference::isKnownAttribute(attrName)) {
164         m_imageLoader.updateFromElementIgnoringPreviousError();
165         return;
166     }
167
168     RenderObject* renderer = this->renderer();
169     if (!renderer)
170         return;
171
172     if (isLengthAttribute) {
173         if (toRenderSVGImage(renderer)->updateImageViewport())
174             RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
175         return;
176     }
177
178     if (attrName == SVGNames::preserveAspectRatioAttr
179         || SVGLangSpace::isKnownAttribute(attrName)
180         || SVGExternalResourcesRequired::isKnownAttribute(attrName)) {
181         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
182         return;
183     }
184
185     ASSERT_NOT_REACHED();
186 }
187
188 bool SVGImageElement::selfHasRelativeLengths() const
189 {
190     return xCurrentValue().isRelative()
191         || yCurrentValue().isRelative()
192         || widthCurrentValue().isRelative()
193         || heightCurrentValue().isRelative();
194 }
195
196 RenderObject* SVGImageElement::createRenderer(RenderStyle*)
197 {
198     return new RenderSVGImage(this);
199 }
200
201 bool SVGImageElement::haveLoadedRequiredResources()
202 {
203     return !externalResourcesRequiredBaseValue() || !m_imageLoader.hasPendingActivity();
204 }
205
206 void SVGImageElement::attach(const AttachContext& context)
207 {
208     SVGGraphicsElement::attach(context);
209
210     if (RenderSVGImage* imageObj = toRenderSVGImage(renderer())) {
211         if (imageObj->imageResource()->hasImage())
212             return;
213
214         imageObj->imageResource()->setImageResource(m_imageLoader.image());
215     }
216 }
217
218 Node::InsertionNotificationRequest SVGImageElement::insertedInto(ContainerNode* rootParent)
219 {
220     SVGGraphicsElement::insertedInto(rootParent);
221     if (!rootParent->inDocument())
222         return InsertionDone;
223     // Update image loader, as soon as we're living in the tree.
224     // We can only resolve base URIs properly, after that!
225     m_imageLoader.updateFromElement();
226     return InsertionDone;
227 }
228
229 const AtomicString SVGImageElement::imageSourceURL() const
230 {
231     return hrefCurrentValue();
232 }
233
234 void SVGImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
235 {
236     SVGGraphicsElement::addSubresourceAttributeURLs(urls);
237
238     addSubresourceURL(urls, document().completeURL(hrefCurrentValue()));
239 }
240
241 void SVGImageElement::didMoveToNewDocument(Document& oldDocument)
242 {
243     m_imageLoader.elementDidMoveToNewDocument();
244     SVGGraphicsElement::didMoveToNewDocument(oldDocument);
245 }
246
247 }