Changeset 8260 in josm


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)

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
4 edited

Legend:

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

    r8238 r8260  
    120120            symbol = createSymbol(env);
    121121        }
    122         final String rotationString = c.get("icon-rotation", null, String.class);
    123122        RotationAngle rotationAngle = null;
    124         if ("way".equalsIgnoreCase(rotationString)) {
    125             rotationAngle = RotationAngle.buildWayDirectionRotation();
    126         } else if (rotationString != null) {
    127             try {
    128                 rotationAngle = RotationAngle.buildStaticRotation(rotationString);
    129             } catch (RuntimeException ignore) {
     123        final Float angle = c.get(ICON_ROTATION, null, Float.class, true);
     124        if (angle != null) {
     125            rotationAngle = RotationAngle.buildStaticRotation(angle);
     126        } else {
     127            final Keyword rotationKW = c.get(ICON_ROTATION, null, Keyword.class);
     128            if (rotationKW != null) {
     129                if ("way".equals(rotationKW.val)) {
     130                    rotationAngle = RotationAngle.buildWayDirectionRotation();
     131                } else {
     132                    try {
     133                        rotationAngle = RotationAngle.buildStaticRotation(rotationKW.val);
     134                    } catch (IllegalArgumentException ignore) {
     135                    }
     136                }
    130137            }
    131138        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleKeys.java

    r8087 r8260  
    2121    String ICON_OFFSET_Y = "icon-offset-y";
    2222    String ICON_OPACITY = "icon-opacity";
     23    String ICON_ROTATION = "icon-rotation";
    2324    String ICON_WIDTH = "icon-width";
    2425    String LINECAP = "linecap";
  • 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
  • trunk/src/org/openstreetmap/josm/gui/util/RotationAngle.java

    r8251 r8260  
    4141     */
    4242    public static RotationAngle buildStaticRotation(final String string) {
    43         final Pattern radiansPattern = Pattern.compile("(\\d+\\.?\\d*)\\s*(rad)?");
    44         final Pattern degreesPattern = Pattern.compile("(\\d+\\.?\\d*)\\s*(deg|°)");
    45         final Pattern gradianPattern = Pattern.compile("(\\d+\\.?\\d*)\\s*(grad)");
    46         final Pattern turnPattern = Pattern.compile("(\\d+\\.?\\d*)\\s*(turn)");
    4743        final double value;
    48         Matcher matcher;
    49         if ((matcher = radiansPattern.matcher(string)).matches()) {
    50             value = Double.parseDouble(matcher.group(1));
    51         } else if ((matcher = degreesPattern.matcher(string)).matches()) {
    52             value = Math.toRadians(Double.parseDouble(matcher.group(1)));
    53         } else if ((matcher = gradianPattern.matcher(string)).matches()) {
    54             value = Double.parseDouble(matcher.group(1)) / 200 * Math.PI;
    55         } else if ((matcher = turnPattern.matcher(string)).matches()) {
    56             value = Double.parseDouble(matcher.group(1)) * 2 * Math.PI;
    57         } else {
    58             try {
    59                 value = parseCardinalRotation(string);
    60             } catch (IllegalArgumentException ignore) {
    61                 throw new IllegalArgumentException("Invalid string: " + string);
    62             }
     44        try {
     45            value = parseCardinalRotation(string);
     46        } catch (IllegalArgumentException ignore) {
     47            throw new IllegalArgumentException("Invalid string: " + string);
    6348        }
    6449        return buildStaticRotation(value);
Note: See TracChangeset for help on using the changeset viewer.