1 18 package org.apache.batik.bridge; 19 20 import java.awt.Color ; 21 import java.awt.Paint ; 22 import java.awt.geom.Rectangle2D ; 23 import java.util.Map ; 24 25 import org.apache.batik.ext.awt.image.PadMode; 26 import org.apache.batik.ext.awt.image.renderable.Filter; 27 import org.apache.batik.ext.awt.image.renderable.FilterAlphaRable; 28 import org.apache.batik.ext.awt.image.renderable.FilterColorInterpolation; 29 import org.apache.batik.ext.awt.image.renderable.FloodRable8Bit; 30 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit; 31 import org.apache.batik.gvt.GraphicsNode; 32 import org.apache.batik.gvt.filter.BackgroundRable8Bit; 33 import org.w3c.dom.Element ; 34 35 41 public abstract class AbstractSVGFilterPrimitiveElementBridge 42 extends AbstractSVGBridge 43 implements FilterPrimitiveBridge, ErrorConstants { 44 45 48 protected AbstractSVGFilterPrimitiveElementBridge() {} 49 50 61 protected static Filter getIn(Element filterElement, 62 Element filteredElement, 63 GraphicsNode filteredNode, 64 Filter inputFilter, 65 Map filterMap, 66 BridgeContext ctx) { 67 68 String s = filterElement.getAttributeNS(null, SVG_IN_ATTRIBUTE); 69 if (s.length() == 0) { 70 return inputFilter; 71 } else { 72 return getFilterSource(filterElement, 73 s, 74 filteredElement, 75 filteredNode, 76 filterMap, 77 ctx); 78 } 79 } 80 81 93 protected static Filter getIn2(Element filterElement, 94 Element filteredElement, 95 GraphicsNode filteredNode, 96 Filter inputFilter, 97 Map filterMap, 98 BridgeContext ctx) { 99 100 String s = filterElement.getAttributeNS(null, SVG_IN2_ATTRIBUTE); 101 if (s.length() == 0) { 102 throw new BridgeException(filterElement, ERR_ATTRIBUTE_MISSING, 103 new Object [] {SVG_IN2_ATTRIBUTE}); 104 } 105 return getFilterSource(filterElement, 106 s, 107 filteredElement, 108 filteredNode, 109 filterMap, 110 ctx); 111 } 112 113 120 protected static void updateFilterMap(Element filterElement, 121 Filter filter, 122 Map filterMap) { 123 124 String s = filterElement.getAttributeNS(null, SVG_RESULT_ATTRIBUTE); 125 if ((s.length() != 0) && (s.trim().length() != 0)) { 126 filterMap.put(s, filter); 127 } 128 } 129 130 136 protected static void handleColorInterpolationFilters(Filter filter, 137 Element filterElement) { 138 if (filter instanceof FilterColorInterpolation) { 139 boolean isLinear 140 = CSSUtilities.convertColorInterpolationFilters(filterElement); 141 ((FilterColorInterpolation)filter).setColorSpaceLinear(isLinear); 144 } 145 } 146 147 157 static Filter getFilterSource(Element filterElement, 158 String s, 159 Element filteredElement, 160 GraphicsNode filteredNode, 161 Map filterMap, 162 BridgeContext ctx) { 163 164 Filter srcG = (Filter)filterMap.get(SVG_SOURCE_GRAPHIC_VALUE); 166 Rectangle2D filterRegion = srcG.getBounds2D(); 167 168 int length = s.length(); 169 Filter source = null; 170 switch (length) { 171 case 13: 172 if (SVG_SOURCE_GRAPHIC_VALUE.equals(s)) { 173 source = srcG; 175 } 176 break; 177 case 11: 178 if (s.charAt(1) == SVG_SOURCE_ALPHA_VALUE.charAt(1)) { 179 if (SVG_SOURCE_ALPHA_VALUE.equals(s)) { 180 source = srcG; 182 source = new FilterAlphaRable(source); 183 } 184 } else if (SVG_STROKE_PAINT_VALUE.equals(s)) { 185 Paint paint = PaintServer.convertStrokePaint 187 (filteredElement,filteredNode, ctx); 188 source = new FloodRable8Bit(filterRegion, paint); 190 } 191 break; 192 case 15: 193 if (s.charAt(10) == SVG_BACKGROUND_IMAGE_VALUE.charAt(10)) { 194 if (SVG_BACKGROUND_IMAGE_VALUE.equals(s)) { 195 source = new BackgroundRable8Bit(filteredNode); 197 source = new PadRable8Bit(source, filterRegion, 198 PadMode.ZERO_PAD); 199 } 200 } else if (SVG_BACKGROUND_ALPHA_VALUE.equals(s)) { 201 source = new BackgroundRable8Bit(filteredNode); 203 source = new FilterAlphaRable(source); 204 source = new PadRable8Bit(source, filterRegion, 205 PadMode.ZERO_PAD); 206 } 207 break; 208 case 9: 209 if (SVG_FILL_PAINT_VALUE.equals(s)) { 210 Paint paint = PaintServer.convertFillPaint 212 (filteredElement,filteredNode, ctx); 213 if (paint == null) { 214 paint = new Color (0, 0, 0, 0); } 216 source = new FloodRable8Bit(filterRegion, paint); 217 } 218 break; 219 } 220 if (source == null) { 221 source = (Filter)filterMap.get(s); 223 } 224 return source; 225 } 226 227 231 static final Rectangle2D INFINITE_FILTER_REGION 232 = new Rectangle2D.Float (-Float.MAX_VALUE/2, 233 -Float.MAX_VALUE/2, 234 Float.MAX_VALUE, 235 Float.MAX_VALUE); 236 237 238 239 248 protected static int convertInteger(Element filterElement, 249 String attrName, 250 int defaultValue) { 251 String s = filterElement.getAttributeNS(null, attrName); 252 if (s.length() == 0) { 253 return defaultValue; 254 } else { 255 try { 256 return SVGUtilities.convertSVGInteger(s); 257 } catch (NumberFormatException ex) { 258 throw new BridgeException 259 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 260 new Object [] {attrName, s}); 261 } 262 } 263 } 264 265 274 protected static float convertNumber(Element filterElement, 275 String attrName, 276 float defaultValue) { 277 278 String s = filterElement.getAttributeNS(null, attrName); 279 if (s.length() == 0) { 280 return defaultValue; 281 } else { 282 try { 283 return SVGUtilities.convertSVGNumber(s); 284 } catch (NumberFormatException ex) { 285 throw new BridgeException 286 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 287 new Object [] {attrName, s, ex}); 288 } 289 } 290 } 291 } 292 | Popular Tags |