Changeset 12966 in josm
- Timestamp:
- 2017-10-09T17:14:50+02:00 (7 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/PaintColors.java
r12537 r12966 5 5 6 6 import java.awt.Color; 7 import java.util.List;8 import java.util.Optional;9 7 10 8 import org.openstreetmap.josm.data.preferences.CachingProperty; 11 9 import org.openstreetmap.josm.data.preferences.ColorProperty; 12 10 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 13 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;14 import org.openstreetmap.josm.gui.mappaint.StyleSource;15 11 16 12 /** … … 91 87 private final CachingProperty<Color> property; 92 88 93 private static volatile Color backgroundColorCache;94 95 private static final MapPaintSylesUpdateListener STYLE_OVERRIDE_LISTENER = new MapPaintSylesUpdateListener() {96 //TODO: Listen to wireframe map mode changes.97 @Override98 public void mapPaintStylesUpdated() {99 backgroundColorCache = null;100 }101 102 @Override103 public void mapPaintStyleEntryUpdated(int idx) {104 mapPaintStylesUpdated();105 }106 };107 108 static {109 MapPaintStyles.addMapPaintSylesUpdateListener(STYLE_OVERRIDE_LISTENER);110 }111 112 89 PaintColors(String name, Color defaultColor) { 113 90 baseProperty = new ColorProperty(name, defaultColor); … … 138 115 */ 139 116 public static Color getBackgroundColor() { 140 if (backgroundColorCache != null) 141 return backgroundColorCache; 142 List<StyleSource> sources = MapPaintStyles.getStyles().getStyleSources(); 143 for (StyleSource s : sources) { 144 if (!s.active) { 145 continue; 146 } 147 Color backgroundColorOverride = s.getBackgroundColorOverride(); 148 if (backgroundColorOverride != null) { 149 backgroundColorCache = backgroundColorOverride; 150 } 151 } 152 return Optional.ofNullable(backgroundColorCache).orElseGet(BACKGROUND::get); 117 return MapPaintStyles.getStyles().getBackgroundColor(); 153 118 } 154 119 -
trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java
r12964 r12966 1372 1372 super.getColors(); 1373 1373 this.highlightColorTransparent = new Color(highlightColor.getRed(), highlightColor.getGreen(), highlightColor.getBlue(), 100); 1374 this.backgroundColor = PaintColors.getBackgroundColor();1374 this.backgroundColor = styles.getBackgroundColor(); 1375 1375 } 1376 1376 -
trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java
r12881 r12966 10 10 import java.util.Map; 11 11 import java.util.Map.Entry; 12 import java.util.Optional; 12 13 13 14 import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent; … … 17 18 import org.openstreetmap.josm.data.osm.Relation; 18 19 import org.openstreetmap.josm.data.osm.Way; 20 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors; 19 21 import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon; 20 22 import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache; … … 69 71 private final Map<String, String> preferenceCache = new HashMap<>(); 70 72 73 private static volatile Color backgroundColorCache; 74 71 75 /** 72 76 * Constructs a new {@code ElemStyles}. … … 75 79 styleSources = new ArrayList<>(); 76 80 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 }); 77 93 } 78 94 … … 94 110 public List<StyleSource> getStyleSources() { 95 111 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); 96 127 } 97 128 -
trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java
r12963 r12966 5 5 6 6 import java.awt.Dimension; 7 import java.awt.image.BufferedImage; 8 import java.io.File; 7 9 import java.io.FileInputStream; 8 10 import java.io.FileNotFoundException; … … 14 16 import java.util.function.Supplier; 15 17 import java.util.logging.Level; 18 19 import javax.imageio.ImageIO; 16 20 17 21 import org.openstreetmap.gui.jmapviewer.OsmMercator; … … 155 159 RenderingArea area = determineRenderingArea(ds); 156 160 RenderingHelper rh = new RenderingHelper(ds, area.bounds, area.scale, argStyles); 157 rh.setOutputFile(argOutput);158 161 checkPreconditions(rh); 159 rh.render(); 162 BufferedImage image = rh.render(); 163 writeImageToFile(image); 160 164 } catch (FileNotFoundException e) { 161 165 if (Logging.isDebugEnabled()) { … … 562 566 } 563 567 } 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 } 564 573 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingHelper.java
r12964 r12966 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Color; 6 7 import java.awt.Dimension; 7 8 import java.awt.Graphics2D; 8 9 import java.awt.Point; 10 import java.awt.RenderingHints; 9 11 import java.awt.image.BufferedImage; 10 import java.io.File;11 12 import java.io.IOException; 12 13 import java.util.Collection; … … 15 16 import java.util.Optional; 16 17 17 import javax.imageio.ImageIO;18 19 18 import org.openstreetmap.josm.Main; 20 19 import org.openstreetmap.josm.data.Bounds; 21 20 import org.openstreetmap.josm.data.ProjectionBounds; 22 21 import org.openstreetmap.josm.data.osm.DataSet; 23 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;24 22 import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer; 25 23 import org.openstreetmap.josm.data.projection.Projection; … … 41 39 private final double scale; 42 40 private final Collection<StyleData> styles; 43 private String outputFile; 41 private Color backgroundColor; 42 private boolean fillBackground = true; 44 43 45 44 /** … … 73 72 74 73 /** 75 * Set the output file for rendering.74 * Set the background color to use for rendering. 76 75 * 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 79 80 */ 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; 82 93 } 83 94 … … 93 104 * Invoke the renderer. 94 105 * 106 * @return the rendered image 95 107 * @throws IOException in case of an IOException 96 108 * @throws IllegalDataException when illegal data is encountered (style has errors, etc.) 97 109 */ 98 public voidrender() throws IOException, IllegalDataException {110 public BufferedImage render() throws IOException, IllegalDataException { 99 111 // load the styles 100 112 ElemStyles elemStyles = new ElemStyles(); … … 152 164 BufferedImage image = new BufferedImage(imgDimPx.width, imgDimPx.height, BufferedImage.TYPE_INT_ARGB); 153 165 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 } 156 182 StyledMapRenderer smr = new StyledMapRenderer(g, nc, false); 157 183 smr.setStyles(elemStyles); 158 184 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; 163 186 } 164 187 -
trunk/test/functional/org/openstreetmap/josm/gui/mappaint/MapCSSRendererTest.java
r12965 r12966 5 5 import static org.junit.Assert.fail; 6 6 7 import java.awt.Graphics2D;8 7 import java.awt.GraphicsEnvironment; 9 8 import java.awt.Point; 10 import java.awt.RenderingHints;11 9 import java.awt.image.BufferedImage; 12 10 import java.io.File; … … 18 16 import java.util.Arrays; 19 17 import java.util.Collection; 18 import java.util.Collections; 20 19 import java.util.List; 21 20 import java.util.stream.Collectors; … … 38 37 import org.openstreetmap.josm.data.osm.OsmPrimitive; 39 38 import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer; 40 import org.openstreetmap.josm.data.preferences.sources.SourceEntry;41 import org.openstreetmap.josm.data.preferences.sources.SourceType;42 import org.openstreetmap.josm.gui.NavigatableComponent;43 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;44 39 import org.openstreetmap.josm.io.IllegalDataException; 45 40 import org.openstreetmap.josm.io.OsmReader; … … 163 158 // load the data 164 159 DataSet dataSet = testConfig.getOsmDataSet(); 165 166 // load the style 167 MapCSSStyleSource.STYLE_SOURCE_LOCK.writeLock().lock(); 168 try { 169 MapPaintStyles.getStyles().clear(); 170 171 MapCSSStyleSource source = new MapCSSStyleSource(testConfig.getStyleSourceEntry()); 172 source.loadStyleSource(); 173 if (!source.getErrors().isEmpty()) { 174 fail("Failed to load style file. Errors: " + source.getErrors()); 175 } 176 MapPaintStyles.getStyles().setStyleSources(Arrays.asList(source)); 177 MapPaintStyles.fireMapPaintSylesUpdated(); 178 MapPaintStyles.getStyles().clearCached(); 179 180 } finally { 181 MapCSSStyleSource.STYLE_SOURCE_LOCK.writeLock().unlock(); 182 } 183 184 // create the renderer 185 BufferedImage image = new BufferedImage(IMAGE_SIZE, IMAGE_SIZE, BufferedImage.TYPE_INT_ARGB); 186 NavigatableComponent nc = new NavigatableComponent() { 187 { 188 setBounds(0, 0, IMAGE_SIZE, IMAGE_SIZE); 189 updateLocationState(); 190 } 191 192 @Override 193 protected boolean isVisibleOnScreen() { 194 return true; 195 } 196 197 @Override 198 public Point getLocationOnScreen() { 199 return new Point(0, 0); 200 } 201 }; 160 dataSet.allPrimitives().stream().forEach(this::loadPrimitiveStyle); 161 dataSet.setSelected(dataSet.allPrimitives().stream().filter(n -> n.isKeyTrue("selected")).collect(Collectors.toList())); 162 202 163 ProjectionBounds pb = new ProjectionBounds(); 203 164 pb.extend(Main.getProjection().latlon2eastNorth(testConfig.testArea.getMin())); 204 165 pb.extend(Main.getProjection().latlon2eastNorth(testConfig.testArea.getMax())); 205 166 double scale = (pb.maxEast - pb.minEast) / IMAGE_SIZE; 206 nc.zoomTo(pb.getCenter(), scale); 207 208 dataSet.allPrimitives().stream().forEach(this::loadPrimitiveStyle); 209 dataSet.setSelected(dataSet.allPrimitives().stream().filter(n -> n.isKeyTrue("selected")).collect(Collectors.toList())); 210 211 Graphics2D g = image.createGraphics(); 212 // Force all render hints to be defaults - do not use platform values 213 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 214 g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 215 g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 216 g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE); 217 g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 218 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 219 g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 220 g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); 221 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 222 new StyledMapRenderer(g, nc, false).render(dataSet, false, testConfig.testArea); 167 168 RenderingHelper.StyleData sd = new RenderingHelper.StyleData(); 169 sd.styleUrl = testConfig.getStyleSourceUrl(); 170 RenderingHelper rh = new RenderingHelper(dataSet, testConfig.testArea, scale, Collections.singleton(sd)); 171 rh.setFillBackground(false); 172 BufferedImage image = rh.render(); 223 173 224 174 if (UPDATE_ALL) { … … 317 267 } 318 268 319 public SourceEntry getStyleSourceEntry() { 320 return new SourceEntry(SourceType.MAP_PAINT_STYLE, getTestDirectory() + "/style.mapcss", 321 "test style", "a test style", true // active 322 ); 269 public String getStyleSourceUrl() { 270 return getTestDirectory() + "/style.mapcss"; 323 271 } 324 272
Note:
See TracChangeset
for help on using the changeset viewer.