Changeset 12476 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2017-07-13T23:02:13+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java
r12456 r12476 582 582 Rectangle2D bounds = text.font.getStringBounds(s, frc); 583 583 584 double x = Math.round(p.getInViewX()) + text.xOffset + bounds.getCenterX();585 double y = Math.round(p.getInViewY()) + text.yOffset + bounds.getCenterY();584 double x = Math.round(p.getInViewX()) + bs.xOffset + bounds.getCenterX(); 585 double y = Math.round(p.getInViewY()) + bs.yOffset + bounds.getCenterY(); 586 586 /** 587 587 * … … 1096 1096 * Draws a text for the given primitive 1097 1097 * @param osm The primitive to draw the text for 1098 * @param text The text definition (font/position/.../text content) to draw. 1098 * @param text The text definition (font/position/.../text content) to draw 1099 * @param labelPositionStrategy The position of the text 1099 1100 * @since 11722 1100 1101 */ 1101 public void drawText(OsmPrimitive osm, TextLabel text ) {1102 public void drawText(OsmPrimitive osm, TextLabel text, PositionForAreaStrategy labelPositionStrategy) { 1102 1103 if (!isShowNames()) { 1103 1104 return; … … 1114 1115 forEachPolygon(osm, path -> { 1115 1116 //TODO: Ignore areas that are out of bounds. 1116 PositionForAreaStrategy position = text.getLabelPositionStrategy();1117 PositionForAreaStrategy position = labelPositionStrategy; 1117 1118 MapViewPositionAndRotation center = position.findLabelPlacement(path, nb); 1118 1119 if (center != null) { -
trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/BoxTextElement.java
r12332 r12476 4 4 import java.awt.Color; 5 5 import java.awt.Rectangle; 6 import java.awt.geom.Point2D; 6 7 import java.util.Objects; 7 8 … … 173 174 public TextLabel text; 174 175 /** 176 * The x offset of the text. 177 */ 178 public int xOffset; 179 /** 180 * The y offset of the text. In screen space (inverted to user space) 181 */ 182 public int yOffset; 183 /** 175 184 * The {@link HorizontalTextAlignment} for this text. 176 185 */ … … 187 196 * @param text The text to display 188 197 * @param boxProvider The box provider to use 198 * @param offsetX x offset, in screen space 199 * @param offsetY y offset, in screen space 189 200 * @param hAlign The {@link HorizontalTextAlignment} 190 201 * @param vAlign The {@link VerticalTextAlignment} 191 202 */ 192 203 public BoxTextElement(Cascade c, TextLabel text, BoxProvider boxProvider, 193 HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign) {204 int offsetX, int offsetY, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign) { 194 205 super(c, 5f); 206 xOffset = offsetX; 207 yOffset = offsetY; 195 208 CheckParameterUtil.ensureParameterNotNull(text); 196 209 CheckParameterUtil.ensureParameterNotNull(hAlign); … … 250 263 vAlign = VerticalTextAlignment.BOTTOM; 251 264 } 252 253 return new BoxTextElement(c, text, boxProvider, hAlign, vAlign); 265 Point2D offset = TextLabel.getTextOffset(c); 266 267 return new BoxTextElement(c, text, boxProvider, (int) offset.getX(), (int) -offset.getY(), hAlign, vAlign); 254 268 } 255 269 … … 283 297 return hAlign == that.hAlign && 284 298 vAlign == that.vAlign && 299 xOffset == that.xOffset && 300 yOffset == that.yOffset && 285 301 Objects.equals(text, that.text) && 286 302 Objects.equals(boxProvider, that.boxProvider); … … 289 305 @Override 290 306 public int hashCode() { 291 return Objects.hash(super.hashCode(), text, boxProvider, hAlign, vAlign );307 return Objects.hash(super.hashCode(), text, boxProvider, hAlign, vAlign, xOffset, yOffset); 292 308 } 293 309 … … 295 311 public String toString() { 296 312 return "BoxTextElement{" + super.toString() + ' ' + text.toStringImpl() 297 + " box=" + getBox() + " hAlign=" + hAlign + " vAlign=" + vAlign + '}';313 + " box=" + getBox() + " hAlign=" + hAlign + " vAlign=" + vAlign + " xOffset=" + xOffset + " yOffset=" + yOffset + '}'; 298 314 } 299 315 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextElement.java
r11932 r12476 12 12 import org.openstreetmap.josm.gui.mappaint.Keyword; 13 13 import org.openstreetmap.josm.gui.mappaint.styleelement.placement.CompletelyInsideAreaStrategy; 14 import org.openstreetmap.josm.gui.mappaint.styleelement.placement.PositionForAreaStrategy; 14 15 15 16 /** … … 21 22 22 23 private final TextLabel text; 24 /** 25 * The position strategy for this text label. 26 */ 27 private final PositionForAreaStrategy labelPositionStrategy; 23 28 24 protected TextElement(Cascade c, TextLabel text) { 29 /** 30 * Create a new way/area text element definition 31 * @param c The cascade 32 * @param text The text 33 * @param labelPositionStrategy The position in the area. 34 */ 35 protected TextElement(Cascade c, TextLabel text, PositionForAreaStrategy labelPositionStrategy) { 25 36 super(c, 4.9f); 26 this.text = text; 37 this.text = Objects.requireNonNull(text, "text"); 38 this.labelPositionStrategy = Objects.requireNonNull(labelPositionStrategy, "labelPositionStrategy"); 39 } 40 41 /** 42 * Gets the strategy that defines where to place the label. 43 * @return The strategy. Never null. 44 * @since 12475 45 */ 46 public PositionForAreaStrategy getLabelPositionStrategy() { 47 return labelPositionStrategy; 27 48 } 28 49 … … 37 58 return null; 38 59 final Cascade c = env.mc.getCascade(env.layer); 39 return new TextElement(c, text); 60 61 Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class); 62 PositionForAreaStrategy position = PositionForAreaStrategy.forKeyword(positionKeyword); 63 position = position.withAddedOffset(TextLabel.getTextOffset(c)); 64 65 return new TextElement(c, text, position); 40 66 } 41 67 … … 58 84 return null; 59 85 } 60 return new TextElement(c, text .withPosition(CompletelyInsideAreaStrategy.INSTANCE));86 return new TextElement(c, text, CompletelyInsideAreaStrategy.INSTANCE); 61 87 } 62 88 … … 64 90 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter, 65 91 boolean selected, boolean outermember, boolean member) { 66 painter.drawText(primitive, text );92 painter.drawText(primitive, text, getLabelPositionStrategy()); 67 93 } 68 94 … … 73 99 if (!super.equals(obj)) return false; 74 100 TextElement that = (TextElement) obj; 75 return Objects.equals(text, that.text); 101 return Objects.equals(labelPositionStrategy, that.labelPositionStrategy) 102 && Objects.equals(text, that.text); 76 103 } 77 104 78 105 @Override 79 106 public int hashCode() { 80 return Objects.hash(super.hashCode(), text );107 return Objects.hash(super.hashCode(), text, labelPositionStrategy); 81 108 } 82 109 83 110 @Override 84 111 public String toString() { 85 return "TextElement{" + super.toString() + "text=" + text + '}';112 return "TextElement{" + super.toString() + "text=" + text + " labelPositionStrategy=" + labelPositionStrategy + '}'; 86 113 } 87 114 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java
r12381 r12476 4 4 import java.awt.Color; 5 5 import java.awt.Font; 6 import java.awt.geom.Point2D; 6 7 import java.util.Objects; 7 8 … … 15 16 import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.StaticLabelCompositionStrategy; 16 17 import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.TagLookupCompositionStrategy; 17 import org.openstreetmap.josm.gui.mappaint.styleelement.placement.PositionForAreaStrategy;18 18 import org.openstreetmap.josm.tools.CheckParameterUtil; 19 19 import org.openstreetmap.josm.tools.Utils; … … 39 39 public Font font; 40 40 /** 41 * The x offset of the text.42 */43 public int xOffset;44 /**45 * The y offset of the text.46 */47 public int yOffset;48 /**49 41 * The color to draw the text in, includes alpha. 50 42 */ … … 58 50 */ 59 51 public Color haloColor; 60 61 /**62 * The position strategy for this text label.63 */64 private final PositionForAreaStrategy labelPositionStrategy;65 52 66 53 /** … … 70 57 * If null, no label is rendered. 71 58 * @param font the font to be used. Must not be null. 72 * @param xOffset x offset73 * @param yOffset y offset74 59 * @param color the color to be used. Must not be null 75 60 * @param haloRadius halo radius 76 61 * @param haloColor halo color 77 * @param labelPositionStrategy The position in the area. 78 */ 79 protected TextLabel(LabelCompositionStrategy strategy, Font font, int xOffset, int yOffset, Color color, Float haloRadius, 80 Color haloColor, PositionForAreaStrategy labelPositionStrategy) { 62 */ 63 protected TextLabel(LabelCompositionStrategy strategy, Font font, Color color, Float haloRadius, 64 Color haloColor) { 81 65 this.labelCompositionStrategy = strategy; 82 66 this.font = Objects.requireNonNull(font, "font"); 83 this.xOffset = xOffset;84 this.yOffset = yOffset;85 67 this.color = Objects.requireNonNull(color, "color"); 86 68 this.haloRadius = haloRadius; 87 69 this.haloColor = haloColor; 88 this.labelPositionStrategy = Objects.requireNonNull(labelPositionStrategy, "labelPositionStrategy");89 70 } 90 71 … … 97 78 this.labelCompositionStrategy = other.labelCompositionStrategy; 98 79 this.font = other.font; 99 this.xOffset = other.xOffset;100 this.yOffset = other.yOffset;101 80 this.color = other.color; 102 81 this.haloColor = other.haloColor; 103 82 this.haloRadius = other.haloRadius; 104 this.labelPositionStrategy = other.labelPositionStrategy;105 }106 107 /**108 * Copy constructor that changes the position strategy.109 *110 * @param other the other element.111 * @param labelPositionStrategy the position112 */113 private TextLabel(TextLabel other, PositionForAreaStrategy labelPositionStrategy) {114 this.labelCompositionStrategy = other.labelCompositionStrategy;115 this.font = other.font;116 this.xOffset = other.xOffset;117 this.yOffset = other.yOffset;118 this.color = other.color;119 this.haloColor = other.haloColor;120 this.haloRadius = other.haloRadius;121 this.labelPositionStrategy = labelPositionStrategy;122 83 } 123 84 … … 176 137 Font font = StyleElement.getFont(c, s); 177 138 139 Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class); 140 float alpha = c.get(TEXT_OPACITY, 1f, Float.class); 141 color = Utils.alphaMultiply(color, alpha); 142 143 Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class); 144 if (haloRadius != null && haloRadius <= 0) { 145 haloRadius = null; 146 } 147 Color haloColor = null; 148 if (haloRadius != null) { 149 haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class); 150 float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class); 151 haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor); 152 } 153 154 return new TextLabel(strategy, font, color, haloRadius, haloColor); 155 } 156 157 /** 158 * Gets the text-offset property from a cascade 159 * @param c The cascade 160 * @return The text offset property 161 */ 162 public static Point2D getTextOffset(Cascade c) { 178 163 float xOffset = 0; 179 164 float yOffset = 0; … … 189 174 xOffset = c.get(TEXT_OFFSET_X, xOffset, Float.class); 190 175 yOffset = c.get(TEXT_OFFSET_Y, yOffset, Float.class); 191 192 Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class); 193 float alpha = c.get(TEXT_OPACITY, 1f, Float.class); 194 color = Utils.alphaMultiply(color, alpha); 195 196 Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class); 197 if (haloRadius != null && haloRadius <= 0) { 198 haloRadius = null; 199 } 200 Color haloColor = null; 201 if (haloRadius != null) { 202 haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class); 203 float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class); 204 haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor); 205 } 206 207 Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class); 208 PositionForAreaStrategy position = PositionForAreaStrategy.forKeyword(positionKeyword); 209 210 return new TextLabel(strategy, font, (int) xOffset, -(int) yOffset, color, haloRadius, haloColor, position); 176 return new Point2D.Double(xOffset, yOffset); 211 177 } 212 178 … … 223 189 } 224 190 225 /**226 * Gets the strategy that defines where to place the label.227 * @return The strategy. Never null.228 * @since 11722229 */230 public PositionForAreaStrategy getLabelPositionStrategy() {231 return labelPositionStrategy;232 }233 234 /**235 * Creates a new text label with a different position strategy236 * @param labelPositionStrategy The new position strategy to use237 * @return The new label238 */239 public TextLabel withPosition(PositionForAreaStrategy labelPositionStrategy) {240 return new TextLabel(this, labelPositionStrategy);241 }242 243 191 @Override 244 192 public String toString() { … … 249 197 StringBuilder sb = new StringBuilder(96); 250 198 sb.append("labelCompositionStrategy=").append(labelCompositionStrategy) 251 .append(" font=").append(font); 252 if (xOffset != 0) { 253 sb.append(" xOffset=").append(xOffset); 254 } 255 if (yOffset != 0) { 256 sb.append(" yOffset=").append(yOffset); 257 } 258 sb.append(" color=").append(Utils.toString(color)); 199 .append(" font=").append(font) 200 .append(" color=").append(Utils.toString(color)); 259 201 if (haloRadius != null) { 260 202 sb.append(" haloRadius=").append(haloRadius) … … 266 208 @Override 267 209 public int hashCode() { 268 return Objects.hash(labelCompositionStrategy, font, xOffset, yOffset,color, haloRadius, haloColor);210 return Objects.hash(labelCompositionStrategy, font, color, haloRadius, haloColor); 269 211 } 270 212 … … 274 216 if (obj == null || getClass() != obj.getClass()) return false; 275 217 TextLabel textLabel = (TextLabel) obj; 276 return xOffset == textLabel.xOffset && 277 yOffset == textLabel.yOffset && 278 Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) && 218 return Objects.equals(labelCompositionStrategy, textLabel.labelCompositionStrategy) && 279 219 Objects.equals(font, textLabel.font) && 280 220 Objects.equals(color, textLabel.color) && -
trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/placement/CompletelyInsideAreaStrategy.java
r12082 r12476 3 3 4 4 import java.awt.Rectangle; 5 import java.awt.geom.Point2D; 5 6 import java.awt.geom.Rectangle2D; 6 7 … … 20 21 * An instance of this class. 21 22 */ 22 public static final CompletelyInsideAreaStrategy INSTANCE = new CompletelyInsideAreaStrategy( );23 public static final CompletelyInsideAreaStrategy INSTANCE = new CompletelyInsideAreaStrategy(0, 0); 23 24 24 protected CompletelyInsideAreaStrategy() { 25 protected final double offsetX; 26 protected final double offsetY; 27 28 protected CompletelyInsideAreaStrategy(double offsetX, double offsetY) { 29 this.offsetX = offsetX; 30 this.offsetY = offsetY; 25 31 } 26 32 … … 89 95 } 90 96 91 private static MapViewPositionAndRotation centerOf(MapViewState mapViewState, Rectangle centeredNBounds) { 92 return new MapViewPositionAndRotation( 93 mapViewState.getForView(centeredNBounds.getCenterX(), centeredNBounds.getCenterY()), 0); 97 private MapViewPositionAndRotation centerOf(MapViewState mapViewState, Rectangle centeredNBounds) { 98 double x = centeredNBounds.getCenterX() + offsetX; 99 double y = centeredNBounds.getCenterY() + offsetY; 100 return new MapViewPositionAndRotation(mapViewState.getForView(x, y), 0); 94 101 } 95 102 … … 98 105 return false; 99 106 } 107 108 @Override 109 public PositionForAreaStrategy withAddedOffset(Point2D addToOffset) { 110 if (Math.abs(addToOffset.getX()) < 1e-5 && Math.abs(addToOffset.getY()) < 1e-5) { 111 return this; 112 } else { 113 return new CompletelyInsideAreaStrategy(offsetX + addToOffset.getX(), offsetY + addToOffset.getY()); 114 } 115 } 116 117 @Override 118 public String toString() { 119 return "CompletelyInsideAreaStrategy [offsetX=" + offsetX + ", offsetY=" + offsetY + "]"; 120 } 121 122 @Override 123 public int hashCode() { 124 final int prime = 31; 125 int result = 1; 126 long temp; 127 temp = Double.doubleToLongBits(offsetX); 128 result = prime * result + (int) (temp ^ (temp >>> 32)); 129 temp = Double.doubleToLongBits(offsetY); 130 result = prime * result + (int) (temp ^ (temp >>> 32)); 131 return result; 132 } 133 134 @Override 135 public boolean equals(Object obj) { 136 if (this == obj) { 137 return true; 138 } 139 if (obj == null) { 140 return false; 141 } 142 if (getClass() != obj.getClass()) { 143 return false; 144 } 145 CompletelyInsideAreaStrategy other = (CompletelyInsideAreaStrategy) obj; 146 return Double.doubleToLongBits(offsetX) == Double.doubleToLongBits(other.offsetX) 147 && Double.doubleToLongBits(offsetY) != Double.doubleToLongBits(other.offsetY); 148 } 100 149 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/placement/OnLineStrategy.java
r11814 r12476 4 4 import java.awt.font.GlyphVector; 5 5 import java.awt.geom.AffineTransform; 6 import java.awt.geom.Point2D; 6 7 import java.awt.geom.Rectangle2D; 7 8 import java.util.ArrayList; … … 332 333 return Math.atan2(end.getInViewY() - start.getInViewY(), end.getInViewX() - start.getInViewX()); 333 334 } 335 336 @Override 337 public PositionForAreaStrategy withAddedOffset(Point2D addToOffset) { 338 if (Math.abs(addToOffset.getY()) < 1e-5) { 339 return this; 340 } else { 341 return new OnLineStrategy(addToOffset.getY() + this.yOffset); 342 } 343 } 344 345 @Override 346 public String toString() { 347 return "OnLineStrategy [yOffset=" + yOffset + "]"; 348 } 349 350 @Override 351 public int hashCode() { 352 final int prime = 31; 353 int result = 1; 354 long temp; 355 temp = Double.doubleToLongBits(yOffset); 356 result = prime * result + (int) (temp ^ (temp >>> 32)); 357 return result; 358 } 359 360 @Override 361 public boolean equals(Object obj) { 362 if (this == obj) { 363 return true; 364 } 365 if (obj == null) { 366 return false; 367 } 368 if (getClass() != obj.getClass()) { 369 return false; 370 } 371 OnLineStrategy other = (OnLineStrategy) obj; 372 if (Double.doubleToLongBits(yOffset) != Double.doubleToLongBits(other.yOffset)) { 373 return false; 374 } 375 return true; 376 } 334 377 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/placement/PartiallyInsideAreaStrategy.java
r12088 r12476 2 2 package org.openstreetmap.josm.gui.mappaint.styleelement.placement; 3 3 4 import java.awt.geom.Point2D; 4 5 import java.awt.geom.Rectangle2D; 5 6 … … 20 21 * An instance of this class. 21 22 */ 22 public static final PartiallyInsideAreaStrategy INSTANCE = new PartiallyInsideAreaStrategy( );23 public static final PartiallyInsideAreaStrategy INSTANCE = new PartiallyInsideAreaStrategy(0, 0); 23 24 24 private PartiallyInsideAreaStrategy() { 25 private PartiallyInsideAreaStrategy(double offsetX, double offsetY) { 26 super(offsetX, offsetY); 25 27 } 26 28 … … 44 46 } 45 47 } 48 49 @Override 50 public PositionForAreaStrategy withAddedOffset(Point2D addToOffset) { 51 if (Math.abs(addToOffset.getX()) < 1e-5 && Math.abs(addToOffset.getY()) < 1e-5) { 52 return this; 53 } else { 54 return new PartiallyInsideAreaStrategy(offsetX + addToOffset.getX(), offsetY + addToOffset.getY()); 55 } 56 } 57 58 @Override 59 public String toString() { 60 return "PartiallyInsideAreaStrategy [offsetX=" + offsetX + ", offsetY=" + offsetY + "]"; 61 } 46 62 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/placement/PositionForAreaStrategy.java
r11755 r12476 3 3 4 4 import java.awt.font.GlyphVector; 5 import java.awt.geom.Point2D; 5 6 import java.awt.geom.Rectangle2D; 6 7 import java.util.List; … … 79 80 } 80 81 } 82 83 /** 84 * Create a new instance of the same strategy adding a offset 85 * @param addToOffset The offset to add 86 * @return The new strategy 87 * @since 12476 88 */ 89 PositionForAreaStrategy withAddedOffset(Point2D addToOffset); 81 90 }
Note:
See TracChangeset
for help on using the changeset viewer.