Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  * Copyright (C) 2004, 2005, 2006 Rob Buis <buis (at) kde.org>
      4  * Copyright (C) 2006 Samuel Weinig <sam.weinig (at) gmail.com>
      5  * Copyright (C) 2009 Dirk Schulze <krit (at) webkit.org>
      6  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Library General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Library General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Library General Public License
     19  * along with this library; see the file COPYING.LIB.  If not, write to
     20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     21  * Boston, MA 02110-1301, USA.
     22  */
     23 
     24 #include "config.h"
     25 
     26 #include "core/svg/SVGFilterElement.h"
     27 
     28 #include "SVGNames.h"
     29 #include "XLinkNames.h"
     30 #include "core/dom/NodeRenderingContext.h"
     31 #include "core/rendering/svg/RenderSVGResourceFilter.h"
     32 #include "core/svg/SVGElementInstance.h"
     33 #include "core/svg/SVGParserUtilities.h"
     34 
     35 namespace WebCore {
     36 
     37 // Animated property definitions
     38 DEFINE_ANIMATED_ENUMERATION(SVGFilterElement, SVGNames::filterUnitsAttr, FilterUnits, filterUnits, SVGUnitTypes::SVGUnitType)
     39 DEFINE_ANIMATED_ENUMERATION(SVGFilterElement, SVGNames::primitiveUnitsAttr, PrimitiveUnits, primitiveUnits, SVGUnitTypes::SVGUnitType)
     40 DEFINE_ANIMATED_LENGTH(SVGFilterElement, SVGNames::xAttr, X, x)
     41 DEFINE_ANIMATED_LENGTH(SVGFilterElement, SVGNames::yAttr, Y, y)
     42 DEFINE_ANIMATED_LENGTH(SVGFilterElement, SVGNames::widthAttr, Width, width)
     43 DEFINE_ANIMATED_LENGTH(SVGFilterElement, SVGNames::heightAttr, Height, height)
     44 DEFINE_ANIMATED_INTEGER_MULTIPLE_WRAPPERS(SVGFilterElement, SVGNames::filterResAttr, filterResXIdentifier(), FilterResX, filterResX)
     45 DEFINE_ANIMATED_INTEGER_MULTIPLE_WRAPPERS(SVGFilterElement, SVGNames::filterResAttr, filterResYIdentifier(), FilterResY, filterResY)
     46 DEFINE_ANIMATED_STRING(SVGFilterElement, XLinkNames::hrefAttr, Href, href)
     47 DEFINE_ANIMATED_BOOLEAN(SVGFilterElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
     48 
     49 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFilterElement)
     50     REGISTER_LOCAL_ANIMATED_PROPERTY(filterUnits)
     51     REGISTER_LOCAL_ANIMATED_PROPERTY(primitiveUnits)
     52     REGISTER_LOCAL_ANIMATED_PROPERTY(x)
     53     REGISTER_LOCAL_ANIMATED_PROPERTY(y)
     54     REGISTER_LOCAL_ANIMATED_PROPERTY(width)
     55     REGISTER_LOCAL_ANIMATED_PROPERTY(height)
     56     REGISTER_LOCAL_ANIMATED_PROPERTY(filterResX)
     57     REGISTER_LOCAL_ANIMATED_PROPERTY(filterResY)
     58     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
     59     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
     60 END_REGISTER_ANIMATED_PROPERTIES
     61 
     62 inline SVGFilterElement::SVGFilterElement(const QualifiedName& tagName, Document* document)
     63     : SVGElement(tagName, document)
     64     , m_filterUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
     65     , m_primitiveUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
     66     , m_x(LengthModeWidth, "-10%")
     67     , m_y(LengthModeHeight, "-10%")
     68     , m_width(LengthModeWidth, "120%")
     69     , m_height(LengthModeHeight, "120%")
     70 {
     71     // Spec: If the x/y attribute is not specified, the effect is as if a value of "-10%" were specified.
     72     // Spec: If the width/height attribute is not specified, the effect is as if a value of "120%" were specified.
     73     ASSERT(hasTagName(SVGNames::filterTag));
     74     ScriptWrappable::init(this);
     75     registerAnimatedPropertiesForSVGFilterElement();
     76 }
     77 
     78 PassRefPtr<SVGFilterElement> SVGFilterElement::create(const QualifiedName& tagName, Document* document)
     79 {
     80     return adoptRef(new SVGFilterElement(tagName, document));
     81 }
     82 
     83 const AtomicString& SVGFilterElement::filterResXIdentifier()
     84 {
     85     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGFilterResX", AtomicString::ConstructFromLiteral));
     86     return s_identifier;
     87 }
     88 
     89 const AtomicString& SVGFilterElement::filterResYIdentifier()
     90 {
     91     DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGFilterResY", AtomicString::ConstructFromLiteral));
     92     return s_identifier;
     93 }
     94 
     95 void SVGFilterElement::setFilterRes(unsigned filterResX, unsigned filterResY)
     96 {
     97     setFilterResXBaseValue(filterResX);
     98     setFilterResYBaseValue(filterResY);
     99 
    100     if (RenderObject* object = renderer())
    101         object->setNeedsLayout();
    102 }
    103 
    104 bool SVGFilterElement::isSupportedAttribute(const QualifiedName& attrName)
    105 {
    106     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
    107     if (supportedAttributes.isEmpty()) {
    108         SVGURIReference::addSupportedAttributes(supportedAttributes);
    109         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
    110         supportedAttributes.add(SVGNames::filterUnitsAttr);
    111         supportedAttributes.add(SVGNames::primitiveUnitsAttr);
    112         supportedAttributes.add(SVGNames::xAttr);
    113         supportedAttributes.add(SVGNames::yAttr);
    114         supportedAttributes.add(SVGNames::widthAttr);
    115         supportedAttributes.add(SVGNames::heightAttr);
    116         supportedAttributes.add(SVGNames::filterResAttr);
    117     }
    118     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
    119 }
    120 
    121 void SVGFilterElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
    122 {
    123     SVGParsingError parseError = NoError;
    124 
    125     if (!isSupportedAttribute(name))
    126         SVGElement::parseAttribute(name, value);
    127     else if (name == SVGNames::filterUnitsAttr) {
    128         SVGUnitTypes::SVGUnitType propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
    129         if (propertyValue > 0)
    130             setFilterUnitsBaseValue(propertyValue);
    131     } else if (name == SVGNames::primitiveUnitsAttr) {
    132         SVGUnitTypes::SVGUnitType propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
    133         if (propertyValue > 0)
    134             setPrimitiveUnitsBaseValue(propertyValue);
    135     } else if (name == SVGNames::xAttr)
    136         setXBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
    137     else if (name == SVGNames::yAttr)
    138         setYBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
    139     else if (name == SVGNames::widthAttr)
    140         setWidthBaseValue(SVGLength::construct(LengthModeWidth, value, parseError));
    141     else if (name == SVGNames::heightAttr)
    142         setHeightBaseValue(SVGLength::construct(LengthModeHeight, value, parseError));
    143     else if (name == SVGNames::filterResAttr) {
    144         float x, y;
    145         if (parseNumberOptionalNumber(value, x, y)) {
    146             setFilterResXBaseValue(x);
    147             setFilterResYBaseValue(y);
    148         }
    149     } else if (SVGURIReference::parseAttribute(name, value)
    150              || SVGExternalResourcesRequired::parseAttribute(name, value)) {
    151     } else
    152         ASSERT_NOT_REACHED();
    153 
    154     reportAttributeParsingError(parseError, name, value);
    155 }
    156 
    157 void SVGFilterElement::svgAttributeChanged(const QualifiedName& attrName)
    158 {
    159     if (!isSupportedAttribute(attrName)) {
    160         SVGElement::svgAttributeChanged(attrName);
    161         return;
    162     }
    163 
    164     SVGElementInstance::InvalidationGuard invalidationGuard(this);
    165 
    166     if (attrName == SVGNames::xAttr
    167         || attrName == SVGNames::yAttr
    168         || attrName == SVGNames::widthAttr
    169         || attrName == SVGNames::heightAttr)
    170         updateRelativeLengthsInformation();
    171 
    172     if (RenderObject* object = renderer())
    173         object->setNeedsLayout();
    174 }
    175 
    176 void SVGFilterElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
    177 {
    178     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
    179 
    180     if (changedByParser)
    181         return;
    182 
    183     if (RenderObject* object = renderer())
    184         object->setNeedsLayout();
    185 }
    186 
    187 RenderObject* SVGFilterElement::createRenderer(RenderStyle*)
    188 {
    189     return new RenderSVGResourceFilter(this);
    190 }
    191 
    192 bool SVGFilterElement::childShouldCreateRenderer(const NodeRenderingContext& childContext) const
    193 {
    194     if (!childContext.node()->isSVGElement())
    195         return false;
    196 
    197     SVGElement* svgElement = toSVGElement(childContext.node());
    198 
    199     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, allowedChildElementTags, ());
    200     if (allowedChildElementTags.isEmpty()) {
    201         allowedChildElementTags.add(SVGNames::feBlendTag);
    202         allowedChildElementTags.add(SVGNames::feColorMatrixTag);
    203         allowedChildElementTags.add(SVGNames::feComponentTransferTag);
    204         allowedChildElementTags.add(SVGNames::feCompositeTag);
    205         allowedChildElementTags.add(SVGNames::feConvolveMatrixTag);
    206         allowedChildElementTags.add(SVGNames::feDiffuseLightingTag);
    207         allowedChildElementTags.add(SVGNames::feDisplacementMapTag);
    208         allowedChildElementTags.add(SVGNames::feDistantLightTag);
    209         allowedChildElementTags.add(SVGNames::feDropShadowTag);
    210         allowedChildElementTags.add(SVGNames::feFloodTag);
    211         allowedChildElementTags.add(SVGNames::feFuncATag);
    212         allowedChildElementTags.add(SVGNames::feFuncBTag);
    213         allowedChildElementTags.add(SVGNames::feFuncGTag);
    214         allowedChildElementTags.add(SVGNames::feFuncRTag);
    215         allowedChildElementTags.add(SVGNames::feGaussianBlurTag);
    216         allowedChildElementTags.add(SVGNames::feImageTag);
    217         allowedChildElementTags.add(SVGNames::feMergeTag);
    218         allowedChildElementTags.add(SVGNames::feMergeNodeTag);
    219         allowedChildElementTags.add(SVGNames::feMorphologyTag);
    220         allowedChildElementTags.add(SVGNames::feOffsetTag);
    221         allowedChildElementTags.add(SVGNames::fePointLightTag);
    222         allowedChildElementTags.add(SVGNames::feSpecularLightingTag);
    223         allowedChildElementTags.add(SVGNames::feSpotLightTag);
    224         allowedChildElementTags.add(SVGNames::feTileTag);
    225         allowedChildElementTags.add(SVGNames::feTurbulenceTag);
    226     }
    227 
    228     return allowedChildElementTags.contains<SVGAttributeHashTranslator>(svgElement->tagQName());
    229 }
    230 
    231 bool SVGFilterElement::selfHasRelativeLengths() const
    232 {
    233     return xCurrentValue().isRelative()
    234         || yCurrentValue().isRelative()
    235         || widthCurrentValue().isRelative()
    236         || heightCurrentValue().isRelative();
    237 }
    238 
    239 }
    240