1 /*
2 Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005 Rob Buis <buis@kde.org>
4 2010 Dirk Schulze <krit@webkit.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) && ENABLE(FILTERS)
25 #include "SVGFEImageElement.h"
26
27 #include "Attr.h"
28 #include "CachedImage.h"
29 #include "DocLoader.h"
30 #include "Document.h"
31 #include "MappedAttribute.h"
32 #include "SVGLength.h"
33 #include "SVGNames.h"
34 #include "SVGPreserveAspectRatio.h"
35 #include "SVGRenderSupport.h"
36 #include "SVGResourceFilter.h"
37
38 namespace WebCore {
39
SVGFEImageElement(const QualifiedName & tagName,Document * doc)40 SVGFEImageElement::SVGFEImageElement(const QualifiedName& tagName, Document* doc)
41 : SVGFilterPrimitiveStandardAttributes(tagName, doc)
42 , SVGURIReference()
43 , SVGLangSpace()
44 , SVGExternalResourcesRequired()
45 {
46 }
47
~SVGFEImageElement()48 SVGFEImageElement::~SVGFEImageElement()
49 {
50 if (m_cachedImage)
51 m_cachedImage->removeClient(this);
52 }
53
requestImageResource()54 void SVGFEImageElement::requestImageResource()
55 {
56 if (m_cachedImage) {
57 m_cachedImage->removeClient(this);
58 m_cachedImage = 0;
59 }
60
61 Element* hrefElement = document()->getElementById(SVGURIReference::getTarget(href()));
62 if (hrefElement && hrefElement->isSVGElement() && hrefElement->renderer())
63 return;
64
65 m_cachedImage = ownerDocument()->docLoader()->requestImage(href());
66
67 if (m_cachedImage)
68 m_cachedImage->addClient(this);
69 }
70
parseMappedAttribute(MappedAttribute * attr)71 void SVGFEImageElement::parseMappedAttribute(MappedAttribute* attr)
72 {
73 const String& value = attr->value();
74 if (attr->name() == SVGNames::preserveAspectRatioAttr)
75 SVGPreserveAspectRatio::parsePreserveAspectRatio(this, value);
76 else {
77 if (SVGURIReference::parseMappedAttribute(attr)) {
78 requestImageResource();
79 return;
80 }
81 if (SVGLangSpace::parseMappedAttribute(attr))
82 return;
83 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
84 return;
85
86 SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
87 }
88 }
89
synchronizeProperty(const QualifiedName & attrName)90 void SVGFEImageElement::synchronizeProperty(const QualifiedName& attrName)
91 {
92 SVGFilterPrimitiveStandardAttributes::synchronizeProperty(attrName);
93
94 if (attrName == anyQName()) {
95 synchronizePreserveAspectRatio();
96 synchronizeHref();
97 synchronizeExternalResourcesRequired();
98 return;
99 }
100
101 if (attrName == SVGNames::preserveAspectRatioAttr)
102 synchronizePreserveAspectRatio();
103 else if (SVGURIReference::isKnownAttribute(attrName))
104 synchronizeHref();
105 else if (SVGExternalResourcesRequired::isKnownAttribute(attrName))
106 synchronizeExternalResourcesRequired();
107 }
108
notifyFinished(CachedResource *)109 void SVGFEImageElement::notifyFinished(CachedResource*)
110 {
111 SVGStyledElement::invalidateResourcesInAncestorChain();
112 }
113
build(SVGResourceFilter * filterResource)114 bool SVGFEImageElement::build(SVGResourceFilter* filterResource)
115 {
116 if (!m_cachedImage && !m_targetImage) {
117 Element* hrefElement = document()->getElementById(SVGURIReference::getTarget(href()));
118 if (!hrefElement || !hrefElement->isSVGElement())
119 return false;
120
121 RenderObject* renderer = hrefElement->renderer();
122 if (!renderer)
123 return false;
124
125 IntRect targetRect = enclosingIntRect(renderer->objectBoundingBox());
126 m_targetImage = ImageBuffer::create(targetRect.size(), LinearRGB);
127
128 renderSubtreeToImage(m_targetImage.get(), renderer);
129 }
130
131 RefPtr<FilterEffect> effect = FEImage::create(m_targetImage ? m_targetImage->image() : m_cachedImage->image(), preserveAspectRatio());
132 filterResource->addFilterEffect(this, effect.release());
133
134 return true;
135 }
136
addSubresourceAttributeURLs(ListHashSet<KURL> & urls) const137 void SVGFEImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
138 {
139 SVGFilterPrimitiveStandardAttributes::addSubresourceAttributeURLs(urls);
140
141 addSubresourceURL(urls, document()->completeURL(href()));
142 }
143
144 }
145
146 #endif // ENABLE(SVG)
147