KickJava   Java API By Example, From Geeks To Geeks.

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


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.AlphaComposite JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.batik.ext.awt.g2d.GraphicContext;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * Utility class that converts an AlphaComposite object into
31  * a set of SVG properties and definitions. Here is
32  * how AlphaComposites are mapped to SVG:
33  * + AlphaComposite.SRC_OVER with extra alpha is mapped
34  * to the opacity attribute
35  * + AlphaComposite's other rules are translated into
36  * predefined filter effects.
37  * One of the big differences between AlphaComposite and
38  * the SVG feComposite filter is that feComposite does not
39  * have the notion of extra alpha applied to the source.
40  * The extra alpha equivalent is obtained by setting the
41  * opacity property on the nodes to be composited.
42  *
43  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
44  * @version $Id: SVGAlphaComposite.java,v 1.13 2004/08/18 07:14:59 vhardy Exp $
45  * @see org.apache.batik.svggen.SVGAlphaComposite
46  */

47 public class SVGAlphaComposite extends AbstractSVGConverter {
48     /**
49      * Map of all possible AlphaComposite filter equivalents
50      */

51     private Map JavaDoc compositeDefsMap = new HashMap JavaDoc();
52
53     /**
54      * By default, access to the background is not required.
55      */

56     private boolean backgroundAccessRequired = false;
57
58     /**
59      * @param generatorContext for use by SVGAlphaComposite to build Elements
60      */

61     public SVGAlphaComposite(SVGGeneratorContext generatorContext) {
62         super(generatorContext);
63
64         //
65
// Initialize map of AlphaComposite filter definitions
66
//
67
compositeDefsMap.put(AlphaComposite.Src,
68                              compositeToSVG(AlphaComposite.Src));
69         compositeDefsMap.put(AlphaComposite.SrcIn,
70                              compositeToSVG(AlphaComposite.SrcIn));
71         compositeDefsMap.put(AlphaComposite.SrcOut,
72                              compositeToSVG(AlphaComposite.SrcOut));
73         compositeDefsMap.put(AlphaComposite.DstIn,
74                              compositeToSVG(AlphaComposite.DstIn));
75         compositeDefsMap.put(AlphaComposite.DstOut,
76                              compositeToSVG(AlphaComposite.DstOut));
77         compositeDefsMap.put(AlphaComposite.DstOver,
78                              compositeToSVG(AlphaComposite.DstOver));
79         compositeDefsMap.put(AlphaComposite.Clear,
80                              compositeToSVG(AlphaComposite.Clear));
81     }
82
83     /**
84      * @return set of all AlphaComposite filter definitions
85      */

86     public List JavaDoc getAlphaCompositeFilterSet() {
87         return new LinkedList JavaDoc(compositeDefsMap.values());
88     }
89
90     /**
91      * @return true if background access is required for any
92      * of the converted AlphaComposite rules
93      */

94     public boolean requiresBackgroundAccess() {
95         return backgroundAccessRequired;
96     }
97
98     /**
99      * Converts part or all of the input GraphicContext into
100      * a set of attribute/value pairs and related definitions
101      *
102      * @param gc GraphicContext to be converted
103      * @return descriptor of the attributes required to represent
104      * some or all of the GraphicContext state, along
105      * with the related definitions
106      * @see org.apache.batik.svggen.SVGDescriptor
107      */

108     public SVGDescriptor toSVG(GraphicContext gc) {
109         return toSVG((AlphaComposite JavaDoc)gc.getComposite());
110     }
111
112     /**
113      * @param composite the AlphaComposite object to convert to SVG
114      * @return an SVGCompositeDescriptor that defines how to map the
115      * input composite in SVG
116      */

117     public SVGCompositeDescriptor toSVG(AlphaComposite JavaDoc composite) {
118         SVGCompositeDescriptor compositeDesc =
119             (SVGCompositeDescriptor)descMap.get(composite);
120
121         if(compositeDesc == null){
122             // Process the composite opacity
123
String JavaDoc opacityValue = doubleString(composite.getAlpha());
124
125             // For all rules different than SRC_OVER, a filter is
126
// needed to represent the composition rule.
127
String JavaDoc filterValue = null;
128             Element JavaDoc filterDef = null;
129             if(composite.getRule() != AlphaComposite.SRC_OVER){
130                 // Note that the extra alpha is ignored by using the
131
// majorComposite. The extra alpha is already represented
132
// by the SVG_OPACITY_ATTRIBUTE value.
133
AlphaComposite JavaDoc majorComposite =
134                     AlphaComposite.getInstance(composite.getRule());
135                 filterDef = (Element JavaDoc)compositeDefsMap.get(majorComposite);
136                 defSet.add(filterDef);
137
138                 // Process the filter value
139
StringBuffer JavaDoc filterAttrBuf = new StringBuffer JavaDoc(URL_PREFIX);
140                 filterAttrBuf.append(SIGN_POUND);
141                 filterAttrBuf.append(filterDef.getAttributeNS(null, ATTR_ID));
142                 filterAttrBuf.append(URL_SUFFIX);
143
144                 filterValue = filterAttrBuf.toString();
145             } else
146                 filterValue = SVG_NONE_VALUE;
147
148             compositeDesc = new SVGCompositeDescriptor(opacityValue, filterValue,
149                                                        filterDef);
150
151             descMap.put(composite, compositeDesc);
152         }
153
154         if (composite.getRule() != AlphaComposite.SRC_OVER)
155             backgroundAccessRequired = true;
156
157         return compositeDesc;
158     }
159
160     /**
161      * @param composite AlphaComposite to convert to a filter effect
162      * @exception Error if an AlphaComposite with SRC_OVER rule in passed to
163      * this method.
164      */

165     private Element JavaDoc compositeToSVG(AlphaComposite JavaDoc composite) {
166         // operator is equivalent to rule
167
String JavaDoc operator = null;
168
169         // input1 is equivalent to Src
170
String JavaDoc input1 = null;
171
172         // input2 is equivalent to Dst
173
String JavaDoc input2 = null;
174
175         // k2 is used only for arithmetic
176
// to obtain the equivalent of SRC
177
String JavaDoc k2 = "0";
178
179         // ID used to identify the composite
180
String JavaDoc id = null;
181
182         switch(composite.getRule()){
183         case AlphaComposite.CLEAR:
184             operator = SVG_ARITHMETIC_VALUE;
185             input1 = SVG_SOURCE_GRAPHIC_VALUE;
186             input2 = SVG_BACKGROUND_IMAGE_VALUE;
187             id = ID_PREFIX_ALPHA_COMPOSITE_CLEAR;
188             break;
189         case AlphaComposite.SRC:
190             operator = SVG_ARITHMETIC_VALUE;
191             input1 = SVG_SOURCE_GRAPHIC_VALUE;
192             input2 = SVG_BACKGROUND_IMAGE_VALUE;
193             id = ID_PREFIX_ALPHA_COMPOSITE_SRC;
194             k2 = SVG_DIGIT_ONE_VALUE;
195             break;
196         case AlphaComposite.SRC_IN:
197             operator = SVG_IN_VALUE;
198             input1 = SVG_SOURCE_GRAPHIC_VALUE;
199             input2 = SVG_BACKGROUND_IMAGE_VALUE;
200             id = ID_PREFIX_ALPHA_COMPOSITE_SRC_IN;
201             break;
202         case AlphaComposite.SRC_OUT:
203             operator = SVG_OUT_VALUE;
204             input1 = SVG_SOURCE_GRAPHIC_VALUE;
205             input2 = SVG_BACKGROUND_IMAGE_VALUE;
206             id = ID_PREFIX_ALPHA_COMPOSITE_SRC_OUT;
207             break;
208         case AlphaComposite.DST_IN:
209             operator = SVG_IN_VALUE;
210             input2 = SVG_SOURCE_GRAPHIC_VALUE;
211             input1 = SVG_BACKGROUND_IMAGE_VALUE;
212             id = ID_PREFIX_ALPHA_COMPOSITE_DST_IN;
213             break;
214         case AlphaComposite.DST_OUT:
215             operator = SVG_OUT_VALUE;
216             input2 = SVG_SOURCE_GRAPHIC_VALUE;
217             input1 = SVG_BACKGROUND_IMAGE_VALUE;
218             id = ID_PREFIX_ALPHA_COMPOSITE_DST_OUT;
219             break;
220         case AlphaComposite.DST_OVER:
221             operator = SVG_OVER_VALUE;
222             input2 = SVG_SOURCE_GRAPHIC_VALUE;
223             input1 = SVG_BACKGROUND_IMAGE_VALUE;
224             id = ID_PREFIX_ALPHA_COMPOSITE_DST_OVER;
225             break;
226         default:
227             throw new Error JavaDoc();
228         }
229
230         Element JavaDoc compositeFilter =
231             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
232                                                         SVG_FILTER_TAG);
233         compositeFilter.setAttributeNS(null, ATTR_ID, id);
234         compositeFilter.setAttributeNS(null, SVG_FILTER_UNITS_ATTRIBUTE,
235                                      SVG_OBJECT_BOUNDING_BOX_VALUE);
236         compositeFilter.setAttributeNS(null, SVG_X_ATTRIBUTE, SVG_ZERO_PERCENT_VALUE);
237         compositeFilter.setAttributeNS(null, SVG_Y_ATTRIBUTE, SVG_ZERO_PERCENT_VALUE);
238         compositeFilter.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
239                                        SVG_HUNDRED_PERCENT_VALUE);
240         compositeFilter.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
241                                        SVG_HUNDRED_PERCENT_VALUE);
242
243         Element JavaDoc feComposite =
244             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
245                                                         SVG_FE_COMPOSITE_TAG);
246         feComposite.setAttributeNS(null, SVG_OPERATOR_ATTRIBUTE, operator);
247         feComposite.setAttributeNS(null, SVG_IN_ATTRIBUTE, input1);
248         feComposite.setAttributeNS(null, SVG_IN2_ATTRIBUTE, input2);
249         feComposite.setAttributeNS(null, SVG_K2_ATTRIBUTE, k2);
250         feComposite.setAttributeNS(null, SVG_RESULT_ATTRIBUTE, SVG_COMPOSITE_VALUE);
251
252         Element JavaDoc feFlood =
253             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
254                                                         SVG_FE_FLOOD_TAG);
255         feFlood.setAttributeNS(null, SVG_FLOOD_COLOR_ATTRIBUTE, "white");
256         feFlood.setAttributeNS(null, SVG_FLOOD_OPACITY_ATTRIBUTE, "1");
257         feFlood.setAttributeNS(null, SVG_RESULT_ATTRIBUTE, SVG_FLOOD_VALUE);
258
259
260         Element JavaDoc feMerge =
261             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
262                                                         SVG_FE_MERGE_TAG);
263         Element JavaDoc feMergeNodeFlood =
264             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
265                                                         SVG_FE_MERGE_NODE_TAG);
266         feMergeNodeFlood.setAttributeNS(null, SVG_IN_ATTRIBUTE, SVG_FLOOD_VALUE);
267         Element JavaDoc feMergeNodeComposite =
268             generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI,
269                                                         SVG_FE_MERGE_NODE_TAG);
270         feMergeNodeComposite.setAttributeNS(null, SVG_IN_ATTRIBUTE,
271                                             SVG_COMPOSITE_VALUE);
272
273         feMerge.appendChild(feMergeNodeFlood);
274         feMerge.appendChild(feMergeNodeComposite);
275
276         compositeFilter.appendChild(feFlood);
277         compositeFilter.appendChild(feComposite);
278         compositeFilter.appendChild(feMerge);
279
280         return compositeFilter;
281     }
282 }
283
Popular Tags