Skip to content
Permalink
Browse files
applied #10301 - extend display of maxspeed nodes (patch by Klumbumbus)
Include Droid Sans font in the JOSM binary distribution.
This unifies the font rendering on different platforms and allows geometric
constructions with text (as demonstrated for maxspeed). Both regular
and bold style are available.


git-svn-id: http://josm.openstreetmap.de/svn/trunk@7383 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
bastiK authored and bastiK committed Aug 13, 2014
1 parent 93df21b commit 91bae7ba95dadfe7932c2d3d3ed448c34044dd97
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 19 deletions.
Binary file not shown.
Binary file not shown.
@@ -59,6 +59,7 @@
import org.openstreetmap.josm.plugins.PluginHandler;
import org.openstreetmap.josm.plugins.PluginInformation;
import org.openstreetmap.josm.tools.BugReportExceptionHandler;
import org.openstreetmap.josm.tools.FontsManager;
import org.openstreetmap.josm.tools.I18n;
import org.openstreetmap.josm.tools.ImageProvider;
import org.openstreetmap.josm.tools.OsmUrlToBounds;
@@ -345,6 +346,8 @@ public PermissionCollection getPermissions(CodeSource codesource) {
}
Main.pref.updateSystemProperties();

FontsManager.initialize();

final JFrame mainFrame = new JFrame(tr("Java OpenStreetMap Editor"));
Main.parent = mainFrame;

@@ -33,7 +33,8 @@ protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement
this.text = text;
}

