1 18 package org.apache.batik.css.engine.value.css2; 19 20 import org.apache.batik.css.engine.CSSContext; 21 import org.apache.batik.css.engine.CSSEngine; 22 import org.apache.batik.css.engine.CSSStylableElement; 23 import org.apache.batik.css.engine.StyleMap; 24 import org.apache.batik.css.engine.value.IdentifierManager; 25 import org.apache.batik.css.engine.value.StringMap; 26 import org.apache.batik.css.engine.value.Value; 27 import org.apache.batik.css.engine.value.ValueConstants; 28 import org.apache.batik.css.engine.value.ValueManager; 29 import org.apache.batik.util.CSSConstants; 30 import org.w3c.css.sac.LexicalUnit; 31 import org.w3c.dom.DOMException ; 32 import org.w3c.dom.css.CSSPrimitiveValue; 33 34 40 public class FontWeightManager extends IdentifierManager { 41 42 45 protected final static StringMap values = new StringMap(); 46 static { 47 values.put(CSSConstants.CSS_ALL_VALUE, 48 ValueConstants.ALL_VALUE); 49 values.put(CSSConstants.CSS_BOLD_VALUE, 50 ValueConstants.BOLD_VALUE); 51 values.put(CSSConstants.CSS_BOLDER_VALUE, 52 ValueConstants.BOLDER_VALUE); 53 values.put(CSSConstants.CSS_LIGHTER_VALUE, 54 ValueConstants.LIGHTER_VALUE); 55 values.put(CSSConstants.CSS_NORMAL_VALUE, 56 ValueConstants.NORMAL_VALUE); 57 } 58 59 62 public boolean isInheritedProperty() { 63 return true; 64 } 65 66 69 public String getPropertyName() { 70 return CSSConstants.CSS_FONT_WEIGHT_PROPERTY; 71 } 72 73 76 public Value getDefaultValue() { 77 return ValueConstants.NORMAL_VALUE; 78 } 79 80 83 public Value createValue(LexicalUnit lu, CSSEngine engine) 84 throws DOMException { 85 if (lu.getLexicalUnitType() == LexicalUnit.SAC_INTEGER) { 86 int i = lu.getIntegerValue(); 87 switch (i) { 88 case 100: 89 return ValueConstants.NUMBER_100; 90 case 200: 91 return ValueConstants.NUMBER_200; 92 case 300: 93 return ValueConstants.NUMBER_300; 94 case 400: 95 return ValueConstants.NUMBER_400; 96 case 500: 97 return ValueConstants.NUMBER_500; 98 case 600: 99 return ValueConstants.NUMBER_600; 100 case 700: 101 return ValueConstants.NUMBER_700; 102 case 800: 103 return ValueConstants.NUMBER_800; 104 case 900: 105 return ValueConstants.NUMBER_900; 106 } 107 throw createInvalidFloatValueDOMException(i); 108 } 109 return super.createValue(lu, engine); 110 } 111 112 115 public Value createFloatValue(short type, float floatValue) 116 throws DOMException { 117 if (type == CSSPrimitiveValue.CSS_NUMBER) { 118 int i = (int)floatValue; 119 if (floatValue == i) { 120 switch (i) { 121 case 100: 122 return ValueConstants.NUMBER_100; 123 case 200: 124 return ValueConstants.NUMBER_200; 125 case 300: 126 return ValueConstants.NUMBER_300; 127 case 400: 128 return ValueConstants.NUMBER_400; 129 case 500: 130 return ValueConstants.NUMBER_500; 131 case 600: 132 return ValueConstants.NUMBER_600; 133 case 700: 134 return ValueConstants.NUMBER_700; 135 case 800: 136 return ValueConstants.NUMBER_800; 137 case 900: 138 return ValueConstants.NUMBER_900; 139 } 140 } 141 } 142 throw createInvalidFloatValueDOMException(floatValue); 143 } 144 145 149 public Value computeValue(CSSStylableElement elt, 150 String pseudo, 151 CSSEngine engine, 152 int idx, 153 StyleMap sm, 154 Value value) { 155 if (value == ValueConstants.BOLDER_VALUE) { 156 sm.putParentRelative(idx, true); 157 158 CSSContext ctx = engine.getCSSContext(); 159 CSSStylableElement p = CSSEngine.getParentCSSStylableElement(elt); 160 float fw; 161 if (p == null) { 162 fw = 400; 163 } else { 164 Value v = engine.getComputedStyle(p, pseudo, idx); 165 fw = v.getFloatValue(); 166 } 167 return createFontWeight(ctx.getBolderFontWeight(fw)); 168 } else if (value == ValueConstants.LIGHTER_VALUE) { 169 sm.putParentRelative(idx, true); 170 171 CSSContext ctx = engine.getCSSContext(); 172 CSSStylableElement p = CSSEngine.getParentCSSStylableElement(elt); 173 float fw; 174 if (p == null) { 175 fw = 400; 176 } else { 177 Value v = engine.getComputedStyle(p, pseudo, idx); 178 fw = v.getFloatValue(); 179 } 180 return createFontWeight(ctx.getLighterFontWeight(fw)); 181 } else if (value == ValueConstants.NORMAL_VALUE) { 182 return ValueConstants.NUMBER_400; 183 } else if (value == ValueConstants.BOLD_VALUE) { 184 return ValueConstants.NUMBER_700; 185 } 186 return value; 187 } 188 189 192 protected Value createFontWeight(float f) { 193 switch ((int)f) { 194 case 100: 195 return ValueConstants.NUMBER_100; 196 case 200: 197 return ValueConstants.NUMBER_200; 198 case 300: 199 return ValueConstants.NUMBER_300; 200 case 400: 201 return ValueConstants.NUMBER_400; 202 case 500: 203 return ValueConstants.NUMBER_500; 204 case 600: 205 return ValueConstants.NUMBER_600; 206 case 700: 207 return ValueConstants.NUMBER_700; 208 case 800: 209 return ValueConstants.NUMBER_800; 210 default: return ValueConstants.NUMBER_900; 212 } 213 } 214 215 218 public StringMap getIdentifiers() { 219 return values; 220 } 221 } 222 | Popular Tags |