1 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 36 public abstract class AbstractSVGLightingElementBridge 37 extends AbstractSVGFilterPrimitiveElementBridge { 38 39 42 protected AbstractSVGLightingElementBridge() {} 43 44 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 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 114 protected static abstract class AbstractSVGLightElementBridge 115 extends AbstractSVGBridge { 116 117 125 public abstract Light createLight(BridgeContext ctx, 126 Element filterElement, 127 Element lightElement, 128 Color color); 129 } 130 131 134 public static class SVGFeSpotLightElementBridge 135 extends AbstractSVGLightElementBridge { 136 137 140 public SVGFeSpotLightElementBridge() {} 141 142 145 public String getLocalName() { 146 return SVG_FE_SPOT_LIGHT_TAG; 147 } 148 149 157 public Light createLight(BridgeContext ctx, 158 Element filterElement, 159 Element lightElement, 160 Color color) { 161 162 double x = convertNumber(lightElement, SVG_X_ATTRIBUTE, 0); 164 165 double y = convertNumber(lightElement, SVG_Y_ATTRIBUTE, 0); 167 168 double z = convertNumber(lightElement, SVG_Z_ATTRIBUTE, 0); 170 171 double px 173 = convertNumber(lightElement, SVG_POINTS_AT_X_ATTRIBUTE, 0); 174 175 double py 177 = convertNumber(lightElement, SVG_POINTS_AT_Y_ATTRIBUTE, 0); 178 179 double pz 181 = convertNumber(lightElement, SVG_POINTS_AT_Z_ATTRIBUTE, 0); 182 183 double specularExponent = convertNumber 185 (lightElement, SVG_SPECULAR_EXPONENT_ATTRIBUTE, 1); 186 187 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 202 public static class SVGFeDistantLightElementBridge 203 extends AbstractSVGLightElementBridge { 204 205 208 public SVGFeDistantLightElementBridge() {} 209 210 213 public String getLocalName() { 214 return SVG_FE_DISTANT_LIGHT_TAG; 215 } 216 217 225 public Light createLight(BridgeContext ctx, 226 Element filterElement, 227 Element lightElement, 228 Color color) { 229 230 double azimuth 232 = convertNumber(lightElement, SVG_AZIMUTH_ATTRIBUTE, 0); 233 234 double elevation 236 = convertNumber(lightElement, SVG_ELEVATION_ATTRIBUTE, 0); 237 238 return new DistantLight(azimuth, elevation, color); 239 } 240 } 241 242 245 public static class SVGFePointLightElementBridge 246 extends AbstractSVGLightElementBridge { 247 248 251 public SVGFePointLightElementBridge() {} 252 253 256 public String getLocalName() { 257 return SVG_FE_POINT_LIGHT_TAG; 258 } 259 260 268 public Light createLight(BridgeContext ctx, 269 Element filterElement, 270 Element lightElement, 271 Color color) { 272 273 double x = convertNumber(lightElement, SVG_X_ATTRIBUTE, 0); 275 276 double y = convertNumber(lightElement, SVG_Y_ATTRIBUTE, 0); 278 279 double z = convertNumber(lightElement, SVG_Z_ATTRIBUTE, 0); 281 282 return new PointLight(x, y, z, color); 283 } 284 } 285 } 286 | Popular Tags |