Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/batik/bridge/AbstractSVGLightingElementBridge.java


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;
21  import java.util.StringTokenizer;
22  
23  import org.apache.batik.ext.awt.image.DistantLight;
24  import org.apache.batik.ext.awt.image.Light;
25  import org.apache.batik.ext.awt.image.PointLight;
26  import org.apache.batik.ext.awt.image.SpotLight;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.Node;
29  
30  /**
31   * Bridge class for the <feDiffuseLighting> element.
32   *
33   * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
34   * @version $Id: AbstractSVGLightingElementBridge.java,v 1.5 2004/08/18 07:12:30 vhardy Exp $
35   */
36  public abstract class AbstractSVGLightingElementBridge
37      extends AbstractSVGFilterPrimitiveElementBridge {
38  
39      /**
40       * Constructs a new bridge for the lighting filter primitives.
41       */
42      protected AbstractSVGLightingElementBridge() {}
43  
44      /**
45       * Returns the light from the specified lighting filter primitive
46       * element or null if any
47       *
48       * @param filterElement the lighting filter primitive element
49       * @param ctx the bridge context
50       */
51      protected static
52          Light extractLight(Element filterElement, BridgeContext ctx) {
53  
54          Color color = CSSUtilities.convertLightingColor(filterElement, ctx);
55  
56          for (Node n = filterElement.getFirstChild();
57               n != null;
58               n = n.getNextSibling()) {
59  
60              if (n.getNodeType() != Node.ELEMENT_NODE) {
61                  continue;
62              }
63  
64              Element e = (Element)n;
65              Bridge bridge = ctx.getBridge(e);
66              if (bridge == null ||
67                  !(bridge instanceof AbstractSVGLightElementBridge)) {
68                  continue;
69              }
70              return ((AbstractSVGLightElementBridge)bridge).createLight
71                  (ctx, filterElement, e, color);
72          }
73          return null;
74      }
75  
76      /**
77       * Convert the 'kernelUnitLength' attribute of the specified
78       * feDiffuseLighting or feSpecularLighting filter primitive element.
79       *
80       * @param filterElement the filter primitive element
81       */
82      protected static double [] convertKernelUnitLength(Element filterElement) {
83          String s = filterElement.getAttributeNS
84              (null, SVG_KERNEL_UNIT_LENGTH_ATTRIBUTE);
85          if (s.length() == 0) {
86              return null;
87          }
88          double [] units = new double[2];
89          StringTokenizer tokens = new StringTokenizer(s, " ,");
90          try {
91              units[0] = SVGUtilities.convertSVGNumber(tokens.nextToken());
92              if (tokens.hasMoreTokens()) {
93                  units[1] = SVGUtilities.convertSVGNumber(tokens.nextToken());
94              } else {
95                  units[1] = units[0];
96              }
97          } catch (NumberFormatException ex) {
98              throw new BridgeException
99                  (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
100                  new Object[] {SVG_KERNEL_UNIT_LENGTH_ATTRIBUTE, s});
101 
102         }
103         if (tokens.hasMoreTokens() || units[0] <= 0 || units[1] <= 0) {
104             throw new BridgeException
105                 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED,
106                  new Object[] {SVG_KERNEL_UNIT_LENGTH_ATTRIBUTE, s});
107         }
108         return units;
109     }
110 
111     /**
112      * The base bridge class for light element.
113      */
114     protected static abstract class AbstractSVGLightElementBridge
115         extends AbstractSVGBridge {
116 
117         /**
118          * Creates a <tt>Light</tt> according to the specified parameters.
119          *
120          * @param ctx the bridge context to use
121          * @param filterElement the lighting filter primitive element
122          * @param lightElement the element describing a light
123          * @param color the color of the light
124          */
125         public abstract Light createLight(BridgeContext ctx,
126                                           Element filterElement,
127                                           Element lightElement,
128                                           Color color);
129     }
130 
131     /**
132      * Bridge class for the &lt;feSpotLight> element.
133      */
134     public static class SVGFeSpotLightElementBridge
135         extends AbstractSVGLightElementBridge {
136 
137         /**
138          * Constructs a new bridge for a light element.
139          */
140         public SVGFeSpotLightElementBridge() {}
141 
142         /**
143          * Returns 'feSpotLight'.
144          */
145         public String getLocalName() {
146             return SVG_FE_SPOT_LIGHT_TAG;
147         }
148 
149         /**
150          * Creates a <tt>Light</tt> according to the specified parameters.
151          *
152          * @param ctx the bridge context to use
153          * @param filterElement the lighting filter primitive element
154          * @param lightElement the element describing a light
155          * @param color the color of the light
156          */
157         public Light createLight(BridgeContext ctx,
158                                  Element filterElement,
159                                  Element lightElement,
160                                  Color color) {
161 
162             // 'x' attribute - default is 0
163             double x = convertNumber(lightElement, SVG_X_ATTRIBUTE, 0);
164 
165             // 'y' attribute - default is 0
166             double y = convertNumber(lightElement, SVG_Y_ATTRIBUTE, 0);
167 
168             // 'z' attribute - default is 0
169             double z = convertNumber(lightElement, SVG_Z_ATTRIBUTE, 0);
170 
171             // 'pointsAtX' attribute - default is 0
172             double px
173                 = convertNumber(lightElement, SVG_POINTS_AT_X_ATTRIBUTE, 0);
174 
175             // 'pointsAtY' attribute - default is 0
176             double py
177                 = convertNumber(lightElement, SVG_POINTS_AT_Y_ATTRIBUTE, 0);
178 
179             // 'pointsAtZ' attribute - default is 0
180             double pz
181                 = convertNumber(lightElement, SVG_POINTS_AT_Z_ATTRIBUTE, 0);
182 
183             // 'specularExponent' attribute - default is 1
184             double specularExponent = convertNumber
185                 (lightElement, SVG_SPECULAR_EXPONENT_ATTRIBUTE, 1);
186 
187             // 'limitingConeAngle' attribute - default is 90
188             double limitingConeAngle = convertNumber
189                 (lightElement, SVG_LIMITING_CONE_ANGLE_ATTRIBUTE, 90);
190 
191             return new SpotLight(x, y, z,
192                                  px, py, pz,
193                                  specularExponent,
194                                  limitingConeAngle,
195                                  color);
196         }
197     }
198 
199     /**
200      * Bridge class for the &lt;feDistantLight> element.
201      */
202     public static class SVGFeDistantLightElementBridge
203         extends AbstractSVGLightElementBridge {
204 
205         /**
206          * Constructs a new bridge for a light element.
207          */
208         public SVGFeDistantLightElementBridge() {}
209 
210         /**
211          * Returns 'feDistantLight'.
212          */
213         public String getLocalName() {
214             return SVG_FE_DISTANT_LIGHT_TAG;
215         }
216 
217         /**
218          * Creates a <tt>Light</tt> according to the specified parameters.
219          *
220          * @param ctx the bridge context to use
221          * @param filterElement the lighting filter primitive element
222          * @param lightElement the element describing a light
223          * @param color the color of the light
224          */
225         public Light createLight(BridgeContext ctx,
226                                  Element filterElement,
227                                  Element lightElement,
228                                  Color color) {
229 
230             // 'azimuth' attribute - default is 0
231             double azimuth
232                 = convertNumber(lightElement, SVG_AZIMUTH_ATTRIBUTE, 0);
233 
234             // 'elevation' attribute - default is 0
235             double elevation
236                 = convertNumber(lightElement, SVG_ELEVATION_ATTRIBUTE, 0);
237 
238             return new DistantLight(azimuth, elevation, color);
239         }
240     }
241 
242     /**
243      * Bridge class for the &lt;fePointLight> element.
244      */
245     public static class SVGFePointLightElementBridge
246         extends AbstractSVGLightElementBridge {
247 
248         /**
249          * Constructs a new bridge for a light element.
250          */
251         public SVGFePointLightElementBridge() {}
252 
253         /**
254          * Returns 'fePointLight'.
255          */
256         public String getLocalName() {
257             return SVG_FE_POINT_LIGHT_TAG;
258         }
259 
260         /**
261          * Creates a <tt>Light</tt> according to the specified parameters.
262          *
263          * @param ctx the bridge context to use
264          * @param filterElement the lighting filter primitive element
265          * @param lightElement the element describing a light
266          * @param color the color of the light
267          */
268         public Light createLight(BridgeContext ctx,
269                                  Element filterElement,
270                                  Element lightElement,
271                                  Color color) {
272 
273             // 'x' attribute - default is 0
274             double x = convertNumber(lightElement, SVG_X_ATTRIBUTE, 0);
275 
276             // 'y' attribute - default is 0
277             double y = convertNumber(lightElement, SVG_Y_ATTRIBUTE, 0);
278 
279             // 'z' attribute - default is 0
280             double z = convertNumber(lightElement, SVG_Z_ATTRIBUTE, 0);
281 
282             return new PointLight(x, y, z, color);
283         }
284     }
285 }