2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4 * Copyright (C) 2010 Dirk Schulze <krit@webkit.org>
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.
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.
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.
24 #include "core/svg/SVGFEImageElement.h"
26 #include "XLinkNames.h"
27 #include "core/dom/Document.h"
28 #include "core/fetch/FetchRequest.h"
29 #include "core/fetch/ResourceFetcher.h"
30 #include "core/rendering/svg/RenderSVGResource.h"
31 #include "core/svg/SVGElementInstance.h"
32 #include "core/svg/SVGPreserveAspectRatio.h"
33 #include "platform/graphics/Image.h"
37 // Animated property definitions
38 DEFINE_ANIMATED_STRING(SVGFEImageElement, XLinkNames::hrefAttr, Href, href)
40 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEImageElement)
41 REGISTER_LOCAL_ANIMATED_PROPERTY(href)
42 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
43 END_REGISTER_ANIMATED_PROPERTIES
45 inline SVGFEImageElement::SVGFEImageElement(Document& document)
46 : SVGFilterPrimitiveStandardAttributes(SVGNames::feImageTag, document)
47 , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
49 ScriptWrappable::init(this);
50 addToPropertyMap(m_preserveAspectRatio);
51 registerAnimatedPropertiesForSVGFEImageElement();
54 PassRefPtr<SVGFEImageElement> SVGFEImageElement::create(Document& document)
56 return adoptRef(new SVGFEImageElement(document));
59 SVGFEImageElement::~SVGFEImageElement()
61 clearResourceReferences();
64 bool SVGFEImageElement::currentFrameHasSingleSecurityOrigin() const
66 if (m_cachedImage && m_cachedImage->image())
67 return m_cachedImage->image()->currentFrameHasSingleSecurityOrigin();
72 void SVGFEImageElement::clearResourceReferences()
75 m_cachedImage->removeClient(this);
79 document().accessSVGExtensions()->removeAllTargetReferencesForElement(this);
82 void SVGFEImageElement::fetchImageResource()
84 FetchRequest request(ResourceRequest(ownerDocument()->completeURL(hrefCurrentValue())), localName());
85 m_cachedImage = document().fetcher()->fetchImage(request);
88 m_cachedImage->addClient(this);
91 void SVGFEImageElement::buildPendingResource()
93 clearResourceReferences();
98 Element* target = SVGURIReference::targetElementFromIRIString(hrefCurrentValue(), document(), &id);
101 fetchImageResource();
103 document().accessSVGExtensions()->addPendingResource(id, this);
104 ASSERT(hasPendingResources());
106 } else if (target->isSVGElement()) {
107 // Register us with the target in the dependencies map. Any change of hrefElement
108 // that leads to relayout/repainting now informs us, so we can react to it.
109 document().accessSVGExtensions()->addElementReferencingTarget(this, toSVGElement(target));
115 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
117 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
118 if (supportedAttributes.isEmpty()) {
119 SVGURIReference::addSupportedAttributes(supportedAttributes);
120 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
122 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
125 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
127 if (!isSupportedAttribute(name)) {
128 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
132 SVGParsingError parseError = NoError;
134 if (name == SVGNames::preserveAspectRatioAttr) {
135 m_preserveAspectRatio->setBaseValueAsString(value, parseError);
136 } else if (SVGURIReference::parseAttribute(name, value)) {
138 ASSERT_NOT_REACHED();
141 reportAttributeParsingError(parseError, name, value);
144 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
146 if (!isSupportedAttribute(attrName)) {
147 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
151 SVGElementInstance::InvalidationGuard invalidationGuard(this);
153 if (attrName == SVGNames::preserveAspectRatioAttr) {
158 if (SVGURIReference::isKnownAttribute(attrName)) {
159 buildPendingResource();
163 ASSERT_NOT_REACHED();
166 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent)
168 SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
169 buildPendingResource();
170 return InsertionDone;
173 void SVGFEImageElement::removedFrom(ContainerNode* rootParent)
175 SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
176 if (rootParent->inDocument())
177 clearResourceReferences();
180 void SVGFEImageElement::notifyFinished(Resource*)
185 Element* parent = parentElement();
188 if (!parent->hasTagName(SVGNames::filterTag) || !parent->renderer())
191 if (RenderObject* renderer = this->renderer())
192 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
195 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
198 return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), m_preserveAspectRatio->currentValue());
199 return FEImage::createWithIRIReference(filter, document(), hrefCurrentValue(), m_preserveAspectRatio->currentValue());