1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.styleelement;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Font;
|
---|
6 | import java.util.Objects;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
10 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
11 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
12 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.TagKeyReference;
|
---|
13 | import org.openstreetmap.josm.gui.mappaint.StyleKeys;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.DeriveLabelFromNameTagsCompositionStrategy;
|
---|
15 | import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.StaticLabelCompositionStrategy;
|
---|
16 | import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.TagLookupCompositionStrategy;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.styleelement.PositionForAreaStrategy.CompletelyInsideAreaStrategy;
|
---|
18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
19 | import 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 | */
|
---|
25 | public 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 | * @deprecated since 11722, To be removed in mid-2017
|
---|
75 | */
|
---|
76 | @Deprecated
|
---|
77 | public TextLabel(LabelCompositionStrategy strategy, Font font, int xOffset, int yOffset, Color color, Float haloRadius, Color haloColor) {
|
---|
78 | this(strategy, font, xOffset, yOffset, color, haloRadius, haloColor, new CompletelyInsideAreaStrategy());
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Creates a new text element
|
---|
83 | *
|
---|
84 | * @param strategy the strategy indicating how the text is composed for a specific {@link OsmPrimitive} to be rendered.
|
---|
85 | * If null, no label is rendered.
|
---|
86 | * @param font the font to be used. Must not be null.
|
---|
87 | * @param xOffset x offset
|
---|
88 | * @param yOffset y offset
|
---|
89 | * @param color the color to be used. Must not be null
|
---|
90 | * @param haloRadius halo radius
|
---|
91 | * @param haloColor halo color
|
---|
92 | * @param labelPositionStrategy The position in the area.
|
---|
93 | */
|
---|
94 | protected TextLabel(LabelCompositionStrategy strategy, Font font, int xOffset, int yOffset, Color color, Float haloRadius,
|
---|
95 | Color haloColor, PositionForAreaStrategy labelPositionStrategy) {
|
---|
96 | this.labelCompositionStrategy = strategy;
|
---|
97 | this.font = Objects.requireNonNull(font, "font");
|
---|
98 | this.xOffset = xOffset;
|
---|
99 | this.yOffset = yOffset;
|
---|
100 | this.color = Objects.requireNonNull(color, "color");
|
---|
101 | this.haloRadius = haloRadius;
|
---|
102 | this.haloColor = haloColor;
|
---|
103 | this.labelPositionStrategy = Objects.requireNonNull(labelPositionStrategy, "labelPositionStrategy");
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Copy constructor
|
---|
108 | *
|
---|
109 | * @param other the other element.
|
---|
110 | */
|
---|
111 | public TextLabel(TextLabel other) {
|
---|
112 | this.labelCompositionStrategy = other.labelCompositionStrategy;
|
---|
113 | this.font = other.font;
|
---|
114 | this.xOffset = other.xOffset;
|
---|
115 | this.yOffset = other.yOffset;
|
---|
116 | this.color = other.color;
|
---|
117 | this.haloColor = other.haloColor;
|
---|
118 | this.haloRadius = other.haloRadius;
|
---|
119 | this.labelPositionStrategy = other.labelPositionStrategy;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Copy constructor that changes the position strategy.
|
---|
124 | *
|
---|
125 | * @param other the other element.
|
---|
126 | * @param labelPositionStrategy the position
|
---|
127 | */
|
---|
128 | private TextLabel(TextLabel other, PositionForAreaStrategy labelPositionStrategy) {
|
---|
129 | this.labelCompositionStrategy = other.labelCompositionStrategy;
|
---|
130 | this.font = other.font;
|
---|
131 | this.xOffset = other.xOffset;
|
---|
132 | this.yOffset = other.yOffset;
|
---|
133 | this.color = other.color;
|
---|
134 | this.haloColor = other.haloColor;
|
---|
135 | this.haloRadius = other.haloRadius;
|
---|
136 | this.labelPositionStrategy = labelPositionStrategy;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Derives a suitable label composition strategy from the style properties in {@code c}.
|
---|
141 | *
|
---|
142 | * @param c the style properties
|
---|
143 | * @param defaultAnnotate whether to return {@link #AUTO_LABEL_COMPOSITION_STRATEGY} if not strategy is found
|
---|
144 | * @return the label composition strategy, or {@code null}
|
---|
145 | */
|
---|
146 | protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade c, boolean defaultAnnotate) {
|
---|
147 | /*
|
---|
148 | * If the cascade includes a TagKeyReference we will lookup the rendered label
|
---|
149 | * from a tag value.
|
---|
150 | */
|
---|
151 | TagKeyReference tkr = c.get(TEXT, null, TagKeyReference.class, true);
|
---|
152 | if (tkr != null)
|
---|
153 | return new TagLookupCompositionStrategy(tkr.key);
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Check whether the label composition strategy is given by a keyword
|
---|
157 | */
|
---|
158 | Keyword keyword = c.get(TEXT, null, Keyword.class, true);
|
---|
159 | if (Keyword.AUTO.equals(keyword))
|
---|
160 | return AUTO_LABEL_COMPOSITION_STRATEGY;
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Do we have a static text label?
|
---|
164 | */
|
---|
165 | String text = c.get(TEXT, null, String.class, true);
|
---|
166 | if (text != null)
|
---|
167 | return new StaticLabelCompositionStrategy(text);
|
---|
168 | return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Builds a text element from style properties in {@code c} and the
|
---|
173 | * default text color {@code defaultTextColor}
|
---|
174 | *
|
---|
175 | * @param env the environment
|
---|
176 | * @param defaultTextColor the default text color. Must not be null.
|
---|
177 | * @param defaultAnnotate true, if a text label shall be rendered by default, even if the style sheet
|
---|
178 | * doesn't include respective style declarations
|
---|
179 | * @return the text element or null, if the style properties don't include
|
---|
180 | * properties for text rendering
|
---|
181 | * @throws IllegalArgumentException if {@code defaultTextColor} is null
|
---|
182 | */
|
---|
183 | public static TextLabel create(Environment env, Color defaultTextColor, boolean defaultAnnotate) {
|
---|
184 | CheckParameterUtil.ensureParameterNotNull(defaultTextColor);
|
---|
185 | Cascade c = env.mc.getCascade(env.layer);
|
---|
186 |
|
---|
187 | LabelCompositionStrategy strategy = buildLabelCompositionStrategy(c, defaultAnnotate);
|
---|
188 | if (strategy == null) return null;
|
---|
189 | String s = strategy.compose(env.osm);
|
---|
190 | if (s == null) return null;
|
---|
191 | Font font = StyleElement.getFont(c, s);
|
---|
192 |
|
---|
193 | float xOffset = 0;
|
---|
194 | float yOffset = 0;
|
---|
195 | float[] offset = c.get(TEXT_OFFSET, null, float[].class);
|
---|
196 | if (offset != null) {
|
---|
197 | if (offset.length == 1) {
|
---|
198 | yOffset = offset[0];
|
---|
199 | } else if (offset.length >= 2) {
|
---|
200 | xOffset = offset[0];
|
---|
201 | yOffset = offset[1];
|
---|
202 | }
|
---|
203 | }
|
---|
204 | xOffset = c.get(TEXT_OFFSET_X, xOffset, Float.class);
|
---|
205 | yOffset = c.get(TEXT_OFFSET_Y, yOffset, Float.class);
|
---|
206 |
|
---|
207 | Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class);
|
---|
208 | float alpha = c.get(TEXT_OPACITY, 1f, Float.class);
|
---|
209 | color = Utils.alphaMultiply(color, alpha);
|
---|
210 |
|
---|
211 | Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class);
|
---|
212 | if (haloRadius != null && haloRadius <= 0) {
|
---|
213 | haloRadius = null;
|
---|
214 | }
|
---|
215 | Color haloColor = null;
|
---|
216 | if (haloRadius != null) {
|
---|
217 | haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class);
|
---|
218 | float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class);
|
---|
219 | haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor);
|
---|
220 | }
|
---|
221 |
|
---|
222 | Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class);
|
---|
223 | PositionForAreaStrategy position = PositionForAreaStrategy.forKeyword(positionKeyword);
|
---|
224 |
|
---|
225 | return new TextLabel(strategy, font, (int) xOffset, -(int) yOffset, color, haloRadius, haloColor, position);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Replies the label to be rendered for the primitive {@code osm}.
|
---|
230 | *
|
---|
231 | * @param osm the OSM object
|
---|
232 | * @return the label, or null, if {@code osm} is null or if no label can be
|
---|
233 | * derived for {@code osm}
|
---|
234 | */
|
---|
235 | public String getString(OsmPrimitive osm) {
|
---|
236 | if (labelCompositionStrategy == null) return null;
|
---|
237 | return labelCompositionStrategy.compose(osm);
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Gets the strategy that defines where to place the label.
|
---|
242 | * @return The strategy. Never null.
|
---|
243 | * @since 11722
|
---|
244 | */
|
---|
245 | public PositionForAreaStrategy getLabelPositionStrategy() {
|
---|
246 | return labelPositionStrategy;
|
---|
247 | }
|
---|
248 |
|
---|
249 | public TextLabel withPosition(PositionForAreaStrategy labelPositionStrategy) {
|
---|
250 | return new TextLabel(this, labelPositionStrategy);
|
---|
251 | }
|
---|
252 |
|
---|
253 | @Override
|
---|
254 | public String toString() {
|
---|
255 | return "TextElement{" + toStringImpl() + '}';
|
---|
256 | }
|
---|
257 |
|
---|
258 | protected String toStringImpl() {
|
---|
259 | StringBuilder sb = new StringBuilder(96);
|
---|
260 | sb.append("labelCompositionStrategy=").append(labelCompositionStrategy)
|
---|
261 | .append(" font=").append(font);
|
---|
262 | if (xOffset != 0) {
|
---|
263 | sb.append(" xOffset=").append(xOffset);
|
---|
264 | }
|
---|
265 | if (yOffset != 0) {
|
---|
266 | sb.append(" yOffset=").append(yOffset);
|
---|
267 | }
|
---|
268 | sb.append(" color=").append(Utils.toString(color));
|
---|
269 | if (haloRadius != null) {
|
---|
270 | sb.append(" haloRadius=").append(haloRadius)
|
---|
271 | .append(" haloColor=").append(haloColor);
|
---|
272 | }
|
---|
273 | return sb.toString();
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Override
|
---|
277 | public int hashCode() {
|
---|
278 | return Objects.hash(labelCompositionStrategy, font, xOffset, yOffset, color, haloRadius, haloColor);
|
---|
279 | }
|
---|
280 |
|
---|
281 | @Override
|
---|
282 | public boolean equals(Object obj) {
|
---|
283 | if (this == obj) return true;
|
---|
284 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
285 | TextLabel textLabel = (TextLabel) obj;
|
---|
286 | return xOffset == textLabel.xOffset &&
|
---|
287 | yOffset == textLabel.yOffset &&
|
---|
288 | Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) &&
|
---|
289 | Objects.equals(font, textLabel.font) &&
|
---|
290 | Objects.equals(color, textLabel.color) &&
|
---|
291 | Objects.equals(haloRadius, textLabel.haloRadius) &&
|
---|
292 | Objects.equals(haloColor, textLabel.haloColor);
|
---|
293 | }
|
---|
294 | }
|
---|