Ticket #6150: mapcss-text-instruction.patch
| File mapcss-text-instruction.patch, 8.0 KB (added by , 15 years ago) |
|---|
-
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. TagLookupCompositionStrategy;11 import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.StaticLabelCompositionStrategy; 12 12 import org.openstreetmap.josm.tools.CheckParameterUtil; 13 13 import org.openstreetmap.josm.tools.Utils; 14 14 … … 80 80 * @return the label composition strategy 81 81 */ 82 82 protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade c, boolean defaultAnnotate){ 83 83 84 Keyword textKW = c.get("text", null, Keyword.class, true); 84 85 if (textKW == null) { 85 86 String textKey = c.get("text", null, String.class); 86 if (textKey == null) 87 if (textKey == null) 87 88 return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null; 88 return new TagLookupCompositionStrategy(textKey);89 return new StaticLabelCompositionStrategy(textKey); 89 90 } else if (textKW.val.equals("auto")) 90 91 return AUTO_LABEL_COMPOSITION_STRATEGY; 91 92 else 92 return new TagLookupCompositionStrategy(textKW.val);93 return new StaticLabelCompositionStrategy(textKW.val); 93 94 } 94 95 95 96 /** … … 183 184 return false; 184 185 final TextElement other = (TextElement) obj; 185 186 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);187 equal(font, other.font) && 188 xOffset == other.xOffset && 189 yOffset == other.yOffset && 190 equal(color, other.color) && 191 equal(haloRadius, other.haloRadius) && 192 equal(haloColor, other.haloColor); 192 193 } 193 194 } -
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
2 2 package org.openstreetmap.josm.gui.mappaint.mapcss; 3 3 4 4 import java.util.Arrays; 5 import java.util.Collections; 5 6 6 7 import org.openstreetmap.josm.gui.mappaint.Environment; 7 8 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; … … 30 31 public AssignmentInstruction(String key, Object val) { 31 32 this.key = key; 32 33 if (val instanceof Expression.LiteralExpression) { 33 this.val = ((Expression.LiteralExpression) val).evaluate(null); 34 if (key.equals("text")) { 35 /* Special case for declaration 'text: ...' 36 * 37 * - Treat the value 'auto' as keyword. 38 * - Treat any other literal value 'litval' as syntactic shortcut for the function expression 39 * 'tag(litval)' 40 * - Accept function expressions as is. This allows for 41 * tag(a_tag_name) value of a tag 42 * text("a static text") a static text 43 * parent_tag(a_tag_name) value of a tag of a parent relation 44 */ 45 Object value = ((Expression.LiteralExpression) val).evaluate(null); 46 if (value != null && value.equals("auto")) { 47 this.val = "auto"; 48 } else { 49 this.val = new Expression.FunctionExpression("tag", Collections.singletonList((Expression)val)); 50 } 51 } else { 52 this.val = ((Expression.LiteralExpression) val).evaluate(null); 53 } 34 54 } else { 35 55 this.val = val; 36 56 }
