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.FloatValue; 25 import org.apache.batik.css.engine.value.IdentifierManager; 26 import org.apache.batik.css.engine.value.LengthManager; 27 import org.apache.batik.css.engine.value.StringMap; 28 import org.apache.batik.css.engine.value.Value; 29 import org.apache.batik.css.engine.value.ValueConstants; 30 import org.apache.batik.css.engine.value.ValueManager; 31 import org.apache.batik.util.CSSConstants; 32 import org.w3c.css.sac.LexicalUnit; 33 import org.w3c.dom.DOMException ; 34 import org.w3c.dom.css.CSSPrimitiveValue; 35 36 42 public class FontSizeManager extends LengthManager { 43 44 47 protected final static StringMap values = new StringMap(); 48 static { 49 values.put(CSSConstants.CSS_ALL_VALUE, 50 ValueConstants.ALL_VALUE); 51 values.put(CSSConstants.CSS_LARGE_VALUE, 52 ValueConstants.LARGE_VALUE); 53 values.put(CSSConstants.CSS_LARGER_VALUE, 54 ValueConstants.LARGER_VALUE); 55 values.put(CSSConstants.CSS_MEDIUM_VALUE, 56 ValueConstants.MEDIUM_VALUE); 57 values.put(CSSConstants.CSS_SMALL_VALUE, 58 ValueConstants.SMALL_VALUE); 59 values.put(CSSConstants.CSS_SMALLER_VALUE, 60 ValueConstants.SMALLER_VALUE); 61 values.put(CSSConstants.CSS_X_LARGE_VALUE, 62 ValueConstants.X_LARGE_VALUE); 63 values.put(CSSConstants.CSS_X_SMALL_VALUE, 64 ValueConstants.X_SMALL_VALUE); 65 values.put(CSSConstants.CSS_XX_LARGE_VALUE, 66 ValueConstants.XX_LARGE_VALUE); 67 values.put(CSSConstants.CSS_XX_SMALL_VALUE, 68 ValueConstants.XX_SMALL_VALUE); 69 } 70 71 74 public StringMap getIdentifiers() { 75 return values; 76 } 77 78 81 public boolean isInheritedProperty() { 82 return true; 83 } 84 85 88 public String getPropertyName() { 89 return CSSConstants.CSS_FONT_SIZE_PROPERTY; 90 } 91 92 95 public Value getDefaultValue() { 96 return ValueConstants.MEDIUM_VALUE; 97 } 98 99 102 public Value createValue(LexicalUnit lu, CSSEngine engine) 103 throws DOMException { 104 switch (lu.getLexicalUnitType()) { 105 case LexicalUnit.SAC_INHERIT: 106 return ValueConstants.INHERIT_VALUE; 107 108 case LexicalUnit.SAC_IDENT: 109 String s = lu.getStringValue().toLowerCase().intern(); 110 Object v = values.get(s); 111 if (v == null) { 112 throw createInvalidIdentifierDOMException(s); 113 } 114 return (Value)v; 115 default: 116 break; 117 } 118 return super.createValue(lu, engine); 119 } 120 121 125 public Value createStringValue(short type, String value, CSSEngine engine) 126 throws DOMException { 127 if (type != CSSPrimitiveValue.CSS_IDENT) { 128 throw createInvalidStringTypeDOMException(type); 129 } 130 Object v = values.get(value.toLowerCase().intern()); 131 if (v == null) { 132 throw createInvalidIdentifierDOMException(value); 133 } 134 return (Value)v; 135 } 136 137 141 public Value computeValue(CSSStylableElement elt, 142 String pseudo, 143 CSSEngine engine, 144 int idx, 145 StyleMap sm, 146 Value value) { 147 float scale = 1.0f; 148 boolean doParentRelative = false; 149 150 switch (value.getPrimitiveType()) { 151 case CSSPrimitiveValue.CSS_NUMBER: 152 case CSSPrimitiveValue.CSS_PX: 153 return value; 154 155 case CSSPrimitiveValue.CSS_MM: 156 CSSContext ctx = engine.getCSSContext(); 157 float v = value.getFloatValue(); 158 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 159 v / ctx.getPixelUnitToMillimeter()); 160 161 case CSSPrimitiveValue.CSS_CM: 162 ctx = engine.getCSSContext(); 163 v = value.getFloatValue(); 164 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 165 v * 10f / ctx.getPixelUnitToMillimeter()); 166 167 case CSSPrimitiveValue.CSS_IN: 168 ctx = engine.getCSSContext(); 169 v = value.getFloatValue(); 170 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 171 v * 25.4f / ctx.getPixelUnitToMillimeter()); 172 173 case CSSPrimitiveValue.CSS_PT: 174 ctx = engine.getCSSContext(); 175 v = value.getFloatValue(); 176 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 177 v * 25.4f / 178 (72f * ctx.getPixelUnitToMillimeter())); 179 180 case CSSPrimitiveValue.CSS_PC: 181 ctx = engine.getCSSContext(); 182 v = value.getFloatValue(); 183 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, 184 (v * 25.4f / 185 (6f * ctx.getPixelUnitToMillimeter()))); 186 187 case CSSPrimitiveValue.CSS_EMS: 188 doParentRelative = true; 189 scale = value.getFloatValue(); 190 break; 191 case CSSPrimitiveValue.CSS_EXS: 192 doParentRelative = true; 193 scale = value.getFloatValue()*0.5f; break; 195 case CSSPrimitiveValue.CSS_PERCENTAGE: 196 doParentRelative = true; 197 scale = value.getFloatValue()*0.01f; 198 break; 199 default: 200 } 201 202 if (value == ValueConstants.LARGER_VALUE) { 203 doParentRelative = true; 204 scale = 1.2f; 205 } else if (value == ValueConstants.SMALLER_VALUE) { 206 doParentRelative = true; 207 scale = 1/1.2f; 208 } 209 210 if (doParentRelative) { 211 sm.putParentRelative(idx, true); 212 213 CSSStylableElement p; 214 p = CSSEngine.getParentCSSStylableElement(elt); 215 float fs; 216 if (p == null) { 217 CSSContext ctx = engine.getCSSContext(); 218 fs = ctx.getMediumFontSize(); 219 } else { 220 fs = engine.getComputedStyle(p, null, idx).getFloatValue(); 221 } 222 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, fs * scale); 223 } 224 225 CSSContext ctx = engine.getCSSContext(); 227 float fs = ctx.getMediumFontSize(); 228 String s = value.getStringValue(); 229 switch (s.charAt(0)) { 230 case 'm': 231 break; 232 233 case 's': 234 fs = (float)(fs / 1.2); 235 break; 236 237 case 'l': 238 fs = (float)(fs * 1.2); 239 break; 240 241 default: switch (s.charAt(1)) { 243 case 'x': 244 switch (s.charAt(3)) { 245 case 's': 246 fs = (float)(((fs / 1.2) / 1.2) / 1.2); 247 break; 248 249 default: fs = (float)(fs * 1.2 * 1.2 * 1.2); 251 } 252 break; 253 254 default: switch (s.charAt(2)) { 256 case 's': 257 fs = (float)((fs / 1.2) / 1.2); 258 break; 259 260 default: fs = (float)(fs * 1.2 * 1.2); 262 } 263 } 264 } 265 return new FloatValue(CSSPrimitiveValue.CSS_NUMBER, fs); 266 } 267 268 269 273 protected int getOrientation() { 274 return VERTICAL_ORIENTATION; } 276 } 277 | Popular Tags |