1 18 package org.apache.batik.bridge; 19 20 import java.awt.geom.AffineTransform ; 21 import java.awt.geom.NoninvertibleTransformException ; 22 import java.awt.geom.Rectangle2D ; 23 import java.util.Map ; 24 25 import org.apache.batik.dom.util.XLinkSupport; 26 import org.apache.batik.util.XMLConstants; 27 import org.apache.batik.ext.awt.image.PadMode; 28 import org.apache.batik.ext.awt.image.renderable.AffineRable8Bit; 29 import org.apache.batik.ext.awt.image.renderable.Filter; 30 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit; 31 import org.apache.batik.ext.awt.image.spi.ImageTagRegistry; 32 import org.apache.batik.gvt.GraphicsNode; 33 import org.apache.batik.util.ParsedURL; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 37 43 public class SVGFeImageElementBridge 44 extends AbstractSVGFilterPrimitiveElementBridge { 45 46 49 public SVGFeImageElementBridge() {} 50 51 54 public String getLocalName() { 55 return SVG_FE_IMAGE_TAG; 56 } 57 58 76 public Filter createFilter(BridgeContext ctx, 77 Element filterElement, 78 Element filteredElement, 79 GraphicsNode filteredNode, 80 Filter inputFilter, 81 Rectangle2D filterRegion, 82 Map filterMap) { 83 84 String uriStr = XLinkSupport.getXLinkHref(filterElement); 86 if (uriStr.length() == 0) { 87 throw new BridgeException(filterElement, ERR_ATTRIBUTE_MISSING, 88 new Object [] {"xlink:href"}); 89 } 90 91 106 Document document = filterElement.getOwnerDocument(); 107 boolean isUse = (uriStr.indexOf("#") != -1); 108 Element contentElement = null; 109 if (isUse) { 110 contentElement = document.createElementNS(SVG_NAMESPACE_URI, 111 SVG_USE_TAG); 112 } else { 113 contentElement = document.createElementNS(SVG_NAMESPACE_URI, 114 SVG_IMAGE_TAG); 115 } 116 117 118 contentElement.setAttributeNS(XLinkSupport.XLINK_NAMESPACE_URI, XMLConstants.XLINK_PREFIX + 119 ":" + SVG_HREF_ATTRIBUTE, 120 uriStr); 121 122 Element proxyElement = document.createElementNS(SVG_NAMESPACE_URI, 123 SVG_G_TAG); 124 proxyElement.appendChild(contentElement); 125 126 Rectangle2D defaultRegion = filterRegion; 128 129 AffineTransform at = new AffineTransform (); 132 133 short coordSystemType; 135 Element filterDefElement = (Element )(filterElement.getParentNode()); 136 boolean isBBox = false; 137 String s = SVGUtilities.getChainableAttributeNS 138 (filterDefElement, null, SVG_PRIMITIVE_UNITS_ATTRIBUTE, ctx); 139 if (s.length() == 0) { 140 coordSystemType = SVGUtilities.USER_SPACE_ON_USE; 141 } else { 142 coordSystemType = SVGUtilities.parseCoordinateSystem 143 (filterDefElement, SVG_PRIMITIVE_UNITS_ATTRIBUTE, s); 144 } 145 146 if (coordSystemType == SVGUtilities.OBJECT_BOUNDING_BOX) { 147 isBBox = true; 148 at = SVGUtilities.toObjectBBox(at, filteredNode); 149 } 150 151 Rectangle2D primitiveRegionUserSpace 153 = SVGUtilities.convertFilterPrimitiveRegion(filterElement, 154 filteredElement, 155 filteredNode, 156 defaultRegion, 157 filterRegion, 158 ctx); 159 Rectangle2D primitiveRegion = primitiveRegionUserSpace; 160 161 if (isBBox) { 162 try { 163 AffineTransform ati = at.createInverse(); 164 primitiveRegion = ati.createTransformedShape(primitiveRegion).getBounds2D(); 165 } catch (NoninvertibleTransformException nite) { 166 throw new Error (); 168 } 169 } 170 171 contentElement.setAttributeNS(null, SVG_X_ATTRIBUTE, "" + primitiveRegion.getX()); 172 contentElement.setAttributeNS(null, SVG_Y_ATTRIBUTE, "" + primitiveRegion.getY()); 173 contentElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, "" + primitiveRegion.getWidth()); 174 contentElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, "" + primitiveRegion.getHeight()); 175 176 179 GraphicsNode node = ctx.getGVTBuilder().build(ctx, proxyElement); 180 Filter filter = node.getGraphicsNodeRable(true); 181 182 filter = new AffineRable8Bit(filter, at); 183 184 handleColorInterpolationFilters(filter, filterElement); 186 187 filter = new PadRable8Bit(filter, primitiveRegionUserSpace, PadMode.ZERO_PAD); 188 189 updateFilterMap(filterElement, filter, filterMap); 191 192 return filter; 193 } 194 195 206 protected static Filter createSVGFeImage(BridgeContext ctx, 207 Rectangle2D primitiveRegion, 208 Element refElement, 209 boolean toBBoxNeeded, 210 Element filterElement, 211 GraphicsNode filteredNode) { 212 213 GraphicsNode node = ctx.getGVTBuilder().build(ctx, refElement); 222 Filter filter = node.getGraphicsNodeRable(true); 223 224 AffineTransform at = new AffineTransform (); 225 226 if (toBBoxNeeded){ 227 short coordSystemType; 229 Element filterDefElement = (Element )(filterElement.getParentNode()); 230 String s = SVGUtilities.getChainableAttributeNS 231 (filterDefElement, null, SVG_PRIMITIVE_UNITS_ATTRIBUTE, ctx); 232 if (s.length() == 0) { 233 coordSystemType = SVGUtilities.USER_SPACE_ON_USE; 234 } else { 235 coordSystemType = SVGUtilities.parseCoordinateSystem 236 (filterDefElement, SVG_PRIMITIVE_UNITS_ATTRIBUTE, s); 237 } 238 239 if (coordSystemType == SVGUtilities.OBJECT_BOUNDING_BOX) { 240 at = SVGUtilities.toObjectBBox(at, filteredNode); 241 } 242 243 Rectangle2D bounds = filteredNode.getGeometryBounds(); 244 at.preConcatenate(AffineTransform.getTranslateInstance 245 (primitiveRegion.getX() - bounds.getX(), 246 primitiveRegion.getY() - bounds.getY())); 247 248 } else { 249 250 at.translate(primitiveRegion.getX(), primitiveRegion.getY()); 253 } 254 255 return new AffineRable8Bit(filter, at); 256 } 257 258 265 protected static Filter createRasterFeImage(BridgeContext ctx, 266 Rectangle2D primitiveRegion, 267 ParsedURL purl) { 268 269 Filter filter = ImageTagRegistry.getRegistry().readURL(purl); 272 273 Rectangle2D bounds = filter.getBounds2D(); 274 AffineTransform scale = new AffineTransform (); 275 scale.translate(primitiveRegion.getX(), primitiveRegion.getY()); 276 scale.scale(primitiveRegion.getWidth()/(bounds.getWidth()-1), 277 primitiveRegion.getHeight()/(bounds.getHeight()-1)); 278 scale.translate(-bounds.getX(), -bounds.getY()); 279 280 return new AffineRable8Bit(filter, scale); 281 } 282 } 283 | Popular Tags |