1 18 package org.apache.batik.bridge; 19 20 import java.awt.font.TextAttribute ; 21 import java.util.ArrayList ; 22 import java.util.StringTokenizer ; 23 24 import org.apache.batik.css.engine.SVGCSSEngine; 25 import org.apache.batik.css.engine.value.Value; 26 import org.apache.batik.gvt.TextNode; 27 import org.apache.batik.util.CSSConstants; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.css.CSSPrimitiveValue; 31 32 39 public abstract class TextUtilities implements CSSConstants, ErrorConstants { 40 41 44 public static String getElementContent(Element e) { 45 StringBuffer result = new StringBuffer (); 46 for (Node n = e.getFirstChild(); 47 n != null; 48 n = n.getNextSibling()) { 49 switch (n.getNodeType()) { 50 case Node.ELEMENT_NODE: 51 result.append(getElementContent((Element )n)); 52 break; 53 case Node.CDATA_SECTION_NODE: 54 case Node.TEXT_NODE: 55 result.append(n.getNodeValue()); 56 } 57 } 58 return result.toString(); 59 } 60 61 70 public static 71 ArrayList svgHorizontalCoordinateArrayToUserSpace(Element element, 72 String attrName, 73 String valueStr, 74 BridgeContext ctx) { 75 76 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, element); 77 ArrayList values = new ArrayList (); 78 StringTokenizer st = new StringTokenizer (valueStr, ", ", false); 79 while (st.hasMoreTokens()) { 80 values.add 81 (new Float (UnitProcessor.svgHorizontalCoordinateToUserSpace 82 (st.nextToken(), attrName, uctx))); 83 } 84 return values; 85 } 86 87 96 public static 97 ArrayList svgVerticalCoordinateArrayToUserSpace(Element element, 98 String attrName, 99 String valueStr, 100 BridgeContext ctx) { 101 102 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, element); 103 ArrayList values = new ArrayList (); 104 StringTokenizer st = new StringTokenizer (valueStr, ", ", false); 105 while (st.hasMoreTokens()) { 106 values.add 107 (new Float (UnitProcessor.svgVerticalCoordinateToUserSpace 108 (st.nextToken(), attrName, uctx))); 109 } 110 return values; 111 } 112 113 114 public static ArrayList svgRotateArrayToFloats(Element element, 115 String attrName, 116 String valueStr, 117 BridgeContext ctx) { 118 119 StringTokenizer st = new StringTokenizer (valueStr, ", ", false); 120 ArrayList values = new ArrayList (); 121 String s; 122 while (st.hasMoreTokens()) { 123 try { 124 s = st.nextToken(); 125 values.add 126 (new Float (Math.toRadians 127 (SVGUtilities.convertSVGNumber(s)))); 128 } catch (NumberFormatException ex) { 129 throw new BridgeException 130 (element, ERR_ATTRIBUTE_VALUE_MALFORMED, 131 new Object [] {attrName, valueStr}); 132 } 133 } 134 return values; 135 } 136 137 141 public static Float convertFontSize(Element e) { 142 Value v = CSSUtilities.getComputedStyle 143 (e, SVGCSSEngine.FONT_SIZE_INDEX); 144 return new Float (v.getFloatValue()); 145 } 146 147 151 public static Float convertFontStyle(Element e) { 152 Value v = CSSUtilities.getComputedStyle 153 (e, SVGCSSEngine.FONT_STYLE_INDEX); 154 switch (v.getStringValue().charAt(0)) { 155 case 'n': 156 return TextAttribute.POSTURE_REGULAR; 157 default: 158 return TextAttribute.POSTURE_OBLIQUE; 159 } 160 } 161 162 166 public static Float convertFontStretch(Element e) { 167 Value v = CSSUtilities.getComputedStyle 168 (e, SVGCSSEngine.FONT_STRETCH_INDEX); 169 String s = v.getStringValue(); 170 switch (s.charAt(0)) { 171 case 'u': 172 if (s.charAt(6) == 'c') { 173 return TextAttribute.WIDTH_CONDENSED; 174 } else { 175 return TextAttribute.WIDTH_EXTENDED; 176 } 177 178 case 'e': 179 if (s.charAt(6) == 'c') { 180 return TextAttribute.WIDTH_CONDENSED; 181 } else { 182 if (s.length() == 8) { 183 return TextAttribute.WIDTH_SEMI_EXTENDED; 184 } else { 185 return TextAttribute.WIDTH_EXTENDED; 186 } 187 } 188 189 case 's': 190 if (s.charAt(6) == 'c') { 191 return TextAttribute.WIDTH_SEMI_CONDENSED; 192 } else { 193 return TextAttribute.WIDTH_SEMI_EXTENDED; 194 } 195 196 default: 197 return TextAttribute.WIDTH_REGULAR; 198 } 199 } 200 201 205 public static Float convertFontWeight(Element e) { 206 Value v = CSSUtilities.getComputedStyle 207 (e, SVGCSSEngine.FONT_WEIGHT_INDEX); 208 float f = v.getFloatValue(); 209 switch ((int)f) { 210 case 100: 211 return TextAttribute.WEIGHT_EXTRA_LIGHT; 212 case 200: 213 return TextAttribute.WEIGHT_LIGHT; 214 case 300: 215 return TextAttribute.WEIGHT_DEMILIGHT; 216 case 400: 217 return TextAttribute.WEIGHT_REGULAR; 218 case 500: 219 return TextAttribute.WEIGHT_SEMIBOLD; 220 default: 221 return TextAttribute.WEIGHT_BOLD; 222 232 } 233 } 234 235 239 public static TextNode.Anchor convertTextAnchor(Element e) { 240 Value v = CSSUtilities.getComputedStyle 241 (e, SVGCSSEngine.TEXT_ANCHOR_INDEX); 242 switch (v.getStringValue().charAt(0)) { 243 case 's': 244 return TextNode.Anchor.START; 245 case 'm': 246 return TextNode.Anchor.MIDDLE; 247 default: 248 return TextNode.Anchor.END; 249 } 250 } 251 252 257 public static Object convertBaselineShift(Element e) { 258 Value v = CSSUtilities.getComputedStyle 259 (e, SVGCSSEngine.BASELINE_SHIFT_INDEX); 260 if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) { 261 String s = v.getStringValue(); 262 switch (s.charAt(2)) { 263 case 'p': return TextAttribute.SUPERSCRIPT_SUPER; 265 266 case 'b': return TextAttribute.SUPERSCRIPT_SUB; 268 269 default: 270 return null; 271 } 272 } else { 273 return new Float (v.getFloatValue()); 274 } 275 } 276 277 282 public static Float convertKerning(Element e) { 283 Value v = CSSUtilities.getComputedStyle 284 (e, SVGCSSEngine.KERNING_INDEX); 285 if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) { 286 return null; 287 } 288 return new Float (v.getFloatValue()); 289 } 290 291 296 public static Float convertLetterSpacing(Element e) { 297 Value v = CSSUtilities.getComputedStyle 298 (e, SVGCSSEngine.LETTER_SPACING_INDEX); 299 if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) { 300 return null; 301 } 302 return new Float (v.getFloatValue()); 303 } 304 305 310 public static Float convertWordSpacing(Element e) { 311 Value v = CSSUtilities.getComputedStyle 312 (e, SVGCSSEngine.WORD_SPACING_INDEX); 313 if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) { 314 return null; 315 } 316 return new Float (v.getFloatValue()); 317 } 318 } 319 | Popular Tags |