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