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

Last change on this file since 3992 was 3992, checked in by bastiK, 14 years ago

applied #6107 - MapCSS enhancement (patch by anonymous)

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