001 /*
002 * PathUtil.java
003 *
004 * Created on May 10, 2005, 5:56 AM
005 *
006 * To change this template, choose Tools | Options and locate the template under
007 * the Source Creation and Management node. Right-click the template and choose
008 * Open. You can then make changes to the template in the Source Editor.
009 */
010
011 package com.kitfox.svg.pathcmd;
012
013 import java.awt.geom.*;
014
015 /**
016 *
017 * @author kitfox
018 */
019 public class PathUtil
020 {
021
022 /** Creates a new instance of PathUtil */
023 public PathUtil()
024 {
025 }
026
027 /**
028 * Converts a GeneralPath into an SVG representation
029 */
030 public static String buildPathString(GeneralPath path)
031 {
032 float[] coords = new float[6];
033
034 StringBuffer sb = new StringBuffer();
035
036 for (PathIterator pathIt = path.getPathIterator(new AffineTransform()); !pathIt.isDone(); pathIt.next())
037 {
038 int segId = pathIt.currentSegment(coords);
039
040 switch (segId)
041 {
042 case PathIterator.SEG_CLOSE:
043 {
044 sb.append(" Z");
045 break;
046 }
047 case PathIterator.SEG_CUBICTO:
048 {
049 sb.append(" C " + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3] + " " + coords[4] + " " + coords[5]);
050 break;
051 }
052 case PathIterator.SEG_LINETO:
053 {
054 sb.append(" L " + coords[0] + " " + coords[1]);
055 break;
056 }
057 case PathIterator.SEG_MOVETO:
058 {
059 sb.append(" M " + coords[0] + " " + coords[1]);
060 break;
061 }
062 case PathIterator.SEG_QUADTO:
063 {
064 sb.append(" Q " + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3]);
065 break;
066 }
067 }
068 }
069
070 return sb.toString();
071 }
072 }