KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
21 import java.awt.Composite JavaDoc;
22 import java.awt.Font JavaDoc;
23 import java.awt.Paint JavaDoc;
24 import java.awt.RenderingHints JavaDoc;
25 import java.awt.Shape JavaDoc;
26 import java.awt.Stroke JavaDoc;
27
28 import java.text.DecimalFormat JavaDoc;
29 import java.text.DecimalFormatSymbols JavaDoc;
30
31 import java.util.Locale JavaDoc;
32
33 import org.w3c.dom.Document JavaDoc;
34
35 /**
36  * This class contains all non graphical contextual information that
37  * are needed by the {@link org.apache.batik.svggen.SVGGraphics2D} to
38  * generate SVG from Java 2D primitives.
39  * You can subclass it to change the defaults.
40  *
41  * @see org.apache.batik.svggen.SVGGraphics2D#SVGGraphics2D(SVGGeneratorContext,boolean)
42  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
43  * @version $Id: SVGGeneratorContext.java,v 1.21 2005/03/27 08:58:35 cam Exp $
44  */

45 public class SVGGeneratorContext implements ErrorConstants {
46     // this fields are package access for read-only purpose
47

48     /**
49      * Factory used by this Graphics2D to create Elements
50      * that make the SVG DOM Tree
51      */

52     Document JavaDoc domFactory;
53
54     /**
55      * Handler that defines how images are referenced in the
56      * generated SVG fragment. This allows different strategies
57      * to be used to handle images.
58      * @see org.apache.batik.svggen.ImageHandler
59      * @see org.apache.batik.svggen.ImageHandlerBase64Encoder
60      * @see org.apache.batik.svggen.ImageHandlerPNGEncoder
61      * @see org.apache.batik.svggen.ImageHandlerJPEGEncoder
62      */

63     ImageHandler imageHandler;
64
65     /**
66      * Generic image handler. This allows more sophisticated
67      * image handling strategies than the <tt>ImageHandler</tt>
68      * interfaces.
69      */

70     GenericImageHandler genericImageHandler;
71
72     /**
73      * To deal with Java 2D extension (custom java.awt.Paint for example).
74      */

75     ExtensionHandler extensionHandler;
76
77     /**
78      * To generate consitent ids.
79      */

80     SVGIDGenerator idGenerator;
81
82     /**
83      * To set style.
84      */

85     StyleHandler styleHandler;
86
87     /**
88      * The comment to insert at generation time.
89      */

90     String JavaDoc generatorComment;
91
92     /**
93      * The error handler.
94      */

95     ErrorHandler errorHandler;
96
97     /**
98      * Do we accept SVG Fonts generation?
99      */

100     boolean svgFont = false;
101
102     /**
103      * GraphicContextDefaults
104      */

105     GraphicContextDefaults gcDefaults;
106
107     /**
108      * Number of decimal places to use in output values.
109      * 3 decimal places are used by default.
110      */

111     int precision;
112
113     /**
114      * Class to describe the GraphicContext defaults to
115      * be used. Note that this class does *not* contain
116      * a default for the initial transform, as this
117      * transform *has to be identity* for the SVGGraphics2D
118      * to operate (the TransformStacks operation is based
119      * on that assumption. See the DOMTreeManager class).
120      */

121     public static class GraphicContextDefaults {
122         protected Paint JavaDoc paint;
123         protected Stroke JavaDoc stroke;
124         protected Composite JavaDoc composite;
125         protected Shape JavaDoc clip;
126         protected RenderingHints JavaDoc hints;
127         protected Font JavaDoc font;
128         protected Color JavaDoc background;
129
130         public void setStroke(Stroke JavaDoc stroke){
131             this.stroke = stroke;
132         }
133
134         public Stroke JavaDoc getStroke(){
135             return stroke;
136         }
137
138         public void setComposite(Composite JavaDoc composite){
139             this.composite = composite;
140         }
141
142         public Composite JavaDoc getComposite(){
143             return composite;
144         }
145
146         public void setClip(Shape JavaDoc clip){
147             this.clip = clip;
148         }
149
150         public Shape JavaDoc getClip(){
151             return clip;
152         }
153
154         public void setRenderingHints(RenderingHints JavaDoc hints){
155             this.hints = hints;
156         }
157
158         public RenderingHints JavaDoc getRenderingHints(){
159             return hints;
160         }
161
162         public void setFont(Font JavaDoc font){
163             this.font = font;
164         }
165
166         public Font JavaDoc getFont(){
167             return font;
168         }
169
170         public void setBackground(Color JavaDoc background){
171             this.background = background;
172         }
173
174         public Color JavaDoc getBackground(){
175             return background;
176         }
177
178         public void setPaint(Paint JavaDoc paint){
179             this.paint = paint;
180         }
181
182         public Paint JavaDoc getPaint(){
183             return paint;
184         }
185     }
186
187     /**
188      * Builds an instance of <code>SVGGeneratorContext</code> with the given
189      * <code>domFactory</code> but let the user set later the other contextual
190      * information. Please note that none of the parameter below should be
191      * <code>null</code>.
192      * @see #setIDGenerator
193      * @see #setExtensionHandler
194      * @see #setImageHandler
195      * @see #setStyleHandler
196      * @see #setErrorHandler
197      */

198     protected SVGGeneratorContext(Document JavaDoc domFactory) {
199         setDOMFactory(domFactory);
200     }
201
202     /**
203      * Creates an instance of <code>SVGGeneratorContext</code> with the
204      * given <code>domFactory</code> and with the default values for the
205      * other information.
206      * @see org.apache.batik.svggen.SVGIDGenerator
207      * @see org.apache.batik.svggen.DefaultExtensionHandler
208      * @see org.apache.batik.svggen.ImageHandlerBase64Encoder
209      * @see org.apache.batik.svggen.DefaultStyleHandler
210      * @see org.apache.batik.svggen.DefaultErrorHandler
211      */

212     public static SVGGeneratorContext createDefault(Document JavaDoc domFactory) {
213         SVGGeneratorContext ctx = new SVGGeneratorContext(domFactory);
214         ctx.setIDGenerator(new SVGIDGenerator());
215         ctx.setExtensionHandler(new DefaultExtensionHandler());
216         ctx.setImageHandler(new ImageHandlerBase64Encoder());
217         ctx.setStyleHandler(new DefaultStyleHandler());
218         ctx.setComment("Generated by the Batik Graphics2D SVG Generator");
219         ctx.setErrorHandler(new DefaultErrorHandler());
220         return ctx;
221     }
222
223     /**
224      * Returns the set of defaults which should be used for the
225      * GraphicContext.
226      */

227     final public GraphicContextDefaults getGraphicContextDefaults(){
228         return gcDefaults;
229     }
230
231     /**
232      * Sets the default to be used for the graphic context.
233      * Note that gcDefaults may be null and that any of its attributes
234      * may be null.
235      */

236     final public void setGraphicContextDefaults(GraphicContextDefaults gcDefaults){
237         this.gcDefaults = gcDefaults;
238     }
239
240     /**
241      * Returns the {@link org.apache.batik.svggen.SVGIDGenerator} that
242      * has been set.
243      */

244     final public SVGIDGenerator getIDGenerator() {
245         return idGenerator;
246     }
247
248     /**
249      * Sets the {@link org.apache.batik.svggen.SVGIDGenerator}
250      * to be used. It should not be <code>null</code>.
251      */

252     final public void setIDGenerator(SVGIDGenerator idGenerator) {
253         if (idGenerator == null)
254             throw new SVGGraphics2DRuntimeException(ERR_ID_GENERATOR_NULL);
255         this.idGenerator = idGenerator;
256     }
257
258     /**
259      * Returns the DOM Factory that
260      * has been set.
261      */

262     final public Document JavaDoc getDOMFactory() {
263         return domFactory;
264     }
265
266     /**
267      * Sets the DOM Factory
268      * to be used. It should not be <code>null</code>.
269      */

270     final public void setDOMFactory(Document JavaDoc domFactory) {
271         if (domFactory == null)
272             throw new SVGGraphics2DRuntimeException(ERR_DOM_FACTORY_NULL);
273         this.domFactory = domFactory;
274     }
275
276     /**
277      * Returns the {@link org.apache.batik.svggen.ExtensionHandler} that
278      * has been set.
279      */

280     final public ExtensionHandler getExtensionHandler() {
281         return extensionHandler;
282     }
283
284     /**
285      * Sets the {@link org.apache.batik.svggen.ExtensionHandler}
286      * to be used. It should not be <code>null</code>.
287      */

288     final public void setExtensionHandler(ExtensionHandler extensionHandler) {
289         if (extensionHandler == null)
290             throw new SVGGraphics2DRuntimeException(ERR_EXTENSION_HANDLER_NULL);
291         this.extensionHandler = extensionHandler;
292     }
293
294     /**
295      * Returns the {@link org.apache.batik.svggen.ImageHandler} that
296      * has been set.
297      */

298     final public ImageHandler getImageHandler() {
299         return imageHandler;
300     }
301
302     /**
303      * Sets the {@link org.apache.batik.svggen.ImageHandler}
304      * to be used. It should not be <code>null</code>.
305      */

306     final public void setImageHandler(ImageHandler imageHandler) {
307         if (imageHandler == null)
308             throw new SVGGraphics2DRuntimeException(ERR_IMAGE_HANDLER_NULL);
309         this.imageHandler = imageHandler;
310         this.genericImageHandler = new SimpleImageHandler(imageHandler);
311     }
312
313     /**
314      * Sets the {@link org.apache.batik.svggen.GenericImageHandler}
315      * to be used.
316      */

317     final public void setGenericImageHandler(GenericImageHandler genericImageHandler){
318         if (genericImageHandler == null){
319             throw new SVGGraphics2DRuntimeException(ERR_IMAGE_HANDLER_NULL);
320         }
321         this.imageHandler = null;
322         this.genericImageHandler = genericImageHandler;
323     }
324
325     /**
326      * Returns the {@link org.apache.batik.svggen.StyleHandler} that
327      * has been set.
328      */

329     final public StyleHandler getStyleHandler() {
330         return styleHandler;
331     }
332
333     /**
334      * Sets the {@link org.apache.batik.svggen.StyleHandler}
335      * to be used. It should not be <code>null</code>.
336      */

337     final public void setStyleHandler(StyleHandler styleHandler) {
338         if (styleHandler == null)
339             throw new SVGGraphics2DRuntimeException(ERR_STYLE_HANDLER_NULL);
340         this.styleHandler = styleHandler;
341     }
342
343     /**
344      * Returns the comment to be generated in the SVG file.
345      */

346     final public String JavaDoc getComment() {
347         return generatorComment;
348     }
349
350     /**
351      * Sets the comment to be used. It can be <code>null</code> if you
352      * want to disable it.
353      */

354     final public void setComment(String JavaDoc generatorComment) {
355         this.generatorComment = generatorComment;
356     }
357
358     /**
359      * Returns the {@link org.apache.batik.svggen.ErrorHandler} that
360      * has been set.
361      */

362     final public ErrorHandler getErrorHandler() {
363         return errorHandler;
364     }
365
366     /**
367      * Sets the {@link org.apache.batik.svggen.ErrorHandler}
368      * to be used. It should not be <code>null</code>.
369      */

370     final public void setErrorHandler(ErrorHandler errorHandler) {
371         if (errorHandler == null)
372             throw new SVGGraphics2DRuntimeException(ERR_ERROR_HANDLER_NULL);
373         this.errorHandler = errorHandler;
374     }
375
376     /**
377      * Returns <code>true</code> if we should generate SVG Fonts for
378      * texts.
379      */

380     final public boolean isEmbeddedFontsOn() {
381         return svgFont;
382     }
383
384     /**
385      * Sets if we should generate SVG Fonts for texts. Default value
386      * is <code>false</code>.
387      */

388     final public void setEmbeddedFontsOn(boolean svgFont) {
389         this.svgFont = svgFont;
390     }
391
392     /**
393      * Returns the current precision used by this context
394      */

395     final public int getPrecision() {
396         return precision;
397     }
398
399     /**
400      * Sets the precision used by this context. The precision controls
401      * the number of decimal places used in floating point values
402      * output by the SVGGraphics2D generator.
403      * Note that the precision is clipped to the [0,12] range.
404      */

405     final public void setPrecision(int precision) {
406         if (precision < 0) {
407             this.precision = 0;
408         } else if (precision > 12) {
409             this.precision = 12;
410         } else {
411             this.precision = precision;
412         }
413         decimalFormat = decimalFormats[this.precision];
414     }
415
416     /**
417      * Converts the input double value to a string with a number of
418      * decimal places controlled by the precision attribute.
419      */

420     final public String JavaDoc doubleString(double value) {
421         double absvalue = Math.abs(value);
422         // above 10e7 we do not output decimals as anyway
423
// in scientific notation they were not available
424
if (absvalue >= 10e7 || (int)value == value) {
425             return Integer.toString((int)value);
426         }
427         // under 10e-3 we have to put decimals
428
else {
429             return decimalFormat.format(value);
430         }
431     }
432
433     /**
434      * Current double value formatter
435      */

436     protected DecimalFormat JavaDoc decimalFormat = decimalFormats[3];
437
438     protected static DecimalFormatSymbols JavaDoc dsf
439         = new DecimalFormatSymbols JavaDoc(Locale.US);
440
441     protected static DecimalFormat JavaDoc decimalFormats[] = new DecimalFormat JavaDoc[13];
442
443     static {
444         decimalFormats[0] = new DecimalFormat JavaDoc("#", dsf);
445
446         String JavaDoc format = "#.";
447         for (int i=0; i<=12; i++) {
448             format += "#";
449             decimalFormats[i] = new DecimalFormat JavaDoc(format, dsf);
450         }
451     }
452
453 }
454
Popular Tags