1 18 package org.apache.batik.bridge; 19 20 import java.awt.geom.Rectangle2D ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.apache.batik.dom.svg.SVGOMDocument; 30 import org.apache.batik.dom.util.XLinkSupport; 31 import org.apache.batik.ext.awt.image.PadMode; 32 import org.apache.batik.ext.awt.image.renderable.Filter; 33 import org.apache.batik.ext.awt.image.renderable.FilterChainRable; 34 import org.apache.batik.ext.awt.image.renderable.FilterChainRable8Bit; 35 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit; 36 import org.apache.batik.gvt.GraphicsNode; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 40 46 public class SVGFilterElementBridge extends AbstractSVGBridge 47 implements FilterBridge, ErrorConstants { 48 49 52 public SVGFilterElementBridge() {} 53 54 57 public String getLocalName() { 58 return SVG_FILTER_TAG; 59 } 60 61 69 public Filter createFilter(BridgeContext ctx, 70 Element filterElement, 71 Element filteredElement, 72 GraphicsNode filteredNode) { 73 74 Rectangle2D filterRegion = SVGUtilities.convertFilterChainRegion 76 (filterElement, filteredElement, filteredNode, ctx); 77 78 Filter sourceGraphic = filteredNode.getGraphicsNodeRable(true); 80 sourceGraphic = new PadRable8Bit(sourceGraphic, filterRegion, 82 PadMode.ZERO_PAD); 83 84 FilterChainRable filterChain 86 = new FilterChainRable8Bit(sourceGraphic, filterRegion); 87 88 float [] filterRes = SVGUtilities.convertFilterRes(filterElement, ctx); 90 filterChain.setFilterResolutionX((int)filterRes[0]); 91 filterChain.setFilterResolutionY((int)filterRes[1]); 92 93 Map filterNodeMap = new HashMap (11); 96 filterNodeMap.put(SVG_SOURCE_GRAPHIC_VALUE, sourceGraphic); 97 98 99 Filter in = buildFilterPrimitives(filterElement, 100 filterRegion, 101 filteredElement, 102 filteredNode, 103 sourceGraphic, 104 filterNodeMap, 105 ctx); 106 if ((in == null) || (in == sourceGraphic)) { 107 return null; } else { 109 filterChain.setSource(in); 110 return filterChain; 111 } 112 } 113 114 130 protected static Filter buildFilterPrimitives(Element filterElement, 131 Rectangle2D filterRegion, 132 Element filteredElement, 133 GraphicsNode filteredNode, 134 Filter in, 135 Map filterNodeMap, 136 BridgeContext ctx) { 137 138 List refs = new LinkedList (); 139 for (;;) { 140 Filter newIn = buildLocalFilterPrimitives(filterElement, 141 filterRegion, 142 filteredElement, 143 filteredNode, 144 in, 145 filterNodeMap, 146 ctx); 147 if (newIn != in) { 148 return newIn; } 150 String uri = XLinkSupport.getXLinkHref(filterElement); 151 if (uri.length() == 0) { 152 return in; } 154 SVGOMDocument doc = (SVGOMDocument)filterElement.getOwnerDocument(); 156 URL url; 157 try { 158 url = new URL (doc.getURLObject(), uri); 159 } catch (MalformedURLException ex) { 160 throw new BridgeException(filterElement, 161 ERR_URI_MALFORMED, 162 new Object [] {uri}); 163 164 } 165 if (contains(refs, url)) { 166 throw new BridgeException(filterElement, 167 ERR_XLINK_HREF_CIRCULAR_DEPENDENCIES, 168 new Object [] {uri}); 169 } 170 refs.add(url); 171 filterElement = ctx.getReferencedElement(filterElement, uri); 172 } 173 } 174 175 189 protected static 190 Filter buildLocalFilterPrimitives(Element filterElement, 191 Rectangle2D filterRegion, 192 Element filteredElement, 193 GraphicsNode filteredNode, 194 Filter in, 195 Map filterNodeMap, 196 BridgeContext ctx) { 197 198 for (Node n = filterElement.getFirstChild(); 199 n != null; 200 n = n.getNextSibling()) { 201 202 if (n.getNodeType() != Node.ELEMENT_NODE) { 203 continue; } 205 Element e = (Element )n; 206 Bridge bridge = ctx.getBridge(e); 207 if (bridge == null || !(bridge instanceof FilterPrimitiveBridge)) { 208 continue; 209 } 210 FilterPrimitiveBridge filterBridge = (FilterPrimitiveBridge)bridge; 211 Filter filterNode = filterBridge.createFilter(ctx, 212 e, 213 filteredElement, 214 filteredNode, 215 in, 216 filterRegion, 217 filterNodeMap); 218 if (filterNode == null) { 219 return null; } else { 221 in = filterNode; 222 } 223 } 224 return in; 225 } 226 227 233 private static boolean contains(List urls, URL key) { 234 Iterator iter = urls.iterator(); 235 while (iter.hasNext()) { 236 URL url = (URL )iter.next(); 237 if (url.sameFile(key) && url.getRef().equals(key.getRef())) { 238 return true; 239 } 240 } 241 return false; 242 } 243 } 244 | Popular Tags |