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

Last change on this file since 3987 was 3987, checked in by bastiK, 13 years ago

mapcss: improvements in text label selection, performance and typo (patch by anonymous, see #6107)

  • Property svn:eol-style set to native
File size: 7.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.TagLookupCompositionStrategy;
12import org.openstreetmap.josm.tools.CheckParameterUtil;
13import org.openstreetmap.josm.tools.Utils;
14
15/**
16 * Represents the rendering style for a textual label placed somewhere on the map.
17 *
18 */
19public class TextElement {
20 //static private final Logger logger = Logger.getLogger(TextElement.class.getName());
21
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){
83 Keyword textKW = c.get("text", null, Keyword.class, true);
84 if (textKW == null) {
85 String textKey = c.get("text", null, String.class);
86 if (textKey == null) return null;
87 return new TagLookupCompositionStrategy(textKey);
88 } else if (textKW.val.equals("auto"))
89 return AUTO_LABEL_COMPOSITION_STRATEGY;
90 else
91 return new TagLookupCompositionStrategy(textKW.val);
92 }
93
94 /**
95 * Builds a text element from style properties in {@code c} and the
96 * default text color {@code defaultTextColor}
97 *
98 * @param c the style properties
99 * @param defaultTextColor the default text color. Must not be null.
100 * @return the text element or null, if the style properties don't include
101 * properties for text rendering
102 * @throws IllegalArgumentException thrown if {@code defaultTextColor} is null
103 */
104 public static TextElement create(Cascade c, Color defaultTextColor) throws IllegalArgumentException{
105 CheckParameterUtil.ensureParameterNotNull(defaultTextColor);
106
107 LabelCompositionStrategy strategy = buildLabelCompositionStrategy(c);
108 Font font = ElemStyle.getFont(c);
109
110 float xOffset = 0;
111 float yOffset = 0;
112 float[] offset = c.get("text-offset", null, float[].class);
113 if (offset != null) {
114 if (offset.length == 1) {
115 yOffset = offset[0];
116 } else if (offset.length >= 2) {
117 xOffset = offset[0];
118 yOffset = offset[1];
119 }
120 }
121 xOffset = c.get("text-offset-x", xOffset, Float.class);
122 yOffset = c.get("text-offset-y", yOffset, Float.class);
123
124 Color color = c.get("text-color", defaultTextColor, Color.class);
125 float alpha = c.get("text-opacity", 1f, Float.class);
126 color = new Color(color.getRed(), color.getGreen(),
127 color.getBlue(), Utils.color_float2int(alpha));
128
129 Float haloRadius = c.get("text-halo-radius", null, Float.class);
130 if (haloRadius != null && haloRadius <= 0) {
131 haloRadius = null;
132 }
133 Color haloColor = null;
134 if (haloRadius != null) {
135 haloColor = c.get("text-halo-color", Utils.complement(color), Color.class);
136 float haloAlpha = c.get("text-halo-opacity", 1f, Float.class);
137 haloColor = new Color(haloColor.getRed(), haloColor.getGreen(),
138 haloColor.getBlue(), Utils.color_float2int(haloAlpha));
139 }
140
141 return new TextElement(strategy, font, (int) xOffset, - (int) yOffset, color, haloRadius, haloColor);
142 }
143
144 /**
145 * Replies the label to be rendered for the primitive {@code osm}.
146 *
147 * @param osm the the OSM object
148 * @return the label, or null, if {@code osm} is null or if no label can be
149 * derived for {@code osm}
150 */
151 public String getString(OsmPrimitive osm) {
152 if (labelCompositionStrategy == null) return null;
153 return labelCompositionStrategy.compose(osm);
154 }
155
156 @Override
157 public String toString() {
158 StringBuilder sb = new StringBuilder();
159 sb.append("{TextElement ");
160 sb.append("strategy=");
161 sb.append(labelCompositionStrategy == null ? "null" : labelCompositionStrategy.toString());
162 sb.append("}");
163 return sb.toString();
164 }
165
166 @Override
167 public int hashCode() {
168 int hash = 3;
169 hash = 79 * hash + (labelCompositionStrategy != null ? labelCompositionStrategy.hashCode() : 0);
170 hash = 79 * hash + font.hashCode();
171 hash = 79 * hash + xOffset;
172 hash = 79 * hash + yOffset;
173 hash = 79 * hash + color.hashCode();
174 hash = 79 * hash + (haloRadius != null ? Float.floatToIntBits(haloRadius) : 0);
175 hash = 79 * hash + (haloColor != null ? haloColor.hashCode() : 0);
176 return hash;
177 }
178
179 @Override
180 public boolean equals(Object obj) {
181 if (obj == null || getClass() != obj.getClass())
182 return false;
183 final TextElement other = (TextElement) obj;
184 return equal(labelCompositionStrategy, other.labelCompositionStrategy) &&
185 equal(font, other.font) &&
186 xOffset == other.xOffset &&
187 yOffset == other.yOffset &&
188 equal(color, other.color) &&
189 equal(haloRadius, other.haloRadius) &&
190 equal(haloColor, other.haloColor);
191 }
192}
Note: See TracBrowser for help on using the repository browser.