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