KickJava   Java API By Example, From Geeks To Geeks.

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


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.RenderingHints JavaDoc;
21
22 import org.apache.batik.ext.awt.g2d.GraphicContext;
23
24 /**
25  * Utility class that converts a RenderingHins object into
26  * a set of SVG properties. Here is how individual hints
27  * are converted.
28  * + RENDERING -> sets all other hints to
29  * initial value.
30  * + FRACTIONAL_METRICS -> sets initial values for
31  * text-rendering and shape-rendering.
32  * + ALPHA_INTERPOLATION -> Not mapped
33  * + ANTIALIASING -> shape-rendering and text-rendering
34  * + COLOR_RENDERING -> color-rendering
35  * + DITHERING -> not mapped
36  * + INTERPOLATION -> image-rendering
37  * + TEXT_ANTIALIASING -> text-rendering
38  *
39  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
40  * @version $Id: SVGRenderingHints.java,v 1.18 2004/08/18 07:15:08 vhardy Exp $
41  */

42 public class SVGRenderingHints extends AbstractSVGConverter{
43     /**
44      * @param generatorContext used by converter to handle precision
45      * or to create elements.
46      */

47     public SVGRenderingHints(SVGGeneratorContext generatorContext) {
48         super(generatorContext);
49     }
50
51     /**
52      * Converts part or all of the input GraphicContext into
53      * a set of attribute/value pairs and related definitions
54      *
55      * @param gc GraphicContext to be converted
56      * @return descriptor of the attributes required to represent
57      * some or all of the GraphicContext state, along
58      * with the related definitions
59      * @see org.apache.batik.svggen.SVGDescriptor
60      */

61     public SVGDescriptor toSVG(GraphicContext gc){
62         return toSVG(gc.getRenderingHints());
63     }
64
65     /**
66      * @param hints RenderingHints object which should be converted
67      * to a set of SVG attributes.
68      * @return map Map of attribute values that describe the hints
69      */

70     public static SVGHintsDescriptor toSVG(RenderingHints JavaDoc hints){
71         // no hints should mean default
72
String JavaDoc colorInterpolation = SVG_AUTO_VALUE;
73         String JavaDoc colorRendering = SVG_AUTO_VALUE;
74         String JavaDoc textRendering = SVG_AUTO_VALUE;
75         String JavaDoc shapeRendering = SVG_AUTO_VALUE;
76         String JavaDoc imageRendering = SVG_AUTO_VALUE;
77
78         //
79
// RENDERING
80
//
81
if(hints != null){
82             Object JavaDoc rendering = hints.get(RenderingHints.KEY_RENDERING);
83             if(rendering == RenderingHints.VALUE_RENDER_DEFAULT){
84                 colorInterpolation = SVG_AUTO_VALUE;
85                 colorRendering = SVG_AUTO_VALUE;
86                 textRendering = SVG_AUTO_VALUE;
87                 shapeRendering = SVG_AUTO_VALUE;
88                 imageRendering = SVG_AUTO_VALUE;
89             }
90             else if(rendering == RenderingHints.VALUE_RENDER_SPEED){
91                 colorInterpolation = SVG_SRGB_VALUE;
92                 colorRendering = SVG_OPTIMIZE_SPEED_VALUE;
93                 textRendering = SVG_OPTIMIZE_SPEED_VALUE;
94                 shapeRendering = SVG_GEOMETRIC_PRECISION_VALUE;
95                 imageRendering = SVG_OPTIMIZE_SPEED_VALUE;
96             }
97             else if(rendering == RenderingHints.VALUE_RENDER_QUALITY){
98                 colorInterpolation = SVG_LINEAR_RGB_VALUE;
99                 colorRendering = SVG_OPTIMIZE_QUALITY_VALUE;
100                 textRendering = SVG_OPTIMIZE_QUALITY_VALUE;
101                 shapeRendering = SVG_GEOMETRIC_PRECISION_VALUE;
102                 imageRendering = SVG_OPTIMIZE_QUALITY_VALUE;
103             }
104
105             //
106
// Fractional Metrics
107
//
108
Object JavaDoc fractionalMetrics = hints.get(RenderingHints.KEY_FRACTIONALMETRICS);
109             if(fractionalMetrics == RenderingHints.VALUE_FRACTIONALMETRICS_ON){
110                 textRendering = SVG_OPTIMIZE_QUALITY_VALUE;
111                 shapeRendering = SVG_GEOMETRIC_PRECISION_VALUE;
112             }
113             else if(fractionalMetrics == RenderingHints.VALUE_FRACTIONALMETRICS_OFF){
114                 textRendering = SVG_OPTIMIZE_SPEED_VALUE;
115                 shapeRendering = SVG_OPTIMIZE_SPEED_VALUE;
116             }
117             else if(fractionalMetrics == RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT){
118                 textRendering = SVG_AUTO_VALUE;
119                 shapeRendering = SVG_AUTO_VALUE;
120             }
121
122             //
123
// Antialiasing
124
//
125
Object JavaDoc antialiasing = hints.get(RenderingHints.KEY_ANTIALIASING);
126             if(antialiasing == RenderingHints.VALUE_ANTIALIAS_ON){
127                 textRendering = SVG_OPTIMIZE_LEGIBILITY_VALUE;
128                 shapeRendering = SVG_AUTO_VALUE;
129             }
130             else if(antialiasing == RenderingHints.VALUE_ANTIALIAS_OFF){
131                 textRendering = SVG_GEOMETRIC_PRECISION_VALUE;
132                 shapeRendering = SVG_CRISP_EDGES_VALUE;
133             }
134             else if(antialiasing == RenderingHints.VALUE_ANTIALIAS_DEFAULT){
135                 textRendering = SVG_AUTO_VALUE;
136                 shapeRendering = SVG_AUTO_VALUE;
137             }
138
139             //
140
// Text Antialiasing
141
//
142
Object JavaDoc textAntialiasing = hints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
143             if(textAntialiasing == RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
144                 textRendering = SVG_GEOMETRIC_PRECISION_VALUE;
145             else if(textAntialiasing == RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)
146                 textRendering = SVG_OPTIMIZE_SPEED_VALUE;
147             else if(textAntialiasing == RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT)
148                 textRendering = SVG_AUTO_VALUE;
149
150             //
151
// Color Rendering
152
//
153
Object JavaDoc colorRenderingHint = hints.get(RenderingHints.KEY_COLOR_RENDERING);
154             if(colorRenderingHint == RenderingHints.VALUE_COLOR_RENDER_DEFAULT)
155                 colorRendering = SVG_AUTO_VALUE;
156             else if(colorRenderingHint == RenderingHints.VALUE_COLOR_RENDER_QUALITY)
157                 colorRendering = SVG_OPTIMIZE_QUALITY_VALUE;
158             else if(colorRenderingHint == RenderingHints.VALUE_COLOR_RENDER_SPEED)
159                 colorRendering = SVG_OPTIMIZE_SPEED_VALUE;
160
161             //
162
// Interpolation
163
//
164
Object JavaDoc interpolation = hints.get(RenderingHints.KEY_INTERPOLATION);
165             if(interpolation == RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)
166                 imageRendering = SVG_OPTIMIZE_SPEED_VALUE;
167             else if(interpolation == RenderingHints.VALUE_INTERPOLATION_BICUBIC
168                     ||
169                     interpolation == RenderingHints.VALUE_INTERPOLATION_BILINEAR)
170                 imageRendering = SVG_OPTIMIZE_QUALITY_VALUE;
171         } // if(hints != null)
172

173         return new SVGHintsDescriptor(colorInterpolation,
174                                       colorRendering,
175                                       textRendering,
176                                       shapeRendering,
177                                       imageRendering);
178     }
179 }
180
Popular Tags