1 18 package org.apache.batik.bridge; 19 20 import java.awt.Shape ; 21 import java.awt.geom.GeneralPath ; 22 import java.awt.geom.Point2D ; 23 24 import org.apache.batik.css.engine.CSSEngineEvent; 25 import org.apache.batik.css.engine.SVGCSSEngine; 26 import org.apache.batik.dom.svg.SVGPathContext; 27 import org.apache.batik.ext.awt.geom.PathLength; 28 import org.apache.batik.gvt.ShapeNode; 29 import org.apache.batik.parser.AWTPathProducer; 30 import org.apache.batik.parser.ParseException; 31 import org.apache.batik.parser.PathParser; 32 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.events.MutationEvent ; 35 36 42 public class SVGPathElementBridge extends SVGDecoratedShapeElementBridge 43 implements SVGPathContext { 44 45 49 protected static final Shape DEFAULT_SHAPE = new GeneralPath (); 50 51 54 public SVGPathElementBridge() {} 55 56 59 public String getLocalName() { 60 return SVG_PATH_TAG; 61 } 62 63 66 public Bridge getInstance() { 67 return new SVGPathElementBridge(); 68 } 69 70 77 protected void buildShape(BridgeContext ctx, 78 Element e, 79 ShapeNode shapeNode) { 80 81 82 String s = e.getAttributeNS(null, SVG_D_ATTRIBUTE); 83 if (s.length() != 0) { 84 AWTPathProducer app = new AWTPathProducer(); 85 app.setWindingRule(CSSUtilities.convertFillRule(e)); 86 try { 87 PathParser pathParser = new PathParser(); 88 pathParser.setPathHandler(app); 89 pathParser.parse(s); 90 } catch (ParseException ex) { 91 BridgeException bex 92 = new BridgeException(e, ERR_ATTRIBUTE_VALUE_MALFORMED, 93 new Object [] {SVG_D_ATTRIBUTE}); 94 bex.setGraphicsNode(shapeNode); 95 throw bex; 96 } finally { 97 shapeNode.setShape(app.getShape()); 98 } 99 } 100 } 101 102 104 107 public void handleDOMAttrModifiedEvent(MutationEvent evt) { 108 String attrName = evt.getAttrName(); 109 if (attrName.equals(SVG_D_ATTRIBUTE)) { 110 if ( evt.getNewValue().length() == 0 ){ 111 ((ShapeNode)node).setShape(DEFAULT_SHAPE); 112 } 113 else{ 114 buildShape(ctx, e, (ShapeNode)node); 115 } 116 handleGeometryChanged(); 117 } else { 118 super.handleDOMAttrModifiedEvent(evt); 119 } 120 } 121 122 protected void handleCSSPropertyChanged(int property) { 123 switch(property) { 124 case SVGCSSEngine.FILL_RULE_INDEX: 125 buildShape(ctx, e, (ShapeNode) node); 126 handleGeometryChanged(); 127 break; 128 default: 129 super.handleCSSPropertyChanged(property); 130 } 131 } 132 133 Shape pathLengthShape = null; 134 PathLength pathLength = null; 135 136 PathLength getPathLengthObj() { 137 Shape s = ((ShapeNode)node).getShape(); 138 if (pathLengthShape != s) { 139 pathLength = new PathLength(s); 140 pathLengthShape = s; 141 } 142 return pathLength; 143 } 144 145 public float getTotalLength() { 147 PathLength pl = getPathLengthObj(); 148 return pl.lengthOfPath(); 149 } 150 151 public Point2D getPointAtLength(float distance) { 152 PathLength pl = getPathLengthObj(); 153 return pl.pointAtLength(distance); 154 } 155 } 156 | Popular Tags |