KickJava   Java API By Example, From Geeks To Geeks.

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


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.Shape JavaDoc;
21 import java.awt.geom.GeneralPath JavaDoc;
22 import java.awt.geom.Line2D JavaDoc;
23
24 import org.apache.batik.ext.awt.g2d.GraphicContext;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * Utility class that converts a Path object into an SVG clip
29  *
30  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
31  * @version $Id: SVGClip.java,v 1.14 2004/08/18 07:14:59 vhardy Exp $
32  */

33 public class SVGClip extends AbstractSVGConverter {
34     /**
35      * Constant used for some degenerate cases
36      */

37     public static final Shape JavaDoc ORIGIN = new Line2D.Float JavaDoc(0,0,0,0);
38
39     /**
40      * Descriptor to use where there is no clip on an element
41      */

42     public static final SVGClipDescriptor NO_CLIP =
43         new SVGClipDescriptor(SVG_NONE_VALUE, null);
44
45     /**
46      * Used to convert clip object to SVG elements
47      */

48     private SVGShape shapeConverter;
49
50     /**
51      * @param generatorContext used to build Elements
52      */

53     public SVGClip(SVGGeneratorContext generatorContext) {
54         super(generatorContext);
55         this.shapeConverter = new SVGShape(generatorContext);
56     }
57
58     /**
59      * Converts part or all of the input GraphicContext into
60      * a set of attribute/value pairs and related definitions.
61      * @param gc GraphicContext to be converted
62      * @return descriptor of the attributes required to represent
63      * some or all of the GraphicContext state, along
64      * with the related definitions
65      * @see org.apache.batik.svggen.SVGDescriptor
66      */

67     public SVGDescriptor toSVG(GraphicContext gc) {
68         Shape JavaDoc clip = gc.getClip();
69
70         SVGClipDescriptor clipDesc = null;
71
72         if (clip != null) {
73             StringBuffer JavaDoc clipPathAttrBuf = new StringBuffer JavaDoc(URL_PREFIX);
74
75             // First, convert to a GeneralPath so that the
76
GeneralPath JavaDoc clipPath = new GeneralPath JavaDoc(clip);
77
78             // Check if this object is already in the Map
79
ClipKey clipKey = new ClipKey(clipPath, generatorContext);
80             clipDesc = (SVGClipDescriptor)descMap.get(clipKey);
81
82             if (clipDesc == null) {
83                 Element JavaDoc clipDef = clipToSVG(clip);
84                 if (clipDef == null)
85                     clipDesc = NO_CLIP;
86                 else {
87                     clipPathAttrBuf.append(SIGN_POUND);
88                     clipPathAttrBuf.append(clipDef.getAttributeNS(null, ATTR_ID));
89                     clipPathAttrBuf.append(URL_SUFFIX);
90
91                     clipDesc = new SVGClipDescriptor(clipPathAttrBuf.toString(),
92                                                      clipDef);
93
94                     descMap.put(clipKey, clipDesc);
95                     defSet.add(clipDef);
96                 }
97             }
98         } else
99             clipDesc = NO_CLIP;
100
101         return clipDesc;
102     }
103
104     /**
105      * In the following method, an clipping Shape is converted to
106      * an SVG clipPath.
107      *
108      * @param clip path to convert to an SVG clipPath
109      * element
110      */

111     private Element JavaDoc clipToSVG(Shape JavaDoc clip) {
112         Element JavaDoc clipDef =
113             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
114                                                         SVG_CLIP_PATH_TAG);
115         clipDef.setAttributeNS(null, SVG_CLIP_PATH_UNITS_ATTRIBUTE,
116                                SVG_USER_SPACE_ON_USE_VALUE);
117
118         clipDef.setAttributeNS(null, ATTR_ID,
119                                generatorContext.
120                                idGenerator.generateID(ID_PREFIX_CLIP_PATH));
121
122         Element JavaDoc clipPath = shapeConverter.toSVG(clip);
123         // unfortunately it may be null because of SVGPath that may produce null
124
// SVG elements.
125
if (clipPath != null) {
126             clipDef.appendChild(clipPath);
127             return clipDef;
128         } else {
129             // Here, we know clip is not null but we got a
130
// null clipDef. This means we ran into a degenerate
131
// case which in Java 2D means everything is clippped.
132
// To provide an equivalent behavior, we clip to a point
133
clipDef.appendChild(shapeConverter.toSVG(ORIGIN));
134             return clipDef;
135         }
136     }
137 }
138
139 /**
140  * Inner class used to key clip definitions in a Map.
141  * This is needed because we need to test equality
142  * on the value of GeneralPath and GeneralPath's equal
143  * method does not implement that behavior.
144  */

145 class ClipKey {
146     /**
147      * This clip hash code. Based on the serialized path
148      * data
149      */

150     int hashCodeValue = 0;
151
152     /**
153      * @param proxiedPath path used as an index in the Map
154      */

155     public ClipKey(GeneralPath JavaDoc proxiedPath, SVGGeneratorContext gc){
156         String JavaDoc pathData = SVGPath.toSVGPathData(proxiedPath, gc);
157         hashCodeValue = pathData.hashCode();
158     }
159
160     /**
161      * @return this object's hashcode
162      */

163     public int hashCode() {
164         return hashCodeValue;
165     }
166
167     /**
168      * @param object to compare
169      * @return true if equal, false otherwise
170      */

171     public boolean equals(Object JavaDoc clipKey){
172         boolean isEqual = false;
173         if((clipKey != null) &&clipKey instanceof ClipKey)
174             isEqual = (hashCodeValue == ((ClipKey)clipKey).hashCodeValue);
175
176         return isEqual;
177     }
178 }
179
Popular Tags