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

mapcss: font attributes & text alignment

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java

    r3867 r3871  
    1313import java.awt.Rectangle;
    1414import java.awt.TexturePaint;
     15import java.awt.font.FontRenderContext;
     16import java.awt.font.LineMetrics;
    1517import java.awt.geom.GeneralPath;
    1618import java.awt.geom.Rectangle2D;
     
    3335import org.openstreetmap.josm.gui.NavigatableComponent;
    3436import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
     37import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.HorizontalTextAlignment;
    3538import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.Symbol;
     39import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.TextElement;
     40import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.VerticalTextAlignment;
    3641import org.openstreetmap.josm.tools.ImageProvider;
    3742import org.openstreetmap.josm.tools.LanguageInfo;
     
    188193    }
    189194
    190     public void drawNodeIcon(Node n, ImageIcon icon, float iconAlpha, boolean selected, boolean member, String name) {
     195    public void drawNodeIcon(Node n, ImageIcon icon, float iconAlpha, boolean selected, boolean member, TextElement text) {
    191196        Point p = nc.getPoint(n);
    192197        if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
     
    198203        icon.paintIcon ( nc, g, p.x-w/2, p.y-h/2 );
    199204        g.setPaintMode();
    200         if(name != null) {
    201             if (inactive || n.isDisabled()) {
    202                 g.setColor(inactiveColor);
    203             } else {
    204                 g.setColor(textColor);
    205             }
    206             Font defaultFont = g.getFont();
    207             g.setFont (orderFont);
    208             g.drawString (name, p.x+w/2+2, p.y+h/2+2);
    209             g.setFont(defaultFont);
    210         }
     205        drawNodeText(n, text, p, w/2, h/2);
    211206        if (selected || member)
    212207        {
     
    216211    }
    217212
    218     public void drawNodeSymbol(Node n, Symbol s, boolean selected, boolean member, String name) {
     213    public void drawNodeSymbol(Node n, Symbol s, boolean selected, boolean member, TextElement text) {
    219214        Point p = nc.getPoint(n);
    220215        if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
    221         int radius = (int) (s.size / 2);
     216        int radius = s.size / 2;
    222217
    223218        if (s.fillColor != null) {
    224             g.setColor(s.fillColor);
     219            if (inactive || n.isDisabled()) {
     220                g.setColor(inactiveColor);
     221            } else {
     222                g.setColor(s.fillColor);
     223            }
    225224            switch (s.symbol) {
     225                case SQUARE:
     226                    g.fillRect(p.x - radius, p.y - radius, s.size, s.size);
     227                    break;
    226228                case CIRCLE:
    227                     g.fillOval(p.x - radius, p.y - radius, (int) s.size, (int) s.size);
     229                    g.fillOval(p.x - radius, p.y - radius, s.size, s.size);
    228230                    break;
    229                 case SQUARE:
    230                     g.fillRect(p.x - radius, p.y - radius, (int) s.size, (int) s.size);
    231                     break;
     231                default:
     232                    throw new AssertionError();
    232233            }
    233234        }
    234235        if (s.stroke != null) {
    235236            g.setStroke(s.stroke);
    236             g.setColor(s.strokeColor);
     237            if (inactive || n.isDisabled()) {
     238                g.setColor(inactiveColor);
     239            } else {
     240                g.setColor(s.strokeColor);
     241            }
    237242            switch (s.symbol) {
     243                case SQUARE:
     244                    g.drawRect(p.x - radius, p.y - radius, s.size - 1, s.size - 1);
     245                    break;
    238246                case CIRCLE:
    239                     g.drawOval(p.x - radius, p.y - radius, (int) s.size - 1, (int) s.size - 1);
     247                    g.drawOval(p.x - radius, p.y - radius, s.size - 1, s.size - 1);
    240248                    break;
    241                 case SQUARE:
    242                     g.drawRect(p.x - radius, p.y - radius, (int) s.size - 1, (int) s.size - 1);
    243                     break;
     249                default:
     250                    throw new AssertionError();
    244251            }
    245252            g.setStroke(new BasicStroke());
    246253        }
     254        drawNodeText(n, text, p, radius, radius);
    247255    }
    248256
     
    253261     * @param color The color of the node.
    254262     */
    255     public void drawNode(Node n, Color color, int size, boolean fill, String name) {
     263    public void drawNode(Node n, Color color, int size, boolean fill, TextElement text) {
    256264        if (size > 1) {
    257265            Point p = nc.getPoint(n);
     
    270278            }
    271279
    272             if(name != null)            {
    273                 if (inactive || n.isDisabled()) {
    274                     g.setColor(inactiveColor);
    275                 } else {
    276                     g.setColor(textColor);
    277                 }
    278                 Font defaultFont = g.getFont();
    279                 g.setFont (orderFont);
    280                 g.drawString (name, p.x+radius+2, p.y+radius+2);
    281                 g.setFont(defaultFont);
    282             }
    283         }
     280            drawNodeText(n, text, p, radius, radius + 4);
     281        }
     282    }
     283
     284    private void drawNodeText(Node n, TextElement text, Point p, int w_half, int h_half) {
     285        if (!isShowNames() || text == null)
     286            return;
     287
     288        String s = text.textKey == null ? getNodeName(n) : n.get(text.textKey);
     289        if (s == null)
     290            return;
     291
     292        if (inactive || n.isDisabled()) {
     293            g.setColor(inactiveColor);
     294        } else {
     295            g.setColor(text.color);
     296        }
     297        Font defaultFont = g.getFont();
     298        g.setFont(text.font);
     299
     300        int x = p.x + text.xOffset;
     301        int y = p.y + text.yOffset;
     302        /**
     303         *
     304         *       left-above __center-above___ right-above
     305         *         left-top|                 |right-top
     306         *                 |                 |
     307         *      left-center|  center-center  |right-center
     308         *                 |                 |
     309         *      left-bottom|_________________|right-bottom
     310         *       left-below   center-below    right-below
     311         *
     312         */
     313        if (text.hAlign == HorizontalTextAlignment.RIGHT) {
     314            x += w_half + 2;
     315        } else {
     316            FontRenderContext frc = g.getFontRenderContext();
     317            Rectangle2D bounds = text.font.getStringBounds(s, frc);
     318            int textWidth = (int) bounds.getWidth();
     319            if (text.hAlign == HorizontalTextAlignment.CENTER) {
     320                x -= textWidth / 2;
     321            } else if (text.hAlign == HorizontalTextAlignment.LEFT) {
     322                x -= w_half + 4 + textWidth;
     323            } else throw new AssertionError();
     324        }
     325
     326        if (text.vAlign == VerticalTextAlignment.BOTTOM) {
     327            y += h_half - 2;
     328        } else {
     329            FontRenderContext frc = g.getFontRenderContext();
     330            LineMetrics metrics = text.font.getLineMetrics(s, frc);
     331            if (text.vAlign == VerticalTextAlignment.ABOVE) {
     332                y -= h_half + metrics.getDescent();
     333            } else if (text.vAlign == VerticalTextAlignment.TOP) {
     334                y -= h_half - metrics.getAscent();
     335            } else if (text.vAlign == VerticalTextAlignment.CENTER) {
     336                y += (metrics.getAscent() - metrics.getDescent()) / 2;
     337            } else if (text.vAlign == VerticalTextAlignment.BELOW) {
     338                y += h_half + metrics.getAscent();
     339            } else throw new AssertionError();
     340        }
     341        g.drawString(s, x, y);
     342        g.setFont(defaultFont);
    284343    }
    285344
Note: See TracChangeset for help on using the changeset viewer.