1 18 package org.apache.batik.css.engine.value.svg; 19 20 import org.apache.batik.css.engine.CSSEngine; 21 import org.apache.batik.css.engine.CSSStylableElement; 22 import org.apache.batik.css.engine.StyleMap; 23 import org.apache.batik.css.engine.value.ListValue; 24 import org.apache.batik.css.engine.value.Value; 25 import org.apache.batik.css.engine.value.ValueManager; 26 import org.apache.batik.util.CSSConstants; 27 import org.w3c.css.sac.LexicalUnit; 28 import org.w3c.dom.DOMException ; 29 import org.w3c.dom.css.CSSValue; 30 31 37 public class SVGColorManager extends ColorManager { 38 39 42 protected String property; 43 44 47 protected Value defaultValue; 48 49 53 public SVGColorManager(String prop) { 54 this(prop, SVGValueConstants.BLACK_RGB_VALUE); 55 } 56 57 60 public SVGColorManager(String prop, Value v) { 61 property = prop; 62 defaultValue = v; 63 } 64 65 68 public boolean isInheritedProperty() { 69 return false; 70 } 71 72 75 public String getPropertyName() { 76 return property; 77 } 78 79 80 84 public Value getDefaultValue() { 85 return defaultValue; 86 } 87 88 91 public Value createValue(LexicalUnit lu, CSSEngine engine) 92 throws DOMException { 93 if (lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) { 94 if (lu.getStringValue().equalsIgnoreCase 95 (CSSConstants.CSS_CURRENTCOLOR_VALUE)) { 96 return SVGValueConstants.CURRENTCOLOR_VALUE; 97 } 98 } 99 Value v = super.createValue(lu, engine); 100 lu = lu.getNextLexicalUnit(); 101 if (lu == null) { 102 return v; 103 } 104 if (lu.getLexicalUnitType() != LexicalUnit.SAC_FUNCTION || 105 !lu.getFunctionName().equalsIgnoreCase("icc-color")) { 106 throw createInvalidLexicalUnitDOMException 107 (lu.getLexicalUnitType()); 108 } 109 lu = lu.getParameters(); 110 if (lu.getLexicalUnitType() != LexicalUnit.SAC_IDENT) { 111 throw createInvalidLexicalUnitDOMException 112 (lu.getLexicalUnitType()); 113 } 114 ListValue result = new ListValue(' '); 115 result.append(v); 116 117 ICCColor icc = new ICCColor(lu.getStringValue()); 118 result.append(icc); 119 120 lu = lu.getNextLexicalUnit(); 121 while (lu != null) { 122 if (lu.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) { 123 throw createInvalidLexicalUnitDOMException 124 (lu.getLexicalUnitType()); 125 } 126 lu = lu.getNextLexicalUnit(); 127 if (lu == null) { 128 throw createInvalidLexicalUnitDOMException((short)-1); 129 } 130 icc.append(getColorValue(lu)); 131 lu = lu.getNextLexicalUnit(); 132 } 133 return result; 134 } 135 136 140 public Value computeValue(CSSStylableElement elt, 141 String pseudo, 142 CSSEngine engine, 143 int idx, 144 StyleMap sm, 145 Value value) { 146 if (value == SVGValueConstants.CURRENTCOLOR_VALUE) { 147 sm.putColorRelative(idx, true); 148 149 int ci = engine.getColorIndex(); 150 return engine.getComputedStyle(elt, pseudo, ci); 151 } 152 if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) { 153 ListValue lv = (ListValue)value; 154 Value v = lv.item(0); 155 Value t = super.computeValue(elt, pseudo, engine, idx, sm, v); 156 if (t != v) { 157 ListValue result = new ListValue(' '); 158 result.append(t); 159 result.append(lv.item(1)); 160 return result; 161 } 162 return value; 163 } 164 return super.computeValue(elt, pseudo, engine, idx, sm, value); 165 } 166 167 170 protected float getColorValue(LexicalUnit lu) { 171 switch (lu.getLexicalUnitType()) { 172 case LexicalUnit.SAC_INTEGER: 173 return lu.getIntegerValue(); 174 case LexicalUnit.SAC_REAL: 175 return lu.getFloatValue(); 176 } 177 throw createInvalidLexicalUnitDOMException(lu.getLexicalUnitType()); 178 } 179 } 180 | Popular Tags |