1 /* 2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann (at) kde.org> 3 * Copyright (C) 2004, 2005 Rob Buis <buis (at) kde.org> 4 * Copyright (C) 2010 Dirk Schulze <krit (at) 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 #include "core/svg/SVGFEImageElement.h" 25 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" 33 34 namespace WebCore { 35 36 inline SVGFEImageElement::SVGFEImageElement(Document& document) 37 : SVGFilterPrimitiveStandardAttributes(SVGNames::feImageTag, document) 38 , SVGURIReference(this) 39 , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create())) 40 { 41 ScriptWrappable::init(this); 42 addToPropertyMap(m_preserveAspectRatio); 43 } 44 45 DEFINE_NODE_FACTORY(SVGFEImageElement) 46 47 SVGFEImageElement::~SVGFEImageElement() 48 { 49 #if ENABLE(OILPAN) 50 if (m_cachedImage) { 51 m_cachedImage->removeClient(this); 52 m_cachedImage = 0; 53 } 54 #else 55 clearResourceReferences(); 56 #endif 57 } 58 59 bool SVGFEImageElement::currentFrameHasSingleSecurityOrigin() const 60 { 61 if (m_cachedImage && m_cachedImage->image()) 62 return m_cachedImage->image()->currentFrameHasSingleSecurityOrigin(); 63 64 return true; 65 } 66 67 void SVGFEImageElement::clearResourceReferences() 68 { 69 if (m_cachedImage) { 70 m_cachedImage->removeClient(this); 71 m_cachedImage = 0; 72 } 73 74 document().accessSVGExtensions().removeAllTargetReferencesForElement(this); 75 } 76 77 void SVGFEImageElement::fetchImageResource() 78 { 79 FetchRequest request(ResourceRequest(ownerDocument()->completeURL(hrefString())), localName()); 80 m_cachedImage = document().fetcher()->fetchImage(request); 81 82 if (m_cachedImage) 83 m_cachedImage->addClient(this); 84 } 85 86 void SVGFEImageElement::buildPendingResource() 87 { 88 clearResourceReferences(); 89 if (!inDocument()) 90 return; 91 92 AtomicString id; 93 Element* target = SVGURIReference::targetElementFromIRIString(hrefString(), treeScope(), &id); 94 if (!target) { 95 if (id.isEmpty()) 96 fetchImageResource(); 97 else { 98 document().accessSVGExtensions().addPendingResource(id, this); 99 ASSERT(hasPendingResources()); 100 } 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 document().accessSVGExtensions().addElementReferencingTarget(this, toSVGElement(target)); 105 } 106 107 invalidate(); 108 } 109 110 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName) 111 { 112 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ()); 113 if (supportedAttributes.isEmpty()) { 114 SVGURIReference::addSupportedAttributes(supportedAttributes); 115 supportedAttributes.add(SVGNames::preserveAspectRatioAttr); 116 } 117 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName); 118 } 119 120 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value) 121 { 122 if (!isSupportedAttribute(name)) { 123 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value); 124 return; 125 } 126 127 SVGParsingError parseError = NoError; 128 129 if (name == SVGNames::preserveAspectRatioAttr) { 130 m_preserveAspectRatio->setBaseValueAsString(value, parseError); 131 } else if (SVGURIReference::parseAttribute(name, value, parseError)) { 132 } else { 133 ASSERT_NOT_REACHED(); 134 } 135 136 reportAttributeParsingError(parseError, name, value); 137 } 138 139 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName) 140 { 141 if (!isSupportedAttribute(attrName)) { 142 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName); 143 return; 144 } 145 146 SVGElement::InvalidationGuard invalidationGuard(this); 147 148 if (attrName == SVGNames::preserveAspectRatioAttr) { 149 invalidate(); 150 return; 151 } 152 153 if (SVGURIReference::isKnownAttribute(attrName)) { 154 buildPendingResource(); 155 return; 156 } 157 158 ASSERT_NOT_REACHED(); 159 } 160 161 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent) 162 { 163 SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent); 164 buildPendingResource(); 165 return InsertionDone; 166 } 167 168 void SVGFEImageElement::removedFrom(ContainerNode* rootParent) 169 { 170 SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent); 171 if (rootParent->inDocument()) 172 clearResourceReferences(); 173 } 174 175 void SVGFEImageElement::notifyFinished(Resource*) 176 { 177 if (!inDocument()) 178 return; 179 180 Element* parent = parentElement(); 181 ASSERT(parent); 182 183 if (!isSVGFilterElement(*parent) || !parent->renderer()) 184 return; 185 186 if (RenderObject* renderer = this->renderer()) 187 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer); 188 } 189 190 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter) 191 { 192 if (m_cachedImage) 193 return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), m_preserveAspectRatio->currentValue()); 194 return FEImage::createWithIRIReference(filter, treeScope(), hrefString(), m_preserveAspectRatio->currentValue()); 195 } 196 197 } 198