KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > AbstractSVGFilterPrimitiveElementBridge


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.bridge;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Paint JavaDoc;
22 import java.awt.geom.Rectangle2D JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.batik.ext.awt.image.PadMode;
26 import org.apache.batik.ext.awt.image.renderable.Filter;
27 import org.apache.batik.ext.awt.image.renderable.FilterAlphaRable;
28 import org.apache.batik.ext.awt.image.renderable.FilterColorInterpolation;
29 import org.apache.batik.ext.awt.image.renderable.FloodRable8Bit;
30 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
31 import org.apache.batik.gvt.GraphicsNode;
32 import org.apache.batik.gvt.filter.BackgroundRable8Bit;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  * The base bridge class for SVG filter primitives.
37  *
38  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
39  * @version $Id: AbstractSVGFilterPrimitiveElementBridge.java,v 1.11 2004/08/18 07:12:30 vhardy Exp $
40  */

41 public abstract class AbstractSVGFilterPrimitiveElementBridge
42     extends AbstractSVGBridge
43     implements FilterPrimitiveBridge, ErrorConstants {
44
45     /**
46      * Constructs a new bridge for a filter primitive element.
47      */

48     protected AbstractSVGFilterPrimitiveElementBridge() {}
49
50     /**
51      * Returns the input source of the specified filter primitive
52      * element defined by its 'in' attribute.
53      *
54      * @param filterElement the filter primitive element
55      * @param filteredElement the element on which the filter is referenced
56      * @param filteredNode the graphics node on which the filter is applied
57      * @param inputFilter the default input filter
58      * @param filterMap the map that containes the named filter primitives
59      * @param ctx the bridge context
60      */

61     protected static Filter getIn(Element JavaDoc filterElement,
62                                   Element JavaDoc filteredElement,
63                                   GraphicsNode filteredNode,
64                                   Filter inputFilter,
65                                   Map JavaDoc filterMap,
66                                   BridgeContext ctx) {
67
68         String JavaDoc s = filterElement.getAttributeNS(null, SVG_IN_ATTRIBUTE);
69         if (s.length() == 0) {
70             return inputFilter;
71         } else {
72             return getFilterSource(filterElement,
73                                    s,
74                                    filteredElement,
75                                    filteredNode,
76                                    filterMap,
77                                    ctx);
78         }
79     }
80
81     /**
82      * Returns the input source of the specified filter primitive
83      * element defined by its 'in2' attribute. The 'in2' attribute is assumed
84      * to be required if the subclasses ask for it.
85      *
86      * @param filterElement the filter primitive element
87      * @param filteredElement the element on which the filter is referenced
88      * @param filteredNode the graphics node on which the filter is applied
89      * @param inputFilter the default input filter
90      * @param filterMap the map that containes the named filter primitives
91      * @param ctx the bridge context
92      */

93     protected static Filter getIn2(Element JavaDoc filterElement,
94                                    Element JavaDoc filteredElement,
95                                    GraphicsNode filteredNode,
96                                    Filter inputFilter,
97                                    Map JavaDoc filterMap,
98                                    BridgeContext ctx) {
99
100         String JavaDoc s = filterElement.getAttributeNS(null, SVG_IN2_ATTRIBUTE);
101         if (s.length() == 0) {
102             throw new BridgeException(filterElement, ERR_ATTRIBUTE_MISSING,
103                                       new Object JavaDoc [] {SVG_IN2_ATTRIBUTE});
104         }
105         return getFilterSource(filterElement,
106                                s,
107                                filteredElement,
108                                filteredNode,
109                                filterMap,
110                                ctx);
111     }
112
113     /**
114      * Updates the filterMap according to the specified parameters.
115      *
116      * @param filterElement the filter primitive element
117      * @param filter the filter that is part of the filter chain
118      * @param filterMap the filter map to update
119      */

120     protected static void updateFilterMap(Element JavaDoc filterElement,
121                                           Filter filter,
122                                           Map JavaDoc filterMap) {
123
124         String JavaDoc s = filterElement.getAttributeNS(null, SVG_RESULT_ATTRIBUTE);
125         if ((s.length() != 0) && (s.trim().length() != 0)) {
126             filterMap.put(s, filter);
127         }
128     }
129
130     /**
131      * Handles the 'color-interpolation-filters' CSS property.
132      *
133      * @param filter the filter
134      * @param filterElement the filter element
135      */

136     protected static void handleColorInterpolationFilters(Filter filter,
137                                                           Element JavaDoc filterElement) {
138         if (filter instanceof FilterColorInterpolation) {
139             boolean isLinear
140                 = CSSUtilities.convertColorInterpolationFilters(filterElement);
141             // System.out.println("IsLinear: " + isLinear +
142
// " Filter: " + filter);
143
((FilterColorInterpolation)filter).setColorSpaceLinear(isLinear);
144         }
145     }
146
147     /**
148      * Returns the filter source according to the specified parameters.
149      *
150      * @param filterElement the filter element
151      * @param s the input of the filter primitive
152      * @param filteredElement the filtered element
153      * @param filteredNode the filtered graphics node
154      * @param filterMap the filter map that contains named filter primitives
155      * @param ctx the bridge context
156      */

157     static Filter getFilterSource(Element JavaDoc filterElement,
158                                   String JavaDoc s,
159                                   Element JavaDoc filteredElement,
160                                   GraphicsNode filteredNode,
161                                   Map JavaDoc filterMap,
162                                   BridgeContext ctx) {
163
164         // SourceGraphic
165
Filter srcG = (Filter)filterMap.get(SVG_SOURCE_GRAPHIC_VALUE);
166         Rectangle2D JavaDoc filterRegion = srcG.getBounds2D();
167
168         int length = s.length();
169         Filter source = null;
170         switch (length) {
171         case 13:
172             if (SVG_SOURCE_GRAPHIC_VALUE.equals(s)) {
173                 // SourceGraphic
174
source = srcG;
175             }
176             break;
177         case 11:
178             if (s.charAt(1) == SVG_SOURCE_ALPHA_VALUE.charAt(1)) {
179                 if (SVG_SOURCE_ALPHA_VALUE.equals(s)) {
180                     // SourceAlpha
181
source = srcG;
182                     source = new FilterAlphaRable(source);
183                 }
184             } else if (SVG_STROKE_PAINT_VALUE.equals(s)) {
185                     // StrokePaint
186
Paint JavaDoc paint = PaintServer.convertStrokePaint
187                         (filteredElement,filteredNode, ctx);
188                     // <!> FIXME: Should we create a transparent flood ???
189
source = new FloodRable8Bit(filterRegion, paint);
190             }
191             break;
192         case 15:
193             if (s.charAt(10) == SVG_BACKGROUND_IMAGE_VALUE.charAt(10)) {
194                 if (SVG_BACKGROUND_IMAGE_VALUE.equals(s)) {
195                     // BackgroundImage
196
source = new BackgroundRable8Bit(filteredNode);
197                     source = new PadRable8Bit(source, filterRegion,
198                                               PadMode.ZERO_PAD);
199                 }
200             } else if (SVG_BACKGROUND_ALPHA_VALUE.equals(s)) {
201                 // BackgroundAlpha
202
source = new BackgroundRable8Bit(filteredNode);
203                 source = new FilterAlphaRable(source);
204                 source = new PadRable8Bit(source, filterRegion,
205                                           PadMode.ZERO_PAD);
206             }
207             break;
208         case 9:
209             if (SVG_FILL_PAINT_VALUE.equals(s)) {
210                 // FillPaint
211
Paint JavaDoc paint = PaintServer.convertFillPaint
212                     (filteredElement,filteredNode, ctx);
213                 if (paint == null) {
214                     paint = new Color JavaDoc(0, 0, 0, 0); // create a transparent flood
215
}
216                 source = new FloodRable8Bit(filterRegion, paint);
217             }
218             break;
219         }
220         if (source == null) {
221             // <identifier>
222
source = (Filter)filterMap.get(s);
223         }
224         return source;
225     }
226
227     /**
228      * This is a bit of a hack but we set the flood bounds to
229      * -floatmax/2 -> floatmax/2 (should cover the area ok).
230      */

231     static final Rectangle2D JavaDoc INFINITE_FILTER_REGION
232         = new Rectangle2D.Float JavaDoc(-Float.MAX_VALUE/2,
233                                 -Float.MAX_VALUE/2,
234                                 Float.MAX_VALUE,
235                                 Float.MAX_VALUE);
236
237
238
239     /**
240      * Converts on the specified filter primitive element, the specified
241      * attribute that represents an integer and with the specified
242      * default value.
243      *
244      * @param filterElement the filter primitive element
245      * @param attrName the name of the attribute
246      * @param defaultValue the default value of the attribute
247      */

248     protected static int convertInteger(Element JavaDoc filterElement,
249                                         String JavaDoc attrName,
250                                         int defaultValue) {
251         String JavaDoc s = filterElement.getAttributeNS(null, attrName);
252         if (s.length() == 0) {
253             return defaultValue;
254         } else {
255             try {
256                 return SVGUtilities.convertSVGInteger(s);
257             } catch (NumberFormatException JavaDoc ex) {
258                 throw new BridgeException
259                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
260                      new Object JavaDoc[] {attrName, s});
261             }
262         }
263     }
264
265     /**
266      * Converts on the specified filter primitive element, the specified
267      * attribute that represents a float and with the specified
268      * default value.
269      *
270      * @param filterElement the filter primitive element
271      * @param attrName the name of the attribute
272      * @param defaultValue the default value of the attribute
273      */

274     protected static float convertNumber(Element JavaDoc filterElement,
275                                          String JavaDoc attrName,
276                                          float defaultValue) {
277
278         String JavaDoc s = filterElement.getAttributeNS(null, attrName);
279         if (s.length() == 0) {
280             return defaultValue;
281         } else {
282             try {
283                 return SVGUtilities.convertSVGNumber(s);
284             } catch (NumberFormatException JavaDoc ex) {
285                 throw new BridgeException
286                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
287                      new Object JavaDoc[] {attrName, s, ex});
288             }
289         }
290     }
291 }
292
Popular Tags