public static AreaElemStyle create(Cascade c) {
public static AreaElemStyle create(final Environment env) {
final Cascade c = env.mc.getCascade(env.layer);
MapImage fillImage = null;
Color color = null;

@@ -74,7 +75,7 @@ public static AreaElemStyle create(Cascade c) {
TextElement text = null;
Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
if (textPos == null || "center".equals(textPos.val)) {
text = TextElement.create(c, PaintColors.AREA_TEXT.get(), true);
text = TextElement.create(env, PaintColors.AREA_TEXT.get(), true);
}

if (color != null)
@@ -110,7 +110,7 @@ public static BoxTextElemStyle create(Environment env, BoxProvider boxProvider,
initDefaultParameters();
Cascade c = env.mc.getCascade(env.layer);

TextElement text = TextElement.create(c, DEFAULT_TEXT_COLOR, false);
TextElement text = TextElement.create(env, DEFAULT_TEXT_COLOR, false);
if (text == null) return null;
// Skip any primitives that don't have text to draw. (Styles are recreated for any tag change.)
// The concrete text to render is not cached in this object, but computed for each
@@ -111,7 +111,7 @@ private static String getDefaultFontName() {
synchronized (lock) {
n = DEFAULT_FONT_NAME;
if (n == null) {
DEFAULT_FONT_NAME = n = Main.pref.get("mappaint.font", "Helvetica");
DEFAULT_FONT_NAME = n = Main.pref.get("mappaint.font", "Droid Sans");
}
}
}
@@ -173,7 +173,7 @@ private static Font getCachedFont(String name, int style, int size){
return getCachedFont(new FontDescriptor(name, style, size));
}

protected static Font getFont(Cascade c) {
protected static Font getFont(Cascade c, String s) {
String name = c.get("font-family", getDefaultFontName(), String.class);
float size = c.get("font-size", getDefaultFontSize(), Float.class);
int weight = Font.PLAIN;
@@ -184,7 +184,14 @@ protected static Font getFont(Cascade c) {
if ("italic".equalsIgnoreCase(c.get("font-style", null, String.class))) {
style = Font.ITALIC;
}
return getCachedFont(name, style | weight, Math.round(size));
Font f = getCachedFont(name, style | weight, Math.round(size));
if (f.canDisplayUpTo(s) == -1)
return f;
else {
// fallback if the string contains characters that cannot be
// rendered by the selected font
return getCachedFont("SansSerif", style | weight, Math.round(size));
}
}

@Override
@@ -336,7 +336,7 @@ public Pair<StyleList, Range> generateStyles(OsmPrimitive osm, double scale, Osm
env.layer = e.getKey();
Cascade c = e.getValue();
if (osm instanceof Way) {
addIfNotNull(sl, AreaElemStyle.create(c));
addIfNotNull(sl, AreaElemStyle.create(env));
addIfNotNull(sl, RepeatImageElemStyle.create(env));
addIfNotNull(sl, LineElemStyle.createLine(env));
addIfNotNull(sl, LineElemStyle.createLeftCasing(env));
@@ -353,7 +353,7 @@ public Pair<StyleList, Range> generateStyles(OsmPrimitive osm, double scale, Osm
}
} else if (osm instanceof Relation) {
if (((Relation)osm).isMultipolygon()) {
addIfNotNull(sl, AreaElemStyle.create(c));
addIfNotNull(sl, AreaElemStyle.create(env));
addIfNotNull(sl, RepeatImageElemStyle.create(env));
addIfNotNull(sl, LineElemStyle.createLine(env));
addIfNotNull(sl, LineElemStyle.createCasing(env));
@@ -17,14 +17,14 @@ protected LineTextElemStyle(Cascade c, TextElement text) {
super(c, 4.9f);
this.text = text;
}
public static LineTextElemStyle create(Environment env) {
Cascade c = env.mc.getCascade(env.layer);
public static LineTextElemStyle create(final Environment env) {
final Cascade c = env.mc.getCascade(env.layer);

Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
if (textPos != null && !"line".equals(textPos.val))
return null;

TextElement text = TextElement.create(c, PaintColors.TEXT.get(), false);
TextElement text = TextElement.create(env, PaintColors.TEXT.get(), false);
if (text == null)
return null;
return new LineTextElemStyle(c, text);
@@ -115,12 +115,15 @@ protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade
* properties for text rendering
* @throws IllegalArgumentException thrown if {@code defaultTextColor} is null
*/
public static TextElement create(Cascade c, Color defaultTextColor, boolean defaultAnnotate) throws IllegalArgumentException{
public static TextElement create(Environment env, Color defaultTextColor, boolean defaultAnnotate) throws IllegalArgumentException{
CheckParameterUtil.ensureParameterNotNull(defaultTextColor);
Cascade c = env.mc.getCascade(env.layer);

LabelCompositionStrategy strategy = buildLabelCompositionStrategy(c, defaultAnnotate);
if (strategy == null) return null;
Font font = ElemStyle.getFont(c);
String s = strategy.compose(env.osm);
if (s == null) return null;
Font font = ElemStyle.getFont(c, s);

float xOffset = 0;
float yOffset = 0;
@@ -0,0 +1,38 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.tools;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import org.openstreetmap.josm.io.CachedFile;

public class FontsManager {

public static Map<String, Font> fonts;
public static Collection<String> includedFonts = Arrays.asList(
"DroidSans.ttf",
"DroidSans-Bold.ttf"
);

public static void initialize() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
for (String fontFile : includedFonts) {
String url = "resource://data/fonts/"+fontFile;
try (InputStream i = new CachedFile(url).getInputStream())
{
Font f = Font.createFont(Font.TRUETYPE_FONT, i);
if (f == null) {
throw new RuntimeException("unable to load font: "+fontFile);
}
ge.registerFont(f);
} catch (IOException | FontFormatException ex) {
throw new RuntimeException(ex);
}
}
}
}
@@ -272,9 +272,6 @@ node[maxwidth] {
node[maxlength] {
icon-image: "vehicle/restriction/maxlength.png";
}
node[maxspeed] {
icon-image: "vehicle/restriction/speed.png";
}
node[minspeed] {
icon-image: "vehicle/restriction/minspeed.png";
}
@@ -3545,6 +3542,67 @@ node[boundary=national_park] {
text: auto;
}

/******************/
/* maxspeed nodes */
/******************/
node[maxspeed=none] {
icon-image: "vehicle/restriction/maxspeed_none.svg";
}
node[maxspeed=~/^[0-9]+$/] {
maxspeedprop: tag(maxspeed);
set maxspeedclass;
}
node[maxspeed=signals] {
maxspeedprop: " ?";
set maxspeedclass;
}
node[maxspeed=~/^[0-9]+ mph/] {
maxspeedprop: get(split(" mph",tag(maxspeed)),0);
set maxspeedclass;
}
node[maxspeed=~/[0-9]+ km\/h/] {
maxspeedprop: get(split(" km/h",tag(maxspeed)),0);
set maxspeedclass;
}
node[maxspeed=~/[0-9]+ knots/] {
maxspeedprop: get(split(" knots",tag(maxspeed)),0);
set maxspeedclass;
}
node[prop(maxspeedclass, default)][!is_prop_set(icon-image, default)]::core_maxnodebg {
/* background (white) */
symbol-shape: circle;
symbol-size: 17;
symbol-fill-color: white;
major-z-index: 4.2;
}
node[maxspeed]["maxspeed:variable"]["maxspeed:variable"!="no"][!is_prop_set(icon-image, default)]::core_maxnodebg,
node[maxspeed=signals][!is_prop_set(icon-image, default)]::core_maxnodebg {
/* background (black) */
symbol-fill-color: black;
}
node[prop(maxspeedclass, default)][!is_prop_set(icon-image, default)]::core_maxnodefg {
/* foreground (black text and red circle) */
symbol-shape: circle;
symbol-size: 15;
symbol-stroke-color: crimson;
symbol-stroke-width: 2;
text: prop(maxspeedprop, default);
font-size: 8;
font-weight: bold;
font-family: "Droid Sans";
text-color: black;
text-anchor-horizontal: center;
text-anchor-vertical: center;
text-offset-x: 0;
text-offset-y: -1;
major-z-index: 4.2;
}
node[maxspeed]["maxspeed:variable"]["maxspeed:variable"!="no"][!is_prop_set(icon-image, default)]::core_maxnodefg,
node[maxspeed=signals][!is_prop_set(icon-image, default)]::core_maxnodefg {
/* foreground (white text) */
text-color: white;
}

/***************/
/* zoom levels */
/***************/
@@ -3589,9 +3647,8 @@ node|z-17 {
text: none;
}

node|z20,area|z20 { font-size: 9; }
node|z21,area|z21 { font-size: 10; }
node|z22-,area|z22- { font-size: 11; }
node|z19,area|z19 { font-size: 9; }
node|z20-,area|z20- { font-size: 10; }

/**************/
/* place tags */

0 comments on commit 91bae7b

Please sign in to comment.