Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis (at) kde.org>
      4  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
      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/SVGGradientElement.h"
     25 
     26 #include "core/XLinkNames.h"
     27 #include "core/dom/Attribute.h"
     28 #include "core/rendering/svg/RenderSVGPath.h"
     29 #include "core/rendering/svg/RenderSVGResourceLinearGradient.h"
     30 #include "core/rendering/svg/RenderSVGResourceRadialGradient.h"
     31 #include "core/svg/SVGStopElement.h"
     32 #include "core/svg/SVGTransformList.h"
     33 
     34 namespace WebCore {
     35 
     36 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGSpreadMethodType>()
     37 {
     38     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
     39     if (entries.isEmpty()) {
     40         entries.append(std::make_pair(SVGSpreadMethodPad, "pad"));
     41         entries.append(std::make_pair(SVGSpreadMethodReflect, "reflect"));
     42         entries.append(std::make_pair(SVGSpreadMethodRepeat, "repeat"));
     43     }
     44     return entries;
     45 }
     46 
     47 SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document& document)
     48     : SVGElement(tagName, document)
     49     , SVGURIReference(this)
     50     , m_gradientTransform(SVGAnimatedTransformList::create(this, SVGNames::gradientTransformAttr, SVGTransformList::create()))
     51     , m_spreadMethod(SVGAnimatedEnumeration<SVGSpreadMethodType>::create(this, SVGNames::spreadMethodAttr, SVGSpreadMethodPad))
     52     , m_gradientUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::gradientUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX))
     53 {
     54     addToPropertyMap(m_gradientTransform);
     55     addToPropertyMap(m_spreadMethod);
     56     addToPropertyMap(m_gradientUnits);
     57 }
     58 
     59 bool SVGGradientElement::isSupportedAttribute(const QualifiedName& attrName)
     60 {
     61     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
     62     if (supportedAttributes.isEmpty()) {
     63         SVGURIReference::addSupportedAttributes(supportedAttributes);
     64         supportedAttributes.add(SVGNames::gradientUnitsAttr);
     65         supportedAttributes.add(SVGNames::gradientTransformAttr);
     66         supportedAttributes.add(SVGNames::spreadMethodAttr);
     67     }
     68     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
     69 }
     70 
     71 void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
     72 {
     73     if (!isSupportedAttribute(name)) {
     74         SVGElement::parseAttribute(name, value);
     75         return;
     76     }
     77 
     78     SVGParsingError parseError = NoError;
     79 
     80     if (name == SVGNames::gradientTransformAttr)
     81         m_gradientTransform->setBaseValueAsString(value, parseError);
     82     else if (name == SVGNames::gradientUnitsAttr)
     83         m_gradientUnits->setBaseValueAsString(value, parseError);
     84     else if (name == SVGNames::spreadMethodAttr)
     85         m_spreadMethod->setBaseValueAsString(value, parseError);
     86     else if (SVGURIReference::parseAttribute(name, value, parseError)) {
     87     } else
     88         ASSERT_NOT_REACHED();
     89 
     90     reportAttributeParsingError(parseError, name, value);
     91 }
     92 
     93 void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
     94 {
     95     if (!isSupportedAttribute(attrName)) {
     96         SVGElement::svgAttributeChanged(attrName);
     97         return;
     98     }
     99 
    100     SVGElement::InvalidationGuard invalidationGuard(this);
    101 
    102     RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
    103     if (renderer)
    104         renderer->invalidateCacheAndMarkForLayout();
    105 }
    106 
    107 void SVGGradientElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
    108 {
    109     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
    110 
    111     if (changedByParser)
    112         return;
    113 
    114     if (RenderObject* object = renderer())
    115         object->setNeedsLayoutAndFullPaintInvalidation();
    116 }
    117 
    118 Vector<Gradient::ColorStop> SVGGradientElement::buildStops()
    119 {
    120     Vector<Gradient::ColorStop> stops;
    121 
    122     float previousOffset = 0.0f;
    123     for (SVGStopElement* stop = Traversal<SVGStopElement>::firstChild(*this); stop; stop = Traversal<SVGStopElement>::nextSibling(*stop)) {
    124         // Figure out right monotonic offset
    125         float offset = stop->offset()->currentValue()->value();
    126         offset = std::min(std::max(previousOffset, offset), 1.0f);
    127         previousOffset = offset;
    128 
    129         stops.append(Gradient::ColorStop(offset, stop->stopColorIncludingOpacity()));
    130     }
    131 
    132     return stops;
    133 }
    134 
    135 }
    136