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/dom/ElementTraversal.h"
     29 #include "core/rendering/svg/RenderSVGPath.h"
     30 #include "core/rendering/svg/RenderSVGResourceLinearGradient.h"
     31 #include "core/rendering/svg/RenderSVGResourceRadialGradient.h"
     32 #include "core/svg/SVGStopElement.h"
     33 #include "core/svg/SVGTransformList.h"
     34 
     35 namespace blink {
     36 
     37 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGSpreadMethodType>()
     38 {
     39     DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
     40     if (entries.isEmpty()) {
     41         entries.append(std::make_pair(SVGSpreadMethodPad, "pad"));
     42         entries.append(std::make_pair(SVGSpreadMethodReflect, "reflect"));
     43         entries.append(std::make_pair(SVGSpreadMethodRepeat, "repeat"));
     44     }
     45     return entries;
     46 }
     47 
     48 SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document& document)
     49     : SVGElement(tagName, document)
     50     , SVGURIReference(this)
     51     , m_gradientTransform(SVGAnimatedTransformList::create(this, SVGNames::gradientTransformAttr, SVGTransformList::create()))
     52     , m_spreadMethod(SVGAnimatedEnumeration<SVGSpreadMethodType>::create(this, SVGNames::spreadMethodAttr, SVGSpreadMethodPad))
     53     , m_gradientUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::gradientUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX))
     54 {
     55     addToPropertyMap(m_gradientTransform);
     56     addToPropertyMap(m_spreadMethod);
     57     addToPropertyMap(m_gradientUnits);
     58 }
     59 
     60 bool SVGGradientElement::isSupportedAttribute(const QualifiedName& attrName)
     61 {
     62     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
     63     if (supportedAttributes.isEmpty()) {
     64         SVGURIReference::addSupportedAttributes(supportedAttributes);
     65         supportedAttributes.add(SVGNames::gradientUnitsAttr);
     66         supportedAttributes.add(SVGNames::gradientTransformAttr);
     67         supportedAttributes.add(SVGNames::spreadMethodAttr);
     68     }
     69     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
     70 }
     71 
     72 void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
     73 {
     74     parseAttributeNew(name, value);
     75 }
     76 
     77 void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
     78 {
     79     if (!isSupportedAttribute(attrName)) {
     80         SVGElement::svgAttributeChanged(attrName);
     81         return;
     82     }
     83 
     84     SVGElement::InvalidationGuard invalidationGuard(this);
     85 
     86     RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
     87     if (renderer)
     88         renderer->invalidateCacheAndMarkForLayout();
     89 }
     90 
     91 void SVGGradientElement::childrenChanged(const ChildrenChange& change)
     92 {
     93     SVGElement::childrenChanged(change);
     94 
     95     if (change.byParser)
     96         return;
     97 
     98     if (RenderObject* object = renderer())
     99         object->setNeedsLayoutAndFullPaintInvalidation();
    100 }
    101 
    102 Vector<Gradient::ColorStop> SVGGradientElement::buildStops()
    103 {
    104     Vector<Gradient::ColorStop> stops;
    105 
    106     float previousOffset = 0.0f;
    107     for (SVGStopElement* stop = Traversal<SVGStopElement>::firstChild(*this); stop; stop = Traversal<SVGStopElement>::nextSibling(*stop)) {
    108         // Figure out right monotonic offset
    109         float offset = stop->offset()->currentValue()->value();
    110         offset = std::min(std::max(previousOffset, offset), 1.0f);
    111         previousOffset = offset;
    112 
    113         stops.append(Gradient::ColorStop(offset, stop->stopColorIncludingOpacity()));
    114     }
    115 
    116     return stops;
    117 }
    118 
    119 }
    120