KickJava   Java API By Example, From Geeks To Geeks.

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


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.GradientPaint JavaDoc;
21 import java.awt.Paint JavaDoc;
22 import java.awt.geom.Point2D JavaDoc;
23
24 import org.apache.batik.ext.awt.g2d.GraphicContext;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * Utility class that converts a Java GradientPaint into an
30  * SVG linear gradient element
31  *
32  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
33  * @version $Id: SVGLinearGradient.java,v 1.15 2004/08/18 07:15:08 vhardy Exp $
34  */

35 public class SVGLinearGradient extends AbstractSVGConverter {
36     /**
37      * @param generatorContext used to build Elements
38      */

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

53     public SVGDescriptor toSVG(GraphicContext gc) {
54         Paint JavaDoc paint = gc.getPaint();
55         return toSVG((GradientPaint JavaDoc)paint);
56     }
57
58     /**
59      * @param gradient the GradientPaint to be converted
60      * @return a description of the SVG paint and opacity corresponding
61      * to the gradient Paint. The definiton of the
62      * linearGradient is put in the linearGradientDefsMap
63      */

64     public SVGPaintDescriptor toSVG(GradientPaint JavaDoc gradient) {
65         // Reuse definition if gradient has already been converted
66
SVGPaintDescriptor gradientDesc =
67             (SVGPaintDescriptor)descMap.get(gradient);
68
69         Document JavaDoc domFactory = generatorContext.domFactory;
70
71         if (gradientDesc == null) {
72             Element JavaDoc gradientDef =
73                 domFactory.createElementNS(SVG_NAMESPACE_URI,
74                                            SVG_LINEAR_GRADIENT_TAG);
75             gradientDef.setAttributeNS(null, SVG_GRADIENT_UNITS_ATTRIBUTE,
76                                        SVG_USER_SPACE_ON_USE_VALUE);
77
78             //
79
// Process gradient vector
80
//
81
Point2D JavaDoc p1 = gradient.getPoint1();
82             Point2D JavaDoc p2 = gradient.getPoint2();
83             gradientDef.setAttributeNS(null, SVG_X1_ATTRIBUTE,
84                                        "" + doubleString(p1.getX()));
85             gradientDef.setAttributeNS(null, SVG_Y1_ATTRIBUTE,
86                                        "" + doubleString(p1.getY()));
87             gradientDef.setAttributeNS(null, SVG_X2_ATTRIBUTE,
88                                        "" + doubleString(p2.getX()));
89             gradientDef.setAttributeNS(null, SVG_Y2_ATTRIBUTE,
90                                        "" + doubleString(p2.getY()));
91
92             //
93
// Spread method
94
//
95
String JavaDoc spreadMethod = SVG_PAD_VALUE;
96             if(gradient.isCyclic())
97                 spreadMethod = SVG_REFLECT_VALUE;
98             gradientDef.setAttributeNS
99                 (null, SVG_SPREAD_METHOD_ATTRIBUTE, spreadMethod);
100
101             //
102
// First gradient stop
103
//
104
Element JavaDoc gradientStop =
105                 domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_STOP_TAG);
106             gradientStop.setAttributeNS(null, SVG_OFFSET_ATTRIBUTE,
107                                       SVG_ZERO_PERCENT_VALUE);
108
109             SVGPaintDescriptor colorDesc = SVGColor.toSVG(gradient.getColor1(), generatorContext);
110             gradientStop.setAttributeNS(null, SVG_STOP_COLOR_ATTRIBUTE,
111                                       colorDesc.getPaintValue());
112             gradientStop.setAttributeNS(null, SVG_STOP_OPACITY_ATTRIBUTE,
113                                       colorDesc.getOpacityValue());
114
115             gradientDef.appendChild(gradientStop);
116
117             //
118
// Second gradient stop
119
//
120
gradientStop =
121                 domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_STOP_TAG);
122             gradientStop.setAttributeNS(null, SVG_OFFSET_ATTRIBUTE,
123                                       SVG_HUNDRED_PERCENT_VALUE);
124
125             colorDesc = SVGColor.toSVG(gradient.getColor2(), generatorContext);
126             gradientStop.setAttributeNS(null, SVG_STOP_COLOR_ATTRIBUTE,
127                                         colorDesc.getPaintValue());
128             gradientStop.setAttributeNS(null, SVG_STOP_OPACITY_ATTRIBUTE,
129                                         colorDesc.getOpacityValue());
130
131             gradientDef.appendChild(gradientStop);
132
133             //
134
// Gradient ID
135
//
136
gradientDef.
137                 setAttributeNS(null, ATTR_ID,
138                                generatorContext.idGenerator.
139                                generateID(ID_PREFIX_LINEAR_GRADIENT));
140
141             //
142
// Build Paint descriptor
143
//
144
StringBuffer JavaDoc paintAttrBuf = new StringBuffer JavaDoc(URL_PREFIX);
145             paintAttrBuf.append(SIGN_POUND);
146             paintAttrBuf.append(gradientDef.getAttributeNS(null, ATTR_ID));
147             paintAttrBuf.append(URL_SUFFIX);
148
149             gradientDesc = new SVGPaintDescriptor(paintAttrBuf.toString(),
150                                                   SVG_OPAQUE_VALUE,
151                                                   gradientDef);
152
153             //
154
// Update maps so that gradient can be reused if needed
155
//
156
descMap.put(gradient, gradientDesc);
157             defSet.add(gradientDef);
158         }
159
160         return gradientDesc;
161     }
162 }
163
Popular Tags