1 18 package org.apache.batik.bridge; 19 20 import java.awt.geom.Rectangle2D ; 21 import java.util.Map ; 22 import java.util.StringTokenizer ; 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 ; 29 30 36 public class SVGFeTurbulenceElementBridge 37 extends AbstractSVGFilterPrimitiveElementBridge { 38 39 42 public SVGFeTurbulenceElementBridge() {} 43 44 47 public String getLocalName() { 48 return SVG_FE_TURBULENCE_TAG; 49 } 50 51 69 public Filter createFilter(BridgeContext ctx, 70 Element filterElement, 71 Element filteredElement, 72 GraphicsNode filteredNode, 73 Filter inputFilter, 74 Rectangle2D filterRegion, 75 Map filterMap) { 76 77 Filter in = getIn(filterElement, 79 filteredElement, 80 filteredNode, 81 inputFilter, 82 filterMap, 83 ctx); 84 if (in == null) { 85 return null; } 87 88 Rectangle2D defaultRegion = filterRegion; 90 Rectangle2D primitiveRegion 91 = SVGUtilities.convertFilterPrimitiveRegion(filterElement, 92 filteredElement, 93 filteredNode, 94 defaultRegion, 95 filterRegion, 96 ctx); 97 98 float [] baseFrequency 100 = convertBaseFrenquency(filterElement); 101 102 int numOctaves 104 = convertInteger(filterElement, SVG_NUM_OCTAVES_ATTRIBUTE, 1); 105 106 int seed 108 = convertInteger(filterElement, SVG_SEED_ATTRIBUTE, 0); 109 110 boolean stitchTiles 112 = convertStitchTiles(filterElement); 113 114 boolean isFractalNoise 116 = convertType(filterElement); 117 118 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 handleColorInterpolationFilters(turbulenceRable, filterElement); 131 132 updateFilterMap(filterElement, turbulenceRable, filterMap); 134 135 return turbulenceRable; 136 } 137 138 144 protected static float [] convertBaseFrenquency(Element e) { 145 String 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 tokens = new StringTokenizer (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 [] {SVG_BASE_FREQUENCY_ATTRIBUTE, s}); 162 } 163 } catch (NumberFormatException ex) { 164 throw new BridgeException 165 (e, ERR_ATTRIBUTE_VALUE_MALFORMED, 166 new Object [] {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 [] {SVG_BASE_FREQUENCY_ATTRIBUTE, s}); 172 } 173 return v; 174 } 175 176 183 protected static boolean convertStitchTiles(Element e) { 184 String 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 [] {SVG_STITCH_TILES_ATTRIBUTE, s}); 196 } 197 198 204 protected static boolean convertType(Element e) { 205 String 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 [] {SVG_TYPE_ATTRIBUTE, s}); 217 } 218 } 219 | Popular Tags |