001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 04:32:12 -0800 (Thu, 27 Dec 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
122: .get("font-family");
123:
124: if (cssParam == null) {
125: return null;
126: }
127:
128: return cssParam.getValue(feature).trim();
129: }
130:
131: /**
132: * Sets the value of the font's CssParameter 'font-family'.
133: * <p>
134: *
135: * @param family
136: * font family to be set
137: */
138: public void setFamily(String family) {
139: CssParameter fontFamily = StyleFactory.createCssParameter(
140: "font-family", "" + family);
141: cssParams.put("font-family", fontFamily);
142: }
143:
144: /**
145: * Returns the (evaluated) value of the font's CssParameter 'font-style'.
146: * <p>
147: *
148: * @param feature
149: * specifies the <tt>Feature</tt> to be used for evaluation of the underlying
150: * 'sld:ParameterValueType'
151: * @return the (evaluated) value of the parameter
152: * @throws FilterEvaluationException
153: * if the evaluation fails or the specified style is not one of the following:
154: * 'normal', 'italic' and 'oblique'
155: */
156: public int getStyle(Feature feature)
157: throws FilterEvaluationException {
158: CssParameter cssParam = (CssParameter) cssParams
159: .get("font-style");
160:
161: if (cssParam == null) {
162: return STYLE_NORMAL;
163: }
164:
165: String s = cssParam.getValue(feature).trim();
166:
167: if (s.equals("normal")) {
168: return STYLE_NORMAL;
169: } else if (s.equals("italic")) {
170: return STYLE_ITALIC;
171: } else if (s.equals("oblique")) {
172: return STYLE_OBLIQUE;
173: }
174:
175: throw new FilterEvaluationException(
176: "Given value ('"
177: + s
178: + "') for CssParameter 'font-style' is "
179: + "invalid: allowed values are 'normal', 'italic' and 'oblique'.");
180: }
181:
182: /**
183: * Sets the value of the font's CssParameter 'font-style'.
184: * <p>
185: *
186: * @param style
187: * font-style to be set
188: */
189: public void setStyle(int style) {
190: CssParameter fontStyle = StyleFactory.createCssParameter(
191: "font-style", "" + style);
192: cssParams.put("font-style", fontStyle);
193: }
194:
195: /**
196: * Returns the (evaluated) value of the font's CssParameter 'font-weight' as a
197: * <tt>ParameterValueType</tt>.
198: * <p>
199: *
200: * @param feature
201: * specifies the <tt>Feature</tt> to be used for evaluation of the underlying
202: * 'sld:ParameterValueType'
203: * @return the (evaluated) value of the parameter
204: * @throws FilterEvaluationException
205: * if the evaluation fails or the specified weight is not one of the following:
206: * 'normal' and 'bold'
207: */
208: public int getWeight(Feature feature)
209: throws FilterEvaluationException {
210: CssParameter cssParam = (CssParameter) cssParams
211: .get("font-weight");
212:
213: if (cssParam == null) {
214: return WEIGHT_NORMAL;
215: }
216:
217: String s = cssParam.getValue(feature).trim();
218:
219: if (s.equals("normal")) {
220: return WEIGHT_NORMAL;
221: } else if (s.equals("bold")) {
222: return WEIGHT_BOLD;
223: }
224:
225: throw new FilterEvaluationException("Given value ('" + s
226: + "') for CssParameter 'font-weight' is "
227: + "invalid: allowed values are 'normal' and 'bold'.");
228: }
229:
230: /**
231: * Sets the value of the font's CssParameter 'font-weight'.
232: * <p>
233: *
234: * @param weight
235: * font-weight to be set
236: */
237: public void setWeight(int weight) {
238: CssParameter fontWeight = StyleFactory.createCssParameter(
239: "font-weight", "" + weight);
240: cssParams.put("font-weight", fontWeight);
241: }
242:
243: /**
244: * Returns the (evaluated) value of the font's CssParameter 'font-size'.
245: * <p>
246: *
247: * @param feature
248: * specifies the <tt>Feature</tt> to be used for evaluation of the underlying
249: * 'sld:ParameterValueType'
250: * @return the (evaluated) value of the parameter
251: * @throws FilterEvaluationException
252: * if the evaluation fails or the value does not denote a valid number or the number
253: * is not greater or equal zero
254: */
255: public int getSize(Feature feature)
256: throws FilterEvaluationException {
257: CssParameter cssParam = (CssParameter) cssParams
258: .get("font-size");
259: int sizeInt = SIZE_DEFAULT;
260:
261: if (cssParam != null) {
262: String s = cssParam.getValue(feature).trim();
263:
264: try {
265: sizeInt = (int) Double.parseDouble(s);
266: } catch (NumberFormatException e) {
267: throw new FilterEvaluationException("Given value ('"
268: + s + "') for CssParameter 'font-size' is "
269: + "not a valid number.");
270: }
271:
272: if (sizeInt <= 0) {
273: throw new FilterEvaluationException(
274: "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(
291: "font-size", "" + size);
292: cssParams.put("font-size", fontSize);
293: }
294:
295: /**
296: * Returns the (evaluated) value of the font's CssParameter 'font-color'.
297: * <p>
298: *
299: * @param feature
300: * specifies the <tt>Feature</tt> to be used for evaluation of the underlying
301: * 'sld:ParameterValueType'
302: * @return the (evaluated) value of the parameter
303: * @throws FilterEvaluationException
304: * if the evaluation fails
305: */
306: public Color getColor(Feature feature)
307: throws FilterEvaluationException {
308: CssParameter cssParam = (CssParameter) cssParams
309: .get("font-color");
310: Color awtColor = COLOR_DEFAULT;
311:
312: if (cssParam != null) {
313: String s = cssParam.getValue(feature).trim();
314:
315: try {
316: awtColor = Color.decode(s);
317: } catch (NumberFormatException e) {
318: throw new FilterEvaluationException("Given value ('"
319: + s + "') for CSS-Parameter 'font-color' "
320: + "does not denote a valid color!");
321: }
322: }
323:
324: return awtColor;
325: }
326:
327: /**
328: * Sets the value of the font's CssParameter 'font-color'.
329: * <p>
330: *
331: * @param color
332: * the font-color to be set
333: */
334: public void setColor(Color color) {
335: String hex = ColorUtils.toHexCode("#", color);
336: CssParameter fontColor = StyleFactory.createCssParameter(
337: "font-color", hex);
338: cssParams.put("font-color", fontColor);
339: }
340:
341: /**
342: * exports the content of the Font as XML formated String
343: *
344: * @return xml representation of the Font
345: */
346: public String exportAsXML() {
347:
348: StringBuffer sb = new StringBuffer(1000);
349: sb.append("<Font>");
350: Iterator iterator = cssParams.values().iterator();
351: while (iterator.hasNext()) {
352: sb.append(((Marshallable) iterator.next()).exportAsXML());
353: }
354:
355: sb.append("</Font>");
356:
357: return sb.toString();
358: }
359:
360: }
|