Changeset 16253 in josm for trunk/src/org


Ignore:
Timestamp:
2020-04-11T09:55:52+02:00 (5 years ago)
Author:
simon04
Message:

fix #18468 - MapCSS: add support for text-rotation

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

    r16208 r16253  
    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;
     
    620621        Rectangle2D bounds = text.font.getStringBounds(s, frc);
    621622
    622         double x = Math.round(p.getInViewX()) + bs.xOffset + bounds.getCenterX();
    623         double y = Math.round(p.getInViewY()) + bs.yOffset + bounds.getCenterY();
     623        double x = p.getInViewX() + bs.xOffset;
     624        double y = p.getInViewY() + bs.yOffset;
    624625        /**
    625626         *
     
    660661        }
    661662
    662         displayText(n, text, s, bounds, new MapViewPositionAndRotation(mapState.getForView(x, y), 0));
     663        final MapViewPoint viewPoint = mapState.getForView(x, y);
     664        final AffineTransform at = new AffineTransform();
     665        at.setToTranslation(
     666                Math.round(viewPoint.getInViewX()),
     667                Math.round(viewPoint.getInViewY()));
     668        if (!RotationAngle.NO_ROTATION.equals(text.rotationAngle)) {
     669            at.rotate(text.rotationAngle.getRotationAngle(n));
     670        }
     671        displayText(n, text, s, at);
    663672        g.setFont(defaultFont);
    664673    }
     
    11881197        if (Math.abs(center.getRotation()) < .01) {
    11891198            // Explicitly no rotation: move to full pixels.
    1190             at.setToTranslation(Math.round(center.getPoint().getInViewX() - nb.getCenterX()),
     1199            at.setToTranslation(
     1200                    Math.round(center.getPoint().getInViewX() - nb.getCenterX()),
    11911201                    Math.round(center.getPoint().getInViewY() - nb.getCenterY()));
    11921202        } else {
    1193             at.setToTranslation(center.getPoint().getInViewX(), center.getPoint().getInViewY());
     1203            at.setToTranslation(
     1204                    center.getPoint().getInViewX(),
     1205                    center.getPoint().getInViewY());
    11941206            at.rotate(center.getRotation());
    11951207            at.translate(-nb.getCenterX(), -nb.getCenterY());
    11961208        }
     1209        displayText(osm, text, name, at);
     1210    }
     1211
     1212    private void displayText(IPrimitive osm, TextLabel text, String name, AffineTransform at) {
    11971213        displayText(() -> {
    11981214            AffineTransform defaultTransform = g.getTransform();
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleKeys.java

    r12381 r16253  
    9292    String ICON_ROTATION = "icon-rotation";
    9393    /**
     94     * MapCSS text-rotation property key
     95     */
     96    String TEXT_ROTATION = "text-rotation";
     97    /**
    9498     * MapCSS icon-width property key
    9599     */
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java

    r16252 r16253  
    8888     */
    8989    public static RotationAngle createRotationAngle(Environment env) {
     90        return createRotationAngle(env, ICON_ROTATION);
     91    }
     92
     93    /**
     94     * Reads the text-rotation property and creates a rotation angle from it.
     95     * @param env The environment
     96     * @return The angle
     97     * @since 16253
     98     */
     99    public static RotationAngle createTextRotationAngle(Environment env) {
     100        return createRotationAngle(env, TEXT_ROTATION);
     101    }
     102
     103    private static RotationAngle createRotationAngle(Environment env, String key) {
    90104        Cascade c = env.mc.getCascade(env.layer);
    91105
    92106        RotationAngle rotationAngle = RotationAngle.NO_ROTATION;
    93         final Float angle = c.get(ICON_ROTATION, null, Float.class, true);
     107        final Float angle = c.get(key, null, Float.class, true);
    94108        if (angle != null) {
    95109            rotationAngle = RotationAngle.buildStaticRotation(angle);
    96110        } else {
    97             final Keyword rotationKW = c.get(ICON_ROTATION, null, Keyword.class);
     111            final Keyword rotationKW = c.get(key, null, Keyword.class);
    98112            if (rotationKW != null) {
    99113                if ("way".equals(rotationKW.val)) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java

    r16252 r16253  
    1919import org.openstreetmap.josm.tools.CheckParameterUtil;
    2020import org.openstreetmap.josm.tools.ColorHelper;
     21import org.openstreetmap.josm.tools.RotationAngle;
    2122
    2223/**
     
    4041    public Font font;
    4142    /**
     43     * The rotation angle to be used when rendering
     44     */
     45    public RotationAngle rotationAngle;
     46    /**
    4247     * The color to draw the text in, includes alpha.
    4348     */
     
    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,
     71                        Color color, Float haloRadius, 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;
     
    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;
     
    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);
     
    153162        }
    154163
    155         return new TextLabel(strategy, font, color, haloRadius, haloColor);
     164        return new TextLabel(strategy, font, rotationAngle, color, haloRadius, haloColor);
    156165    }
    157166
     
    199208        sb.append("labelCompositionStrategy=").append(labelCompositionStrategy)
    200209          .append(" font=").append(font)
     210          .append(" rotationAngle=").append(rotationAngle)
    201211          .append(" color=").append(ColorHelper.color2html(color));
    202212        if (haloRadius != null) {
     
    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
     
    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) &&
Note: See TracChangeset for help on using the changeset viewer.