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 "core/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/SVGPreserveAspectRatio.h"
32 #include "platform/graphics/Image.h"
36 inline SVGFEImageElement::SVGFEImageElement(Document& document)
37 : SVGFilterPrimitiveStandardAttributes(SVGNames::feImageTag, document)
38 , SVGURIReference(this)
39 , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
41 ScriptWrappable::init(this);
42 addToPropertyMap(m_preserveAspectRatio);
45 DEFINE_NODE_FACTORY(SVGFEImageElement)
47 SVGFEImageElement::~SVGFEImageElement()
51 m_cachedImage->removeClient(this);
55 clearResourceReferences();
59 bool SVGFEImageElement::currentFrameHasSingleSecurityOrigin() const
61 if (m_cachedImage && m_cachedImage->image())
62 return m_cachedImage->image()->currentFrameHasSingleSecurityOrigin();
67 void SVGFEImageElement::clearResourceReferences()
70 m_cachedImage->removeClient(this);
74 removeAllOutgoingReferences();
77 void SVGFEImageElement::fetchImageResource()
79 FetchRequest request(ResourceRequest(ownerDocument()->completeURL(hrefString())), localName());
80 m_cachedImage = document().fetcher()->fetchImage(request);
83 m_cachedImage->addClient(this);
86 void SVGFEImageElement::buildPendingResource()
88 clearResourceReferences();
93 Element* target = SVGURIReference::targetElementFromIRIString(hrefString(), treeScope(), &id);
98 document().accessSVGExtensions().addPendingResource(id, this);
99 ASSERT(hasPendingResources());
101 } else if (target->isSVGElement()) {
102 // Register us with the target in the dependencies map. Any change of hrefElement
103 // that leads to relayout/repainting now informs us, so we can react to it.
104 addReferenceTo(toSVGElement(target));
110 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
112 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
113 if (supportedAttributes.isEmpty()) {
114 SVGURIReference::addSupportedAttributes(supportedAttributes);
115 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
117 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
120 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
122 parseAttributeNew(name, value);
125 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
127 if (!isSupportedAttribute(attrName)) {
128 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
132 SVGElement::InvalidationGuard invalidationGuard(this);
134 if (attrName == SVGNames::preserveAspectRatioAttr) {
139 if (SVGURIReference::isKnownAttribute(attrName)) {
140 buildPendingResource();
144 ASSERT_NOT_REACHED();
147 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent)
149 SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
150 buildPendingResource();
151 return InsertionDone;
154 void SVGFEImageElement::removedFrom(ContainerNode* rootParent)
156 SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
157 if (rootParent->inDocument())
158 clearResourceReferences();
161 void SVGFEImageElement::notifyFinished(Resource*)
166 Element* parent = parentElement();
169 if (!isSVGFilterElement(*parent) || !parent->renderer())
172 if (RenderObject* renderer = this->renderer())
173 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
176 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
179 return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), m_preserveAspectRatio->currentValue());
180 return FEImage::createWithIRIReference(filter, treeScope(), hrefString(), m_preserveAspectRatio->currentValue());