Ignore:
Timestamp:
2017-10-09T17:14:50+02:00 (7 years ago)
Author:
bastiK
Message:

use RenderingHelper in MapCSSRendererTest; move getBackgroundColor() from PaintColors to ElemStyles (no longer global) (see #15273)

File:
1 edited

Legend:

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

    r12964 r12966  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Color;
    67import java.awt.Dimension;
    78import java.awt.Graphics2D;
    89import java.awt.Point;
     10import java.awt.RenderingHints;
    911import java.awt.image.BufferedImage;
    10 import java.io.File;
    1112import java.io.IOException;
    1213import java.util.Collection;
     
    1516import java.util.Optional;
    1617
    17 import javax.imageio.ImageIO;
    18 
    1918import org.openstreetmap.josm.Main;
    2019import org.openstreetmap.josm.data.Bounds;
    2120import org.openstreetmap.josm.data.ProjectionBounds;
    2221import org.openstreetmap.josm.data.osm.DataSet;
    23 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    2422import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
    2523import org.openstreetmap.josm.data.projection.Projection;
     
    4139    private final double scale;
    4240    private final Collection<StyleData> styles;
    43     private String outputFile;
     41    private Color backgroundColor;
     42    private boolean fillBackground = true;
    4443
    4544    /**
     
    7372
    7473    /**
    75      * Set the output file for rendering.
     74     * Set the background color to use for rendering.
    7675     *
    77      * Default is {@code out.png}.
    78      * @param outputFile the output file for rendering
     76     * @param backgroundColor the background color to use, {@code} means
     77     * to determine the background color automatically from the style
     78     * @see #setFillBackground(boolean)
     79     * @since 12966
    7980     */
    80     public void setOutputFile(String outputFile) {
    81         this.outputFile = outputFile;
     81    public void setBackgroundColor(Color backgroundColor) {
     82        this.backgroundColor = backgroundColor;
     83    }
     84
     85    /**
     86     * Decide if background should be filled or left transparent.
     87     * @param fillBackground true, if background should be filled
     88     * @see #setBackgroundColor(java.awt.Color)
     89     * @since 12966
     90     */
     91    public void setFillBackground(boolean fillBackground) {
     92        this.fillBackground = fillBackground;
    8293    }
    8394
     
    93104     * Invoke the renderer.
    94105     *
     106     * @return the rendered image
    95107     * @throws IOException in case of an IOException
    96108     * @throws IllegalDataException when illegal data is encountered (style has errors, etc.)
    97109     */
    98     public void render() throws IOException, IllegalDataException {
     110    public BufferedImage render() throws IOException, IllegalDataException {
    99111        // load the styles
    100112        ElemStyles elemStyles = new ElemStyles();
     
    152164        BufferedImage image = new BufferedImage(imgDimPx.width, imgDimPx.height, BufferedImage.TYPE_INT_ARGB);
    153165        Graphics2D g = image.createGraphics();
    154         g.setColor(PaintColors.getBackgroundColor());
    155         g.fillRect(0, 0, imgDimPx.width, imgDimPx.height);
     166
     167        // Force all render hints to be defaults - do not use platform values
     168        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     169        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
     170        g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
     171        g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
     172        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
     173        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
     174        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
     175        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
     176        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     177
     178        if (fillBackground) {
     179            g.setColor(Optional.ofNullable(backgroundColor).orElse(elemStyles.getBackgroundColor()));
     180            g.fillRect(0, 0, imgDimPx.width, imgDimPx.height);
     181        }
    156182        StyledMapRenderer smr = new StyledMapRenderer(g, nc, false);
    157183        smr.setStyles(elemStyles);
    158184        smr.render(ds, false, bounds);
    159 
    160         // write to file
    161         String output = Optional.ofNullable(outputFile).orElse("out.png");
    162         ImageIO.write(image, "png", new File(output));
     185        return image;
    163186    }
    164187
Note: See TracChangeset for help on using the changeset viewer.