1 18 package org.apache.batik.svggen; 19 20 import java.awt.BasicStroke ; 21 22 import org.apache.batik.ext.awt.g2d.GraphicContext; 23 24 31 public class SVGBasicStroke extends AbstractSVGConverter{ 32 36 public SVGBasicStroke(SVGGeneratorContext generatorContext) { 37 super(generatorContext); 38 } 39 40 50 public SVGDescriptor toSVG(GraphicContext gc){ 51 if(gc.getStroke() instanceof BasicStroke ) 52 return toSVG((BasicStroke )gc.getStroke()); 53 else 54 return null; 55 } 56 57 62 public final SVGStrokeDescriptor toSVG(BasicStroke stroke) 63 { 64 String strokeWidth = doubleString(stroke.getLineWidth()); 66 67 String capStyle = endCapToSVG(stroke.getEndCap()); 69 70 String joinStyle = joinToSVG(stroke.getLineJoin()); 72 73 String miterLimit = doubleString(stroke.getMiterLimit()); 75 76 float[] array = stroke.getDashArray(); 78 String dashArray = null; 79 if(array != null) 80 dashArray = dashArrayToSVG(array); 81 else 82 dashArray = SVG_NONE_VALUE; 83 84 String dashOffset = doubleString(stroke.getDashPhase()); 86 87 return new SVGStrokeDescriptor(strokeWidth, capStyle, 88 joinStyle, miterLimit, 89 dashArray, dashOffset); 90 } 91 92 95 private final String dashArrayToSVG(float dashArray[]){ 96 StringBuffer dashArrayBuf = new StringBuffer (); 97 if(dashArray.length > 0) 98 dashArrayBuf.append(doubleString(dashArray[0])); 99 100 for(int i=1; i<dashArray.length; i++){ 101 dashArrayBuf.append(COMMA); 102 dashArrayBuf.append(doubleString(dashArray[i])); 103 } 104 105 return dashArrayBuf.toString(); 106 } 107 108 111 private static String joinToSVG(int lineJoin){ 112 switch(lineJoin){ 113 case BasicStroke.JOIN_BEVEL: 114 return SVG_BEVEL_VALUE; 115 case BasicStroke.JOIN_ROUND: 116 return SVG_ROUND_VALUE; 117 case BasicStroke.JOIN_MITER: 118 default: 119 return SVG_MITER_VALUE; 120 } 121 } 122 123 126 private static String endCapToSVG(int endCap){ 127 switch(endCap){ 128 case BasicStroke.CAP_BUTT: 129 return SVG_BUTT_VALUE; 130 case BasicStroke.CAP_ROUND: 131 return SVG_ROUND_VALUE; 132 default: 133 case BasicStroke.CAP_SQUARE: 134 return SVG_SQUARE_VALUE; 135 } 136 } 137 } 138 | Popular Tags |