1 18 package org.apache.batik.bridge; 19 import java.util.List ; 20 import java.util.LinkedList ; 21 22 import org.apache.batik.css.engine.CSSEngine; 23 import org.apache.batik.css.engine.FontFaceRule; 24 import org.apache.batik.css.engine.StyleMap; 25 import org.apache.batik.css.engine.SVGCSSEngine; 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.gvt.font.GVTFontFamily; 30 import org.apache.batik.util.ParsedURL; 31 import org.apache.batik.util.SVGConstants; 32 import org.w3c.dom.css.CSSValue; 33 import org.w3c.dom.css.CSSPrimitiveValue; 34 35 41 public class CSSFontFace extends FontFace implements SVGConstants { 42 43 GVTFontFamily fontFamily = null; 44 45 48 public CSSFontFace 49 (List srcs, 50 String familyName, float unitsPerEm, String fontWeight, 51 String fontStyle, String fontVariant, String fontStretch, 52 float slope, String panose1, float ascent, float descent, 53 float strikethroughPosition, float strikethroughThickness, 54 float underlinePosition, float underlineThickness, 55 float overlinePosition, float overlineThickness) { 56 super(srcs, 57 familyName, unitsPerEm, fontWeight, fontStyle, 58 fontVariant, fontStretch, slope, panose1, ascent, descent, 59 strikethroughPosition, strikethroughThickness, 60 underlinePosition, underlineThickness, 61 overlinePosition, overlineThickness); 62 } 63 64 protected CSSFontFace(String familyName) { 65 super(familyName); 66 } 67 68 public static CSSFontFace createCSSFontFace(CSSEngine eng, 69 FontFaceRule ffr) { 70 StyleMap sm = ffr.getStyleMap(); 71 String familyName = getStringProp 72 (sm, eng, SVGCSSEngine.FONT_FAMILY_INDEX); 73 74 CSSFontFace ret = new CSSFontFace(familyName); 75 76 Value v; 77 v = sm.getValue(SVGCSSEngine.FONT_WEIGHT_INDEX); 78 if (v != null) 79 ret.fontWeight = v.getCssText(); 80 v = sm.getValue(SVGCSSEngine.FONT_STYLE_INDEX); 81 if (v != null) 82 ret.fontStyle = v.getCssText(); 83 v = sm.getValue(SVGCSSEngine.FONT_VARIANT_INDEX); 84 if (v != null) 85 ret.fontVariant = v.getCssText(); 86 v = sm.getValue(SVGCSSEngine.FONT_STRETCH_INDEX); 87 if (v != null) 88 ret.fontStretch = v.getCssText(); 89 v = sm.getValue(SVGCSSEngine.SRC_INDEX); 90 91 ParsedURL base = ffr.getURL(); 92 if ((v != null) && (v != ValueConstants.NONE_VALUE)) { 93 if (v.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) { 94 ret.srcs = new LinkedList (); 95 ret.srcs.add(getSrcValue(v, base)); 96 } else if (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) { 97 ret.srcs = new LinkedList (); 98 for (int i=0; i<v.getLength(); i++) { 99 ret.srcs.add(getSrcValue(v.item(i), base)); 100 } 101 } 102 } 103 127 return ret; 128 } 129 130 public static Object getSrcValue(Value v, ParsedURL base) { 131 if (v.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE) 132 return null; 133 if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_URI) { 134 if (base != null) 135 return new ParsedURL(base, v.getStringValue()); 136 return new ParsedURL(v.getStringValue()); 137 } 138 if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_STRING) 139 return v.getStringValue(); 140 return null; 141 } 142 public static String getStringProp(StyleMap sm, CSSEngine eng, int pidx) { 143 Value v = sm.getValue(pidx); 144 ValueManager [] vms = eng.getValueManagers(); 145 if (v == null) { 146 ValueManager vm = vms[pidx]; 147 v = vm.getDefaultValue(); 148 } 149 while (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) { 150 v = v.item(0); 151 } 152 return v.getStringValue(); 153 } 154 155 public static float getFloatProp(StyleMap sm, CSSEngine eng, int pidx) { 156 Value v = sm.getValue(pidx); 157 ValueManager [] vms = eng.getValueManagers(); 158 if (v == null) { 159 ValueManager vm = vms[pidx]; 160 v = vm.getDefaultValue(); 161 } 162 while (v.getCssValueType() == CSSValue.CSS_VALUE_LIST) { 163 v = v.item(0); 164 } 165 return v.getFloatValue(); 166 } 167 168 171 public GVTFontFamily getFontFamily(BridgeContext ctx) { 172 if (fontFamily != null) 173 return fontFamily ; 174 175 fontFamily = super.getFontFamily(ctx); 176 return fontFamily; 177 } 178 } 179 | Popular Tags |