Ignore:
Timestamp:
2017-07-13T23:02:13+02:00 (7 years ago)
Author:
michael2402
Message:

Fix #15006: Separate offset handling for ways, areas and node. Handle offset for all three of them.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java

    r12381 r12476  
    44import java.awt.Color;
    55import java.awt.Font;
     6import java.awt.geom.Point2D;
    67import java.util.Objects;
    78
     
    1516import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.StaticLabelCompositionStrategy;
    1617import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.TagLookupCompositionStrategy;
    17 import org.openstreetmap.josm.gui.mappaint.styleelement.placement.PositionForAreaStrategy;
    1818import org.openstreetmap.josm.tools.CheckParameterUtil;
    1919import org.openstreetmap.josm.tools.Utils;
     
    3939    public Font font;
    4040    /**
    41      * The x offset of the text.
    42      */
    43     public int xOffset;
    44     /**
    45      * The y offset of the text.
    46      */
    47     public int yOffset;
    48     /**
    4941     * The color to draw the text in, includes alpha.
    5042     */
     
    5850     */
    5951    public Color haloColor;
    60 
    61     /**
    62      * The position strategy for this text label.
    63      */
    64     private final PositionForAreaStrategy labelPositionStrategy;
    6552
    6653    /**
     
    7057     * If null, no label is rendered.
    7158     * @param font the font to be used. Must not be null.
    72      * @param xOffset x offset
    73      * @param yOffset y offset
    7459     * @param color the color to be used. Must not be null
    7560     * @param haloRadius halo radius
    7661     * @param haloColor halo color
    77      * @param labelPositionStrategy The position in the area.
    78      */
    79     protected TextLabel(LabelCompositionStrategy strategy, Font font, int xOffset, int yOffset, Color color, Float haloRadius,
    80             Color haloColor, PositionForAreaStrategy labelPositionStrategy) {
     62     */
     63    protected TextLabel(LabelCompositionStrategy strategy, Font font, Color color, Float haloRadius,
     64            Color haloColor) {
    8165        this.labelCompositionStrategy = strategy;
    8266        this.font = Objects.requireNonNull(font, "font");
    83         this.xOffset = xOffset;
    84         this.yOffset = yOffset;
    8567        this.color = Objects.requireNonNull(color, "color");
    8668        this.haloRadius = haloRadius;
    8769        this.haloColor = haloColor;
    88         this.labelPositionStrategy = Objects.requireNonNull(labelPositionStrategy, "labelPositionStrategy");
    8970    }
    9071
     
    9778        this.labelCompositionStrategy = other.labelCompositionStrategy;
    9879        this.font = other.font;
    99         this.xOffset = other.xOffset;
    100         this.yOffset = other.yOffset;
    10180        this.color = other.color;
    10281        this.haloColor = other.haloColor;
    10382        this.haloRadius = other.haloRadius;
    104         this.labelPositionStrategy = other.labelPositionStrategy;
    105     }
    106 
    107     /**
    108      * Copy constructor that changes the position strategy.
    109      *
    110      * @param other the other element.
    111      * @param labelPositionStrategy the position
    112      */
    113     private TextLabel(TextLabel other, PositionForAreaStrategy labelPositionStrategy) {
    114         this.labelCompositionStrategy = other.labelCompositionStrategy;
    115         this.font = other.font;
    116         this.xOffset = other.xOffset;
    117         this.yOffset = other.yOffset;
    118         this.color = other.color;
    119         this.haloColor = other.haloColor;
    120         this.haloRadius = other.haloRadius;
    121         this.labelPositionStrategy = labelPositionStrategy;
    12283    }
    12384
     
    176137        Font font = StyleElement.getFont(c, s);
    177138
     139        Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class);
     140        float alpha = c.get(TEXT_OPACITY, 1f, Float.class);
     141        color = Utils.alphaMultiply(color, alpha);
     142
     143        Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class);
     144        if (haloRadius != null && haloRadius <= 0) {
     145            haloRadius = null;
     146        }
     147        Color haloColor = null;
     148        if (haloRadius != null) {
     149            haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class);
     150            float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class);
     151            haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor);
     152        }
     153
     154        return new TextLabel(strategy, font, color, haloRadius, haloColor);
     155    }
     156
     157    /**
     158     * Gets the text-offset property from a cascade
     159     * @param c The cascade
     160     * @return The text offset property
     161     */
     162    public static Point2D getTextOffset(Cascade c) {
    178163        float xOffset = 0;
    179164        float yOffset = 0;
     
    189174        xOffset = c.get(TEXT_OFFSET_X, xOffset, Float.class);
    190175        yOffset = c.get(TEXT_OFFSET_Y, yOffset, Float.class);
    191 
    192         Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class);
    193         float alpha = c.get(TEXT_OPACITY, 1f, Float.class);
    194         color = Utils.alphaMultiply(color, alpha);
    195 
    196         Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class);
    197         if (haloRadius != null && haloRadius <= 0) {
    198             haloRadius = null;
    199         }
    200         Color haloColor = null;
    201         if (haloRadius != null) {
    202             haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class);
    203             float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class);
    204             haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor);
    205         }
    206 
    207         Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class);
    208         PositionForAreaStrategy position = PositionForAreaStrategy.forKeyword(positionKeyword);
    209 
    210         return new TextLabel(strategy, font, (int) xOffset, -(int) yOffset, color, haloRadius, haloColor, position);
     176        return new Point2D.Double(xOffset, yOffset);
    211177    }
    212178
     
    223189    }
    224190
    225     /**
    226      * Gets the strategy that defines where to place the label.
    227      * @return The strategy. Never null.
    228      * @since 11722
    229      */
    230     public PositionForAreaStrategy getLabelPositionStrategy() {
    231         return labelPositionStrategy;
    232     }
    233 
    234     /**
    235      * Creates a new text label with a different position strategy
    236      * @param labelPositionStrategy The new position strategy to use
    237      * @return The new label
    238      */
    239     public TextLabel withPosition(PositionForAreaStrategy labelPositionStrategy) {
    240         return new TextLabel(this, labelPositionStrategy);
    241     }
    242 
    243191    @Override
    244192    public String toString() {
     
    249197        StringBuilder sb = new StringBuilder(96);
    250198        sb.append("labelCompositionStrategy=").append(labelCompositionStrategy)
    251           .append(" font=").append(font);
    252         if (xOffset != 0) {
    253             sb.append(" xOffset=").append(xOffset);
    254         }
    255         if (yOffset != 0) {
    256             sb.append(" yOffset=").append(yOffset);
    257         }
    258         sb.append(" color=").append(Utils.toString(color));
     199          .append(" font=").append(font)
     200          .append(" color=").append(Utils.toString(color));
    259201        if (haloRadius != null) {
    260202            sb.append(" haloRadius=").append(haloRadius)
     
    266208    @Override
    267209    public int hashCode() {
    268         return Objects.hash(labelCompositionStrategy, font, xOffset, yOffset, color, haloRadius, haloColor);
     210        return Objects.hash(labelCompositionStrategy, font, color, haloRadius, haloColor);
    269211    }
    270212
     
    274216        if (obj == null || getClass() != obj.getClass()) return false;
    275217        TextLabel textLabel = (TextLabel) obj;
    276         return xOffset == textLabel.xOffset &&
    277                 yOffset == textLabel.yOffset &&
    278                 Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) &&
     218        return Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) &&
    279219                Objects.equals(font, textLabel.font) &&
    280220                Objects.equals(color, textLabel.color) &&
Note: See TracChangeset for help on using the changeset viewer.