001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/graphics/sld/Font.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 package org.deegree.graphics.sld; 037 038 import java.awt.Color; 039 import java.util.Iterator; 040 import java.util.Map; 041 042 import org.deegree.framework.util.ColorUtils; 043 import org.deegree.framework.xml.Marshallable; 044 import org.deegree.model.feature.Feature; 045 import org.deegree.model.filterencoding.FilterEvaluationException; 046 047 /** 048 * The Font element identifies a font of a certain family, style, weight, size and color. 049 * <p> 050 * The supported CSS-Parameter names are: 051 * <ul> 052 * <li>font-family 053 * <li>font-style 054 * <li>font-weight 055 * <li>font-size 056 * <li>font-color 057 * <p> 058 * 059 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 060 * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a> 061 * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $ 062 */ 063 public class Font implements Marshallable { 064 065 /** 066 * java.awt.Font.PLAIN 067 */ 068 public static final int STYLE_NORMAL = java.awt.Font.PLAIN; 069 070 /** 071 * java.awt.Font.ITALIC 072 */ 073 public static final int STYLE_ITALIC = java.awt.Font.ITALIC; 074 075 /** 076 * java.awt.Font.ITALIC 077 */ 078 public static final int STYLE_OBLIQUE = java.awt.Font.ITALIC; 079 080 /** 081 * java.awt.Font.PLAIN 082 */ 083 public static final int WEIGHT_NORMAL = java.awt.Font.PLAIN; 084 085 /** 086 * java.awt.Font.BOLD 087 */ 088 public static final int WEIGHT_BOLD = java.awt.Font.BOLD; 089 090 /** 091 * Default character size initialized with 10. 092 */ 093 public static final int SIZE_DEFAULT = 10; 094 095 /** 096 * Default color is 127,127,127 (white) 097 */ 098 public static final Color COLOR_DEFAULT = new Color( 127, 127, 127 ); 099 100 private Map<String, CssParameter> cssParams = null; 101 102 /** 103 * Constructs a new <tt>Font<tt>. 104 * <p> 105 * @param cssParams keys are <tt>Strings<tt> (see above), values are 106 * <tt>CssParameters</tt> 107 */ 108 public Font( Map<String, CssParameter> cssParams ) { 109 this.cssParams = cssParams; 110 } 111 112 /** 113 * returns the Map of the CssParameters describing a Font 114 * 115 * @return the Map of the CssParameters describing a Font 116 */ 117 public Map<String, CssParameter> getCssParameters() { 118 return cssParams; 119 } 120 121 /** 122 * Returns the (evaluated) value of the font's CssParameter 'font-family'. 123 * <p> 124 * 125 * @param feature 126 * specifies the <tt>Feature</tt> to be used for evaluation of the underlying 127 * 'sld:ParameterValueType' 128 * @return the (evaluated) <tt>String</tt> value of the parameter 129 * @throws FilterEvaluationException 130 * if the evaluation fails 131 */ 132 public String getFamily( Feature feature ) 133 throws FilterEvaluationException { 134 CssParameter cssParam = cssParams.get( "font-family" ); 135 136 if ( cssParam == null ) { 137 return null; 138 } 139 140 return cssParam.getValue( feature ).trim(); 141 } 142 143 /** 144 * Sets the value of the font's CssParameter 'font-family'. 145 * <p> 146 * 147 * @param family 148 * font family to be set 149 */ 150 public void setFamily( String family ) { 151 CssParameter fontFamily = StyleFactory.createCssParameter( "font-family", "" + family ); 152 cssParams.put( "font-family", fontFamily ); 153 } 154 155 /** 156 * Returns the (evaluated) value of the font's CssParameter 'font-style'. 157 * <p> 158 * 159 * @param feature 160 * specifies the <tt>Feature</tt> to be used for evaluation of the underlying 161 * 'sld:ParameterValueType' 162 * @return the (evaluated) value of the parameter 163 * @throws FilterEvaluationException 164 * if the evaluation fails or the specified style is not one of the following: 165 * 'normal', 'italic' and 'oblique' 166 */ 167 public int getStyle( Feature feature ) 168 throws FilterEvaluationException { 169 CssParameter cssParam = cssParams.get( "font-style" ); 170 171 if ( cssParam == null ) { 172 return STYLE_NORMAL; 173 } 174 175 String s = cssParam.getValue( feature ).trim(); 176 177 if ( s.equals( "normal" ) ) { 178 return STYLE_NORMAL; 179 } else if ( s.equals( "italic" ) ) { 180 return STYLE_ITALIC; 181 } else if ( s.equals( "oblique" ) ) { 182 return STYLE_OBLIQUE; 183 } 184 185 throw new FilterEvaluationException( "Given value ('" + s + "') for CssParameter 'font-style' is " 186 + "invalid: allowed values are 'normal', 'italic' and 'oblique'." ); 187 } 188 189 /** 190 * Sets the value of the font's CssParameter 'font-style'. 191 * <p> 192 * 193 * @param style 194 * font-style to be set 195 */ 196 public void setStyle( int style ) { 197 CssParameter fontStyle = StyleFactory.createCssParameter( "font-style", "" + style ); 198 cssParams.put( "font-style", fontStyle ); 199 } 200 201 /** 202 * Returns the (evaluated) value of the font's CssParameter 'font-weight' as a 203 * <tt>ParameterValueType</tt>. 204 * <p> 205 * 206 * @param feature 207 * specifies the <tt>Feature</tt> to be used for evaluation of the underlying 208 * 'sld:ParameterValueType' 209 * @return the (evaluated) value of the parameter 210 * @throws FilterEvaluationException 211 * if the evaluation fails or the specified weight is not one of the following: 212 * 'normal' and 'bold' 213 */ 214 public int getWeight( Feature feature ) 215 throws FilterEvaluationException { 216 CssParameter cssParam = cssParams.get( "font-weight" ); 217 218 if ( cssParam == null ) { 219 return WEIGHT_NORMAL; 220 } 221 222 String s = cssParam.getValue( feature ).trim(); 223 224 if ( s.equals( "normal" ) ) { 225 return WEIGHT_NORMAL; 226 } else if ( s.equals( "bold" ) ) { 227 return WEIGHT_BOLD; 228 } 229 230 throw new FilterEvaluationException( "Given value ('" + s + "') for CssParameter 'font-weight' is " 231 + "invalid: allowed values are 'normal' and 'bold'." ); 232 } 233 234 /** 235 * Sets the value of the font's CssParameter 'font-weight'. 236 * <p> 237 * 238 * @param weight 239 * font-weight to be set 240 */ 241 public void setWeight( int weight ) { 242 CssParameter fontWeight = StyleFactory.createCssParameter( "font-weight", "" + weight ); 243 cssParams.put( "font-weight", fontWeight ); 244 } 245 246 /** 247 * Returns the (evaluated) value of the font's CssParameter 'font-size'. 248 * <p> 249 * 250 * @param feature 251 * specifies the <tt>Feature</tt> to be used for evaluation of the underlying 252 * 'sld:ParameterValueType' 253 * @return the (evaluated) value of the parameter 254 * @throws FilterEvaluationException 255 * if the evaluation fails or the value does not denote a valid number or the number 256 * is not greater or equal zero 257 */ 258 public int getSize( Feature feature ) 259 throws FilterEvaluationException { 260 CssParameter cssParam = cssParams.get( "font-size" ); 261 int sizeInt = SIZE_DEFAULT; 262 263 if ( cssParam != null ) { 264 String s = cssParam.getValue( feature ).trim(); 265 266 try { 267 sizeInt = (int) Double.parseDouble( s ); 268 } catch ( NumberFormatException e ) { 269 throw new FilterEvaluationException( "Given value ('" + s + "') for CssParameter 'font-size' is " 270 + "not a valid number." ); 271 } 272 273 if ( sizeInt <= 0 ) { 274 throw new FilterEvaluationException( "Value of CssParameter 'font-size' must be greater or " 275 + "equal zero." ); 276 } 277 } 278 279 return sizeInt; 280 } 281 282 /** 283 * Returns the (evaluated) value of the font's CssParameter 'font-size'. 284 * <p> 285 * 286 * @param size 287 * font-size to be set 288 */ 289 public void setSize( int size ) { 290 CssParameter fontSize = StyleFactory.createCssParameter( "font-size", "" + size ); 291 cssParams.put( "font-size", fontSize ); 292 } 293 294 /** 295 * Returns the (evaluated) value of the font's CssParameter 'font-color'. 296 * <p> 297 * 298 * @param feature 299 * specifies the <tt>Feature</tt> to be used for evaluation of the underlying 300 * 'sld:ParameterValueType' 301 * @return the (evaluated) value of the parameter 302 * @throws FilterEvaluationException 303 * if the evaluation fails 304 */ 305 public Color getColor( Feature feature ) 306 throws FilterEvaluationException { 307 CssParameter cssParam = cssParams.get( "font-color" ); 308 Color awtColor = COLOR_DEFAULT; 309 310 if ( cssParam != null ) { 311 String s = cssParam.getValue( feature ).trim(); 312 313 try { 314 awtColor = Color.decode( s ); 315 } catch ( NumberFormatException e ) { 316 throw new FilterEvaluationException( "Given value ('" + s + "') for CSS-Parameter 'font-color' " 317 + "does not denote a valid color!" ); 318 } 319 } 320 321 return awtColor; 322 } 323 324 /** 325 * Sets the value of the font's CssParameter 'font-color'. 326 * <p> 327 * 328 * @param color 329 * the font-color to be set 330 */ 331 public void setColor( Color color ) { 332 String hex = ColorUtils.toHexCode( "#", color ); 333 CssParameter fontColor = StyleFactory.createCssParameter( "font-color", hex ); 334 cssParams.put( "font-color", fontColor ); 335 } 336 337 /** 338 * exports the content of the Font as XML formated String 339 * 340 * @return xml representation of the Font 341 */ 342 public String exportAsXML() { 343 344 StringBuffer sb = new StringBuffer( 1000 ); 345 sb.append( "<Font>" ); 346 Iterator<CssParameter> iterator = cssParams.values().iterator(); 347 while ( iterator.hasNext() ) { 348 sb.append( ( (Marshallable) iterator.next() ).exportAsXML() ); 349 } 350 351 sb.append( "</Font>" ); 352 353 return sb.toString(); 354 } 355 356 }