1 18 package org.apache.batik.bridge; 19 20 import java.awt.RenderingHints ; 21 import java.awt.Shape ; 22 import java.awt.geom.AffineTransform ; 23 import java.awt.geom.Area ; 24 import java.awt.geom.GeneralPath ; 25 26 import org.apache.batik.css.engine.CSSImportNode; 27 import org.apache.batik.dom.svg.SVGOMCSSImportedElementRoot; 28 import org.apache.batik.ext.awt.image.renderable.ClipRable; 29 import org.apache.batik.ext.awt.image.renderable.ClipRable8Bit; 30 import org.apache.batik.ext.awt.image.renderable.Filter; 31 import org.apache.batik.gvt.GraphicsNode; 32 import org.apache.batik.gvt.ShapeNode; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 36 42 public class SVGClipPathElementBridge extends AbstractSVGBridge 43 implements ClipBridge { 44 45 48 public SVGClipPathElementBridge() {} 49 50 53 public String getLocalName() { 54 return SVG_CLIP_PATH_TAG; 55 } 56 57 65 public ClipRable createClip(BridgeContext ctx, 66 Element clipElement, 67 Element clipedElement, 68 GraphicsNode clipedNode) { 69 70 String s; 71 72 AffineTransform Tx; 74 s = clipElement.getAttributeNS(null, SVG_TRANSFORM_ATTRIBUTE); 75 if (s.length() != 0) { 76 Tx = SVGUtilities.convertTransform 77 (clipElement, SVG_TRANSFORM_ATTRIBUTE, s); 78 } else { 79 Tx = new AffineTransform (); 80 } 81 82 short coordSystemType; 84 s = clipElement.getAttributeNS(null, SVG_CLIP_PATH_UNITS_ATTRIBUTE); 85 if (s.length() == 0) { 86 coordSystemType = SVGUtilities.USER_SPACE_ON_USE; 87 } else { 88 coordSystemType = SVGUtilities.parseCoordinateSystem 89 (clipElement, SVG_CLIP_PATH_UNITS_ATTRIBUTE, s); 90 } 91 if (coordSystemType == SVGUtilities.OBJECT_BOUNDING_BOX) { 93 Tx = SVGUtilities.toObjectBBox(Tx, clipedNode); 94 } 95 96 Area clipPath = new Area (); 106 GVTBuilder builder = ctx.getGVTBuilder(); 107 boolean hasChildren = false; 108 for(Node node = clipElement.getFirstChild(); 109 node != null; 110 node = node.getNextSibling()) { 111 112 if (node.getNodeType() != Node.ELEMENT_NODE) { 114 continue; 115 } 116 117 Element child = (Element )node; 118 GraphicsNode clipNode = builder.build(ctx, child) ; 119 if (clipNode == null) { 121 continue; 122 } 123 hasChildren = true; 124 125 if (child instanceof CSSImportNode) { 127 SVGOMCSSImportedElementRoot shadow = 128 (SVGOMCSSImportedElementRoot) 129 ((CSSImportNode) child).getCSSImportedElementRoot(); 130 131 if (shadow != null) { 132 Node shadowChild = shadow.getFirstChild(); 133 if (shadowChild != null 134 && shadowChild.getNodeType() == Node.ELEMENT_NODE) { 135 child = (Element ) shadowChild; 136 } 137 } 138 } 139 140 int wr = CSSUtilities.convertClipRule(child); 142 GeneralPath path = new GeneralPath (clipNode.getOutline()); 143 path.setWindingRule(wr); 144 145 AffineTransform at = clipNode.getTransform(); 146 if (at == null) at = Tx; 147 else at.preConcatenate(Tx); 148 149 Shape outline = at.createTransformedShape(path); 150 151 ShapeNode outlineNode = new ShapeNode(); 153 outlineNode.setShape(outline); 154 ClipRable clip = CSSUtilities.convertClipPath(child, 155 outlineNode, 156 ctx); 157 if (clip != null) { 158 Area area = new Area (outline); 159 area.subtract(new Area (clip.getClipPath())); 160 outline = area; 161 } 162 clipPath.add(new Area (outline)); 163 } 164 if (!hasChildren) { 165 return null; } 167 168 ShapeNode clipPathNode = new ShapeNode(); 170 clipPathNode.setShape(clipPath); 171 172 ClipRable clipElementClipPath = 174 CSSUtilities.convertClipPath(clipElement, clipPathNode, ctx); 175 if (clipElementClipPath != null) { 176 clipPath.subtract(new Area (clipElementClipPath.getClipPath())); 177 } 178 179 Filter filter = clipedNode.getFilter(); 180 if (filter == null) { 181 filter = clipedNode.getGraphicsNodeRable(true); 183 } 184 185 boolean useAA = false; 186 RenderingHints hints; 187 hints = CSSUtilities.convertShapeRendering(clipElement, null); 188 if (hints != null) { 189 Object o = hints.get(RenderingHints.KEY_ANTIALIASING); 190 useAA = (o == RenderingHints.VALUE_ANTIALIAS_ON); 191 } 192 193 return new ClipRable8Bit(filter, clipPath, useAA); 194 } 195 } 196 | Popular Tags |