KickJava   Java API By Example, From Geeks To Geeks.

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


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.geom.Rectangle2D JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.batik.ext.awt.image.renderable.Filter;
25 import org.apache.batik.ext.awt.image.renderable.TurbulenceRable;
26 import org.apache.batik.ext.awt.image.renderable.TurbulenceRable8Bit;
27 import org.apache.batik.gvt.GraphicsNode;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Bridge class for the <feTurbulence> element.
32  *
33  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
34  * @version $Id: SVGFeTurbulenceElementBridge.java,v 1.11 2004/08/18 07:12:34 vhardy Exp $
35  */

36 public class SVGFeTurbulenceElementBridge
37     extends AbstractSVGFilterPrimitiveElementBridge {
38
39     /**
40      * Constructs a new bridge for the &lt;feTurbulence> element.
41      */

42     public SVGFeTurbulenceElementBridge() {}
43
44     /**
45      * Returns 'feTurbulence'.
46      */

47     public String JavaDoc getLocalName() {
48         return SVG_FE_TURBULENCE_TAG;
49     }
50
51     /**
52      * Creates a <tt>Filter</tt> primitive according to the specified
53      * parameters.
54      *
55      * @param ctx the bridge context to use
56      * @param filterElement the element that defines a filter
57      * @param filteredElement the element that references the filter
58      * @param filteredNode the graphics node to filter
59      *
60      * @param inputFilter the <tt>Filter</tt> that represents the current
61      * filter input if the filter chain.
62      * @param filterRegion the filter area defined for the filter chain
63      * the new node will be part of.
64      * @param filterMap a map where the mediator can map a name to the
65      * <tt>Filter</tt> it creates. Other <tt>FilterBridge</tt>s
66      * can then access a filter node from the filterMap if they
67      * know its name.
68      */

69     public Filter createFilter(BridgeContext ctx,
70                                Element JavaDoc filterElement,
71                                Element JavaDoc filteredElement,
72                                GraphicsNode filteredNode,
73                                Filter inputFilter,
74                                Rectangle2D JavaDoc filterRegion,
75                                Map JavaDoc filterMap) {
76
77         // 'in' attribute
78
Filter in = getIn(filterElement,
79                           filteredElement,
80                           filteredNode,
81                           inputFilter,
82                           filterMap,
83                           ctx);
84         if (in == null) {
85             return null; // disable the filter
86
}
87
88         // default region is the filter chain region
89
Rectangle2D JavaDoc defaultRegion = filterRegion;
90         Rectangle2D JavaDoc primitiveRegion
91             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
92                                                         filteredElement,
93                                                         filteredNode,
94                                                         defaultRegion,
95                                                         filterRegion,
96                                                         ctx);
97
98         // 'baseFrequency' attribute - default is [0, 0]
99
float [] baseFrequency
100             = convertBaseFrenquency(filterElement);
101
102         // 'numOctaves' attribute - default is 1
103
int numOctaves
104             = convertInteger(filterElement, SVG_NUM_OCTAVES_ATTRIBUTE, 1);
105
106         // 'seed' attribute - default is 0
107
int seed
108             = convertInteger(filterElement, SVG_SEED_ATTRIBUTE, 0);
109
110         // 'stitchTiles' attribute - default is 'noStitch'
111
boolean stitchTiles
112             = convertStitchTiles(filterElement);
113
114         // 'fractalNoise' attribute - default is 'turbulence'
115
boolean isFractalNoise
116             = convertType(filterElement);
117
118         // create the filter primitive
119
TurbulenceRable turbulenceRable
120             = new TurbulenceRable8Bit(primitiveRegion);
121
122         turbulenceRable.setBaseFrequencyX(baseFrequency[0]);
123         turbulenceRable.setBaseFrequencyY(baseFrequency[1]);
124         turbulenceRable.setNumOctaves(numOctaves);
125         turbulenceRable.setSeed(seed);
126         turbulenceRable.setStitched(stitchTiles);
127         turbulenceRable.setFractalNoise(isFractalNoise);
128
129         // handle the 'color-interpolation-filters' property
130
handleColorInterpolationFilters(turbulenceRable, filterElement);
131
132         // update the filter Map
133
updateFilterMap(filterElement, turbulenceRable, filterMap);
134
135         return turbulenceRable;
136     }
137
138     /**
139      * Converts the 'baseFrequency' attribute of the specified
140      * feTurbulence element.
141      *
142      * @param e the feTurbulence element
143      */

