Ticket #6150: mapcss-text-instruction.2.patch
| File mapcss-text-instruction.2.patch, 16.1 KB (added by , 15 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/mappaint/LabelCompositionStrategy.java
47 47 48 48 static public class StaticLabelCompositionStrategy extends LabelCompositionStrategy { 49 49 private String defaultLabel; 50 50 51 public StaticLabelCompositionStrategy(String defaultLabel){ 51 52 this.defaultLabel = defaultLabel; 52 53 } -
src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
20 20 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 21 21 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource; 22 22 import org.openstreetmap.josm.gui.mappaint.xml.XmlStyleSource; 23 import org.openstreetmap.josm.gui.preferences.MapPaintPreference.MapPaintPrefMigration; 23 24 import org.openstreetmap.josm.gui.preferences.SourceEntry; 24 import org.openstreetmap.josm.gui.preferences.MapPaintPreference.MapPaintPrefMigration;25 25 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 26 26 import org.openstreetmap.josm.io.MirroredInputStream; 27 27 import org.openstreetmap.josm.tools.ImageProvider; … … 41 41 { 42 42 return styles; 43 43 } 44 44 45 /** 46 * Value holder for a reference to a tag name. A style instruction 47 * <pre> 48 * text: a_tag_name; 49 * </pre> 50 * results in a tag reference for the tag <tt>a_tag_name</tt> in the 51 * style cascade. 52 */ 53 public static class TagKeyReference { 54 public String key; 55 public TagKeyReference(String key){ 56 this.key = key; 57 } 58 59 @Override 60 public String toString() { 61 return "TagKeyReference{" + "key='" + key + "'}"; 62 } 63 64 @Override 65 public int hashCode() { 66 final int prime = 31; 67 int result = 1; 68 result = prime * result + ((key == null) ? 0 : key.hashCode()); 69 return result; 70 } 71 72 @Override 73 public boolean equals(Object obj) { 74 if (this == obj) 75 return true; 76 if (obj == null) 77 return false; 78 if (getClass() != obj.getClass()) 79 return false; 80 TagKeyReference other = (TagKeyReference) obj; 81 if (key == null) { 82 if (other.key != null) 83 return false; 84 } else if (!key.equals(other.key)) 85 return false; 86 return true; 87 } 88 } 89 45 90 public static class IconReference { 46 91 47 92 public String iconName; … … 111 156 dirs.add("resource://images/styles/standard/"); 112 157 dirs.add("resource://images/styles/"); 113 158 } 114 159 115 160 return dirs; 116 161 } 117 162 … … 129 174 for (StyleSource source : styles.getStyleSources()) { 130 175 source.loadStyleSource(); 131 176 } 132 177 133 178 fireMapPaintSylesUpdated(); 134 179 } 135 180 … … 138 183 try { 139 184 in = new MirroredInputStream(entry.url); 140 185 InputStream zip = in.getZipEntry("xml", "style"); 141 if (zip != null) {186 if (zip != null) 142 187 return new XmlStyleSource(entry); 143 }144 188 zip = in.getZipEntry("mapcss", "style"); 145 if (zip != null) {189 if (zip != null) 146 190 return new MapCSSStyleSource(entry); 147 } 148 if (entry.url.toLowerCase().endsWith(".mapcss")) { 191 if (entry.url.toLowerCase().endsWith(".mapcss")) 149 192 return new MapCSSStyleSource(entry); 150 } 151 if (entry.url.toLowerCase().endsWith(".xml")) { 193 if (entry.url.toLowerCase().endsWith(".xml")) 152 194 return new XmlStyleSource(entry); 153 }else {195 else { 154 196 InputStreamReader reader = new InputStreamReader(in); 155 197 WHILE: while (true) { 156 198 int c = reader.read(); 157 199 switch (c) { 158 case -1:159 break WHILE;160 case ' ':161 case '\t':162 case '\n':163 case '\r':164 continue;165 case '<':166 return new XmlStyleSource(entry);167 default:168 return new MapCSSStyleSource(entry);200 case -1: 201 break WHILE; 202 case ' ': 203 case '\t': 204 case '\n': 205 case '\r': 206 continue; 207 case '<': 208 return new XmlStyleSource(entry); 209 default: 210 return new MapCSSStyleSource(entry); 169 211 } 170 212 } 171 213 System.err.println("Warning: Could not detect style type. Using default (xml)."); … … 271 313 int[] selSorted = Arrays.copyOf(sel, sel.length); 272 314 Arrays.sort(selSorted); 273 315 274 if (i < 0) { // Up316 if (i < 0) 275 317 return selSorted[0] >= -i; 276 }else277 if (i > 0) { // Down278 return selSorted[selSorted.length-1] <= styles.getStyleSources().size() - 1 - i;279 }else280 return true;318 else 319 if (i > 0) 320 return selSorted[selSorted.length-1] <= styles.getStyleSources().size() - 1 - i; 321 else 322 return true; 281 323 } 282 324 283 325 public static void toggleStyleActive(int... sel) { … … 319 361 } 320 362 321 363 protected static final CopyOnWriteArrayList<MapPaintSylesUpdateListener> listeners 322 = new CopyOnWriteArrayList<MapPaintSylesUpdateListener>();364 = new CopyOnWriteArrayList<MapPaintSylesUpdateListener>(); 323 365 324 366 public static void addMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) { 325 367 if (listener != null) { -
src/org/openstreetmap/josm/gui/mappaint/TextElement.java
8 8 9 9 import org.openstreetmap.josm.data.osm.OsmPrimitive; 10 10 import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.DeriveLabelFromNameTagsCompositionStrategy; 11 import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.StaticLabelCompositionStrategy; 11 12 import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.TagLookupCompositionStrategy; 13 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.TagKeyReference; 12 14 import org.openstreetmap.josm.tools.CheckParameterUtil; 13 15 import org.openstreetmap.josm.tools.Utils; 14 16 … … 80 82 * @return the label composition strategy 81 83 */ 82 84 protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade c, boolean defaultAnnotate){ 83 Keyword textKW = c.get("text", null, Keyword.class, true); 84 if (textKW == null) { 85 String textKey = c.get("text", null, String.class); 86 if (textKey == null) 87 return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null; 88 return new TagLookupCompositionStrategy(textKey); 89 } else if (textKW.val.equals("auto")) 90 return AUTO_LABEL_COMPOSITION_STRATEGY; 91 else 92 return new TagLookupCompositionStrategy(textKW.val); 85 /* 86 * If the cascade includes a TagKeyReference we will lookup the rendered label 87 * from a tag value. 88 */ 89 TagKeyReference tkr = c.get("text", null, TagKeyReference.class, true); 90 if (tkr != null) 91 return new TagLookupCompositionStrategy(tkr.key); 92 93 /* 94 * Check whether the label composition strategy is given by 95 * a keyword 96 */ 97 Keyword keyword = c.get("text", null, Keyword.class, true); 98 if (keyword != null){ 99 if (keyword.equals(Keyword.AUTO)) 100 return AUTO_LABEL_COMPOSITION_STRATEGY; 101 return null; // unsupported keyword 102 } 103 104 /* 105 * Do we have a static text label? 106 */ 107 String text = c.get("text", null, String.class, true); 108 if (text != null) 109 return new StaticLabelCompositionStrategy(text); 110 return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null; 93 111 } 94 112 95 113 /** … … 183 201 return false; 184 202 final TextElement other = (TextElement) obj; 185 203 return equal(labelCompositionStrategy, other.labelCompositionStrategy) && 186 equal(font, other.font) &&187 xOffset == other.xOffset &&188 yOffset == other.yOffset &&189 equal(color, other.color) &&190 equal(haloRadius, other.haloRadius) &&191 equal(haloColor, other.haloColor);204 equal(font, other.font) && 205 xOffset == other.xOffset && 206 yOffset == other.yOffset && 207 equal(color, other.color) && 208 equal(haloRadius, other.haloRadius) && 209 equal(haloColor, other.haloColor); 192 210 } 193 211 } -
src/org/openstreetmap/josm/gui/mappaint/mapcss/Expression.java
15 15 import org.openstreetmap.josm.actions.search.SearchCompiler; 16 16 import org.openstreetmap.josm.actions.search.SearchCompiler.Match; 17 17 import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError; 18 import org.openstreetmap.josm.data.osm.OsmPrimitive; 19 import org.openstreetmap.josm.data.osm.Relation; 18 20 import org.openstreetmap.josm.gui.mappaint.Cascade; 19 21 import org.openstreetmap.josm.gui.mappaint.Environment; 20 22 import org.openstreetmap.josm.tools.Utils; 21 23 22 24 public interface Expression { 23 24 25 public Object evaluate(Environment env); 25 26 26 27 public static class LiteralExpression implements Expression { … … 46 47 } 47 48 48 49 public static class FunctionExpression implements Expression { 50 //static Logger logger = Logger.getLogger(FunctionExpression.class.getName()); 51 49 52 String name; 50 53 List<Expression> args; 51 54 … … 72 75 public Float minus(float... args) { 73 76 if (args.length == 0) 74 77 return 0f; 75 if (args.length == 1) { // unary minus78 if (args.length == 1) 76 79 return -args[0]; 77 }78 80 float res = args[0]; 79 81 for (int i=1; i<args.length; ++i) { 80 82 res -= args[i]; … … 163 165 return env.osm.get(key); 164 166 } 165 167 168 public String tag(String key) { 169 return get_tag_value(key); 170 } 171 172 public String text(String value){ 173 return value; 174 } 175 176 /** 177 * <p>Replies the value of a tag with name <tt>key</tt> provided by one of the parent 178 * relations of the OSM object in the environment, or null, if no such tag exists.</p> 179 * 180 * @param key the tag key 181 * @return the tag value 182 */ 183 public String parent_tag(String key) { 184 for (Relation parent: OsmPrimitive.getFilteredList(env.osm.getReferrers(), Relation.class)) { 185 String value = parent.get(key); 186 if (value != null) return value; 187 } 188 return null; 189 } 190 166 191 public boolean has_tag_key(String key) { 167 192 return env.osm.hasKey(key); 168 193 } … … 285 310 throw new RuntimeException(ex); 286 311 } 287 312 for (Method m : allMethods) { 288 if (!m.getName().equals(name)) 313 if (!m.getName().equals(name)) { 289 314 continue; 315 } 290 316 Class<?>[] expectedParameterTypes = m.getParameterTypes(); 291 317 Object[] convertedArgs = new Object[expectedParameterTypes.length]; 292 318 … … 303 329 } 304 330 convertedArgs[0] = arrayArg; 305 331 } else { 306 if (args.size() != expectedParameterTypes.length) 332 if (args.size() != expectedParameterTypes.length) { 307 333 continue; 334 } 308 335 for (int i=0; i<args.size(); ++i) { 309 336 convertedArgs[i] = Cascade.convertTo(args.get(i).evaluate(env), expectedParameterTypes[i]); 310 337 if (convertedArgs[i] == null) -
src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java
4 4 import java.util.Arrays; 5 5 6 6 import org.openstreetmap.josm.gui.mappaint.Environment; 7 import org.openstreetmap.josm.gui.mappaint.Keyword; 8 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 7 9 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 8 10 9 11 abstract public class Instruction { … … 24 26 } 25 27 26 28 public static class AssignmentInstruction extends Instruction { 29 //static private final Logger logger = Logger.getLogger(AssignmentInstruction.class.getName()); 27 30 String key; 28 31 Object val; 29 32 30 33 public AssignmentInstruction(String key, Object val) { 31 34 this.key = key; 32 35 if (val instanceof Expression.LiteralExpression) { 33 this.val = ((Expression.LiteralExpression) val).evaluate(null); 36 if (key.equals("text")) { 37 /* Special case for declaration 'text: ...' 38 * 39 * - Treat the value 'auto' as keyword. 40 * - Treat any other literal value 'litval' as as reference to tag with key 'litval' 41 * 42 * - Accept function expressions as is. This allows for 43 * tag(a_tag_name) value of a tag 44 * text("a static text") a static text 45 * parent_tag(a_tag_name) value of a tag of a parent relation 46 */ 47 Object value = ((Expression.LiteralExpression) val).evaluate(null); 48 if (value != null){ 49 if (value.equals(Keyword.AUTO)) { 50 this.val = Keyword.AUTO; 51 } else { 52 this.val = new MapPaintStyles.TagKeyReference(value.toString()); 53 } 54 } 55 } else { 56 this.val = ((Expression.LiteralExpression) val).evaluate(null); 57 } 34 58 } else { 35 59 this.val = val; 36 60 } … … 38 62 39 63 @Override 40 64 public void execute(Environment env) { 41 Object value = (val instanceof Expression) ? ((Expression) val).evaluate(env) : val; 65 Object value = null; 66 if (val instanceof Expression) { 67 value = ((Expression) val).evaluate(env); 68 } else { 69 value = val; 70 } 42 71 if (key.equals("icon-image") || key.equals("fill-image")) { 43 72 if (value instanceof String) { 44 73 value = new IconReference((String) value, env.source);
