KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import org.apache.batik.ext.awt.image.Light;
24 import org.apache.batik.ext.awt.image.renderable.Filter;
25 import org.apache.batik.ext.awt.image.renderable.SpecularLightingRable8Bit;
26 import org.apache.batik.gvt.GraphicsNode;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * Bridge class for the <feSpecularLighting> element.
31  *
32  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
33  * @version $Id: SVGFeSpecularLightingElementBridge.java,v 1.16 2004/08/18 07:12:34 vhardy Exp $
34  */

35 public class SVGFeSpecularLightingElementBridge
36     extends AbstractSVGLightingElementBridge {
37
38
39     /**
40      * Constructs a new bridge for the &lt;feSpecularLighting> element.
41      */

42     public SVGFeSpecularLightingElementBridge() {}
43
44     /**
45      * Returns 'feSpecularLighting'.
46      */

47     public String JavaDoc getLocalName() {
48         return SVG_FE_SPECULAR_LIGHTING_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
78         // 'surfaceScale' attribute - default is 1
79
float surfaceScale
80             = convertNumber(filterElement, SVG_SURFACE_SCALE_ATTRIBUTE, 1);
81
82         // 'specularConstant' attribute - default is 1
83
float specularConstant
84             = convertNumber(filterElement, SVG_SPECULAR_CONSTANT_ATTRIBUTE, 1);
85
86         // 'specularExponent' attribute - default is 1
87
float specularExponent = convertSpecularExponent(filterElement);
88
89         // extract the light definition from the filterElement's children list
90
Light light = extractLight(filterElement, ctx);
91
92         // 'kernelUnitLength' attribute
93
double [] kernelUnitLength = convertKernelUnitLength(filterElement);
94
95         // 'in' attribute
96
Filter in = getIn(filterElement,
97                           filteredElement,
98                           filteredNode,
99                           inputFilter,
100                           filterMap,
101                           ctx);
102         if (in == null) {
103             return null; // disable the filter
104
}
105
106         // Default region is the size of in (if in is SourceGraphic or
107
// SourceAlpha it will already include a pad/crop to the
108
// proper filter region size).
109
Rectangle2D JavaDoc defaultRegion = in.getBounds2D();
110         Rectangle2D JavaDoc primitiveRegion
111             = SVGUtilities.convertFilterPrimitiveRegion(filterElement,
112                                                         filteredElement,
113                                                         filteredNode,
114                                                         defaultRegion,
115                                                         filterRegion,
116                                                         ctx);
117
118         Filter filter = new SpecularLightingRable8Bit(in,
119                                                        primitiveRegion,
120                                                        light,
121                                                        specularConstant,
122                                                        specularExponent,
123                                                        surfaceScale,
124                                                        kernelUnitLength);
125
126
127         // handle the 'color-interpolation-filters' property
128
handleColorInterpolationFilters(filter, filterElement);
129
130         // update the filter Map
131
updateFilterMap(filterElement, filter, filterMap);
132
133         return filter;
134     }
135
136     /**
137      * Returns the specular exponent of the specular feSpecularLighting
138      * filter primitive element.
139      *
140      * @param filterElement the feSpecularLighting filter primitive element
141      */

142     protected static float convertSpecularExponent(Element JavaDoc filterElement) {
143         String JavaDoc s = filterElement.getAttributeNS
144             (null, SVG_SPECULAR_EXPONENT_ATTRIBUTE);
145         if (s.length() == 0) {
146             return 1; // default is 1
147
} else {
148             try {
149                 float v = SVGUtilities.convertSVGNumber(s);
150                 if (v < 1 || v > 128) {
151                     throw new BridgeException
152                         (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
153                          new Object JavaDoc[] {SVG_SPECULAR_CONSTANT_ATTRIBUTE, s});
154                 }
155                 return v;
156             } catch (NumberFormatException JavaDoc ex) {
157                 throw new BridgeException
158                     (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
159                      new Object JavaDoc[] {SVG_SPECULAR_CONSTANT_ATTRIBUTE, s, ex});
160             }
161         }
162     }
163 }
164
Popular Tags