1 18 package org.apache.batik.svggen; 19 20 import java.awt.geom.Line2D ; 21 import java.awt.geom.Rectangle2D ; 22 import java.awt.geom.RectangularShape ; 23 import java.awt.geom.RoundRectangle2D ; 24 25 import org.w3c.dom.Element ; 26 27 34 public class SVGRectangle extends SVGGraphicObjectConverter { 35 38 private SVGLine svgLine; 39 40 43 public SVGRectangle(SVGGeneratorContext generatorContext) { 44 super(generatorContext); 45 svgLine = new SVGLine(generatorContext); 46 } 47 48 51 public Element toSVG(Rectangle2D rect) { 52 return toSVG((RectangularShape )rect); 53 } 54 55 56 62 public Element toSVG(RoundRectangle2D rect) { 63 Element svgRect = toSVG((RectangularShape )rect); 64 if(svgRect != null && svgRect.getTagName() == SVG_RECT_TAG){ 65 svgRect.setAttributeNS(null, SVG_RX_ATTRIBUTE, 66 doubleString(Math.abs(rect.getArcWidth()/2))); 67 svgRect.setAttributeNS(null, SVG_RY_ATTRIBUTE, 68 doubleString(Math.abs(rect.getArcHeight()/2))); 69 } 70 71 return svgRect; 72 } 73 74 75 78 private Element toSVG(RectangularShape rect) { 79 if(rect.getWidth() > 0 && rect.getHeight() > 0){ 80 Element svgRect = 81 generatorContext.domFactory.createElementNS(SVG_NAMESPACE_URI, 82 SVG_RECT_TAG); 83 svgRect.setAttributeNS(null, SVG_X_ATTRIBUTE, doubleString(rect.getX())); 84 svgRect.setAttributeNS(null, SVG_Y_ATTRIBUTE, doubleString(rect.getY())); 85 svgRect.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, 86 doubleString(rect.getWidth())); 87 svgRect.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, 88 doubleString(rect.getHeight())); 89 90 return svgRect; 91 } 92 else{ 93 if(rect.getWidth() == 0 && rect.getHeight() > 0){ 95 Line2D line = new Line2D.Double (rect.getX(), rect.getY(), rect.getX(), 97 rect.getY() + rect.getHeight()); 98 return svgLine.toSVG(line); 99 } 100 else if(rect.getWidth() > 0 && rect.getHeight() == 0){ 101 Line2D line = new Line2D.Double (rect.getX(), rect.getY(), 103 rect.getX() + rect.getWidth(), 104 rect.getY()); 105 return svgLine.toSVG(line); 106 } 107 return null; 108 } 109 } 110 } 111 | Popular Tags |