1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Font;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
12 | import org.openstreetmap.josm.tools.Utils;
|
---|
13 |
|
---|
14 | public class TextElement {
|
---|
15 | // textKey == null means automatic generation of text string, otherwise
|
---|
16 | // the corresponding tag value is used
|
---|
17 | public String textKey;
|
---|
18 | public Font font;
|
---|
19 | public int xOffset;
|
---|
20 | public int yOffset;
|
---|
21 | public Color color;
|
---|
22 | public Float haloRadius;
|
---|
23 | public Color haloColor;
|
---|
24 |
|
---|
25 | public TextElement(String textKey, Font font, int xOffset, int yOffset, Color color, Float haloRadius, Color haloColor) {
|
---|
26 | CheckParameterUtil.ensureParameterNotNull(font);
|
---|
27 | CheckParameterUtil.ensureParameterNotNull(color);
|
---|
28 | this.textKey = textKey;
|
---|
29 | this.font = font;
|
---|
30 | this.xOffset = xOffset;
|
---|
31 | this.yOffset = yOffset;
|
---|
32 | this.color = color;
|
---|
33 | this.haloRadius = haloRadius;
|
---|
34 | this.haloColor = haloColor;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public TextElement(TextElement other) {
|
---|
38 | this.textKey = other.textKey;
|
---|
39 | this.font = other.font;
|
---|
40 | this.xOffset = other.xOffset;
|
---|
41 | this.yOffset = other.yOffset;
|
---|
42 | this.color = other.color;
|
---|
43 | this.haloColor = other.haloColor;
|
---|
44 | this.haloRadius = other.haloRadius;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static TextElement create(Cascade c, Color defTextColor) {
|
---|
48 |
|
---|
49 | String textKey = null;
|
---|
50 | Keyword textKW = c.get("text", null, Keyword.class, true);
|
---|
51 | if (textKW == null) {
|
---|
52 | textKey = c.get("text", null, String.class);
|
---|
53 | if (textKey == null)
|
---|
54 | return null;
|
---|
55 | } else if (!textKW.val.equals("auto"))
|
---|
56 | return null;
|
---|
57 |
|
---|
58 | Font font = ElemStyle.getFont(c);
|
---|
59 |
|
---|
60 | float xOffset = 0;
|
---|
61 | float yOffset = 0;
|
---|
62 | float[] offset = c.get("text-offset", null, float[].class);
|
---|
63 | if (offset != null) {
|
---|
64 | if (offset.length == 1) {
|
---|
65 | yOffset = offset[0];
|
---|
66 | } else if (offset.length >= 2) {
|
---|
67 | xOffset = offset[0];
|
---|
68 | yOffset = offset[1];
|
---|
69 | }
|
---|
70 | }
|
---|
71 | xOffset = c.get("text-offset-x", xOffset, Float.class);
|
---|
72 | yOffset = c.get("text-offset-y", yOffset, Float.class);
|
---|
73 |
|
---|
74 | Color color = c.get("text-color", defTextColor, Color.class);
|
---|
75 | float alpha = c.get("text-opacity", 1f, Float.class);
|
---|
76 | color = new Color(color.getRed(), color.getGreen(),
|
---|
77 | color.getBlue(), Utils.color_float2int(alpha));
|
---|
78 |
|
---|
79 | Float haloRadius = c.get("text-halo-radius", null, Float.class);
|
---|
80 | if (haloRadius != null && haloRadius <= 0) {
|
---|
81 | haloRadius = null;
|
---|
82 | }
|
---|
83 | Color haloColor = null;
|
---|
84 | if (haloRadius != null) {
|
---|
85 | haloColor = c.get("text-halo-color", Utils.complement(color), Color.class);
|
---|
86 | float haloAlpha = c.get("text-halo-opacity", 1f, Float.class);
|
---|
87 | haloColor = new Color(haloColor.getRed(), haloColor.getGreen(),
|
---|
88 | haloColor.getBlue(), Utils.color_float2int(haloAlpha));
|
---|
89 | }
|
---|
90 |
|
---|
91 | return new TextElement(textKey, font, (int) xOffset, - (int) yOffset, color, haloRadius, haloColor);
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public boolean equals(Object obj) {
|
---|
96 | if (obj == null || getClass() != obj.getClass())
|
---|
97 | return false;
|
---|
98 | final TextElement other = (TextElement) obj;
|
---|
99 | return equal(textKey, other.textKey) &&
|
---|
100 | equal(font, other.font) &&
|
---|
101 | xOffset == other.xOffset &&
|
---|
102 | yOffset == other.yOffset &&
|
---|
103 | equal(color, other.color) &&
|
---|
104 | equal(haloRadius, other.haloRadius) &&
|
---|
105 | equal(haloColor, other.haloColor);
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public int hashCode() {
|
---|
110 | int hash = 3;
|
---|
111 | hash = 79 * hash + (textKey != null ? textKey.hashCode() : 0);
|
---|
112 | hash = 79 * hash + font.hashCode();
|
---|
113 | hash = 79 * hash + xOffset;
|
---|
114 | hash = 79 * hash + yOffset;
|
---|
115 | hash = 79 * hash + color.hashCode();
|
---|
116 | hash = 79 * hash + (haloRadius != null ? Float.floatToIntBits(haloRadius) : 0);
|
---|
117 | hash = 79 * hash + (haloColor != null ? haloColor.hashCode() : 0);
|
---|
118 | return hash;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public String getString(OsmPrimitive osm, MapPainter painter) {
|
---|
122 | if (textKey == null)
|
---|
123 | return painter.getAreaName(osm);
|
---|
124 | else
|
---|
125 | return osm.get(textKey);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | }
|
---|