KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.batik.ext.awt.g2d.GraphicContext;
26 import org.apache.batik.ext.awt.g2d.TransformStackElement;
27
28 /**
29  * This class performs the task of converting the state of the
30  * Java 2D API graphic context into a set of graphic attributes.
31  * It also manages a set of SVG definitions referenced by the
32  * SVG attributes.
33  *
34  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
35  * @version $Id: SVGGraphicContextConverter.java,v 1.14 2004/08/18 07:15:00 vhardy Exp $
36  */

37 public class SVGGraphicContextConverter {
38     private static final int GRAPHIC_CONTEXT_CONVERTER_COUNT = 6;
39
40     private SVGTransform transformConverter;
41     private SVGPaint paintConverter;
42     private SVGBasicStroke strokeConverter;
43     private SVGComposite compositeConverter;
44     private SVGClip clipConverter;
45     private SVGRenderingHints hintsConverter;
46     private SVGFont fontConverter;
47     private SVGConverter converters[] =
48         new SVGConverter[GRAPHIC_CONTEXT_CONVERTER_COUNT];
49
50     public SVGTransform getTransformConverter() { return transformConverter; }
51     public SVGPaint getPaintConverter(){ return paintConverter; }
52     public SVGBasicStroke getStrokeConverter(){ return strokeConverter; }
53     public SVGComposite getCompositeConverter(){ return compositeConverter; }
54     public SVGClip getClipConverter(){ return clipConverter; }
55     public SVGRenderingHints getHintsConverter(){ return hintsConverter; }
56     public SVGFont getFontConverter(){ return fontConverter; }
57
58     /**
59      * @param generatorContext the context that will be used to create
60      * elements, handle extension and images.
61      */

62     public SVGGraphicContextConverter(SVGGeneratorContext generatorContext) {
63         if (generatorContext == null)
64             throw new SVGGraphics2DRuntimeException(ErrorConstants.ERR_CONTEXT_NULL);
65
66         transformConverter = new SVGTransform(generatorContext);
67         paintConverter = new SVGPaint(generatorContext);
68         strokeConverter = new SVGBasicStroke(generatorContext);
69         compositeConverter = new SVGComposite(generatorContext);
70         clipConverter = new SVGClip(generatorContext);
71         hintsConverter = new SVGRenderingHints(generatorContext);
72         fontConverter = new SVGFont(generatorContext);
73
74         int i=0;
75         converters[i++] = paintConverter;
76         converters[i++] = strokeConverter;
77         converters[i++] = compositeConverter;
78         converters[i++] = clipConverter;
79         converters[i++] = hintsConverter;
80         converters[i++] = fontConverter;
81     }
82
83     /**
84      * @return a String containing the transform attribute value
85      * equivalent of the input transform stack.
86      */

87     public String JavaDoc toSVG(TransformStackElement transformStack[]) {
88         return transformConverter.toSVGTransform(transformStack);
89     }
90
91     /**
92      * @return an object that describes the set of SVG attributes that
93      * represent the equivalent of the input GraphicContext state.
94      */

95     public SVGGraphicContext toSVG(GraphicContext gc) {
96         // no need for synchronized map => use HashMap
97
Map JavaDoc groupAttrMap = new HashMap JavaDoc();
98
99         for (int i=0; i<converters.length; i++) {
100             SVGDescriptor desc = converters[i].toSVG(gc);
101             if (desc != null)
102                 desc.getAttributeMap(groupAttrMap);
103         }
104
105         // the ctor will to the splitting (group/element) job
106
return new SVGGraphicContext(groupAttrMap,
107                                      gc.getTransformStack());
108     }
109
110     /**
111      * @return a set of element containing definitions for the attribute
112      * values generated by this converter since its creation.
113      */

114     public List JavaDoc getDefinitionSet() {
115         List JavaDoc defSet = new LinkedList JavaDoc();
116         for(int i=0; i<converters.length; i++)
117             defSet.addAll(converters[i].getDefinitionSet());
118
119         return defSet;
120     }
121 }
122
Popular Tags