001package com.hfg.svg; 002 003import com.hfg.css.CssUtil; 004import com.hfg.graphics.units.GfxSize; 005import com.hfg.xml.XMLTag; 006import com.hfg.xml.XMLizable; 007 008import java.awt.Font; 009import java.awt.Point; 010import java.awt.Rectangle; 011import java.awt.geom.Point2D; 012import java.awt.geom.Rectangle2D; 013 014//------------------------------------------------------------------------------ 015/** 016 * Object representation of an SVG (Scalable Vector Graphics) 'switch' tag. 017 * 018 * @author J. Alex Taylor, hairyfatguy.com 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041public class SvgSwitch extends AbstractSvgNode implements SvgNode 042{ 043 044 //************************************************************************** 045 // CONSTRUCTORS 046 //************************************************************************** 047 048 //--------------------------------------------------------------------------- 049 public SvgSwitch() 050 { 051 super(SVG.switch_); 052 } 053 054 //--------------------------------------------------------------------------- 055 public SvgSwitch(XMLTag inXMLTag) 056 { 057 this(); 058 initFromXMLTag(inXMLTag); 059 } 060 061 //************************************************************************** 062 // PUBLIC METHODS 063 //************************************************************************** 064 065 //--------------------------------------------------------------------------- 066 @Override 067 public SvgSwitch setClass(String inValue) 068 { 069 return (SvgSwitch) super.setClass(inValue); 070 } 071 072 //--------------------------------------------------------------------------- 073 /** 074 Sets both the 'font-family' and 'font-size' attributes based on the Font data. 075 */ 076 public SvgSwitch setFont(Font inFont) 077 { 078 setFontFamily(inFont.getName()); 079 setFontSize(inFont.getSize() + "pt;"); 080 return this; 081 } 082 083 //--------------------------------------------------------------------------- 084 public SvgSwitch setFontFamily(String inValue) 085 { 086 addStyle(SvgAttr.fontFamily, inValue); 087 return this; 088 } 089 090 //--------------------------------------------------------------------------- 091 public SvgSwitch setFontWeight(String inValue) 092 { 093 addStyle(SvgAttr.fontWeight, inValue); 094 return this; 095 } 096 097 //--------------------------------------------------------------------------- 098 public SvgSwitch setFontSize(String inValue) 099 { 100 setAttribute(SvgAttr.fontSize, inValue); 101 return this; 102 } 103 104 //--------------------------------------------------------------------------- 105 public SvgSwitch setFontSize(GfxSize inValue) 106 { 107 setAttribute(SvgAttr.fontSize, inValue); 108 return this; 109 } 110 111 //--------------------------------------------------------------------------- 112 /** 113 * Specifies a value for the opacity of children of this group (fill, stroke, and text). 114 * @param inOpacity A numeric value between 0.0 (completely transparent) and 1.0 (completely opaque). 115 * @return this object to allow for method chaining 116 */ 117 public SvgSwitch setOpacity(float inOpacity) 118 { 119 rangeCheckOpacityValue(inOpacity); 120 121 setAttribute(SvgAttr.opacity, inOpacity); 122 return this; 123 } 124 125 //--------------------------------------------------------------------------- 126 @Override 127 public SvgSwitch setStyle(String inValue) 128 { 129 setAttribute(SvgAttr.style, inValue); 130 return this; 131 } 132 133 //-------------------------------------------------------------------------- 134 @Override 135 public SvgSwitch addStyle(String inValue) 136 { 137 CssUtil.addStyle(this, inValue); 138 return this; 139 } 140 141 //-------------------------------------------------------------------------- 142 public SvgSwitch addStyle(String inName, String inValue) 143 { 144 CssUtil.addStyle(this, inName + ":" + inValue); 145 return this; 146 } 147 148 //--------------------------------------------------------------------------- 149 @Override 150 public SvgSwitch setTransform(String inValue) 151 { 152 return (SvgSwitch) super.setTransform(inValue); 153 } 154 155 //--------------------------------------------------------------------------- 156 @Override 157 public SvgSwitch setFilter(String inValue) 158 { 159 return (SvgSwitch) super.setFilter(inValue); 160 } 161 162 //--------------------------------------------------------------------------- 163 public SvgSwitch setId(String inValue) 164 { 165 setAttribute(SvgAttr.id, inValue); 166 return this; 167 } 168 169 //--------------------------------------------------------------------------- 170 public SvgGroup addGroup() 171 { 172 SvgGroup newGroup = new SvgGroup(); 173 addSubtag(newGroup); 174 return newGroup; 175 } 176 177 //--------------------------------------------------------------------------- 178 public SvgLink addLink(CharSequence inURL) 179 { 180 SvgLink newLink = new SvgLink(inURL); 181 addSubtag(newLink); 182 return newLink; 183 } 184 185 //--------------------------------------------------------------------------- 186 public SvgLine addLine(Point inStart, Point inEnd) 187 { 188 SvgLine line = new SvgLine(inStart, inEnd); 189 addSubtag(line); 190 return line; 191 } 192 193 //--------------------------------------------------------------------------- 194 public SvgLine addLine(Point2D inStart, Point2D inEnd) 195 { 196 SvgLine line = new SvgLine(inStart, inEnd); 197 addSubtag(line); 198 return line; 199 } 200 201 //--------------------------------------------------------------------------- 202 public SvgRect addRect(Rectangle2D inRect) 203 { 204 SvgRect rect = new SvgRect(inRect); 205 addSubtag(rect); 206 return rect; 207 } 208 209 //--------------------------------------------------------------------------- 210 public SvgText addText(String inText) 211 { 212 SvgText text = new SvgText(inText); 213 addSubtag(text); 214 return text; 215 } 216 217 //--------------------------------------------------------------------------- 218 public SvgText addText(String inText, Font inFont, Point2D inLocation) 219 { 220 SvgText text = new SvgText(inText, inFont, inLocation); 221 addSubtag(text); 222 return text; 223 } 224 225 //--------------------------------------------------------------------------- 226 public SvgCircle addCircle() 227 { 228 SvgCircle circle = new SvgCircle(); 229 addSubtag(circle); 230 return circle; 231 } 232 233 //--------------------------------------------------------------------------- 234 public SvgCircle addCircle(Point2D inCenter, int inRadius) 235 { 236 SvgCircle circle = new SvgCircle(inCenter, inRadius); 237 addSubtag(circle); 238 return circle; 239 } 240 241 //--------------------------------------------------------------------------- 242 public SvgPath addPath() 243 { 244 SvgPath path = new SvgPath(); 245 addSubtag(path); 246 return path; 247 } 248 249 //--------------------------------------------------------------------------- 250 public SvgPath addPath(String inPathData) 251 { 252 SvgPath path = new SvgPath(inPathData); 253 addSubtag(path); 254 return path; 255 } 256 257 //--------------------------------------------------------------------------- 258 public SvgEllipse addEllipse() 259 { 260 SvgEllipse ellipse = new SvgEllipse(); 261 addSubtag(ellipse); 262 return ellipse; 263 } 264 265 //--------------------------------------------------------------------------- 266 public SvgPolygon addPolygon() 267 { 268 SvgPolygon polygon = new SvgPolygon(); 269 addSubtag(polygon); 270 return polygon; 271 } 272 273 //--------------------------------------------------------------------------- 274 @Override 275 public Rectangle2D getBoundsBox() 276 { 277 Double minX = null; 278 Double minY = null; 279 Double maxX = null; 280 Double maxY = null; 281 282 for (XMLizable node : getSubtags()) 283 { 284 if (node instanceof SvgNode) 285 { 286 Rectangle2D rect = ((SvgNode)node).getBoundsBox(); 287 if (rect != null) 288 { 289 if (null == minX || rect.getX() < minX) minX = rect.getX(); 290 if (null == minY || rect.getY() < minY) minY = rect.getY(); 291 if (null == maxX || rect.getMaxX() > maxX) maxX = rect.getMaxX(); 292 if (null == maxY || rect.getMaxY() > maxY) maxY = rect.getMaxY(); 293 } 294 } 295 } 296 297 Rectangle2D boundsBox = null; 298 if (minX != null 299 && minY != null 300 && maxX != null 301 && maxY != null) 302 { 303 boundsBox = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY); 304 adjustBoundsForTransform(boundsBox); 305 } 306 307 return boundsBox; 308 } 309 310}