1 18 package org.apache.batik.bridge; 19 20 import java.awt.Shape ; 21 import java.awt.geom.Rectangle2D ; 22 import java.awt.geom.RoundRectangle2D ; 23 24 import org.apache.batik.gvt.ShapeNode; 25 import org.apache.batik.gvt.ShapePainter; 26 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.events.MutationEvent ; 29 30 36 public class SVGRectElementBridge extends SVGShapeElementBridge { 37 38 41 public SVGRectElementBridge() {} 42 43 46 public String getLocalName() { 47 return SVG_RECT_TAG; 48 } 49 50 53 public Bridge getInstance() { 54 return new SVGRectElementBridge(); 55 } 56 57 64 protected void buildShape(BridgeContext ctx, 65 Element e, 66 ShapeNode shapeNode) { 67 68 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e); 69 String s; 70 71 s = e.getAttributeNS(null, SVG_X_ATTRIBUTE); 73 float x = 0; 74 if (s.length() != 0) { 75 x = UnitProcessor.svgHorizontalCoordinateToUserSpace 76 (s, SVG_X_ATTRIBUTE, uctx); 77 } 78 79 s = e.getAttributeNS(null, SVG_Y_ATTRIBUTE); 81 float y = 0; 82 if (s.length() != 0) { 83 y = UnitProcessor.svgVerticalCoordinateToUserSpace 84 (s, SVG_Y_ATTRIBUTE, uctx); 85 } 86 87 s = e.getAttributeNS(null, SVG_WIDTH_ATTRIBUTE); 89 float w; 90 if (s.length() != 0) { 91 w = UnitProcessor.svgHorizontalLengthToUserSpace 92 (s, SVG_WIDTH_ATTRIBUTE, uctx); 93 } else { 94 throw new BridgeException(e, ERR_ATTRIBUTE_MISSING, 95 new Object [] {SVG_WIDTH_ATTRIBUTE, s}); 96 } 97 98 s = e.getAttributeNS(null, SVG_HEIGHT_ATTRIBUTE); 100 float h; 101 if (s.length() != 0) { 102 h = UnitProcessor.svgVerticalLengthToUserSpace 103 (s, SVG_HEIGHT_ATTRIBUTE, uctx); 104 } else { 105 throw new BridgeException(e, ERR_ATTRIBUTE_MISSING, 106 new Object [] {SVG_HEIGHT_ATTRIBUTE, s}); 107 } 108 109 s = e.getAttributeNS(null, SVG_RX_ATTRIBUTE); 111 boolean rxs = (s.length() != 0); 112 float rx = 0; 113 if (rxs) { 114 rx = UnitProcessor.svgHorizontalLengthToUserSpace 115 (s, SVG_RX_ATTRIBUTE, uctx); 116 } 117 rx = (rx > w / 2) ? w / 2 : rx; 118 119 s = e.getAttributeNS(null, SVG_RY_ATTRIBUTE); 121 boolean rys = (s.length() != 0); 122 float ry = 0; 123 if (rys) { 124 ry = UnitProcessor.svgVerticalLengthToUserSpace 125 (s, SVG_RY_ATTRIBUTE, uctx); 126 } 127 ry = (ry > h / 2) ? h / 2 : ry; 128 129 Shape shape = null; 130 if (rxs && rys) { 131 if (rx == 0 || ry == 0) { 132 shape = new Rectangle2D.Float (x, y, w, h); 133 } else { 134 shape = new RoundRectangle2D.Float (x, y, w, h, rx*2, ry*2); 135 } 136 } else if (rxs) { 137 if (rx == 0) { 138 shape = new Rectangle2D.Float (x, y, w, h); 139 } else { 140 shape = new RoundRectangle2D.Float (x, y, w, h, rx*2, rx*2); 141 } 142 } else if (rys) { 143 if (ry == 0) { 144 shape = new Rectangle2D.Float (x, y, w, h); 145 } else { 146 shape = new RoundRectangle2D.Float (x, y, w, h, ry*2, ry*2); 147 } 148 } else { 149 shape = new Rectangle2D.Float (x, y, w, h); 150 } 151 shapeNode.setShape(shape); 152 } 153 154 156 159 public void handleDOMAttrModifiedEvent(MutationEvent evt) { 160 String attrName = evt.getAttrName(); 161 if (attrName.equals(SVG_X_ATTRIBUTE) || 162 attrName.equals(SVG_Y_ATTRIBUTE) || 163 attrName.equals(SVG_WIDTH_ATTRIBUTE) || 164 attrName.equals(SVG_HEIGHT_ATTRIBUTE) || 165 attrName.equals(SVG_RX_ATTRIBUTE) || 166 attrName.equals(SVG_RY_ATTRIBUTE)) { 167 168 buildShape(ctx, e, (ShapeNode)node); 169 handleGeometryChanged(); 170 } else { 171 super.handleDOMAttrModifiedEvent(evt); 172 } 173 } 174 175 176 protected ShapePainter createShapePainter(BridgeContext ctx, 177 Element e, 178 ShapeNode shapeNode) { 179 Shape shape = shapeNode.getShape(); 180 Rectangle2D r2d = shape.getBounds2D(); 181 if ((r2d.getWidth() == 0) || (r2d.getHeight() == 0)) 182 return null; 183 return super.createShapePainter(ctx, e, shapeNode); 184 } 185 } 186 | Popular Tags |