Ticket #6150: mapcss-text-instruction.patch

File mapcss-text-instruction.patch, 8.0 KB (added by Gubaer, 15 years ago)

patch

  • src/org/openstreetmap/josm/gui/mappaint/TextElement.java

     
    88
    99import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1010import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.DeriveLabelFromNameTagsCompositionStrategy;
    11 import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.TagLookupCompositionStrategy;
     11import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.StaticLabelCompositionStrategy;
    1212import org.openstreetmap.josm.tools.CheckParameterUtil;
    1313import org.openstreetmap.josm.tools.Utils;
    1414
     
    8080     * @return the label composition strategy
    8181     */
    8282    protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade c, boolean defaultAnnotate){
     83
    8384        Keyword textKW = c.get("text", null, Keyword.class, true);
    8485        if (textKW == null) {
    8586            String textKey = c.get("text", null, String.class);
    86             if (textKey == null) 
     87            if (textKey == null)
    8788                return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null;
    88             return new TagLookupCompositionStrategy(textKey);
     89            return new StaticLabelCompositionStrategy(textKey);
    8990        } else if (textKW.val.equals("auto"))
    9091            return AUTO_LABEL_COMPOSITION_STRATEGY;
    9192        else
    92             return new TagLookupCompositionStrategy(textKW.val);
     93            return new StaticLabelCompositionStrategy(textKW.val);
    9394    }
    9495
    9596    /**
     
    183184            return false;
    184185        final TextElement other = (TextElement) obj;
    185186        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);
    192193    }
    193194}
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/Expression.java

     
    1515import org.openstreetmap.josm.actions.search.SearchCompiler;
    1616import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
    1717import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError;
     18import org.openstreetmap.josm.data.osm.OsmPrimitive;
     19import org.openstreetmap.josm.data.osm.Relation;
    1820import org.openstreetmap.josm.gui.mappaint.Cascade;
    1921import org.openstreetmap.josm.gui.mappaint.Environment;
    2022import org.openstreetmap.josm.tools.Utils;
    2123
    2224public interface Expression {
    23 
    2425    public Object evaluate(Environment env);
    2526
    2627    public static class LiteralExpression implements Expression {
     
    4647    }
    4748
    4849    public static class FunctionExpression implements Expression {
     50        //static Logger logger = Logger.getLogger(FunctionExpression.class.getName());
     51
    4952        String name;
    5053        List<Expression> args;
    5154
     
    7275            public Float minus(float... args) {
    7376                if (args.length == 0)
    7477                    return 0f;
    75                 if (args.length == 1) { // unary minus
     78                if (args.length == 1)
    7679                    return -args[0];
    77                 }
    7880                float res = args[0];
    7981                for (int i=1; i<args.length; ++i) {
    8082                    res -= args[i];
     
    163165                return env.osm.get(key);
    164166            }
    165167
     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
    166191            public boolean has_tag_key(String key) {
    167192                return env.osm.hasKey(key);
    168193            }
     
    285310                throw  new RuntimeException(ex);
    286311            }
    287312            for (Method m : allMethods) {
    288                 if (!m.getName().equals(name))
     313                if (!m.getName().equals(name)) {
    289314                    continue;
     315                }
    290316                Class<?>[] expectedParameterTypes = m.getParameterTypes();
    291317                Object[] convertedArgs = new Object[expectedParameterTypes.length];
    292318
     
    303329                    }
    304330                    convertedArgs[0] = arrayArg;
    305331                } else {
    306                     if (args.size() != expectedParameterTypes.length)
     332                    if (args.size() != expectedParameterTypes.length) {
    307333                        continue;
     334                    }
    308335                    for (int i=0; i<args.size(); ++i) {
    309336                        convertedArgs[i] = Cascade.convertTo(args.get(i).evaluate(env), expectedParameterTypes[i]);
    310337                        if (convertedArgs[i] == null)
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java

     
    22package org.openstreetmap.josm.gui.mappaint.mapcss;
    33
    44import java.util.Arrays;
     5import java.util.Collections;
    56
    67import org.openstreetmap.josm.gui.mappaint.Environment;
    78import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
     
    3031        public AssignmentInstruction(String key, Object val) {
    3132            this.key = key;
    3233            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                }
    3454            } else {
    3555                this.val = val;
    3656            }