File indexing completed on 2024-04-28 15:24:32

0001 /*
0002     Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
0003                   2004, 2005, 2006 Rob Buis <buis@kde.org>
0004 
0005     This file is part of the KDE project
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #if ENABLE(SVG) && ENABLE(SVG_FILTERS)
0024 #include "SVGFilterPrimitiveStandardAttributes.h"
0025 
0026 #include "SVGFilterElement.h"
0027 #include "SVGFilterEffect.h"
0028 #include "SVGLength.h"
0029 #include "SVGNames.h"
0030 #include "SVGUnitTypes.h"
0031 
0032 namespace WebCore
0033 {
0034 
0035 SVGFilterPrimitiveStandardAttributes::SVGFilterPrimitiveStandardAttributes(const QualifiedName &tagName, Document *doc)
0036     : SVGStyledElement(tagName, doc)
0037     , m_x(this, LengthModeWidth)
0038     , m_y(this, LengthModeHeight)
0039     , m_width(this, LengthModeWidth)
0040     , m_height(this, LengthModeHeight)
0041 {
0042     // Spec: If the attribute is not specified, the effect is as if a value of "0%" were specified.
0043     setXBaseValue(SVGLength(this, LengthModeWidth, "0%"));
0044     setYBaseValue(SVGLength(this, LengthModeHeight, "0%"));
0045 
0046     // Spec: If the attribute is not specified, the effect is as if a value of "100%" were specified.
0047     setWidthBaseValue(SVGLength(this, LengthModeWidth, "100%"));
0048     setHeightBaseValue(SVGLength(this, LengthModeHeight, "100%"));
0049 }
0050 
0051 SVGFilterPrimitiveStandardAttributes::~SVGFilterPrimitiveStandardAttributes()
0052 {
0053 }
0054 
0055 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterPrimitiveStandardAttributes, SVGLength, Length, length, X, x, SVGNames::xAttr, m_x)
0056 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterPrimitiveStandardAttributes, SVGLength, Length, length, Y, y, SVGNames::yAttr, m_y)
0057 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterPrimitiveStandardAttributes, SVGLength, Length, length, Width, width, SVGNames::widthAttr, m_width)
0058 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterPrimitiveStandardAttributes, SVGLength, Length, length, Height, height, SVGNames::heightAttr, m_height)
0059 ANIMATED_PROPERTY_DEFINITIONS(SVGFilterPrimitiveStandardAttributes, String, String, string, Result, result, SVGNames::resultAttr, m_result)
0060 
0061 void SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(MappedAttribute *attr)
0062 {
0063     const AtomicString &value = attr->value();
0064     if (attr->name() == SVGNames::xAttr) {
0065         setXBaseValue(SVGLength(this, LengthModeWidth, value));
0066     } else if (attr->name() == SVGNames::yAttr) {
0067         setYBaseValue(SVGLength(this, LengthModeHeight, value));
0068     } else if (attr->name() == SVGNames::widthAttr) {
0069         setWidthBaseValue(SVGLength(this, LengthModeWidth, value));
0070     } else if (attr->name() == SVGNames::heightAttr) {
0071         setHeightBaseValue(SVGLength(this, LengthModeHeight, value));
0072     } else if (attr->name() == SVGNames::resultAttr) {
0073         setResultBaseValue(value);
0074     } else {
0075         return SVGStyledElement::parseMappedAttribute(attr);
0076     }
0077 }
0078 
0079 void SVGFilterPrimitiveStandardAttributes::setStandardAttributes(SVGFilterEffect *filterEffect) const
0080 {
0081     ASSERT(filterEffect);
0082     if (!filterEffect) {
0083         return;
0084     }
0085 
0086     ASSERT(filterEffect->filter());
0087 
0088     float _x, _y, _width, _height;
0089 
0090     if (filterEffect->filter()->effectBoundingBoxMode()) {
0091         _x = x().valueAsPercentage();
0092         _y = y().valueAsPercentage();
0093         _width = width().valueAsPercentage();
0094         _height = height().valueAsPercentage();
0095     } else {
0096         // We need to resolve any percentages in filter rect space.
0097         if (x().unitType() == LengthTypePercentage) {
0098             filterEffect->setXBoundingBoxMode(true);
0099             _x = x().valueAsPercentage();
0100         } else {
0101             filterEffect->setXBoundingBoxMode(false);
0102             _x = x().value();
0103         }
0104 
0105         if (y().unitType() == LengthTypePercentage) {
0106             filterEffect->setYBoundingBoxMode(true);
0107             _y = y().valueAsPercentage();
0108         } else {
0109             filterEffect->setYBoundingBoxMode(false);
0110             _y = y().value();
0111         }
0112 
0113         if (width().unitType() == LengthTypePercentage) {
0114             filterEffect->setWidthBoundingBoxMode(true);
0115             _width = width().valueAsPercentage();
0116         } else {
0117             filterEffect->setWidthBoundingBoxMode(false);
0118             _width = width().value();
0119         }
0120 
0121         if (height().unitType() == LengthTypePercentage) {
0122             filterEffect->setHeightBoundingBoxMode(true);
0123             _height = height().valueAsPercentage();
0124         } else {
0125             filterEffect->setHeightBoundingBoxMode(false);
0126             _height = height().value();
0127         }
0128     }
0129 
0130     filterEffect->setSubRegion(FloatRect(_x, _y, _width, _height));
0131     filterEffect->setResult(result());
0132 }
0133 
0134 }
0135 
0136 #endif // ENABLE(SVG)
0137