1 18 19 package org.apache.batik.css.engine.value.css2; 20 21 import java.util.HashSet ; 22 import java.util.Set ; 23 24 import org.apache.batik.css.engine.CSSEngine; 25 import org.apache.batik.css.engine.value.ValueManager; 26 import org.apache.batik.css.engine.value.IdentifierManager; 27 import org.apache.batik.css.engine.value.AbstractValueFactory; 28 import org.apache.batik.css.engine.value.ShorthandManager; 29 import org.apache.batik.css.engine.value.StringMap; 30 import org.apache.batik.css.parser.CSSLexicalUnit; 31 import org.apache.batik.util.CSSConstants; 32 import org.w3c.css.sac.LexicalUnit; 33 import org.w3c.dom.DOMException ; 34 35 53 public class FontShorthandManager 54 extends AbstractValueFactory 55 implements ShorthandManager { 56 57 public FontShorthandManager() { } 58 59 62 public String getPropertyName() { 63 return CSSConstants.CSS_FONT_PROPERTY; 64 } 65 66 static LexicalUnit NORMAL_LU = CSSLexicalUnit.createString 67 (LexicalUnit.SAC_IDENT, CSSConstants.CSS_NORMAL_VALUE, null); 68 static LexicalUnit BOLD_LU = CSSLexicalUnit.createString 69 (LexicalUnit.SAC_IDENT, CSSConstants.CSS_BOLD_VALUE, null); 70 71 static LexicalUnit MEDIUM_LU = CSSLexicalUnit.createString 72 (LexicalUnit.SAC_IDENT, CSSConstants.CSS_MEDIUM_VALUE, null); 73 74 static LexicalUnit SZ_10PT_LU = CSSLexicalUnit.createFloat 75 (LexicalUnit.SAC_POINT, 10, null); 76 static LexicalUnit SZ_8PT_LU = CSSLexicalUnit.createFloat 77 (LexicalUnit.SAC_POINT, 8, null); 78 79 80 static LexicalUnit FONT_FAMILY_LU; 81 static { 82 LexicalUnit lu; 83 FONT_FAMILY_LU = CSSLexicalUnit.createString 84 (LexicalUnit.SAC_IDENT, "Dialog", null); 85 lu = CSSLexicalUnit.createString 86 (LexicalUnit.SAC_IDENT, "Helvetica", FONT_FAMILY_LU); 87 CSSLexicalUnit.createString 88 (LexicalUnit.SAC_IDENT, 89 CSSConstants.CSS_SANS_SERIF_VALUE, lu); 90 } 91 92 protected final static Set values = new HashSet (); 93 static { 94 values.add(CSSConstants.CSS_CAPTION_VALUE); 95 values.add(CSSConstants.CSS_ICON_VALUE); 96 values.add(CSSConstants.CSS_MENU_VALUE); 97 values.add(CSSConstants.CSS_MESSAGE_BOX_VALUE); 98 values.add(CSSConstants.CSS_SMALL_CAPTION_VALUE); 99 values.add(CSSConstants.CSS_STATUS_BAR_VALUE); 100 } 101 102 public void handleSystemFont(CSSEngine eng, 103 ShorthandManager.PropertyHandler ph, 104 String s, 105 boolean imp) { 106 107 LexicalUnit fontStyle = NORMAL_LU; 108 LexicalUnit fontVariant = NORMAL_LU; 109 LexicalUnit fontWeight = NORMAL_LU; 110 LexicalUnit lineHeight = NORMAL_LU; 111 LexicalUnit fontFamily = FONT_FAMILY_LU; 112 113 LexicalUnit fontSize; 114 if (s.equals(CSSConstants.CSS_SMALL_CAPTION_VALUE)) { 115 fontSize = SZ_8PT_LU; 116 } else { 117 fontSize = SZ_10PT_LU; 118 } 119 ph.property(CSSConstants.CSS_FONT_FAMILY_PROPERTY, fontFamily, imp); 120 ph.property(CSSConstants.CSS_FONT_STYLE_PROPERTY, fontStyle, imp); 121 ph.property(CSSConstants.CSS_FONT_VARIANT_PROPERTY, fontVariant, imp); 122 ph.property(CSSConstants.CSS_FONT_WEIGHT_PROPERTY, fontWeight, imp); 123 ph.property(CSSConstants.CSS_FONT_SIZE_PROPERTY, fontSize, imp); 124 ph.property(CSSConstants.CSS_LINE_HEIGHT_PROPERTY, lineHeight, imp); 125 } 126 127 130 public void setValues(CSSEngine eng, 131 ShorthandManager.PropertyHandler ph, 132 LexicalUnit lu, 133 boolean imp) { 134 switch (lu.getLexicalUnitType()) { 135 case LexicalUnit.SAC_INHERIT: return; 136 case LexicalUnit.SAC_IDENT: { 137 String s= lu.getStringValue().toLowerCase(); 138 if (values.contains(s)) { 139 handleSystemFont(eng, ph, s, imp); 140 return; 141 } 142 } 143 } 144 145 LexicalUnit fontStyle = null; 146 LexicalUnit fontVariant = null; 147 LexicalUnit fontWeight = null; 148 LexicalUnit fontSize = null; 149 LexicalUnit lineHeight = null; 150 LexicalUnit fontFamily = null; 151 152 ValueManager[]vMgrs = eng.getValueManagers(); 153 int fst, fv, fw, fsz, lh, ff; 154 fst = eng.getPropertyIndex(CSSConstants.CSS_FONT_STYLE_PROPERTY); 155 fv = eng.getPropertyIndex(CSSConstants.CSS_FONT_VARIANT_PROPERTY); 156 fw = eng.getPropertyIndex(CSSConstants.CSS_FONT_WEIGHT_PROPERTY); 157 fsz = eng.getPropertyIndex(CSSConstants.CSS_FONT_SIZE_PROPERTY); 158 lh = eng.getPropertyIndex(CSSConstants.CSS_LINE_HEIGHT_PROPERTY); 159 ff = eng.getPropertyIndex(CSSConstants.CSS_FONT_FAMILY_PROPERTY); 160 161 IdentifierManager fstVM = (IdentifierManager)vMgrs[fst]; 162 IdentifierManager fvVM = (IdentifierManager)vMgrs[fv]; 163 IdentifierManager fwVM = (IdentifierManager)vMgrs[fw]; 164 FontSizeManager fszVM = (FontSizeManager)vMgrs[fsz]; 165 ValueManager ffVM = vMgrs[ff]; 166 167 StringMap fstSM = fstVM.getIdentifiers(); 168 StringMap fvSM = fvVM.getIdentifiers(); 169 StringMap fwSM = fwVM.getIdentifiers(); 170 StringMap fszSM = fszVM.getIdentifiers(); 171 172 173 176 boolean svwDone= false; 177 LexicalUnit intLU = null;; 178 while (!svwDone && (lu != null)) { 179 switch (lu.getLexicalUnitType()) { 180 case LexicalUnit.SAC_IDENT: { 181 String s= lu.getStringValue().toLowerCase().intern(); 182 if ((fontStyle == null) && (fstSM.get(s) != null)) { 183 fontStyle = lu; 184 if (intLU != null) { 185 if (fontWeight == null) { 186 fontWeight = intLU; 187 intLU = null; 188 } else 189 throw createInvalidLexicalUnitDOMException 190 (intLU.getLexicalUnitType()); 191 } 192 break; 193 } 194 195 if ((fontVariant == null) && (fvSM.get(s) != null)) { 196 fontVariant = lu; 197 if (intLU != null) { 198 if (fontWeight == null) { 199 fontWeight = intLU; 200 intLU = null; 201 } else 202 throw createInvalidLexicalUnitDOMException 203 (intLU.getLexicalUnitType()); 204 } 205 break; 206 } 207 208 if ((intLU == null) && (fontWeight == null) && 209 (fwSM.get(s) != null)) { 210 fontWeight = lu; 211 break; 212 } 213 214 svwDone = true; 215 break; 216 } 217 case LexicalUnit.SAC_INTEGER: 218 if ((intLU == null) && (fontWeight == null)) { 219 intLU = lu; 220 break; 221 } 222 svwDone = true; 223 break; 224 225 default: svwDone = true; 227 break; 228 } 229 if (!svwDone) lu = lu.getNextLexicalUnit(); 230 } 231 232 if (lu == null) 234 throw createMalformedLexicalUnitDOMException(); 235 236 switch (lu.getLexicalUnitType()) { 238 case LexicalUnit.SAC_IDENT: { 239 String s= lu.getStringValue().toLowerCase().intern(); 240 if (fszSM.get(s) != null) { 241 fontSize = lu; lu = lu.getNextLexicalUnit(); 243 } 244 } 245 break; 246 247 case LexicalUnit.SAC_EM: 248 case LexicalUnit.SAC_EX: 249 case LexicalUnit.SAC_PIXEL: 250 case LexicalUnit.SAC_CENTIMETER: 251 case LexicalUnit.SAC_MILLIMETER: 252 case LexicalUnit.SAC_INCH: 253 case LexicalUnit.SAC_POINT: 254 case LexicalUnit.SAC_PICA: 255 case LexicalUnit.SAC_INTEGER: 256 case LexicalUnit.SAC_REAL: 257 case LexicalUnit.SAC_PERCENTAGE: 258 fontSize = lu; 259 lu = lu.getNextLexicalUnit(); 260 break; 261 } 262 263 264 if (fontSize == null) { 265 if (intLU != null) { 267 fontSize = intLU; intLU = null; 269 } else { 270 throw createInvalidLexicalUnitDOMException 271 (lu.getLexicalUnitType()); 272 } 273 } 274 275 if (intLU != null) { 276 if (fontWeight == null) { 278 fontWeight = intLU; } else { 280 throw createInvalidLexicalUnitDOMException 282 (intLU.getLexicalUnitType()); 283 } 284 } 285 286 if (lu == null) 288 throw createMalformedLexicalUnitDOMException(); 289 290 switch (lu.getLexicalUnitType()) { 293 case LexicalUnit.SAC_OPERATOR_SLASH: lu = lu.getNextLexicalUnit(); 295 if (lu == null) throw createMalformedLexicalUnitDOMException(); 297 lineHeight = lu; 298 lu = lu.getNextLexicalUnit(); 299 break; 300 } 301 302 if (lu == null) 304 throw createMalformedLexicalUnitDOMException(); 305 fontFamily = lu; 306 307 if (fontStyle == null) fontStyle = NORMAL_LU; 308 if (fontVariant == null) fontVariant = NORMAL_LU; 309 if (fontWeight == null) fontWeight = NORMAL_LU; 310 if (lineHeight == null) lineHeight = NORMAL_LU; 311 312 ph.property(CSSConstants.CSS_FONT_FAMILY_PROPERTY, fontFamily, imp); 313 ph.property(CSSConstants.CSS_FONT_STYLE_PROPERTY, fontStyle, imp); 314 ph.property(CSSConstants.CSS_FONT_VARIANT_PROPERTY, fontVariant, imp); 315 ph.property(CSSConstants.CSS_FONT_WEIGHT_PROPERTY, fontWeight, imp); 316 ph.property(CSSConstants.CSS_FONT_SIZE_PROPERTY, fontSize, imp); 317 if (lh!=-1) 318 ph.property(CSSConstants.CSS_LINE_HEIGHT_PROPERTY, 319 lineHeight, imp); 320 } 321 } 322 | Popular Tags |