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.tools.CheckParameterUtil;
|
---|
18 | import org.openstreetmap.josm.tools.Utils;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Represents the rendering style for a textual label placed somewhere on the map.
|
---|
22 | * @since 3880
|
---|
23 | */
|
---|
24 | public class TextLabel implements StyleKeys {
|
---|
25 | public static final LabelCompositionStrategy AUTO_LABEL_COMPOSITION_STRATEGY = new DeriveLabelFromNameTagsCompositionStrategy();
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * The strategy for building the actual label value for a given a {@link OsmPrimitive}.
|
---|
29 | * Check for null before accessing.
|
---|
30 | */
|
---|
31 | public LabelCompositionStrategy labelCompositionStrategy;
|
---|
32 | /**
|
---|
33 | * the font to be used when rendering
|
---|
34 | */
|
---|
35 | public Font font;
|
---|
36 | /**
|
---|
37 | * The x offset of the text.
|
---|
38 | */
|
---|
39 | public int xOffset;
|
---|
40 | /**
|
---|
41 | * The y offset of the text.
|
---|
42 | */
|
---|
43 | public int yOffset;
|
---|
44 | /**
|
---|
45 | * The color to draw the text in, includes alpha.
|
---|
46 | */
|
---|
47 | public Color color;
|
---|
48 | /**
|
---|
49 | * The radius of the halo effect.
|
---|
50 | */
|
---|
51 | public Float haloRadius;
|
---|
52 | /**
|
---|
53 | * The color of the halo effect.
|
---|
54 | */
|
---|
55 | public Color haloColor;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Creates a new text element
|
---|
59 | *
|
---|
60 | * @param strategy the strategy indicating how the text is composed for a specific {@link OsmPrimitive} to be rendered.
|
---|
61 | * If null, no label is rendered.
|
---|
62 | * @param font the font to be used. Must not be null.
|
---|
63 | * @param xOffset x offset
|
---|
64 | * @param yOffset y offset
|
---|
65 | * @param color the color to be used. Must not be null
|
---|
66 | * @param haloRadius halo radius
|
---|
67 | * @param haloColor halo color
|
---|
68 | */
|
---|
69 | public TextLabel(LabelCompositionStrategy strategy, Font font, int xOffset, int yOffset, Color color, Float haloRadius, Color haloColor) {
|
---|
70 | CheckParameterUtil.ensureParameterNotNull(font);
|
---|
71 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
72 | labelCompositionStrategy = strategy;
|
---|
73 | this.font = font;
|
---|
74 | this.xOffset = xOffset;
|
---|
75 | this.yOffset = yOffset;
|
---|
76 | this.color = color;
|
---|
77 | this.haloRadius = haloRadius;
|
---|
78 | this.haloColor = haloColor;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Copy constructor
|
---|
83 | *
|
---|
84 | * @param other the other element.
|
---|
85 | */
|
---|
86 | public TextLabel(TextLabel other) {
|
---|
87 | this.labelCompositionStrategy = other.labelCompositionStrategy;
|
---|
88 | this.font = other.font;
|
---|
89 | this.xOffset = other.xOffset;
|
---|
90 | this.yOffset = other.yOffset;
|
---|
91 | this.color = other.color;
|
---|
92 | this.haloColor = other.haloColor;
|
---|
93 | this.haloRadius = other.haloRadius;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Derives a suitable label composition strategy from the style properties in {@code c}.
|
---|
98 | *
|
---|
99 | * @param c the style properties
|
---|
100 | * @param defaultAnnotate whether to return {@link #AUTO_LABEL_COMPOSITION_STRATEGY} if not strategy is found
|
---|
101 | * @return the label composition strategy, or {@code null}
|
---|
102 | */
|
---|
103 | protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade c, boolean defaultAnnotate) {
|
---|
104 | /*
|
---|
105 | * If the cascade includes a TagKeyReference we will lookup the rendered label
|
---|
106 | * from a tag value.
|
---|
107 | */
|
---|
108 | TagKeyReference tkr = c.get(TEXT, null, TagKeyReference.class, true);
|
---|
109 | if (tkr != null)
|
---|
110 | return new TagLookupCompositionStrategy(tkr.key);
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Check whether the label composition strategy is given by a keyword
|
---|
114 | */
|
---|
115 | Keyword keyword = c.get(TEXT, null, Keyword.class, true);
|
---|
116 | if (Keyword.AUTO.equals(keyword))
|
---|
117 | return AUTO_LABEL_COMPOSITION_STRATEGY;
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Do we have a static text label?
|
---|
121 | */
|
---|
122 | String text = c.get(TEXT, null, String.class, true);
|
---|
123 | if (text != null)
|
---|
124 | return new StaticLabelCompositionStrategy(text);
|
---|
125 | return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Builds a text element from style properties in {@code c} and the
|
---|
130 | * default text color {@code defaultTextColor}
|
---|
131 | *
|
---|
132 | * @param env the environment
|
---|
133 | * @param defaultTextColor the default text color. Must not be null.
|
---|
134 | * @param defaultAnnotate true, if a text label shall be rendered by default, even if the style sheet
|
---|
135 | * doesn't include respective style declarations
|
---|
136 | * @return the text element or null, if the style properties don't include
|
---|
137 | * properties for text rendering
|
---|
138 | * @throws IllegalArgumentException if {@code defaultTextColor} is null
|
---|
139 | */
|
---|
140 | public static TextLabel create(Environment env, Color defaultTextColor, boolean defaultAnnotate) {
|
---|
141 | CheckParameterUtil.ensureParameterNotNull(defaultTextColor);
|
---|
142 | Cascade c = env.mc.getCascade(env.layer);
|
---|
143 |
|
---|
144 | LabelCompositionStrategy strategy = buildLabelCompositionStrategy(c, defaultAnnotate);
|
---|
145 | if (strategy == null) return null;
|
---|
146 | String s = strategy.compose(env.osm);
|
---|
147 | if (s == null) return null;
|
---|
148 | Font font = StyleElement.getFont(c, s);
|
---|
149 |
|
---|
150 | float xOffset = 0;
|
---|
151 | float yOffset = 0;
|
---|
152 | float[] offset = c.get(TEXT_OFFSET, null, float[].class);
|
---|
153 | if (offset != null) {
|
---|
154 | if (offset.length == 1) {
|
---|
155 | yOffset = offset[0];
|
---|
156 | } else if (offset.length >= 2) {
|
---|
157 | xOffset = offset[0];
|
---|
158 | yOffset = offset[1];
|
---|
159 | }
|
---|
160 | }
|
---|
161 | xOffset = c.get(TEXT_OFFSET_X, xOffset, Float.class);
|
---|
162 | yOffset = c.get(TEXT_OFFSET_Y, yOffset, Float.class);
|
---|
163 |
|
---|
164 | Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class);
|
---|
165 | float alpha = c.get(TEXT_OPACITY, 1f, Float.class);
|
---|
166 | color = Utils.alphaMultiply(color, alpha);
|
---|
167 |
|
---|
168 | Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class);
|
---|
169 | if (haloRadius != null && haloRadius <= 0) {
|
---|
170 | haloRadius = null;
|
---|
171 | }
|
---|
172 | Color haloColor = null;
|
---|
173 | if (haloRadius != null) {
|
---|
174 | haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class);
|
---|
175 | float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class);
|
---|
176 | haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor);
|
---|
177 | }
|
---|
178 |
|
---|
179 | return new TextLabel(strategy, font, (int) xOffset, -(int) yOffset, color, haloRadius, haloColor);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Replies the label to be rendered for the primitive {@code osm}.
|
---|
184 | *
|
---|
185 | * @param osm the OSM object
|
---|
186 | * @return the label, or null, if {@code osm} is null or if no label can be
|
---|
187 | * derived for {@code osm}
|
---|
188 | */
|
---|
189 | public String getString(OsmPrimitive osm) {
|
---|
190 | if (labelCompositionStrategy == null) return null;
|
---|
191 | return labelCompositionStrategy.compose(osm);
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Override
|
---|
195 | public String toString() {
|
---|
196 | return "TextElement{" + toStringImpl() + '}';
|
---|
197 | }
|
---|
198 |
|
---|
199 | protected String toStringImpl() {
|
---|
200 | StringBuilder sb = new StringBuilder(96);
|
---|
201 | sb.append("labelCompositionStrategy=").append(labelCompositionStrategy)
|
---|
202 | .append(" font=").append(font);
|
---|
203 | if (xOffset != 0) {
|
---|
204 | sb.append(" xOffset=").append(xOffset);
|
---|
205 | }
|
---|
206 | if (yOffset != 0) {
|
---|
207 | sb.append(" yOffset=").append(yOffset);
|
---|
208 | }
|
---|
209 | sb.append(" color=").append(Utils.toString(color));
|
---|
210 | if (haloRadius != null) {
|
---|
211 | sb.append(" haloRadius=").append(haloRadius)
|
---|
212 | .append(" haloColor=").append(haloColor);
|
---|
213 | }
|
---|
214 | return sb.toString();
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Override
|
---|
218 | public int hashCode() {
|
---|
219 | return Objects.hash(labelCompositionStrategy, font, xOffset, yOffset, color, haloRadius, haloColor);
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Override
|
---|
223 | public boolean equals(Object obj) {
|
---|
224 | if (this == obj) return true;
|
---|
225 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
226 | TextLabel textLabel = (TextLabel) obj;
|
---|
227 | return xOffset == textLabel.xOffset &&
|
---|
228 | yOffset == textLabel.yOffset &&
|
---|
229 | Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) &&
|
---|
230 | Objects.equals(font, textLabel.font) &&
|
---|
231 | Objects.equals(color, textLabel.color) &&
|
---|
232 | Objects.equals(haloRadius, textLabel.haloRadius) &&
|
---|
233 | Objects.equals(haloColor, textLabel.haloColor);
|
---|
234 | }
|
---|
235 | }
|
---|