001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/graphics/sld/TextSymbolizer.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 org.deegree.framework.xml.Marshallable;
039 import org.deegree.model.filterencoding.Expression;
040
041 /**
042 * Used to render a text label, according to the parameters. A missing Geometry, Label, Font, or
043 * LabelPlacement element selects the default value or behavior for the element. The default Label,
044 * Font, and LabelPlacement are system- dependent. Multiple Font elements may be used to specify
045 * alternate fonts in order of preference in case a map server does not support the first
046 * preference. A missing Halo or Fill element means that no halo or fill will be plotted,
047 * respectively. The Fill is rendered over top of the Halo, and the Halo includes the interiors of
048 * the font glyphs.
049 * <p>
050 *
051 * @author <a href="mailto:k.lupp@web.de">Katharina Lupp</a>
052 * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
053 * @version $Revision: 18405 $ $Date: 2009-07-09 10:31:42 +0200 (Do, 09. Jul 2009) $
054 */
055
056 public class TextSymbolizer extends AbstractSymbolizer implements Marshallable {
057
058 private Fill fill = null;
059
060 private Font font = null;
061
062 private Halo halo = null;
063
064 private LabelPlacement labelPlacement = null;
065
066 private ParameterValueType label = null;
067
068 private ParameterValueType[] bbox;
069
070 /**
071 * constructor initializing the class with the <code>TextSymbolizer</code>
072 * @param geometry
073 * @param label
074 * @param font
075 * @param labelPlacement
076 * @param halo
077 * @param fill
078 * @param min
079 * @param max
080 */
081 TextSymbolizer( Geometry geometry, ParameterValueType label, Font font, LabelPlacement labelPlacement, Halo halo,
082 Fill fill, double min, double max ) {
083 super( geometry, "org.deegree.graphics.displayelements.LabelDisplayElement" );
084 setLabel( label );
085 setFont( font );
086 setLabelPlacement( labelPlacement );
087 setHalo( halo );
088 setFill( fill );
089 setMinScaleDenominator( min );
090 setMaxScaleDenominator( max );
091 }
092
093 /**
094 * constructor initializing the class with the <code>TextSymbolizer</code>
095 * @param geometry
096 * @param responsibleClass
097 * @param label
098 * @param font
099 * @param labelPlacement
100 * @param halo
101 * @param fill
102 * @param min
103 * @param max
104 */
105 TextSymbolizer( Geometry geometry, String responsibleClass, ParameterValueType label, Font font,
106 LabelPlacement labelPlacement, Halo halo, Fill fill, double min, double max ) {
107 super( geometry, responsibleClass );
108 setLabel( label );
109 setFont( font );
110 setLabelPlacement( labelPlacement );
111 setHalo( halo );
112 setFill( fill );
113 setMinScaleDenominator( min );
114 setMaxScaleDenominator( max );
115 }
116
117 /**
118 * Initializing with bounding box.
119 *
120 * @param geometry
121 * @param text
122 * @param font
123 * @param halo
124 * @param minx
125 * @param miny
126 * @param maxx
127 * @param maxy
128 */
129 public TextSymbolizer( Geometry geometry, ParameterValueType text, Font font, Halo halo, ParameterValueType minx,
130 ParameterValueType miny, ParameterValueType maxx, ParameterValueType maxy ) {
131 super( geometry, "org.deegree.graphics.displayelements.LabelDisplayElement" );
132 setLabel( text );
133 setFont( font );
134 setHalo( halo );
135 bbox = new ParameterValueType[] { minx, miny, maxx, maxy };
136 setLabelPlacement( new LabelPlacement( (LinePlacement) null ) );
137 }
138
139 /**
140 * @return the bounding box, or null, if not set (minx, miny, maxx, maxy)
141 */
142 public ParameterValueType[] getBoundingBox() {
143 return bbox;
144 }
145
146 /**
147 * returns the Label as a <tt>ParameterValueType</tt> to be renderd
148 *
149 * @return the label
150 *
151 */
152 public ParameterValueType getLabel() {
153 return label;
154 }
155
156 /**
157 * returns the label as a String to be renderd
158 *
159 * @return the label or null, if the label is not set or not an expression
160 *
161 */
162 public String getLabelAsString() {
163 Object[] o = label.getComponents();
164 for ( int i = 0; i < o.length; i++ ) {
165 if ( o[i] instanceof Expression ) {
166 return o[i].toString().trim();
167 }
168 }
169 return null;
170 }
171
172 /**
173 * sets the <Label>
174 *
175 * @param label
176 * the label
177 *
178 */
179 public void setLabel( ParameterValueType label ) {
180 this.label = label;
181 }
182
183 /**
184 * Identifies a Font of a certain family, style, and size.
185 *
186 * @return the font
187 *
188 */
189 public Font getFont() {
190 return font;
191 }
192
193 /**
194 * Sets a Font of a certain family, style, and size.
195 *
196 * @param font
197 * the font
198 *
199 */
200 public void setFont( Font font ) {
201 this.font = font;
202 }
203
204 /**
205 * Used to position a label relative to a point or a line string. For a point, you can specify
206 * the anchor point of the label and a linear displacement from the point (so that you can also
207 * plot a graphic symbol at the point). For a line-string placement, you can specify a
208 * perpendicular offset (so you can draw a stroke on the line).
209 * <p>
210 * </p>
211 * MORE PARAMETERS ARE PROBABLY NEEDED HERE.
212 *
213 * @return the labelPlacement
214 *
215 */
216 public LabelPlacement getLabelPlacement() {
217 return labelPlacement;
218 }
219
220 /**
221 * sets the <LabelPlacement>
222 *
223 * @param labelPlacement
224 * the labelPlacement
225 *
226 */
227 public void setLabelPlacement( LabelPlacement labelPlacement ) {
228 this.labelPlacement = labelPlacement;
229 }
230
231 /**
232 * A Halo is an extension (sub-type) of a Fill and is applied to the backgrounds of font glyphs.
233 * Either a Radius or a Block halo type can be used. The radius is computed from the outside
234 * edge of the font glyph (or inside of "holes"). The default is a Radius of 1.0 (pixels) but if
235 * no Halo is selected in a containing structure, no halo will be rendered. The default is a
236 * solid white (Color "#FFFFFF") opaque halo.
237 *
238 * @return the halo
239 *
240 */
241 public Halo getHalo() {
242 return halo;
243 }
244
245 /**
246 * sets <Halo>
247 *
248 * @param halo
249 * the halo
250 *
251 */
252 public void setHalo( Halo halo ) {
253 this.halo = halo;
254 }
255
256 /**
257 * A Fill allows area geometries to be filled. There are two types of fills: solid-color and
258 * repeated GraphicFill. In general, if a Fill element is omitted in its containing element, no
259 * fill will be rendered. The default is a solid 50%-gray (color "#808080") opaque fill.
260 *
261 * @return the fill
262 *
263 */
264 public Fill getFill() {
265 return fill;
266 }
267
268 /**
269 * sets the <Fill>
270 *
271 * @param fill
272 * the fill
273 *
274 */
275 public void setFill( Fill fill ) {
276 this.fill = fill;
277 }
278
279 /**
280 * exports the content of the TextSymbolizer as XML formated String
281 *
282 * @return xml representation of the TextSymbolizer
283 */
284 public String exportAsXML() {
285
286 StringBuffer sb = new StringBuffer( 1000 );
287 sb.append( "<TextSymbolizer>" );
288 if ( geometry != null ) {
289 sb.append( ( (Marshallable) geometry ).exportAsXML() );
290 }
291 if ( label != null ) {
292 sb.append( "<Label>" );
293 sb.append( ( (Marshallable) label ).exportAsXML() );
294 sb.append( "</Label>" );
295 }
296 if ( font != null ) {
297 sb.append( ( (Marshallable) font ).exportAsXML() );
298 }
299 if ( labelPlacement != null ) {
300 sb.append( ( (Marshallable) labelPlacement ).exportAsXML() );
301 }
302 if ( halo != null ) {
303 sb.append( ( (Marshallable) halo ).exportAsXML() );
304 }
305 if ( fill != null ) {
306 sb.append( ( (Marshallable) fill ).exportAsXML() );
307 }
308 sb.append( "</TextSymbolizer>" );
309
310 return sb.toString();
311 }
312 }