source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java@ 12285

Last change on this file since 12285 was 12285, checked in by Don-vip, 7 years ago

remove some deprecated code, unused by now

  • Property svn:eol-style set to native
File size: 10.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.styleelement;
3
4import java.awt.Color;
5import java.awt.Font;
6import java.util.Objects;
7
8import org.openstreetmap.josm.data.osm.OsmPrimitive;
9import org.openstreetmap.josm.gui.mappaint.Cascade;
10import org.openstreetmap.josm.gui.mappaint.Environment;
11import org.openstreetmap.josm.gui.mappaint.Keyword;
12import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.TagKeyReference;
13import org.openstreetmap.josm.gui.mappaint.StyleKeys;
14import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.DeriveLabelFromNameTagsCompositionStrategy;
15import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.StaticLabelCompositionStrategy;
16import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.TagLookupCompositionStrategy;
17import org.openstreetmap.josm.gui.mappaint.styleelement.placement.PositionForAreaStrategy;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
19import org.openstreetmap.josm.tools.Utils;
20
21/**
22 * Represents the rendering style for a textual label placed somewhere on the map.
23 * @since 3880
24 */
25public class TextLabel implements StyleKeys {
26 public static final LabelCompositionStrategy AUTO_LABEL_COMPOSITION_STRATEGY = new DeriveLabelFromNameTagsCompositionStrategy();
27
28 /**
29 * The strategy for building the actual label value for a given a {@link OsmPrimitive}.
30 * Check for null before accessing.
31 */
32 public LabelCompositionStrategy labelCompositionStrategy;
33 /**
34 * the font to be used when rendering
35 */
36 public Font font;
37 /**
38 * The x offset of the text.
39 */
40 public int xOffset;
41 /**
42 * The y offset of the text.
43 */
44 public int yOffset;
45 /**
46 * The color to draw the text in, includes alpha.
47 */
48 public Color color;
49 /**
50 * The radius of the halo effect.
51 */
52 public Float haloRadius;
53 /**
54 * The color of the halo effect.
55 */
56 public Color haloColor;
57
58 /**
59 * The position strategy for this text label.
60 */
61 private final PositionForAreaStrategy labelPositionStrategy;
62
63 /**
64 * Creates a new text element
65 *
66 * @param strategy the strategy indicating how the text is composed for a specific {@link OsmPrimitive} to be rendered.
67 * If null, no label is rendered.
68 * @param font the font to be used. Must not be null.
69 * @param xOffset x offset
70 * @param yOffset y offset
71 * @param color the color to be used. Must not be null
72 * @param haloRadius halo radius
73 * @param haloColor halo color
74 * @param labelPositionStrategy The position in the area.
75 */
76 protected TextLabel(LabelCompositionStrategy strategy, Font font, int xOffset, int yOffset, Color color, Float haloRadius,
77 Color haloColor, PositionForAreaStrategy labelPositionStrategy) {
78 this.labelCompositionStrategy = strategy;
79 this.font = Objects.requireNonNull(font, "font");
80 this.xOffset = xOffset;
81 this.yOffset = yOffset;
82 this.color = Objects.requireNonNull(color, "color");
83 this.haloRadius = haloRadius;
84 this.haloColor = haloColor;
85 this.labelPositionStrategy = Objects.requireNonNull(labelPositionStrategy, "labelPositionStrategy");
86 }
87
88 /**
89 * Copy constructor
90 *
91 * @param other the other element.
92 */
93 public TextLabel(TextLabel other) {
94 this.labelCompositionStrategy = other.labelCompositionStrategy;
95 this.font = other.font;
96 this.xOffset = other.xOffset;
97 this.yOffset = other.yOffset;
98 this.color = other.color;
99 this.haloColor = other.haloColor;
100 this.haloRadius = other.haloRadius;
101 this.labelPositionStrategy = other.labelPositionStrategy;
102 }
103
104 /**
105 * Copy constructor that changes the position strategy.
106 *
107 * @param other the other element.
108 * @param labelPositionStrategy the position
109 */
110 private TextLabel(TextLabel other, PositionForAreaStrategy labelPositionStrategy) {
111 this.labelCompositionStrategy = other.labelCompositionStrategy;
112 this.font = other.font;
113 this.xOffset = other.xOffset;
114 this.yOffset = other.yOffset;
115 this.color = other.color;
116 this.haloColor = other.haloColor;
117 this.haloRadius = other.haloRadius;
118 this.labelPositionStrategy = labelPositionStrategy;
119 }
120
121 /**
122 * Derives a suitable label composition strategy from the style properties in {@code c}.
123 *
124 * @param c the style properties
125 * @param defaultAnnotate whether to return {@link #AUTO_LABEL_COMPOSITION_STRATEGY} if not strategy is found
126 * @return the label composition strategy, or {@code null}
127 */
128 protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade c, boolean defaultAnnotate) {
129 /*
130 * If the cascade includes a TagKeyReference we will lookup the rendered label
131 * from a tag value.
132 */
133 TagKeyReference tkr = c.get(TEXT, null, TagKeyReference.class, true);
134 if (tkr != null)
135 return new TagLookupCompositionStrategy(tkr.key);
136
137 /*
138 * Check whether the label composition strategy is given by a keyword
139 */
140 Keyword keyword = c.get(TEXT, null, Keyword.class, true);
141 if (Keyword.AUTO.equals(keyword))
142 return AUTO_LABEL_COMPOSITION_STRATEGY;
143
144 /*
145 * Do we have a static text label?
146 */
147 String text = c.get(TEXT, null, String.class, true);
148 if (text != null)
149 return new StaticLabelCompositionStrategy(text);
150 return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null;
151 }
152
153 /**
154 * Builds a text element from style properties in {@code c} and the
155 * default text color {@code defaultTextColor}
156 *
157 * @param env the environment
158 * @param defaultTextColor the default text color. Must not be null.
159 * @param defaultAnnotate true, if a text label shall be rendered by default, even if the style sheet
160 * doesn't include respective style declarations
161 * @return the text element or null, if the style properties don't include
162 * properties for text rendering
163 * @throws IllegalArgumentException if {@code defaultTextColor} is null
164 */
165 public static TextLabel create(Environment env, Color defaultTextColor, boolean defaultAnnotate) {
166 CheckParameterUtil.ensureParameterNotNull(defaultTextColor);
167 Cascade c = env.mc.getCascade(env.layer);
168
169 LabelCompositionStrategy strategy = buildLabelCompositionStrategy(c, defaultAnnotate);
170 if (strategy == null) return null;
171 String s = strategy.compose(env.osm);
172 if (s == null) return null;
173 Font font = StyleElement.getFont(c, s);
174
175 float xOffset = 0;
176 float yOffset = 0;
177 float[] offset = c.get(TEXT_OFFSET, null, float[].class);
178 if (offset != null) {
179 if (offset.length == 1) {
180 yOffset = offset[0];
181 } else if (offset.length >= 2) {
182 xOffset = offset[0];
183 yOffset = offset[1];
184 }
185 }
186 xOffset = c.get(TEXT_OFFSET_X, xOffset, Float.class);
187 yOffset = c.get(TEXT_OFFSET_Y, yOffset, Float.class);
188
189 Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class);
190 float alpha = c.get(TEXT_OPACITY, 1f, Float.class);
191 color = Utils.alphaMultiply(color, alpha);
192
193 Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class);
194 if (haloRadius != null && haloRadius <= 0) {
195 haloRadius = null;
196 }
197 Color haloColor = null;
198 if (haloRadius != null) {
199 haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class);
200 float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class);
201 haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor);
202 }
203
204 Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class);
205 PositionForAreaStrategy position = PositionForAreaStrategy.forKeyword(positionKeyword);
206
207 return new TextLabel(strategy, font, (int) xOffset, -(int) yOffset, color, haloRadius, haloColor, position);
208 }
209
210 /**
211 * Replies the label to be rendered for the primitive {@code osm}.
212 *
213 * @param osm the OSM object
214 * @return the label, or null, if {@code osm} is null or if no label can be
215 * derived for {@code osm}
216 */
217 public String getString(OsmPrimitive osm) {
218 if (labelCompositionStrategy == null) return null;
219 return labelCompositionStrategy.compose(osm);
220 }
221
222 /**
223 * Gets the strategy that defines where to place the label.
224 * @return The strategy. Never null.
225 * @since 11722
226 */
227 public PositionForAreaStrategy getLabelPositionStrategy() {
228 return labelPositionStrategy;
229 }
230
231 public TextLabel withPosition(PositionForAreaStrategy labelPositionStrategy) {
232 return new TextLabel(this, labelPositionStrategy);
233 }
234
235 @Override
236 public String toString() {
237 return "TextLabel{" + toStringImpl() + '}';
238 }
239
240 protected String toStringImpl() {
241 StringBuilder sb = new StringBuilder(96);
242 sb.append("labelCompositionStrategy=").append(labelCompositionStrategy)
243 .append(" font=").append(font);
244 if (xOffset != 0) {
245 sb.append(" xOffset=").append(xOffset);
246 }
247 if (yOffset != 0) {
248 sb.append(" yOffset=").append(yOffset);
249 }
250 sb.append(" color=").append(Utils.toString(color));
251 if (haloRadius != null) {
252 sb.append(" haloRadius=").append(haloRadius)
253 .append(" haloColor=").append(haloColor);
254 }
255 return sb.toString();
256 }
257
258 @Override
259 public int hashCode() {
260 return Objects.hash(labelCompositionStrategy, font, xOffset, yOffset, color, haloRadius, haloColor);
261 }
262
263 @Override
264 public boolean equals(Object obj) {
265 if (this == obj) return true;
266 if (obj == null || getClass() != obj.getClass()) return false;
267 TextLabel textLabel = (TextLabel) obj;
268 return xOffset == textLabel.xOffset &&
269 yOffset == textLabel.yOffset &&
270 Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) &&
271 Objects.equals(font, textLabel.font) &&
272 Objects.equals(color, textLabel.color) &&
273 Objects.equals(haloRadius, textLabel.haloRadius) &&
274 Objects.equals(haloColor, textLabel.haloColor);
275 }
276}
Note: See TracBrowser for help on using the repository browser.