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

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

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
3 edited

Legend:

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

    r12881 r12966  
    1010import java.util.Map;
    1111import java.util.Map.Entry;
     12import java.util.Optional;
    1213
    1314import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent;
     
    1718import org.openstreetmap.josm.data.osm.Relation;
    1819import org.openstreetmap.josm.data.osm.Way;
     20import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    1921import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
    2022import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
     
    6971    private final Map<String, String> preferenceCache = new HashMap<>();
    7072
     73    private static volatile Color backgroundColorCache;
     74
    7175    /**
    7276     * Constructs a new {@code ElemStyles}.
     
    7579        styleSources = new ArrayList<>();
    7680        Config.getPref().addPreferenceChangeListener(this);
     81        MapPaintStyles.addMapPaintSylesUpdateListener(new MapPaintStyles.MapPaintSylesUpdateListener() {
     82            //TODO: Listen to wireframe map mode changes.
     83            @Override
     84            public void mapPaintStylesUpdated() {
     85                backgroundColorCache = null;
     86            }
     87
     88            @Override
     89            public void mapPaintStyleEntryUpdated(int idx) {
     90                mapPaintStylesUpdated();
     91            }
     92        });
    7793    }
    7894
     
    94110    public List<StyleSource> getStyleSources() {
    95111        return Collections.<StyleSource>unmodifiableList(styleSources);
     112    }
     113
     114    public Color getBackgroundColor() {
     115        if (backgroundColorCache != null)
     116            return backgroundColorCache;
     117        for (StyleSource s : styleSources) {
     118            if (!s.active) {
     119                continue;
     120            }
     121            Color backgroundColorOverride = s.getBackgroundColorOverride();
     122            if (backgroundColorOverride != null) {
     123                backgroundColorCache = backgroundColorOverride;
     124            }
     125        }
     126        return Optional.ofNullable(backgroundColorCache).orElseGet(PaintColors.BACKGROUND::get);
    96127    }
    97128
  • trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java

    r12963 r12966  
    55
    66import java.awt.Dimension;
     7import java.awt.image.BufferedImage;
     8import java.io.File;
    79import java.io.FileInputStream;
    810import java.io.FileNotFoundException;
     
    1416import java.util.function.Supplier;
    1517import java.util.logging.Level;
     18
     19import javax.imageio.ImageIO;
    1620
    1721import org.openstreetmap.gui.jmapviewer.OsmMercator;
     
    155159            RenderingArea area = determineRenderingArea(ds);
    156160            RenderingHelper rh = new RenderingHelper(ds, area.bounds, area.scale, argStyles);
    157             rh.setOutputFile(argOutput);
    158161            checkPreconditions(rh);
    159             rh.render();
     162            BufferedImage image = rh.render();
     163            writeImageToFile(image);
    160164        } catch (FileNotFoundException e) {
    161165            if (Logging.isDebugEnabled()) {
     
    562566        }
    563567    }
     568
     569    private void writeImageToFile(BufferedImage image) throws IOException {
     570        String output = Optional.ofNullable(argOutput).orElse("out.png");
     571        ImageIO.write(image, "png", new File(output));
     572    }
    564573}
  • 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.