source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/TextElement.java@ 4191

Last change on this file since 4191 was 4191, checked in by stoecker, 13 years ago

remove old debug stuff

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import static org.openstreetmap.josm.tools.Utils.equal;
5
6import java.awt.Color;
7import java.awt.Font;
8
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.DeriveLabelFromNameTagsCompositionStrategy;
11import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.StaticLabelCompositionStrategy;
12import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.TagLookupCompositionStrategy;
13import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.TagKeyReference;
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15import org.openstreetmap.josm.tools.Utils;
16
17/**
18 * Represents the rendering style for a textual label placed somewhere on the map.
19 *
20 */
21public class TextElement {
22 static private final LabelCompositionStrategy AUTO_LABEL_COMPOSITION_STRATEGY = new DeriveLabelFromNameTagsCompositionStrategy();
23
24 /** the strategy for building the actual label value for a given a {@link OsmPrimitive}.
25 * Check for null before accessing.
26 */
27 public LabelCompositionStrategy labelCompositionStrategy;
28 /** the font to be used when rendering*/
29 public Font font;
30 public int xOffset;
31 public int yOffset;
32 public Color color;
33 public Float haloRadius;
34 public Color haloColor;
35
36 /**
37 * Creates a new text element
38 *
39 * @param strategy the strategy indicating how the text is composed for a specific {@link OsmPrimitive} to be rendered.
40 * If null, no label is rendered.
41 * @param font the font to be used. Must not be null.
42 * @param xOffset
43 * @param yOffset
44 * @param color the color to be used. Must not be null
45 * @param haloRadius
46 * @param haloColor
47 */
48 public TextElement(LabelCompositionStrategy strategy, Font font, int xOffset, int yOffset, Color color, Float haloRadius, Color haloColor) {
49 CheckParameterUtil.ensureParameterNotNull(font);
50 CheckParameterUtil.ensureParameterNotNull(color);
51 labelCompositionStrategy = strategy;
52 this.font = font;
53 this.xOffset = xOffset;
54 this.yOffset = yOffset;
55 this.color = color;
56 this.haloRadius = haloRadius;
57 this.haloColor = haloColor;
58 }
59
60 /**
61 * Copy constructor
62 *
63 * @param other the other element.
64 */
65 public TextElement(TextElement other) {
66 this.labelCompositionStrategy = other.labelCompositionStrategy;
67 this.font = other.font;
68 this.xOffset = other.xOffset;
69 this.yOffset = other.yOffset;
70 this.color = other.color;
71 this.haloColor = other.haloColor;
72 this.haloRadius = other.haloRadius;
73 }
74
75 /**
76 * Derives a suitable label composition strategy from the style properties in
77 * {@code c}.
78 *
79 * @param c the style properties
80 * @return the label composition strategy
81 */
82 protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade c, boolean defaultAnnotate){
83 /*
84 * If the cascade includes a TagKeyReference we will lookup the rendered label
85 * from a tag value.
86 */
87 TagKeyReference tkr = c.get("text", null, TagKeyReference.class, true);
88 if (tkr != null)
89 return new TagLookupCompositionStrategy(tkr.key);
90
91 /*
92 * Check whether the label composition strategy is given by
93 * a keyword
94 */
95 Keyword keyword = c.get("text", null, Keyword.class, true);
96 if (equal(keyword, Keyword.AUTO))
97 return AUTO_LABEL_COMPOSITION_STRATEGY;
98
99 /*
100 * Do we have a static text label?
101 */
102 String text = c.get("text", null, String.class, true);
103 if (text != null)
104 return new StaticLabelCompositionStrategy(text);
105 return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null;
106 }
107
108 /**
109 * Builds a text element from style properties in {@code c} and the
110 * default text color {@code defaultTextColor}
111 *
112 * @param c the style properties
113 * @param defaultTextColor the default text color. Must not be null.
114 * @param defaultAnnotate true, if a text label shall be rendered by default, even if the style sheet
115 * doesn't include respective style declarations
116 * @return the text element or null, if the style properties don't include
117 * properties for text rendering
118 * @throws IllegalArgumentException thrown if {@code defaultTextColor} is null
119 */
120 public static TextElement create(Cascade c, Color defaultTextColor, boolean defaultAnnotate) throws IllegalArgumentException{
121 CheckParameterUtil.ensureParameterNotNull(defaultTextColor);
122
123 LabelCompositionStrategy strategy = buildLabelCompositionStrategy(c, defaultAnnotate);
124 Font font = ElemStyle.getFont(c);
125
126 float xOffset = 0;
127 float yOffset = 0;
128 float[] offset = c.get("text-offset", null, float[].class);
129 if (offset != null) {
130 if (offset.length == 1) {
131 yOffset = offset[0];
132 } else if (offset.length >= 2) {
133 xOffset = offset[0];
134 yOffset = offset[1];
135 }
136 }
137 xOffset = c.get("text-offset-x", xOffset, Float.class);
138 yOffset = c.get("text-offset-y", yOffset, Float.class);
139
140 Color color = c.get("text-color", defaultTextColor, Color.class);
141 float alpha = c.get("text-opacity", 1f, Float.class);
142 color = new Color(color.getRed(), color.getGreen(),
143 color.getBlue(), Utils.color_float2int(alpha));
144
145 Float haloRadius = c.get("text-halo-radius", null, Float.class);
146 if (haloRadius != null && haloRadius <= 0) {
147 haloRadius = null;
148 }
149 Color haloColor = null;
150 if (haloRadius != null) {
151 haloColor = c.get("text-halo-color", Utils.complement(color), Color.class);
152 float haloAlpha = c.get("text-halo-opacity", 1f, Float.class);
153 haloColor = new Color(haloColor.getRed(), haloColor.getGreen(),
154 haloColor.getBlue(), Utils.color_float2int(haloAlpha));
155 }
156
157 return new TextElement(strategy, font, (int) xOffset, - (int) yOffset, color, haloRadius, haloColor);
158 }
159
160 /**
161 * Replies the label to be rendered for the primitive {@code osm}.
162 *
163 * @param osm the the OSM object
164 * @return the label, or null, if {@code osm} is null or if no label can be
165 * derived for {@code osm}
166 */
167 public String getString(OsmPrimitive osm) {
168 if (labelCompositionStrategy == null) return null;
169 return labelCompositionStrategy.compose(osm);
170 }
171
172 @Override
173 public String toString() {
174 return "TextElement{" + toStringImpl() + '}';
175 }
176
177 protected String toStringImpl() {
178 StringBuilder sb = new StringBuilder();
179 sb.append("labelCompositionStrategy=" + labelCompositionStrategy);
180 sb.append(" font=" + font);
181 if (xOffset != 0) {
182 sb.append(" xOffset=" + xOffset);
183 }
184 if (yOffset != 0) {
185 sb.append(" yOffset=" + yOffset);
186 }
187 sb.append(" color=" + Utils.toString(color));
188 if (haloRadius != null) {
189 sb.append(" haloRadius=" + haloRadius);
190 sb.append(" haloColor=" + haloColor);
191 }
192 return sb.toString();
193 }
194
195 @Override
196 public int hashCode() {
197 int hash = 3;
198 hash = 79 * hash + (labelCompositionStrategy != null ? labelCompositionStrategy.hashCode() : 0);
199 hash = 79 * hash + font.hashCode();
200 hash = 79 * hash + xOffset;
201 hash = 79 * hash + yOffset;
202 hash = 79 * hash + color.hashCode();
203 hash = 79 * hash + (haloRadius != null ? Float.floatToIntBits(haloRadius) : 0);
204 hash = 79 * hash + (haloColor != null ? haloColor.hashCode() : 0);
205 return hash;
206 }
207
208 @Override
209 public boolean equals(Object obj) {
210 if (obj == null || getClass() != obj.getClass())
211 return false;
212 final TextElement other = (TextElement) obj;
213 return equal(labelCompositionStrategy, other.labelCompositionStrategy) &&
214 equal(font, other.font) &&
215 xOffset == other.xOffset &&
216 yOffset == other.yOffset &&
217 equal(color, other.color) &&
218 equal(haloRadius, other.haloRadius) &&
219 equal(haloColor, other.haloColor);
220 }
221}
Note: See TracBrowser for help on using the repository browser.