KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SVGBasicStroke


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen;
19
20 import java.awt.BasicStroke JavaDoc;
21
22 import org.apache.batik.ext.awt.g2d.GraphicContext;
23
24 /**
25  * Utility class that converts a Java BasicStroke object into
26  * a set of SVG style attributes
27  *
28  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
29  * @version $Id: SVGBasicStroke.java,v 1.14 2004/08/18 07:14:59 vhardy Exp $
30  */

31 public class SVGBasicStroke extends AbstractSVGConverter{
32     /**
33      * @param generatorContext used by converter to handle precision
34      * or to create elements.
35      */

36     public SVGBasicStroke(SVGGeneratorContext generatorContext) {
37         super(generatorContext);
38     }
39
40     /**
41      * Converts part or all of the input GraphicContext into
42      * a set of attribute/value pairs and related definitions
43      *
44      * @param gc GraphicContext to be converted
45      * @return descriptor of the attributes required to represent
46      * some or all of the GraphicContext state, along
47      * with the related definitions
48      * @see org.apache.batik.svggen.SVGDescriptor
49      */

50     public SVGDescriptor toSVG(GraphicContext gc){
51         if(gc.getStroke() instanceof BasicStroke JavaDoc)
52             return toSVG((BasicStroke JavaDoc)gc.getStroke());
53         else
54             return null;
55     }
56
57     /**
58      * @param stroke BasicStroke to convert to a set of
59      * SVG attributes
60      * @return map of attributes describing the stroke
61      */

62     public final SVGStrokeDescriptor toSVG(BasicStroke JavaDoc stroke)
63     {
64         // Stroke width
65
String JavaDoc strokeWidth = doubleString(stroke.getLineWidth());
66
67         // Cap style
68
String JavaDoc capStyle = endCapToSVG(stroke.getEndCap());
69
70         // Join style
71
String JavaDoc joinStyle = joinToSVG(stroke.getLineJoin());
72
73         // Miter limit
74
String JavaDoc miterLimit = doubleString(stroke.getMiterLimit());
75
76         // Dash array
77
float[] array = stroke.getDashArray();
78         String JavaDoc dashArray = null;
79         if(array != null)
80             dashArray = dashArrayToSVG(array);
81         else
82             dashArray = SVG_NONE_VALUE;
83
84         // Dash offset
85
String JavaDoc dashOffset = doubleString(stroke.getDashPhase());
86
87         return new SVGStrokeDescriptor(strokeWidth, capStyle,
88                                        joinStyle, miterLimit,
89                                        dashArray, dashOffset);
90     }
91
92     /**
93      * @param dashArray float array to convert to a string
94      */

95     private final String JavaDoc dashArrayToSVG(float dashArray[]){
96         StringBuffer JavaDoc dashArrayBuf = new StringBuffer JavaDoc();
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     /**
109      * @param lineJoin join style
110      */

111     private static String JavaDoc 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     /**
124      * @param endCap cap style
125      */

126     private static String JavaDoc 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