Ignore:
Timestamp:
2015-04-25T14:00:05+02:00 (9 years ago)
Author:
bastiK
Message:

see #10217 - move unit handling to parser
adds support for negative angles
new: length units px,cm,mm,in,q,pc,pt as in CSS (immediately converted to px)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj

    r8256 r8260  
    2020import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
    2121import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory;
     22import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory.NullExpression;
    2223import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
    2324import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression;
     
    184185|   < CARET: "^" >
    185186|   < FULLSTOP: "." >
     187|   < DEG: "°" >
    186188|   < ELEMENT_OF: "∈" >
    187189|   < CROSSING: "⧉" >
     
    10281030        fn=function() { return fn; }
    10291031    |
    1030         lit=literal() { return new LiteralExpression(lit); }
     1032        lit=literal()
     1033        {
     1034            if (lit == null)
     1035                return NullExpression.INSTANCE;
     1036            return new LiteralExpression(lit);
     1037        }
    10311038    |
    10321039        <LPAR> w() nested=expression() <RPAR> { return nested; }
     
    10541061    String val, pref;
    10551062    Token t;
    1056     float f;
     1063    Float f;
    10571064}
    10581065{
     
    10671074        <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
    10681075    |
     1076        LOOKAHEAD(2)
     1077        f=ufloat_unit() { return f; }
     1078    |
    10691079        f=ufloat() { return f; }
    10701080    |
    10711081        t=<HEXCOLOR> { return ColorHelper.html2color(t.image); }
     1082}
     1083
     1084/**
     1085 * Number followed by a unit.
     1086 *
     1087 * Returns angles in radians and lengths in pixels.
     1088 */
     1089Float ufloat_unit() :
     1090{
     1091    float f;
     1092    String u;
     1093}
     1094{
     1095    f=ufloat() ( u=ident() | <DEG> { u = "°"; } )
     1096    {
     1097        Double m = unit_factor(u);
     1098        if (m == null)
     1099            return null;
     1100        return (float) (f * m);
     1101    }
     1102}
     1103
     1104JAVACODE
     1105private Double unit_factor(String unit) {
     1106    switch (unit) {
     1107        case "deg":
     1108        case "°": return Math.PI / 180;
     1109        case "rad": return 1.;
     1110        case "grad": return Math.PI / 200;
     1111        case "turn": return 2 * Math.PI;
     1112        case "px": return 1.;
     1113        case "cm": return 96/2.54;
     1114        case "mm": return 9.6/2.54;
     1115        case "in": return 96.;
     1116        case "q": return 2.4/2.54;
     1117        case "pc": return 16.;
     1118        case "pt": return 96./72;
     1119        default: return null;
     1120    }
    10721121}
    10731122
Note: See TracChangeset for help on using the changeset viewer.