144     protected static float [] convertBaseFrenquency(Element JavaDoc e) {
145         String JavaDoc s = e.getAttributeNS(null, SVG_BASE_FREQUENCY_ATTRIBUTE);
146         if (s.length() == 0) {
147             return new float[] {0.001f, 0.001f};
148         }
149         float[] v = new float[2];
150         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(s, " ,");
151         try {
152             v[0] = SVGUtilities.convertSVGNumber(tokens.nextToken());
153             if (tokens.hasMoreTokens()) {
154                 v[1] = SVGUtilities.convertSVGNumber(tokens.nextToken());
155             } else {
156                 v[1] = v[0];
157             }
158             if (tokens.hasMoreTokens()) {
159                 throw new BridgeException
160                     (e, ERR_ATTRIBUTE_VALUE_MALFORMED,
161                      new Object JavaDoc[] {SVG_BASE_FREQUENCY_ATTRIBUTE, s});
162             }
163         } catch (NumberFormatException JavaDoc ex) {
164             throw new BridgeException
165                 (e, ERR_ATTRIBUTE_VALUE_MALFORMED,
166                  new Object JavaDoc[] {SVG_BASE_FREQUENCY_ATTRIBUTE, s});
167         }
168         if (v[0] < 0 || v[1] < 0) {
169             throw new BridgeException
170                 (e, ERR_ATTRIBUTE_VALUE_MALFORMED,
171                  new Object JavaDoc[] {SVG_BASE_FREQUENCY_ATTRIBUTE, s});
172         }
173         return v;
174     }
175
176     /**
177      * Converts the 'stitchTiles' attribute of the specified
178      * feTurbulence element.
179      *
180      * @param e the feTurbulence element
181      * @return true if stitchTiles attribute is 'stitch', false otherwise
182      */

183     protected static boolean convertStitchTiles(Element JavaDoc e) {
184         String JavaDoc s = e.getAttributeNS(null, SVG_STITCH_TILES_ATTRIBUTE);
185         if (s.length() == 0) {
186             return false;
187         }
188         if (SVG_STITCH_VALUE.equals(s)) {
189             return true;
190         }
191         if (SVG_NO_STITCH_VALUE.equals(s)) {
192             return false;
193         }
194         throw new BridgeException(e, ERR_ATTRIBUTE_VALUE_MALFORMED,
195                                   new Object JavaDoc[] {SVG_STITCH_TILES_ATTRIBUTE, s});
196     }
197
198     /**
199      * Converts the 'type' attribute of the specified feTurbulence element.
200      *
201      * @param e the feTurbulence element
202      * @return true if type attribute value is 'fractalNoise', false otherwise
203      */

204     protected static boolean convertType(Element JavaDoc e) {
205         String JavaDoc s = e.getAttributeNS(null, SVG_TYPE_ATTRIBUTE);
206         if (s.length() == 0) {
207             return false;
208         }
209         if (SVG_FRACTAL_NOISE_VALUE.equals(s)) {
210             return true;
211         }
212         if (SVG_TURBULENCE_VALUE.equals(s)) {
213             return false;
214         }
215         throw new BridgeException(e, ERR_ATTRIBUTE_VALUE_MALFORMED,
216                                   new Object JavaDoc[] {SVG_TYPE_ATTRIBUTE, s});
217     }
218 }
219
Popular Tags