Ticket #18468: 18468-v2.patch

File 18468-v2.patch, 10.2 KB (added by simon04, 6 years ago)
  • src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

    commit 12c9c5ea7e92f812fcd0ed03f81f3a273f176646
    Author: Simon Legner <Simon.Legner@gmail.com>
    Date:   Sat Jan 18 19:12:55 2020 +0100
    
        fox #18468 - MapCSS: add support for text-rotation
    
    diff --git a/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java b/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java
    index c477314de..af1bc7929 100644
    a b  
    8787import org.openstreetmap.josm.tools.ImageProvider;
    8888import org.openstreetmap.josm.tools.JosmRuntimeException;
    8989import org.openstreetmap.josm.tools.Logging;
     90import org.openstreetmap.josm.tools.RotationAngle;
    9091import org.openstreetmap.josm.tools.ShapeClipper;
    9192import org.openstreetmap.josm.tools.Utils;
    9293import org.openstreetmap.josm.tools.bugreport.BugReport;
    public void drawBoxText(INode n, BoxTextElement bs) {  
    620621        FontRenderContext frc = g.getFontRenderContext();
    621622        Rectangle2D bounds = text.font.getStringBounds(s, frc);
    622623
    623         double x = Math.round(p.getInViewX()) + bs.xOffset + bounds.getCenterX();
    624         double y = Math.round(p.getInViewY()) + bs.yOffset + bounds.getCenterY();
     624        double x = p.getInViewX() + bs.xOffset;
     625        double y = p.getInViewY() + bs.yOffset;
    625626        /**
    626627         *
    627628         *       left-above __center-above___ right-above
    public void drawBoxText(INode n, BoxTextElement bs) {  
    660661            } else throw new AssertionError();
    661662        }
    662663
    663         displayText(n, text, s, bounds, new MapViewPositionAndRotation(mapState.getForView(x, y), 0));
     664        final MapViewPoint viewPoint = mapState.getForView(x, y);
     665        final AffineTransform at = new AffineTransform();
     666        at.setToTranslation(
     667                Math.round(viewPoint.getInViewX()),
     668                Math.round(viewPoint.getInViewY()));
     669        if (!RotationAngle.NO_ROTATION.equals(text.rotationAngle)) {
     670            at.rotate(text.rotationAngle.getRotationAngle(n));
     671        }
     672        displayText(n, text, s, at);
    664673        g.setFont(defaultFont);
    665674    }
    666675
    private void displayText(IPrimitive osm, TextLabel text, String name, Rectangle2  
    11881197        AffineTransform at = new AffineTransform();
    11891198        if (Math.abs(center.getRotation()) < .01) {
    11901199            // Explicitly no rotation: move to full pixels.
    1191             at.setToTranslation(Math.round(center.getPoint().getInViewX() - nb.getCenterX()),
     1200            at.setToTranslation(
     1201                    Math.round(center.getPoint().getInViewX() - nb.getCenterX()),
    11921202                    Math.round(center.getPoint().getInViewY() - nb.getCenterY()));
    11931203        } else {
    1194             at.setToTranslation(center.getPoint().getInViewX(), center.getPoint().getInViewY());
     1204            at.setToTranslation(
     1205                    center.getPoint().getInViewX(),
     1206                    center.getPoint().getInViewY());
    11951207            at.rotate(center.getRotation());
    11961208            at.translate(-nb.getCenterX(), -nb.getCenterY());
    11971209        }
     1210        displayText(osm, text, name, at);
     1211    }
     1212
     1213    private void displayText(IPrimitive osm, TextLabel text, String name, AffineTransform at) {
    11981214        displayText(() -> {
    11991215            AffineTransform defaultTransform = g.getTransform();
    12001216            g.transform(at);
  • src/org/openstreetmap/josm/gui/mappaint/StyleKeys.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/StyleKeys.java b/src/org/openstreetmap/josm/gui/mappaint/StyleKeys.java
    index 1b45272cb..53a216257 100644
    a b  
    9090     * MapCSS icon-rotation property key
    9191     */
    9292    String ICON_ROTATION = "icon-rotation";
     93    /**
     94     * MapCSS text-rotation property key
     95     */
     96    String TEXT_ROTATION = "text-rotation";
    9397    /**
    9498     * MapCSS icon-width property key
    9599     */
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java
    index a0a184537..d39fa21b3 100644
    a b static NodeElement create(Environment env, float defaultMajorZindex, boolean all  
    8686     * @since 11670
    8787     */
    8888    public static RotationAngle createRotationAngle(Environment env) {
     89        return createRotationAngle(env, ICON_ROTATION);
     90    }
     91
     92    /**
     93     * Reads the text-rotation property and creates a rotation angle from it.
     94     * @param env The environment
     95     * @return The angle
     96     * @since xxx
     97     */
     98    public static RotationAngle createTextRotationAngle(Environment env) {
     99        return createRotationAngle(env, TEXT_ROTATION);
     100    }
     101
     102    private static RotationAngle createRotationAngle(Environment env, String key) {
    89103        Cascade c = env.mc.getCascade(env.layer);
    90104
    91105        RotationAngle rotationAngle = RotationAngle.NO_ROTATION;
    92         final Float angle = c.get(ICON_ROTATION, null, Float.class, true);
     106        final Float angle = c.get(key, null, Float.class, true);
    93107        if (angle != null) {
    94108            rotationAngle = RotationAngle.buildStaticRotation(angle);
    95109        } else {
    96             final Keyword rotationKW = c.get(ICON_ROTATION, null, Keyword.class);
     110            final Keyword rotationKW = c.get(key, null, Keyword.class);
    97111            if (rotationKW != null) {
    98112                if ("way".equals(rotationKW.val)) {
    99113                    rotationAngle = RotationAngle.buildWayDirectionRotation();
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java
    index 829385f8a..8e84b3555 100644
    a b  
    1717import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.StaticLabelCompositionStrategy;
    1818import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.TagLookupCompositionStrategy;
    1919import org.openstreetmap.josm.tools.CheckParameterUtil;
     20import org.openstreetmap.josm.tools.RotationAngle;
    2021import org.openstreetmap.josm.tools.Utils;
    2122
    2223/**
     
    3839     * the font to be used when rendering
    3940     */
    4041    public Font font;
     42    /**
     43     * The rotation angle to be used when rendering
     44     */
     45    public RotationAngle rotationAngle;
    4146    /**
    4247     * The color to draw the text in, includes alpha.
    4348     */
     
    5762     * @param strategy the strategy indicating how the text is composed for a specific {@link OsmPrimitive} to be rendered.
    5863     * If null, no label is rendered.
    5964     * @param font the font to be used. Must not be null.
     65     * @param rotationAngle the rotation angle to be used. Must not be null.
    6066     * @param color the color to be used. Must not be null
    6167     * @param haloRadius halo radius
    6268     * @param haloColor halo color
    6369     */
    64     protected TextLabel(LabelCompositionStrategy strategy, Font font, Color color, Float haloRadius,
    65             Color haloColor) {
     70    protected TextLabel(LabelCompositionStrategy strategy, Font font, RotationAngle rotationAngle, Color color, Float haloRadius,
     71                        Color haloColor) {
    6672        this.labelCompositionStrategy = strategy;
    6773        this.font = Objects.requireNonNull(font, "font");
     74        this.rotationAngle = Objects.requireNonNull(rotationAngle, "rotationAngle");
    6875        this.color = Objects.requireNonNull(color, "color");
    6976        this.haloRadius = haloRadius;
    7077        this.haloColor = haloColor;
    protected TextLabel(LabelCompositionStrategy strategy, Font font, Color color, F  
    7885    public TextLabel(TextLabel other) {
    7986        this.labelCompositionStrategy = other.labelCompositionStrategy;
    8087        this.font = other.font;
     88        this.rotationAngle = other.rotationAngle;
    8189        this.color = other.color;
    8290        this.haloColor = other.haloColor;
    8391        this.haloRadius = other.haloRadius;
    public static TextLabel create(Environment env, Color defaultTextColor, boolean  
    136144        String s = strategy.compose(env.osm);
    137145        if (s == null) return null;
    138146        Font font = StyleElement.getFont(c, s);
     147        RotationAngle rotationAngle = NodeElement.createTextRotationAngle(env);
    139148
    140149        Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class);
    141150        float alpha = c.get(TEXT_OPACITY, 1f, Float.class);
    public static TextLabel create(Environment env, Color defaultTextColor, boolean  
    152161            haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor);
    153162        }
    154163
    155         return new TextLabel(strategy, font, color, haloRadius, haloColor);
     164        return new TextLabel(strategy, font, rotationAngle, color, haloRadius, haloColor);
    156165    }
    157166
    158167    /**
    protected String toStringImpl() {  
    198207        StringBuilder sb = new StringBuilder(96);
    199208        sb.append("labelCompositionStrategy=").append(labelCompositionStrategy)
    200209          .append(" font=").append(font)
    201           .append(" color=").append(Utils.toString(color));
     210          .append(" color=").append(Utils.toString(color))
     211          .append(" rotationAngle=").append(rotationAngle);
    202212        if (haloRadius != null) {
    203213            sb.append(" haloRadius=").append(haloRadius)
    204214              .append(" haloColor=").append(haloColor);
    protected String toStringImpl() {  
    208218
    209219    @Override
    210220    public int hashCode() {
    211         return Objects.hash(labelCompositionStrategy, font, color, haloRadius, haloColor);
     221        return Objects.hash(labelCompositionStrategy, font, rotationAngle, color, haloRadius, haloColor);
    212222    }
    213223
    214224    @Override
    public boolean equals(Object obj) {  
    218228        TextLabel textLabel = (TextLabel) obj;
    219229        return Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) &&
    220230                Objects.equals(font, textLabel.font) &&
     231                Objects.equals(rotationAngle, textLabel.rotationAngle) &&
    221232                Objects.equals(color, textLabel.color) &&
    222233                Objects.equals(haloRadius, textLabel.haloRadius) &&
    223234                Objects.equals(haloColor, textLabel.haloColor);