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