Changeset 27313 in osm for applications


Ignore:
Timestamp:
2011-12-23T10:39:53+01:00 (13 years ago)
Author:
kpastor
Message:

Text background rendering improved for better readability.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintableMapView.java

    r27283 r27313  
    2727
    2828import java.awt.AlphaComposite;
     29import java.awt.BasicStroke;
    2930import java.awt.Color;
    3031import java.awt.Dimension;
     
    3334import java.awt.Graphics2D;
    3435import java.awt.Image;
     36import java.awt.Shape;
    3537import java.awt.event.ActionEvent;
    3638import java.awt.event.KeyEvent;
     39import java.awt.font.FontRenderContext;
     40import java.awt.font.GlyphVector;
    3741import java.awt.geom.AffineTransform;
    3842import java.awt.geom.Rectangle2D;
     
    303307        int ws = (int)(distScale * 20.0);   // length of a segment
    304308       
    305         /* white background & shadow */
     309        /* white background */
    306310        g2d.setColor(Color.WHITE);
    307311        g2d.fillRect(xLeft-1, yBar-1, w+2, h+2);
    308         g2d.setFont(labelFont.deriveFont(AffineTransform.getTranslateInstance(0.5,0.4)));
    309         g2d.drawString("0", 0, yLabel);
    310         g2d.drawString(rightLabel, xRight, yLabel);
    311312       
    312313        /* black foreground */
     
    317318        g2d.fillRect(xLeft+w-ws, yBar, ws, h);
    318319        g2d.setFont(labelFont);
    319         g2d.drawString("0", 0, yLabel);
    320         g2d.drawString(rightLabel, xRight, yLabel);
     320        paintText(g2d, "0", 0, yLabel);
     321        paintText(g2d, rightLabel, xRight, yLabel);
    321322       
    322323        /* lexical scale */
     
    328329        bound = g2d.getFontMetrics().getStringBounds(lexicalScale, g2d);
    329330        int xLexical = Math.max(0, xLeft + (w - (int)bound.getWidth()) / 2);
    330         g2d.setColor(Color.WHITE);
    331         g2d.setFont(scaleFront.deriveFont(AffineTransform.getTranslateInstance(0.5,0.4)));
    332         g2d.drawString(lexicalScale, xLexical, yLexical);
    333         g2d.setColor(Color.BLACK);
    334         g2d.setFont(scaleFront);
    335         g2d.drawString(lexicalScale, xLexical, yLexical);
     331        paintText(g2d, lexicalScale, xLexical, yLexical);
    336332    }
    337333   
     
    344340    public void paintMapAttribution(Graphics2D g2d, PageFormat pageFormat) {
    345341        String text = Main.pref.get("print.attribution", PrintPlugin.DEF_ATTRIBUTION);
    346         if (text == null || text.length() > 0) {
     342        if (text != null && text.length() > 0) {
     343            text += " ";
     344
    347345            Font attributionFont = new Font("Arial", Font.PLAIN, FONT_SIZE * 8 / 10);
    348346            g2d.setFont(attributionFont);
     
    350348            int x = (int)((pageFormat.getImageableWidth() - bound.getWidth()));
    351349            int y = FONT_SIZE * 3 / 2;
     350            paintText(g2d, text, x, y);
     351        }
     352    }
     353   
     354    /**
     355     * Paint a text.
     356     *
     357     * This method will not only draw the letters but also a background
     358     * which improves redability.
     359     *
     360     * @param g2d the graphics context to use for painting
     361     * @param text the text to be drawn
     362     * @param x the x coordinate
     363     * @param y the y coordinate
     364     */
     365   
     366    public void paintText(Graphics2D g2d, String text, int x, int y) {
     367            AffineTransform ax = g2d.getTransform();
     368            g2d.translate(x,y);
     369
     370            FontRenderContext frc = g2d.getFontRenderContext();
     371            GlyphVector gv = g2d.getFont().createGlyphVector(frc, text);
     372            Shape textOutline = gv.getOutline();
     373
     374            g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
    352375            g2d.setColor(Color.WHITE);
    353             g2d.setFont(attributionFont.deriveFont(AffineTransform.getTranslateInstance(0.5,0.4)));
    354             g2d.drawString(text, x, y);
     376            g2d.draw(textOutline);
     377           
     378            g2d.setStroke(new BasicStroke());
    355379            g2d.setColor(Color.BLACK);
    356             g2d.setFont(attributionFont);
    357             g2d.drawString(text, x, y);
    358         }
     380            g2d.drawString(text, 0, 0);
     381
     382            g2d.setTransform(ax);
    359383    }
    360384
Note: See TracChangeset for help on using the changeset viewer.