Changeset 6896 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2014-03-02T16:23:58+01:00 (11 years ago)
Author:
bastiK
Message:

mapcss: add support for @media expressions
includes part of the style sheet under certain condition
e.g. min josm version.

There is a major limitation: the intire file is still parsed.
So if new syntax is introduced, it will still generate
a parsing error for old josm version even if the new code
is wrapped in an @media section.

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r6310 r6896  
    184184        }
    185185
    186         int alpha = 255;
     186        int alpha = color.getAlpha();
    187187        Integer pAlpha = Utils.color_float2int(c.get(type.prefix + OPACITY, null, Float.class));
    188188        if (pAlpha != null) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r6889 r6896  
    2121import org.openstreetmap.josm.tools.Utils;
    2222
     23/**
     24 * A mappaint style (abstract class).
     25 *
     26 * Handles everything from parsing the style definition to application
     27 * of the style to an osm primitive.
     28 */
    2329public abstract class StyleSource extends SourceEntry {
    2430
     
    4450    }
    4551
     52    /**
     53     * Apply style to osm primitive.
     54     *
     55     * Adds properties to a MultiCascade. All active {@link StyleSource}s add
     56     * their properties on after the other. At a later stage, concrete painting
     57     * primitives (lines, icons, text, ...) are derived from the MultiCascade.
     58     * @param mc the current MultiCascade, empty for the first StyleSource
     59     * @param osm the primitive
     60     * @param scale the map scale
     61     * @param multipolyOuterWay support for a very old multipolygon tagging style
     62     * where you add the tags both to the outer and the inner way.
     63     * However, independent inner way style is also possible.
     64     * @param pretendWayIsClosed For styles that require the way to be closed,
     65     * we pretend it is. This is useful for generating area styles from the (segmented)
     66     * outer ways of a multipolygon.
     67     */
    4668    public abstract void apply(MultiCascade mc, OsmPrimitive osm, double scale, OsmPrimitive multipolyOuterWay, boolean pretendWayIsClosed);
    4769
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

    r6883 r6896  
    731731            return result;
    732732        }
     733
     734        @Override
     735        public String toString() {
     736            StringBuilder b = new StringBuilder("ParameterFunction~");
     737            b.append(m.getName()).append("(");
     738            for (int i = 0; i < args.size(); ++i) {
     739                if (i > 0) b.append(",");
     740                b.append(expectedParameterTypes[i]);
     741                b.append(" ").append(args.get(i));
     742            }
     743            b.append(')');
     744            return b.toString();
     745        }
     746
    733747    }
    734748
     
    780794            return result;
    781795        }
     796        @Override
     797        public String toString() {
     798            StringBuilder b = new StringBuilder("ArrayFunction~");
     799            b.append(m.getName()).append("(");
     800            for (int i = 0; i < args.size(); ++i) {
     801                if (i > 0) b.append(",");
     802                b.append(arrayComponentType);
     803                b.append(" ").append(args.get(i));
     804            }
     805            b.append(')');
     806            return b.toString();
     807        }
     808
    782809    }
    783810
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java

    r6883 r6896  
    8585        @Override
    8686        public String toString() {
    87             return key + ':' + (val instanceof float[] ? Arrays.toString((float[]) val) : val) + ';';
     87            return key + ": " + (val instanceof float[] ? Arrays.toString((float[]) val) : val instanceof String ? "String<"+val+">" : val) + ';';
    8888        }
    8989    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj

    r6830 r6896  
    4141
    4242<DEFAULT>
     43TOKEN [IGNORE_CASE]:
     44{
     45 /* Special keywords in some contexts, ordinary identifiers in other contexts.
     46    Don't use the javacc token contexts (except DEFAULT and COMMENT) because
     47    they complicate error handling. Instead simply add a parsing rule <code>ident()</code>
     48    that parses the IDENT token and in addition all the keyword tokens. */
     49    < AND: "and" >
     50|   < SET: "set" >
     51|   < NOT: "not" >
     52|   < MEDIA: "@media" >
     53}
     54
     55<DEFAULT>
    4356TOKEN:
    4457{
    45     < SET: ("set" | "SET") >
    46 |   < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
     58    < IDENT: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","-","0"-"9"] )* >
    4759|   < UINT: ["1"-"9"] ( ["0"-"9"] )* >
    4860|   < UFLOAT: ( ["0"-"9"] )+ ( "." ( ["0"-"9"] )+ )? >
     
    122134 * 'val' can be a literal, or an expression like "prop(width, default) + 0.8".
    123135 *
     136 * @media { ... } queries are supported, following http://www.w3.org/TR/css3-mediaqueries/#syntax
     137 *
     138 *                               media_query
     139 *         ___________________________|_______________________________
     140 *        |                                                           |
     141 * @media all and (min-josm-version: 7789) and (max-josm-version: 7790), all and (user-agent: xyz) { ... }
     142 *                |______________________|
     143 *                          |
     144 *                    media_expression
    124145 */
    125146
     
    166187}
    167188
     189String ident():
     190{
     191    Token t;
     192    String s;
     193}
     194{
     195    ( t=<IDENT> | t=<SET> | t=<NOT> | t=<AND> ) { return t.image; }
     196}
     197
    168198String string_or_ident() :
    169199{
     
    172202}
    173203{
    174     t=<IDENT> { return t.image; } | s=string() { return s; }
     204    ( s=ident() | s=string() ) { return s; }
    175205}
    176206
     
    229259{
    230260    MapCSSRule r;
    231     Token com = null;
    232261}
    233262{
    234263    { this.sheet = sheet; }
    235264    w()
     265    rules(true) ( media() w() rules(true) )*
     266    <EOF>
     267}
     268
     269void media():
     270{
     271    boolean pass = false;
     272    boolean q;
     273    boolean empty = true;
     274}
     275{
     276    <MEDIA> w()
     277    ( q=media_query() { pass = pass || q; empty = false; }
     278        ( <COMMA> w() q=media_query() { pass = pass || q; } )*
     279    )?
     280    <LBRACE> w()
     281    rules(empty || pass)
     282    <RBRACE>
     283}
     284
     285boolean media_query():
     286{
     287    Token t;
     288    String mediatype = "all";
     289    boolean pass = true;
     290    boolean invert = false;
     291    boolean e;
     292}
     293{
     294    ( <NOT> { invert = true; } w() )?
     295    (
     296            t=<IDENT> { mediatype = t.image.toLowerCase(); } w()
     297            ( <AND> w() e=media_expression() { pass = pass && e; } w() )*
     298        |
     299            e=media_expression() { pass = pass && e; } w()
     300            ( <AND> w() e=media_expression() { pass = pass && e; } w() )*
     301    )
     302    {
     303        if (!"all".equals(mediatype)) {
     304            pass = false;
     305        }
     306        return invert ? (!pass) : pass;
     307    }
     308}
     309
     310boolean media_expression():
     311{
     312    Token t;
     313    String feature;
     314    Object val = null;
     315}
     316{
     317    <LPAR> w() t=<IDENT> { feature = t.image; } w() ( <COLON> w() val=literal() )? <RPAR>
     318    { return this.sheet.evalMediaExpression(feature, val); }
     319}
     320
     321void rules(boolean add):
     322{
     323    MapCSSRule r;
     324}
     325{
    236326    (
    237327        try {
    238                 r=rule() { if (r != null) { sheet.rules.add(r); } } w()
     328            r=rule() { if (add && r != null) { sheet.rules.add(r); } } w()
    239329        } catch (MapCSSException mex) {
    240330            error_skipto(RBRACE, mex);
     
    245335        }
    246336    )*
    247     <EOF>
    248337}
    249338
     
    345434String tag_key() :
    346435{
    347     String s;
     436    String s, s2;
    348437    Token t;
    349438}
     
    351440        s=string() { return s; }
    352441    |
    353         t=<IDENT> { s = t.image; } ( <COLON> t=<IDENT> { s += ':' + t.image; } )* { return s; }
     442        s=ident() ( <COLON> s2=ident() { s += ':' + s2; } )* { return s; }
    354443}
    355444
     
    435524Condition class_or_pseudoclass(Context context) :
    436525{
    437     Token t;
     526    String s;
    438527    boolean not = false;
    439528    boolean pseudo;
     
    446535        <COLON> { pseudo = true; }
    447536    )
    448     t=<IDENT>
     537    s=ident()
    449538    { return pseudo
    450         ? Condition.createPseudoClassCondition(t.image, not, context)
    451         : Condition.createClassCondition(t.image, not, context); }
     539        ? Condition.createPseudoClassCondition(s, not, context)
     540        : Condition.createClassCondition(s, not, context); }
    452541}
    453542
    454543String subpart() :
    455544{
    456     Token t;
     545    String s;
    457546}
    458547{
    459548    <DCOLON>
    460     ( t=<IDENT> | t=<STAR> )
    461     { return t.image; }
     549    ( s=ident() { return s; } | <STAR> { return "*"; } )
    462550}
    463551
     
    573661}
    574662{
    575     tmp=<IDENT> { name = tmp.image; } w()
     663    name=ident() w()
    576664    <LPAR> w()
    577665    (
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r6868 r6896  
    1616
    1717import org.openstreetmap.josm.Main;
     18import org.openstreetmap.josm.data.Version;
    1819import org.openstreetmap.josm.data.osm.Node;
    1920import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    213214    }
    214215
     216    public boolean evalMediaExpression(String feature, Object val) {
     217        if ("user-agent".equals(feature)) {
     218            String s = Cascade.convertTo(val, String.class);
     219            if ("josm".equals(s)) return true;
     220        }
     221        if ("min-josm-version".equals(feature)) {
     222            Float v = Cascade.convertTo(val, Float.class);
     223            if (v != null) return Math.round(v) <= Version.getInstance().getVersion();
     224        }
     225        if ("max-josm-version".equals(feature)) {
     226            Float v = Cascade.convertTo(val, Float.class);
     227            if (v != null) return Math.round(v) >= Version.getInstance().getVersion();
     228        }
     229        return false;
     230    }
     231
    215232    @Override
    216233    public String toString() {
Note: See TracChangeset for help on using the changeset viewer.