Ignore:
Timestamp:
2011-02-10T00:05:18+01:00 (13 years ago)
Author:
bastiK
Message:

mapcss: text along line

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
1 added
7 edited

Legend:

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

    r3879 r3880  
    2626    public BufferedImage fillImage;
    2727    public float fillImageAlpha;
     28    public TextElement text;
    2829
    29     protected AreaElemStyle(Cascade c, Color color, BufferedImage fillImage, float fillImageAlpha) {
     30    protected AreaElemStyle(Cascade c, Color color, BufferedImage fillImage, float fillImageAlpha, TextElement text) {
    3031        super(c);
    3132        CheckParameterUtil.ensureParameterNotNull(color);
     
    3334        this.fillImage = fillImage;
    3435        this.fillImageAlpha = fillImageAlpha;
     36        this.text = text;
    3537    }
    3638
     
    7375            }
    7476        }
     77
     78        TextElement text = null;
     79        String textPos = c.get("text-position", null, String.class);
     80        if (textPos == null || Utils.equal(textPos, "center")) {
     81            text = TextElement.create(c);
     82        }
    7583       
    7684        if (color != null)
    77             return new AreaElemStyle(c, color, fillImage, fillImageAlpha);
     85            return new AreaElemStyle(c, color, fillImage, fillImageAlpha, text);
    7886        else
    7987            return null;
     
    9098                }
    9199            }
    92             painter.drawArea((Way) osm, myColor, fillImage, fillImageAlpha,
    93                     painter.isShowNames() ? painter.getAreaName(osm) : null);
     100            painter.drawArea((Way) osm, myColor, fillImage, fillImageAlpha, text);
    94101        } else if (osm instanceof Relation)
    95102        {
     
    100107                }
    101108            }
    102             painter.drawArea((Relation) osm, myColor, fillImage, fillImageAlpha,
    103                     painter.getAreaName(osm));
     109            painter.drawArea((Relation) osm, myColor, fillImage, fillImageAlpha, text);
    104110        }
    105111    }
     
    119125        if (fillImageAlpha != other.fillImageAlpha)
    120126            return false;
     127        if (!Utils.equal(text, other.text))
     128            return false;
    121129        return true;
    122130    }
     
    128136        hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
    129137        hash = 61 * hash + Float.floatToIntBits(fillImageAlpha);
     138        hash = 61 * hash + (text != null ? text.hashCode() : 0);
    130139        return hash;
    131140    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyle.java

    r3865 r3880  
    22package org.openstreetmap.josm.gui.mappaint;
    33
     4import static org.openstreetmap.josm.tools.Utils.equal;
     5
     6import java.awt.Font;
     7
     8import org.openstreetmap.josm.Main;
    49import org.openstreetmap.josm.data.osm.OsmPrimitive;
    510import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
    611import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
     12import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
    713
    814abstract public class ElemStyle {
     
    2632
    2733    public abstract void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member);
     34
     35    protected static Float getWidth(Cascade c, String key, Float relativeTo) {
     36        Float width = c.get(key, null, Float.class, true);
     37        if (width != null) {
     38            if (width == -1f)
     39                return (float) MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
     40            if (width > 0)
     41                return width;
     42        } else {
     43            String width_key = c.get(key, null, String.class, true);
     44            if (equal(width_key, "thinnest"))
     45                return 0f;
     46            else if (relativeTo != null) {
     47                RelativeFloat width_rel = c.get(key, null, RelativeFloat.class, true);
     48                if (width_rel != null)
     49                    return relativeTo + width_rel.val;
     50            }
     51        }
     52        return null;
     53    }
     54
     55    protected static Font getFont(Cascade c) {
     56        String name = c.get("font-family", Main.pref.get("mappaint.font", "Helvetica"), String.class);
     57        float size = c.get("font-size", (float) Main.pref.getInteger("mappaint.fontsize", 8), Float.class);
     58        int weight = Font.PLAIN;
     59        String weightStr = c.get("font-wheight", null, String.class);
     60        if (equal(weightStr, "bold")) {
     61            weight = Font.BOLD;
     62        }
     63        int style = Font.PLAIN;
     64        String styleStr = c.get("font-style", null, String.class);
     65        if (equal(styleStr, "italic")) {
     66            style = Font.ITALIC;
     67        }
     68        return new Font(name, weight | style, Math.round(size));
     69    }
    2870
    2971    @Override
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r3871 r3880  
    1414import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
    1515import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    16 import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
    1716import org.openstreetmap.josm.tools.Utils;
    1817
     
    3231    public Color color;
    3332    public Color dashesBackground;
     33    public TextElement text;
    3434    public float realWidth; // the real width of this line in meter
    3535
    3636    private BasicStroke dashesLine;
    3737
    38     protected LineElemStyle(Cascade c, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, float realWidth) {
     38    protected LineElemStyle(Cascade c, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, TextElement text, float realWidth) {
    3939        super(c);
    4040        this.line = line;
     
    4242        this.dashesLine = dashesLine;
    4343        this.dashesBackground = dashesBackground;
     44        this.text = text;
    4445        this.realWidth = realWidth;
    4546    }
    4647
    4748    public static LineElemStyle createLine(Environment env) {
    48         return createImpl(env, "");
     49        return createImpl(env, false);
    4950    }
    5051
    5152    public static LineElemStyle createCasing(Environment env) {
    52         LineElemStyle casing =  createImpl(env, "casing-");
     53        LineElemStyle casing =  createImpl(env, true);
    5354        if (casing != null) {
    5455            casing.object_z_index = -1;
     
    5758    }
    5859
    59     private static LineElemStyle createImpl(Environment env, String prefix) {
     60    private static LineElemStyle createImpl(Environment env, boolean casing) {
    6061        Cascade c = env.getCascade();
    61         Float width = c.get(prefix + "width", null, Float.class, true);
    62         if (width != null) {
    63             if (width == -1f) {
    64                 width = (float) MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
    65             }
    66             if (width <= 0)
    67                 return null;
     62        Cascade c_def = env.mc.getCascade("default");
     63
     64        String prefix = casing ? "casing-" : "";
     65
     66        Float width;
     67        if (casing) {
     68            Float widthOnDefault = getWidth(c_def, "width", null);
     69            Float widthLine = getWidth(c, "width", widthOnDefault);
     70            width = getWidth(c, "casing-width", widthLine);
    6871        } else {
    69             String width_key = c.get(prefix + "width", null, String.class, true);
    70             if (equal(width_key, "thinnest")) {
    71                 width = 0f;
    72             } else if (! equal(env.layer, "default")) {
    73                 RelativeFloat width_rel = c.get(prefix + "width", null, RelativeFloat.class, true);
    74                 if (width_rel != null) {
    75                     width = env.mc.getCascade("default").get("width", 0f, Float.class) + width_rel.val;
    76                 } else
    77                     return null;
    78             } else
    79                 return null;
    80         }
     72            Float widthOnDefault = getWidth(c_def, "width", null);
     73            width = getWidth(c, "width", widthOnDefault);
     74        }
     75
     76        if (width == null)
     77            return null;
    8178
    8279        float realWidth = c.get(prefix + "real-width", 0f, Float.class);
     
    178175        }
    179176
    180         return new LineElemStyle(c, line, color, dashesLine, dashesBackground, realWidth);
     177        TextElement text = null;
     178        if (!casing) {
     179            String textPos = c.get("text-position", null, String.class);
     180            if (textPos == null || equal(textPos, "line")) {
     181                text = TextElement.create(c);
     182            }
     183        }
     184
     185        return new LineElemStyle(c, line, color, dashesLine, dashesBackground, text, realWidth);
    181186    }
    182187
     
    222227        }
    223228
    224         painter.drawWay(w, markColor != null ? markColor : color, myLine, myDashLine, myDashedColor, showDirection,
     229        painter.drawWay(w, markColor != null ? markColor : color, myLine, myDashLine, myDashedColor, text, showDirection,
    225230                selected ? false : reversedDirection, showOnlyHeadArrowOnly);
    226231
     
    249254                equal(dashesLine, other.dashesLine) &&
    250255                equal(dashesBackground, other.dashesBackground) &&
     256                equal(text, other.text) &&
    251257                realWidth == other.realWidth;
    252258    }
     
    259265        hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0);
    260266        hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0);
     267        hash = 29 * hash + (text != null ? text.hashCode() : 0);
    261268        hash = 29 * hash + Float.floatToIntBits(realWidth);
    262269        return hash;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r3879 r3880  
    1818import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
    1919import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
    20 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    2120import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    2221import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    3130    public int iconAlpha;
    3231    public Symbol symbol;
    33     public TextElement text;
     32    public NodeTextElement text;
    3433
    3534    private ImageIcon disabledIcon;
     
    8988    }
    9089
    91     public static class TextElement {
    92         public String textKey;
     90    public static class NodeTextElement extends TextElement {
    9391        public HorizontalTextAlignment hAlign;
    9492        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) {
     93
     94        public NodeTextElement(String textKey, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign, Font font, int xOffset, int yOffset, Color color) {
     95            super(textKey, font, xOffset, yOffset, color);
    10196            CheckParameterUtil.ensureParameterNotNull(hAlign);
    10297            CheckParameterUtil.ensureParameterNotNull(vAlign);
    103             CheckParameterUtil.ensureParameterNotNull(font);
    104             CheckParameterUtil.ensureParameterNotNull(color);
    105             this.textKey = textKey;
    10698            this.hAlign = hAlign;
    10799            this.vAlign = vAlign;
    108             this.font = font;
    109             this.xOffset = xOffset;
    110             this.yOffset = yOffset;
    111             this.color = color;
    112100        }
    113101
    114102        @Override
    115103        public boolean equals(Object obj) {
     104            if (!super.equals(obj))
     105                return false;
    116106            if (obj == null || getClass() != obj.getClass())
    117107                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);
     108            final NodeTextElement other = (NodeTextElement) obj;
     109            return hAlign == other.hAlign &&
     110                    vAlign == other.vAlign;
    126111        }
    127112
    128113        @Override
    129114        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();
     115            int hash = super.hashCode();
     116            hash = 97 * hash + hAlign.hashCode();
     117            hash = 97 * hash + vAlign.hashCode();
    138118            return hash;
    139119        }
     120
    140121    }
    141122   
     
    147128    }
    148129
    149     protected NodeElemStyle(Cascade c, ImageIcon icon, int iconAlpha, Symbol symbol, TextElement text) {
     130    protected NodeElemStyle(Cascade c, ImageIcon icon, int iconAlpha, Symbol symbol, NodeTextElement text) {
    150131        super(c);
    151132        this.icon = icon;
     
    179160            return null;
    180161
    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             }
     162        NodeTextElement text = null;
     163        TextElement te = TextElement.create(c);
     164        if (te != null) {
    188165            HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT;
    189166            String hAlignStr = c.get("text-anchor-horizontal", null, String.class);
     
    208185                vAlign = VerticalTextAlignment.BELOW;
    209186            }
    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);
     187            text = new NodeTextElement(te.textKey, hAlign, vAlign, te.font, te.xOffset, te.yOffset, te.color);
    227188        }
    228189       
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Expression.java

    r3867 r3880  
    191191            }
    192192
     193            @SuppressWarnings("unchecked")
    193194            public boolean equal(Object a, Object b) {
    194195                // make sure the casts are done in a meaningful way, so
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

    r3860 r3880  
    4949            return true;
    5050        if (base.equals("area")) {
    51             if (osm instanceof Way && ((Way)osm).isClosed())
     51            if (osm instanceof Way)
    5252                return true;
    5353            if (osm instanceof Relation && ((Relation) osm).isMultipolygon())
  • trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java

    r3865 r3880  
    350350            if (p.area != null) {
    351351                def.putOrClear("fill-color", p.area.color);
     352                def.putOrClear("text-position", "center");
     353                def.putOrClear("text", "auto");
    352354                def.remove("fill-image");
    353355            }
Note: See TracChangeset for help on using the changeset viewer.