Ignore:
Timestamp:
2011-02-08T14:29:23+01:00 (13 years ago)
Author:
bastiK
Message:

mapcss: font attributes & text alignment

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
3 edited

Legend:

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

    r3865 r3871  
    99
    1010import org.openstreetmap.josm.gui.mappaint.mapcss.CSSColors;
     11import org.openstreetmap.josm.tools.Utils;
    1112
    1213/**
     
    183184                res.append(Arrays.toString((float[]) val));
    184185            } else if (val instanceof Color) {
    185                 res.append(String.format("#%x", ((Color) val).getRGB()));
     186                res.append(Utils.toString((Color)val));
    186187            } else {
    187188                res.append(val+"");
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r3865 r3871  
    164164
    165165        float miterlimit = c.get(prefix + "miterlimit", 10f, Float.class);
     166        if (miterlimit < 1f) {
     167            miterlimit = 10f;
     168        }
    166169
    167170        BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r3865 r3871  
    66import java.awt.BasicStroke;
    77import java.awt.Color;
     8import java.awt.Font;
    89import java.awt.Stroke;
    910
     
    1718import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
    1819import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
     20import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    1921import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
     22import org.openstreetmap.josm.tools.CheckParameterUtil;
    2023import org.openstreetmap.josm.tools.Utils;
    2124
     
    2427 */
    2528public class NodeElemStyle extends ElemStyle {
    26     public boolean annotate;
    27     public String annotation_key;
     29
    2830    public ImageIcon icon;
    2931    public int iconAlpha;
    3032    public Symbol symbol;
     33    public TextElement text;
    3134
    3235    private ImageIcon disabledIcon;
     36
     37    public enum SymbolShape { SQUARE, CIRCLE }
     38    public enum HorizontalTextAlignment { LEFT, CENTER, RIGHT }
     39    public enum VerticalTextAlignment { ABOVE, TOP, CENTER, BOTTOM, BELOW }
    3340
    3441    public static class Symbol {
    3542        public SymbolShape symbol;
    36         public float size;
     43        public int size;
    3744        public Stroke stroke;
    3845        public Color strokeColor;
    3946        public Color fillColor;
    4047
    41         public Symbol(SymbolShape symbol, float size, Stroke stroke, Color strokeColor, Color fillColor) {
     48        public Symbol(SymbolShape symbol, int size, Stroke stroke, Color strokeColor, Color fillColor) {
    4249            if (stroke != null && strokeColor == null)
    4350                throw new IllegalArgumentException();
     
    6774            int hash = 7;
    6875            hash = 67 * hash + symbol.hashCode();
    69             hash = 67 * hash + Float.floatToIntBits(size);
     76            hash = 67 * hash + size;
    7077            hash = 67 * hash + (stroke != null ? stroke.hashCode() : 0);
    7178            hash = 67 * hash + (strokeColor != null ? strokeColor.hashCode() : 0);
     
    8188        }
    8289    }
     90
     91    public static class TextElement {
     92        public String textKey;
     93        public HorizontalTextAlignment hAlign;
     94        public VerticalTextAlignment vAlign;
     95        public Font font;
     96        public int xOffset;
     97        public int yOffset;
     98        public Color color;
     99
     100        public TextElement(String textKey, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign, Font font, int xOffset, int yOffset, Color color) {
     101            CheckParameterUtil.ensureParameterNotNull(hAlign);
     102            CheckParameterUtil.ensureParameterNotNull(vAlign);
     103            CheckParameterUtil.ensureParameterNotNull(font);
     104            CheckParameterUtil.ensureParameterNotNull(color);
     105            this.textKey = textKey;
     106            this.hAlign = hAlign;
     107            this.vAlign = vAlign;
     108            this.font = font;
     109            this.xOffset = xOffset;
     110            this.yOffset = yOffset;
     111            this.color = color;
     112        }
     113
     114        @Override
     115        public boolean equals(Object obj) {
     116            if (obj == null || getClass() != obj.getClass())
     117                return false;
     118            final TextElement other = (TextElement) obj;
     119            return  equal(textKey, other.textKey) &&
     120                    hAlign == other.hAlign &&
     121                    vAlign == other.vAlign &&
     122                    equal(font, other.font) &&
     123                    xOffset == other.xOffset &&
     124                    yOffset == other.yOffset &&
     125                    equal(color, other.color);
     126        }
     127
     128        @Override
     129        public int hashCode() {
     130            int hash = 3;
     131            hash = 79 * hash + (textKey != null ? textKey.hashCode() : 0);
     132            hash = 79 * hash + hAlign.hashCode();
     133            hash = 79 * hash + vAlign.hashCode();
     134            hash = 79 * hash + font.hashCode();
     135            hash = 79 * hash + xOffset;
     136            hash = 79 * hash + yOffset;
     137            hash = 79 * hash + color.hashCode();
     138            return hash;
     139        }
     140    }
    83141   
    84     public enum SymbolShape { SQUARE, CIRCLE }
    85 
    86     public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE = new NodeElemStyle(Cascade.EMPTY_CASCADE, true, null, null, 0, null);
    87 
    88     protected NodeElemStyle(Cascade c, boolean annotate, String annotation_key, ImageIcon icon, int iconAlpha, Symbol symbol) {
     142    public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE;
     143    static {
     144        Cascade c = new Cascade(false);
     145        c.put("text", "auto");
     146        SIMPLE_NODE_ELEMSTYLE = create(c, true);
     147    }
     148
     149    protected NodeElemStyle(Cascade c, ImageIcon icon, int iconAlpha, Symbol symbol, TextElement text) {
    89150        super(c);
    90         this.annotate = annotate;
    91         this.annotation_key = annotation_key;
    92151        this.icon = icon;
    93152        this.iconAlpha = iconAlpha;
    94153        this.symbol = symbol;
     154        this.text = text;
    95155    }
    96156
    97157    public static NodeElemStyle create(Cascade c) {
     158        return create(c, false);
     159    }
     160
     161    private static NodeElemStyle create(Cascade c, boolean allowOnlyText) {
    98162        IconReference iconRef = c.get("icon-image", null, IconReference.class);
    99163        ImageIcon icon = null;
     
    109173            }
    110174        } else {
    111             SymbolShape shape;
    112             String shapeStr = c.get("symbol-shape", null, String.class);
    113             if (equal(shapeStr, "square")) {
    114                 shape = SymbolShape.SQUARE;
    115             } else if (equal(shapeStr, "circle")) {
    116                 shape = SymbolShape.CIRCLE;
    117             } else
    118                 return null;
    119 
    120             Float size = c.get("symbol-size", null, Float.class);
    121             if (size == null || size <= 0)
    122                 return null;
    123 
    124             Float strokeWidth = c.get("symbol-stroke-width", null, Float.class);
    125             Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
    126             if (strokeColor != null) {
    127                 float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class);
    128                 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
    129                         strokeColor.getBlue(), Utils.color_float2int(strokeAlpha));
    130             }
    131             Stroke stroke = null;
    132             if (strokeWidth != null && strokeWidth > 0 && strokeColor != null) {
    133                 stroke = new BasicStroke(strokeWidth);
    134             }
    135 
    136             Color fillColor = c.get("symbol-fill-color", null, Color.class);
    137             if (fillColor != null) {
    138                 float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class);
    139                 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
    140                         fillColor.getBlue(), Utils.color_float2int(fillAlpha));
    141             }
    142 
    143             if ((stroke == null || strokeColor == null) && fillColor == null)
    144                 return null;
    145 
    146             symbol = new Symbol(shape, size, stroke, strokeColor, fillColor);
    147         }
    148 
    149         String text = c.get("text", null, String.class);
    150 
    151         boolean annotate = text != null;
    152         String annotation_key = null;
    153 
    154         if (annotate && !"auto".equalsIgnoreCase(text)) {
    155             annotation_key = text;
    156         }
    157         return new NodeElemStyle(c, annotate, annotation_key, icon, iconAlpha, symbol);
     175            symbol = createSymbol(c);
     176        }
     177
     178        if (icon == null && symbol == null && !allowOnlyText)
     179            return null;
     180
     181        TextElement text = null;
     182        String textStr = c.get("text", null, String.class);
     183        if (textStr != null) {
     184            String textKey = null;
     185            if (!"auto".equalsIgnoreCase(textStr)) {
     186                textKey = textStr;
     187            }
     188            HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT;
     189            String hAlignStr = c.get("text-anchor-horizontal", null, String.class);
     190            if (equal(hAlignStr, "left")) {
     191                hAlign = HorizontalTextAlignment.LEFT;
     192            } else if (equal(hAlignStr, "center")) {
     193                hAlign = HorizontalTextAlignment.CENTER;
     194            } else if (equal(hAlignStr, "right")) {
     195                hAlign = HorizontalTextAlignment.RIGHT;
     196            }
     197            VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM;
     198            String vAlignStr = c.get("text-anchor-vertical", null, String.class);
     199            if (equal(vAlignStr, "above")) {
     200                vAlign = VerticalTextAlignment.ABOVE;
     201            } else if (equal(vAlignStr, "top")) {
     202                vAlign = VerticalTextAlignment.TOP;
     203            } else if (equal(vAlignStr, "center")) {
     204                vAlign = VerticalTextAlignment.CENTER;
     205            } else if (equal(vAlignStr, "bottom")) {
     206                vAlign = VerticalTextAlignment.BOTTOM;
     207            } else if (equal(vAlignStr, "below")) {
     208                vAlign = VerticalTextAlignment.BELOW;
     209            }
     210            String name = c.get("font-family", Main.pref.get("mappaint.font", "Helvetica"), String.class);
     211            float size = c.get("font-size", (float) Main.pref.getInteger("mappaint.fontsize", 8), Float.class);
     212            int weight = Font.PLAIN;
     213            String weightStr = c.get("font-wheight", null, String.class);
     214            if (equal(weightStr, "bold")) {
     215                weight = Font.BOLD;
     216            }
     217            int style = Font.PLAIN;
     218            String styleStr = c.get("font-style", null, String.class);
     219            if (equal(styleStr, "italic")) {
     220                style = Font.ITALIC;
     221            }
     222            Font font = new Font(name, weight | style, Math.round(size));
     223            int xOffset = c.get("text-offset-x", 0f, Float.class).intValue();
     224            int yOffset = c.get("text-offset-y", 0f, Float.class).intValue();
     225            Color color = c.get("text-color", PaintColors.TEXT.get(), Color.class);
     226            text = new TextElement(textKey, hAlign, vAlign, font, xOffset, yOffset, color);
     227        }
     228       
     229        return new NodeElemStyle(c, icon, iconAlpha, symbol, text);
     230    }
     231
     232    private static Symbol createSymbol(Cascade c) {
     233        SymbolShape shape;
     234        String shapeStr = c.get("symbol-shape", null, String.class);
     235        if (equal(shapeStr, "square")) {
     236            shape = SymbolShape.SQUARE;
     237        } else if (equal(shapeStr, "circle")) {
     238            shape = SymbolShape.CIRCLE;
     239        } else
     240            return null;
     241
     242        Float size = c.get("symbol-size", null, Float.class);
     243        if (size == null || size <= 0)
     244            return null;
     245
     246        Float strokeWidth = c.get("symbol-stroke-width", null, Float.class);
     247        Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
     248        if (strokeColor != null) {
     249            float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class);
     250            strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
     251                    strokeColor.getBlue(), Utils.color_float2int(strokeAlpha));
     252        }
     253        Stroke stroke = null;
     254        if (strokeWidth != null && strokeWidth > 0 && strokeColor != null) {
     255            stroke = new BasicStroke(strokeWidth);
     256        }
     257
     258        Color fillColor = c.get("symbol-fill-color", null, Color.class);
     259        if (fillColor != null) {
     260            float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class);
     261            fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
     262                    fillColor.getBlue(), Utils.color_float2int(fillAlpha));
     263        }
     264
     265        if ((stroke == null || strokeColor == null) && fillColor == null)
     266            return null;
     267
     268        return new Symbol(shape, size.intValue(), stroke, strokeColor, fillColor);
    158269    }
    159270
     
    164275            if (icon != null && painter.isShowIcons()) {
    165276                painter.drawNodeIcon(n, (painter.isInactive() || n.isDisabled()) ? getDisabledIcon() : icon,
    166                         Utils.color_int2float(iconAlpha), selected, member, getName(n, painter));
     277                        Utils.color_int2float(iconAlpha), selected, member, text);
    167278            } else if (symbol != null) {
    168                 painter.drawNodeSymbol(n, symbol, selected, member, getName(n, painter));
     279                painter.drawNodeSymbol(n, symbol, selected, member, text);
    169280            } else {
    170281                if (n.isHighlighted()) {
    171                     painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), getName(n, painter));
     282                    painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), text);
    172283                } else {
    173284                    Color color;
     
    204315                                            settings.isFillUnselectedNode();
    205316
    206                     painter.drawNode(n, color, size, fill, getName(n, painter));
     317                    painter.drawNode(n, color, size, fill, text);
    207318                }
    208319            }
     
    220331    }
    221332
    222     protected String getName(Node n, MapPainter painter) {
    223         if (painter.isShowNames() && annotate) {
    224             if (annotation_key != null) {
    225                 return n.get(annotation_key);
    226             } else {
    227                 return painter.getNodeName(n);
    228             }
    229         }
    230         return null;
    231     }
    232 
    233333    @Override
    234334    public int hashCode() {
    235335        int hash = super.hashCode();
    236         hash = 17 * hash + (annotate ? 1 : 0);
    237         hash = 17 * hash + (annotation_key != null ? annotation_key.hashCode() : 0);
    238336        hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0);
    239         hash = 17 * hash + this.iconAlpha;
    240         hash = 17 * hash + (this.symbol != null ? this.symbol.hashCode() : 0);
     337        hash = 17 * hash + iconAlpha;
     338        hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
     339        hash = 17 * hash + (text != null ? text.hashCode() : 0);
    241340        return hash;
    242341    }
     
    253352        if (icon != other.icon && (icon == null || other.icon == null || icon.getImage() != other.icon.getImage()))
    254353            return false;
    255         if (annotate != other.annotate)
    256             return false;
    257         if (!equal(annotation_key, annotation_key))
    258             return false;
    259354        if (this.iconAlpha != other.iconAlpha)
    260355            return false;
    261356        if (!equal(symbol, other.symbol))
     357            return false;
     358        if (!equal(text, other.text))
    262359            return false;
    263360        return true;
     
    267364    @Override
    268365    public String toString() {
    269         return "NodeElemStyle{" + super.toString() + "annotate=" + annotate + " annotation_key=" + annotation_key +
    270                 (icon != null ? (" icon=" + icon + " iconAlpha=" + iconAlpha) : "") +
     366        return "NodeElemStyle{" + super.toString() +
     367                (icon != null ? ("icon=" + icon + " iconAlpha=" + iconAlpha) : "") +
    271368                (symbol != null ? (" symbol=[" + symbol + "]") : "") + '}';
    272369    }
Note: See TracChangeset for help on using the changeset viewer.