Ticket #23654: josm_23654_3.patch

File josm_23654_3.patch, 5.6 KB (added by gaben, 4 days ago)
  • src/org/openstreetmap/gui/jmapviewer/AttributionSupport.java

    Subject: [PATCH] fix #23654 - improve map imagery attribute padding
    ---
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/gui/jmapviewer/AttributionSupport.java b/src/org/openstreetmap/gui/jmapviewer/AttributionSupport.java
    a b  
    1313import java.awt.geom.Rectangle2D;
    1414import java.awt.image.ImageObserver;
    1515import java.util.HashMap;
     16import java.util.Map;
    1617
    1718import org.openstreetmap.gui.jmapviewer.interfaces.Attributed;
    1819import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
     
    2021public class AttributionSupport {
    2122    public static final Font ATTR_FONT = new Font("Arial", Font.PLAIN, 10);
    2223    public static final Font ATTR_LINK_FONT;
     24    private static final int PADDING = 5;
    2325
    2426    private Attributed source;
    2527
     
    3234    protected Rectangle attrImageBounds;
    3335
    3436    static {
    35         HashMap<TextAttribute, Integer> aUnderline = new HashMap<>();
     37        Map<TextAttribute, Integer> aUnderline = new HashMap<>();
    3638        aUnderline.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    3739        ATTR_LINK_FONT = ATTR_FONT.deriveFont(aUnderline);
    3840    }
     
    6668        Font font = g.getFont();
    6769        g.setFont(ATTR_LINK_FONT);
    6870
    69         // Draw terms of use text
     71        // Draw terms of use text (bottom left corner)
     72        final int fontDescent = g.getFontMetrics().getDescent();
    7073        int termsTextHeight = 0;
    71         int termsTextY = height;
     74        int textY = height - 1 - PADDING; // 1px offset to compensate for the text shadow
    7275
    7376        if (attrTermsText != null) {
    7477            Rectangle2D termsStringBounds = g.getFontMetrics().getStringBounds(attrTermsText, g);
    7578            int textRealHeight = (int) termsStringBounds.getHeight();
    76             termsTextHeight = textRealHeight - 5;
     79            termsTextHeight = textRealHeight;
    7780            int termsTextWidth = (int) termsStringBounds.getWidth();
    78             termsTextY = height - termsTextHeight;
    79             int x = 2;
    80             int y = height - termsTextHeight;
    81             attrToUBounds = new Rectangle(x, y-termsTextHeight, termsTextWidth, textRealHeight);
    82             g.setColor(Color.black);
    83             g.drawString(attrTermsText, x + 1, y + 1);
    84             g.setColor(Color.white);
    85             g.drawString(attrTermsText, x, y);
     81            attrToUBounds = new Rectangle(PADDING, textY - termsTextHeight + fontDescent, termsTextWidth, textRealHeight);
     82            drawShadedText(g, attrTermsText, PADDING, textY);
    8683        } else {
    8784            attrToUBounds = null;
    8885        }
    8986
    90         // Draw attribution logo
     87        // Draw attribution logo (on top of the terms of use text)
    9188        if (attrImage != null) {
    92             int x = 2;
    9389            int imgWidth = attrImage.getWidth(observer);
    9490            int imgHeight = attrImage.getHeight(observer);
    95             int y = termsTextY - imgHeight - termsTextHeight - 5;
    96             attrImageBounds = new Rectangle(x, y, imgWidth, imgHeight);
    97             g.drawImage(attrImage, x, y, null);
     91            int y = textY - imgHeight - termsTextHeight;
     92            attrImageBounds = new Rectangle(PADDING, y, imgWidth, imgHeight);
     93            g.drawImage(attrImage, PADDING, y, null);
    9894        } else {
    9995            attrImageBounds = null;
    10096        }
    10197
     98        // Draw attribution (bottom right corner)
    10299        g.setFont(ATTR_FONT);
    103100        String attributionText = source.getAttributionText(zoom, topLeft, bottomRight);
    104101        if (attributionText == null) {
    105             // In case attribution text has been forgotte, display URL
     102            // In case the attribution text has been forgotten, display URL
    106103            attributionText = source.getAttributionLinkURL();
    107104        }
    108105        if (attributionText != null) {
    109106            Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(attributionText, g);
    110             int textHeight = (int) stringBounds.getHeight() - 5;
    111             int x = width - (int) stringBounds.getWidth();
    112             int y = height - textHeight;
    113             g.setColor(Color.black);
    114             g.drawString(attributionText, x + 1, y + 1);
    115             g.setColor(Color.white);
    116             g.drawString(attributionText, x, y);
    117             attrTextBounds = new Rectangle(x, y-textHeight, (int) stringBounds.getWidth(), (int) stringBounds.getHeight());
     107            int textWidth = (int) stringBounds.getWidth();
     108            int textHeight = (int) stringBounds.getHeight();
     109            int x = width - textWidth - PADDING;
     110            drawShadedText(g, attributionText, x, textY);
     111            attrTextBounds = new Rectangle(x, textY - textHeight + fontDescent, textWidth, textHeight);
    118112        } else {
    119113            attrTextBounds = null;
    120114        }
     
    122116        g.setFont(font);
    123117    }
    124118
     119    /**
     120     * Draws a string with 1px shadow.
     121     * @param g the graphics
     122     * @param text the string to be drawn
     123     * @param x the x coordinate
     124     * @param y the y coordinate
     125     */
     126    private static void drawShadedText(Graphics g, String text, int x, int y) {
     127        g.setColor(Color.black);
     128        g.drawString(text, x + 1, y + 1);
     129        g.setColor(Color.white);
     130        g.drawString(text, x, y);
     131    }
     132
    125133    public boolean handleAttributionCursor(Point p) {
    126134        if (p == null) return false;
    127135