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 36 public class BatikRegularPolygonElementBridge 37 extends SVGDecoratedShapeElementBridge 38 implements BatikExtConstants { 39 40 43 public BatikRegularPolygonElementBridge() { } 44 45 48 public String getNamespaceURI() { 49 return BATIK_EXT_NAMESPACE_URI; 50 } 51 52 55 public String getLocalName() { 56 return BATIK_EXT_REGULAR_POLYGON_TAG; 57 } 58 59 62 public Bridge getInstance() { 63 return new BatikRegularPolygonElementBridge(); 64 } 65 66 73 protected void buildShape(BridgeContext ctx, 74 Element e, 75 ShapeNode shapeNode) { 76 77 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e); 78 String s; 79 80 s = e.getAttributeNS(null, SVG_CX_ATTRIBUTE); 82 float cx = 0; 83 if (s.length() != 0) { 84 cx = UnitProcessor.svgHorizontalCoordinateToUserSpace 85 (s, SVG_CX_ATTRIBUTE, uctx); 86 } 87 88 s = e.getAttributeNS(null, SVG_CY_ATTRIBUTE); 90 float cy = 0; 91 if (s.length() != 0) { 92 cy = UnitProcessor.svgVerticalCoordinateToUserSpace 93 (s, SVG_CY_ATTRIBUTE, uctx); 94 } 95 96 s = e.getAttributeNS(null, SVG_R_ATTRIBUTE); 98 float r; 99 if (s.length() != 0) { 100 r = UnitProcessor.svgOtherLengthToUserSpace 101 (s, SVG_R_ATTRIBUTE, uctx); 102 } else { 103 throw new BridgeException(e, ERR_ATTRIBUTE_MISSING, 104 new Object [] {SVG_R_ATTRIBUTE, s}); 105 } 106 107 int sides = convertSides(e, BATIK_EXT_SIDES_ATTRIBUTE, 3); 109 110 GeneralPath gp = new GeneralPath (); 111 for (int i=0; i<sides; i++) { 112 double angle = (i+0.5)*(2*Math.PI/sides) - (Math.PI/2); 113 double x = cx + r*Math.cos(angle); 114 double y = cy - r*Math.sin(angle); 115 if (i==0) 116 gp.moveTo((float)x, (float)y); 117 else 118 gp.lineTo((float)x, (float)y); 119 } 120 gp.closePath(); 121 122 shapeNode.setShape(gp); 123 } 124 125 135 protected static int convertSides(Element filterElement, 136 String attrName, 137 int defaultValue) { 138 String s = filterElement.getAttributeNS(null, attrName); 139 if (s.length() == 0) { 140 return defaultValue; 141 } else { 142 int ret = 0; 143 try { 144 ret = SVGUtilities.convertSVGInteger(s); 145 } catch (NumberFormatException ex) { 146 throw new BridgeException 147 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 148 new Object [] {attrName, s}); 149 } 150 151 if (ret <3) 152 throw new BridgeException 153 (filterElement, ERR_ATTRIBUTE_VALUE_MALFORMED, 154 new Object [] {attrName, s}); 155 return ret; 156 } 157 } 158 } 159 | Popular Tags |