1 18 package org.apache.batik.svggen; 19 20 import java.awt.Shape ; 21 import java.awt.geom.GeneralPath ; 22 import java.awt.geom.PathIterator ; 23 24 import org.w3c.dom.Element ; 25 26 46 public class SVGPath extends SVGGraphicObjectConverter { 47 50 public SVGPath(SVGGeneratorContext generatorContext) { 51 super(generatorContext); 52 } 53 54 59 public Element toSVG(Shape path) { 60 String dAttr = toSVGPathData(path, generatorContext); 63 if (dAttr==null || dAttr.length() == 0){ 64 return null; 67 } 68 69 Element svgPath = generatorContext.domFactory.createElementNS 70 (SVG_NAMESPACE_URI, SVG_PATH_TAG); 71 svgPath.setAttributeNS(null, SVG_D_ATTRIBUTE, dAttr); 72 73 if (path.getPathIterator(null).getWindingRule() == GeneralPath.WIND_EVEN_ODD) 75 svgPath.setAttributeNS(null, SVG_FILL_RULE_ATTRIBUTE, SVG_EVEN_ODD_VALUE); 76 77 return svgPath; 78 } 79 80 84 public static String toSVGPathData(Shape path, SVGGeneratorContext gc) { 85 StringBuffer d = new StringBuffer (""); 86 PathIterator pi = path.getPathIterator(null); 87 float seg[] = new float[6]; 88 int segType = 0; 89 while (!pi.isDone()) { 90 segType = pi.currentSegment(seg); 91 switch(segType) { 92 case PathIterator.SEG_MOVETO: 93 d.append(PATH_MOVE); 94 appendPoint(d, seg[0], seg[1], gc); 95 break; 96 case PathIterator.SEG_LINETO: 97 d.append(PATH_LINE_TO); 98 appendPoint(d, seg[0], seg[1], gc); 99 break; 100 case PathIterator.SEG_CLOSE: 101 d.append(PATH_CLOSE); 102 break; 103 case PathIterator.SEG_QUADTO: 104 d.append(PATH_QUAD_TO); 105 appendPoint(d, seg[0], seg[1], gc); 106 appendPoint(d, seg[2], seg[3], gc); 107 break; 108 case PathIterator.SEG_CUBICTO: 109 d.append(PATH_CUBIC_TO); 110 appendPoint(d, seg[0], seg[1], gc); 111 appendPoint(d, seg[2], seg[3], gc); 112 appendPoint(d, seg[4], seg[5], gc); 113 break; 114 default: 115 throw new Error (); 116 } 117 pi.next(); 118 } 120 if (d.length() > 0) 121 return d.toString().trim(); 122 else { 123 return ""; 132 } 133 } 134 135 138 private static void appendPoint(StringBuffer d, float x, float y, SVGGeneratorContext gc) { 139 d.append(gc.doubleString(x)); 140 d.append(SPACE); 141 d.append(gc.doubleString(y)); 142 d.append(SPACE); 143 } 144 } 145 | Popular Tags |