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 "SVGNames.h" 27 #include "XLinkNames.h" 28 #include "core/dom/Attribute.h" 29 #include "core/rendering/svg/RenderSVGHiddenContainer.h" 30 #include "core/rendering/svg/RenderSVGPath.h" 31 #include "core/rendering/svg/RenderSVGResourceLinearGradient.h" 32 #include "core/rendering/svg/RenderSVGResourceRadialGradient.h" 33 #include "core/svg/SVGElementInstance.h" 34 #include "core/svg/SVGStopElement.h" 35 #include "core/svg/SVGTransformList.h" 36 #include "core/svg/SVGTransformable.h" 37 38 namespace WebCore { 39 40 // Animated property definitions 41 DEFINE_ANIMATED_ENUMERATION(SVGGradientElement, SVGNames::spreadMethodAttr, SpreadMethod, spreadMethod, SVGSpreadMethodType) 42 DEFINE_ANIMATED_ENUMERATION(SVGGradientElement, SVGNames::gradientUnitsAttr, GradientUnits, gradientUnits, SVGUnitTypes::SVGUnitType) 43 DEFINE_ANIMATED_TRANSFORM_LIST(SVGGradientElement, SVGNames::gradientTransformAttr, GradientTransform, gradientTransform) 44 DEFINE_ANIMATED_STRING(SVGGradientElement, XLinkNames::hrefAttr, Href, href) 45 DEFINE_ANIMATED_BOOLEAN(SVGGradientElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired) 46 47 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGGradientElement) 48 REGISTER_LOCAL_ANIMATED_PROPERTY(spreadMethod) 49 REGISTER_LOCAL_ANIMATED_PROPERTY(gradientUnits) 50 REGISTER_LOCAL_ANIMATED_PROPERTY(gradientTransform) 51 REGISTER_LOCAL_ANIMATED_PROPERTY(href) 52 REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired) 53 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGElement) 54 END_REGISTER_ANIMATED_PROPERTIES 55 56 SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document* document) 57 : SVGElement(tagName, document) 58 , m_spreadMethod(SVGSpreadMethodPad) 59 , m_gradientUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) 60 { 61 ScriptWrappable::init(this); 62 registerAnimatedPropertiesForSVGGradientElement(); 63 } 64 65 bool SVGGradientElement::isSupportedAttribute(const QualifiedName& attrName) 66 { 67 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ()); 68 if (supportedAttributes.isEmpty()) { 69 SVGURIReference::addSupportedAttributes(supportedAttributes); 70 SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes); 71 supportedAttributes.add(SVGNames::gradientUnitsAttr); 72 supportedAttributes.add(SVGNames::gradientTransformAttr); 73 supportedAttributes.add(SVGNames::spreadMethodAttr); 74 } 75 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName); 76 } 77 78 void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value) 79 { 80 if (!isSupportedAttribute(name)) { 81 SVGElement::parseAttribute(name, value); 82 return; 83 } 84 85 if (name == SVGNames::gradientUnitsAttr) { 86 SVGUnitTypes::SVGUnitType propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value); 87 if (propertyValue > 0) 88 setGradientUnitsBaseValue(propertyValue); 89 return; 90 } 91 92 if (name == SVGNames::gradientTransformAttr) { 93 SVGTransformList newList; 94 newList.parse(value); 95 detachAnimatedGradientTransformListWrappers(newList.size()); 96 setGradientTransformBaseValue(newList); 97 return; 98 } 99 100 if (name == SVGNames::spreadMethodAttr) { 101 SVGSpreadMethodType propertyValue = SVGPropertyTraits<SVGSpreadMethodType>::fromString(value); 102 if (propertyValue > 0) 103 setSpreadMethodBaseValue(propertyValue); 104 return; 105 } 106 107 if (SVGURIReference::parseAttribute(name, value)) 108 return; 109 if (SVGExternalResourcesRequired::parseAttribute(name, value)) 110 return; 111 112 ASSERT_NOT_REACHED(); 113 } 114 115 void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName) 116 { 117 if (!isSupportedAttribute(attrName)) { 118 SVGElement::svgAttributeChanged(attrName); 119 return; 120 } 121 122 SVGElementInstance::InvalidationGuard invalidationGuard(this); 123 124 if (RenderObject* object = renderer()) 125 object->setNeedsLayout(); 126 } 127 128 void SVGGradientElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) 129 { 130 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); 131 132 if (changedByParser) 133 return; 134 135 if (RenderObject* object = renderer()) 136 object->setNeedsLayout(); 137 } 138 139 Vector<Gradient::ColorStop> SVGGradientElement::buildStops() 140 { 141 Vector<Gradient::ColorStop> stops; 142 143 float previousOffset = 0.0f; 144 for (Node* n = firstChild(); n; n = n->nextSibling()) { 145 SVGElement* element = n->isSVGElement() ? toSVGElement(n) : 0; 146 if (!element || !element->isGradientStop()) 147 continue; 148 149 SVGStopElement* stop = toSVGStopElement(element); 150 Color color = stop->stopColorIncludingOpacity(); 151 152 // Figure out right monotonic offset 153 float offset = stop->offsetCurrentValue(); 154 offset = std::min(std::max(previousOffset, offset), 1.0f); 155 previousOffset = offset; 156 157 // Extract individual channel values 158 // FIXME: Why doesn't ColorStop take a Color and an offset?? 159 float r, g, b, a; 160 color.getRGBA(r, g, b, a); 161 162 stops.append(Gradient::ColorStop(offset, r, g, b, a)); 163 } 164 165 return stops; 166 } 167 168 } 169