1 /* 2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann (at) kde.org> 3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis (at) kde.org> 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21 #include "config.h" 22 23 #include "core/svg/SVGFEColorMatrixElement.h" 24 25 #include "SVGNames.h" 26 #include "core/platform/graphics/filters/FilterEffect.h" 27 #include "core/svg/SVGElementInstance.h" 28 #include "core/svg/graphics/filters/SVGFilterBuilder.h" 29 30 namespace WebCore { 31 32 // Animated property definitions 33 DEFINE_ANIMATED_STRING(SVGFEColorMatrixElement, SVGNames::inAttr, In1, in1) 34 DEFINE_ANIMATED_ENUMERATION(SVGFEColorMatrixElement, SVGNames::typeAttr, Type, type, ColorMatrixType) 35 DEFINE_ANIMATED_NUMBER_LIST(SVGFEColorMatrixElement, SVGNames::valuesAttr, Values, values) 36 37 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEColorMatrixElement) 38 REGISTER_LOCAL_ANIMATED_PROPERTY(in1) 39 REGISTER_LOCAL_ANIMATED_PROPERTY(type) 40 REGISTER_LOCAL_ANIMATED_PROPERTY(values) 41 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes) 42 END_REGISTER_ANIMATED_PROPERTIES 43 44 inline SVGFEColorMatrixElement::SVGFEColorMatrixElement(const QualifiedName& tagName, Document* document) 45 : SVGFilterPrimitiveStandardAttributes(tagName, document) 46 , m_type(FECOLORMATRIX_TYPE_MATRIX) 47 { 48 ASSERT(hasTagName(SVGNames::feColorMatrixTag)); 49 ScriptWrappable::init(this); 50 registerAnimatedPropertiesForSVGFEColorMatrixElement(); 51 } 52 53 PassRefPtr<SVGFEColorMatrixElement> SVGFEColorMatrixElement::create(const QualifiedName& tagName, Document* document) 54 { 55 return adoptRef(new SVGFEColorMatrixElement(tagName, document)); 56 } 57 58 bool SVGFEColorMatrixElement::isSupportedAttribute(const QualifiedName& attrName) 59 { 60 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ()); 61 if (supportedAttributes.isEmpty()) { 62 supportedAttributes.add(SVGNames::typeAttr); 63 supportedAttributes.add(SVGNames::valuesAttr); 64 supportedAttributes.add(SVGNames::inAttr); 65 } 66 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName); 67 } 68 69 void SVGFEColorMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value) 70 { 71 if (!isSupportedAttribute(name)) { 72 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value); 73 return; 74 } 75 76 if (name == SVGNames::typeAttr) { 77 ColorMatrixType propertyValue = SVGPropertyTraits<ColorMatrixType>::fromString(value); 78 if (propertyValue > 0) 79 setTypeBaseValue(propertyValue); 80 return; 81 } 82 83 if (name == SVGNames::inAttr) { 84 setIn1BaseValue(value); 85 return; 86 } 87 88 if (name == SVGNames::valuesAttr) { 89 SVGNumberList newList; 90 newList.parse(value); 91 detachAnimatedValuesListWrappers(newList.size()); 92 setValuesBaseValue(newList); 93 return; 94 } 95 96 ASSERT_NOT_REACHED(); 97 } 98 99 bool SVGFEColorMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName) 100 { 101 FEColorMatrix* colorMatrix = static_cast<FEColorMatrix*>(effect); 102 if (attrName == SVGNames::typeAttr) 103 return colorMatrix->setType(typeCurrentValue()); 104 if (attrName == SVGNames::valuesAttr) 105 return colorMatrix->setValues(valuesCurrentValue().toFloatVector()); 106 107 ASSERT_NOT_REACHED(); 108 return false; 109 } 110 111 void SVGFEColorMatrixElement::svgAttributeChanged(const QualifiedName& attrName) 112 { 113 if (!isSupportedAttribute(attrName)) { 114 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName); 115 return; 116 } 117 118 SVGElementInstance::InvalidationGuard invalidationGuard(this); 119 120 if (attrName == SVGNames::typeAttr || attrName == SVGNames::valuesAttr) { 121 primitiveAttributeChanged(attrName); 122 return; 123 } 124 125 if (attrName == SVGNames::inAttr) { 126 invalidate(); 127 return; 128 } 129 130 ASSERT_NOT_REACHED(); 131 } 132 133 PassRefPtr<FilterEffect> SVGFEColorMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter) 134 { 135 FilterEffect* input1 = filterBuilder->getEffectById(in1CurrentValue()); 136 137 if (!input1) 138 return 0; 139 140 Vector<float> filterValues; 141 ColorMatrixType filterType = typeCurrentValue(); 142 143 // Use defaults if values is empty (SVG 1.1 15.10). 144 if (!hasAttribute(SVGNames::valuesAttr)) { 145 switch (filterType) { 146 case FECOLORMATRIX_TYPE_MATRIX: 147 for (size_t i = 0; i < 20; i++) 148 filterValues.append((i % 6) ? 0 : 1); 149 break; 150 case FECOLORMATRIX_TYPE_HUEROTATE: 151 filterValues.append(0); 152 break; 153 case FECOLORMATRIX_TYPE_SATURATE: 154 filterValues.append(1); 155 break; 156 default: 157 break; 158 } 159 } else { 160 SVGNumberList& values = valuesCurrentValue(); 161 unsigned size = values.size(); 162 163 if ((filterType == FECOLORMATRIX_TYPE_MATRIX && size != 20) 164 || (filterType == FECOLORMATRIX_TYPE_HUEROTATE && size != 1) 165 || (filterType == FECOLORMATRIX_TYPE_SATURATE && size != 1)) 166 return 0; 167 168 filterValues = values.toFloatVector(); 169 } 170 171 RefPtr<FilterEffect> effect = FEColorMatrix::create(filter, filterType, filterValues); 172 effect->inputEffects().append(input1); 173 return effect.release(); 174 } 175 176 } // namespace WebCore 177