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
39 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEImageElement)
40 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
41 END_REGISTER_ANIMATED_PROPERTIES
43 inline SVGFEImageElement::SVGFEImageElement(Document& document)
44 : SVGFilterPrimitiveStandardAttributes(SVGNames::feImageTag, document)
45 , SVGURIReference(this)
46 , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
48 ScriptWrappable::init(this);
49 addToPropertyMap(m_preserveAspectRatio);
50 registerAnimatedPropertiesForSVGFEImageElement();
53 PassRefPtr<SVGFEImageElement> SVGFEImageElement::create(Document& document)
55 return adoptRef(new SVGFEImageElement(document));
58 SVGFEImageElement::~SVGFEImageElement()
60 clearResourceReferences();
63 bool SVGFEImageElement::currentFrameHasSingleSecurityOrigin() const
65 if (m_cachedImage && m_cachedImage->image())
66 return m_cachedImage->image()->currentFrameHasSingleSecurityOrigin();
71 void SVGFEImageElement::clearResourceReferences()
74 m_cachedImage->removeClient(this);
78 document().accessSVGExtensions()->removeAllTargetReferencesForElement(this);
81 void SVGFEImageElement::fetchImageResource()
83 FetchRequest request(ResourceRequest(ownerDocument()->completeURL(hrefString())), localName());
84 m_cachedImage = document().fetcher()->fetchImage(request);
87 m_cachedImage->addClient(this);
90 void SVGFEImageElement::buildPendingResource()
92 clearResourceReferences();
97 Element* target = SVGURIReference::targetElementFromIRIString(hrefString(), document(), &id);
100 fetchImageResource();
102 document().accessSVGExtensions()->addPendingResource(id, this);
103 ASSERT(hasPendingResources());
105 } else if (target->isSVGElement()) {
106 // Register us with the target in the dependencies map. Any change of hrefElement
107 // that leads to relayout/repainting now informs us, so we can react to it.
108 document().accessSVGExtensions()->addElementReferencingTarget(this, toSVGElement(target));
114 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
116 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
117 if (supportedAttributes.isEmpty()) {
118 SVGURIReference::addSupportedAttributes(supportedAttributes);
119 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
121 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
124 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
126 if (!isSupportedAttribute(name)) {
127 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
131 SVGParsingError parseError = NoError;
133 if (name == SVGNames::preserveAspectRatioAttr) {
134 m_preserveAspectRatio->setBaseValueAsString(value, parseError);
135 } else if (SVGURIReference::parseAttribute(name, value, parseError)) {
137 ASSERT_NOT_REACHED();
140 reportAttributeParsingError(parseError, name, value);
143 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
145 if (!isSupportedAttribute(attrName)) {
146 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
150 SVGElementInstance::InvalidationGuard invalidationGuard(this);
152 if (attrName == SVGNames::preserveAspectRatioAttr) {
157 if (SVGURIReference::isKnownAttribute(attrName)) {
158 buildPendingResource();
162 ASSERT_NOT_REACHED();
165 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent)
167 SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
168 buildPendingResource();
169 return InsertionDone;
172 void SVGFEImageElement::removedFrom(ContainerNode* rootParent)
174 SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
175 if (rootParent->inDocument())
176 clearResourceReferences();
179 void SVGFEImageElement::notifyFinished(Resource*)
184 Element* parent = parentElement();
187 if (!parent->hasTagName(SVGNames::filterTag) || !parent->renderer())
190 if (RenderObject* renderer = this->renderer())
191 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
194 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
197 return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), m_preserveAspectRatio->currentValue());
198 return FEImage::createWithIRIReference(filter, document(), hrefString(), m_preserveAspectRatio->currentValue());