1 18 package org.apache.batik.extension.svg; 19 20 import java.awt.geom.GeneralPath ; 21 22 import org.apache.batik.bridge.Bridge; 23 import org.apache.batik.bridge.BridgeContext; 24 import org.apache.batik.bridge.BridgeException; 25 import org.apache.batik.bridge.SVGDecoratedShapeElementBridge; 26 import org.apache.batik.bridge.SVGUtilities; 27 import org.apache.batik.bridge.UnitProcessor; 28 import org.apache.batik.gvt.ShapeNode; 29 import org.w3c.dom.Element ; 30 31 37 public class BatikStarElementBridge 38 extends SVGDecoratedShapeElementBridge 39 implements BatikExtConstants { 40 41 44 public BatikStarElementBridge() { } 45 46 49 public String getNamespaceURI() { 50 return BATIK_EXT_NAMESPACE_URI; 51 } 52 53 56 public String getLocalName() { 57 return BATIK_EXT_STAR_TAG; 58 } 59 60 63 public Bridge getInstance() { 64 return new BatikStarElementBridge(); 65 } 66 67 74 protected void buildShape(BridgeContext ctx, 75 Element e, 76 ShapeNode shapeNode) { 77 78 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e); 79 String s; 80 81 s = e.getAttributeNS(null, SVG_CX_ATTRIBUTE); 83 float cx = 0; 84 if (s.length() != 0) { 85 cx = UnitProcessor.svgHorizontalCoordinateToUserSpace 86 (s, SVG_CX_ATTRIBUTE, uctx); 87 } 88 89 s = e.getAttributeNS(null, SVG_CY_ATTRIBUTE); 91 float cy = 0; 92 if (s.length() != 0) { 93 cy = UnitProcessor.svgVerticalCoordinateToUserSpace 94 (s, SVG_CY_ATTRIBUTE, uctx); 95 } 96 97 s = e.getAttributeNS(null, SVG_R_ATTRIBUTE); 99 float r; 100 if (s.length() == 0) 101 throw new BridgeException(e, ERR_ATTRIBUTE_MISSING, 102 new Object [] {SVG_R_ATTRIBUTE, s}); 103 r = UnitProcessor.svgOtherLengthToUserSpace 104 (s, SVG_R_ATTRIBUTE, uctx); 105 106 s = e.getAttributeNS(null, BATIK_EXT_IR_ATTRIBUTE); 108 float ir; 109 if (s.length() == 0) 110 throw new BridgeException 111 (e, ERR_ATTRIBUTE_MISSING, 112 new Object [] {BATIK_EXT_IR_ATTRIBUTE, s}); 113 114 ir = UnitProcessor.svgOtherLengthToUserSpace 115 (s, BATIK_EXT_IR_ATTRIBUTE, uctx); 116 117 int sides = convertSides(e, BATIK_EXT_SIDES_ATTRIBUTE, 3); 119 120 GeneralPath gp = new GeneralPath (); 121 double angle, x, y; 122 for (int i=0; i<sides; i++) { 123 angle = (i)*(2*Math.PI/sides) - (Math.PI/2); 124 x = cx + ir*Math.cos(angle); 125 y = cy - ir*Math.sin(angle); 126 if (i==0) 127 gp.moveTo((float)x, (float)y); 128 else 129 gp.lineTo((float)x, (float)y); 130 131 angle = (i+0.5)*(2*Math.PI/sides) - (Math.PI/2); 132 x = cx + r*Math.cos(angle); 133 y = cy - r*Math.sin(angle); 134 gp.lineTo((float)x, (float)y); 135 } 136 gp.closePath(); 137 138 shapeNode.setShape(gp); 139 } 140 141 151 protected static int convertSides(Element filterElement, 152 String attrName, 153 int defaultValue) { 154 String s = filterElement.getAttributeNS(null, attrName); 155 if (s.length() == 0) { 156 return defaultValue; 157 } else { 158 int ret = 0; 159 try { 160 ret = SVGUtilities.convertSVGInteger(s); 161 } catch (NumberFormatException ex) { 162 throw new BridgeException 163 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 164 new Object [] {attrName, s}); 165 } 166 167 if (ret <3) 168 throw new BridgeException 169 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 170 new Object [] {attrName, s}); 171 return ret; 172 } 173 } 174 } 175 | Popular Tags |