001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.mappaint; 003 004import java.awt.BasicStroke; 005import java.awt.Color; 006import java.util.Arrays; 007import java.util.Objects; 008 009import org.openstreetmap.josm.Main; 010import org.openstreetmap.josm.data.osm.Node; 011import org.openstreetmap.josm.data.osm.OsmPrimitive; 012import org.openstreetmap.josm.data.osm.Way; 013import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings; 014import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors; 015import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer; 016import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat; 017import org.openstreetmap.josm.tools.Utils; 018 019public class LineElemStyle extends ElemStyle { 020 021 public static LineElemStyle createSimpleLineStyle(Color color, boolean isAreaEdge) { 022 MultiCascade mc = new MultiCascade(); 023 Cascade c = mc.getOrCreateCascade("default"); 024 c.put(WIDTH, Keyword.DEFAULT); 025 c.put(COLOR, color != null ? color : PaintColors.UNTAGGED.get()); 026 c.put(OPACITY, 1f); 027 if (isAreaEdge) { 028 c.put(Z_INDEX, -3f); 029 } 030 return createLine(new Environment(null, mc, "default", null)); 031 } 032 public static final LineElemStyle UNTAGGED_WAY = createSimpleLineStyle(null, false); 033 034 private BasicStroke line; 035 public Color color; 036 public Color dashesBackground; 037 public float offset; 038 public float realWidth; // the real width of this line in meter 039 040 private BasicStroke dashesLine; 041 042 public enum LineType { 043 NORMAL("", 3f), 044 CASING("casing-", 2f), 045 LEFT_CASING("left-casing-", 2.1f), 046 RIGHT_CASING("right-casing-", 2.1f); 047 048 public final String prefix; 049 public final float default_major_z_index; 050 051 LineType(String prefix, float default_major_z_index) { 052 this.prefix = prefix; 053 this.default_major_z_index = default_major_z_index; 054 } 055 } 056 057 protected LineElemStyle(Cascade c, float default_major_z_index, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, float offset, float realWidth) { 058 super(c, default_major_z_index); 059 this.line = line; 060 this.color = color; 061 this.dashesLine = dashesLine; 062 this.dashesBackground = dashesBackground; 063 this.offset = offset; 064 this.realWidth = realWidth; 065 } 066 067 public static LineElemStyle createLine(Environment env) { 068 return createImpl(env, LineType.NORMAL); 069 } 070 071 public static LineElemStyle createLeftCasing(Environment env) { 072 LineElemStyle leftCasing = createImpl(env, LineType.LEFT_CASING); 073 if (leftCasing != null) { 074 leftCasing.isModifier = true; 075 } 076 return leftCasing; 077 } 078 079 public static LineElemStyle createRightCasing(Environment env) { 080 LineElemStyle rightCasing = createImpl(env, LineType.RIGHT_CASING); 081 if (rightCasing != null) { 082 rightCasing.isModifier = true; 083 } 084 return rightCasing; 085 } 086 087 public static LineElemStyle createCasing(Environment env) { 088 LineElemStyle casing = createImpl(env, LineType.CASING); 089 if (casing != null) { 090 casing.isModifier = true; 091 } 092 return casing; 093 } 094 095 private static LineElemStyle createImpl(Environment env, LineType type) { 096 Cascade c = env.mc.getCascade(env.layer); 097 Cascade c_def = env.mc.getCascade("default"); 098 Float width; 099 switch (type) { 100 case NORMAL: 101 width = getWidth(c, WIDTH, getWidth(c_def, WIDTH, null)); 102 break; 103 case CASING: 104 Float casingWidth = c.get(type.prefix + WIDTH, null, Float.class, true); 105 if (casingWidth == null) { 106 RelativeFloat rel_casingWidth = c.get(type.prefix + WIDTH, null, RelativeFloat.class, true); 107 if (rel_casingWidth != null) { 108 casingWidth = rel_casingWidth.val / 2; 109 } 110 } 111 if (casingWidth == null) 112 return null; 113 width = getWidth(c, WIDTH, getWidth(c_def, WIDTH, null)); 114 if (width == null) { 115 width = 0f; 116 } 117 width += 2 * casingWidth; 118 break; 119 case LEFT_CASING: 120 case RIGHT_CASING: 121 width = getWidth(c, type.prefix + WIDTH, null); 122 break; 123 default: 124 throw new AssertionError(); 125 } 126 if (width == null) 127 return null; 128 129 float realWidth = c.get(type.prefix + REAL_WIDTH, 0f, Float.class); 130 if (realWidth > 0 && MapPaintSettings.INSTANCE.isUseRealWidth()) { 131 132 /* if we have a "width" tag, try use it */ 133 String widthTag = env.osm.get("width"); 134 if (widthTag == null) { 135 widthTag = env.osm.get("est_width"); 136 } 137 if (widthTag != null) { 138 try { 139 realWidth = Float.valueOf(widthTag); 140 } catch(NumberFormatException nfe) { 141 Main.warn(nfe); 142 } 143 } 144 } 145 146 Float offset = c.get(OFFSET, 0f, Float.class); 147 switch (type) { 148 case NORMAL: 149 break; 150 case CASING: 151 offset += c.get(type.prefix + OFFSET, 0f, Float.class); 152 break; 153 case LEFT_CASING: 154 case RIGHT_CASING: 155 Float baseWidthOnDefault = getWidth(c_def, WIDTH, null); 156 Float baseWidth = getWidth(c, WIDTH, baseWidthOnDefault); 157 if (baseWidth == null || baseWidth < 2f) { 158 baseWidth = 2f; 159 } 160 float casingOffset = c.get(type.prefix + OFFSET, 0f, Float.class); 161 casingOffset += baseWidth / 2 + width / 2; 162 /* flip sign for the right-casing-offset */ 163 if (type == LineType.RIGHT_CASING) { 164 casingOffset *= -1f; 165 } 166 offset += casingOffset; 167 break; 168 } 169 170 int alpha = 255; 171 Color color = c.get(type.prefix + COLOR, null, Color.class); 172 if (color != null) { 173 alpha = color.getAlpha(); 174 } 175 if (type == LineType.NORMAL && color == null) { 176 color = c.get(FILL_COLOR, null, Color.class); 177 } 178 if (color == null) { 179 color = PaintColors.UNTAGGED.get(); 180 } 181 182 Integer pAlpha = Utils.color_float2int(c.get(type.prefix + OPACITY, null, Float.class)); 183 if (pAlpha != null) { 184 alpha = pAlpha; 185 } 186 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); 187 188 float[] dashes = c.get(type.prefix + DASHES, null, float[].class, true); 189 if (dashes != null) { 190 boolean hasPositive = false; 191 for (float f : dashes) { 192 if (f > 0) { 193 hasPositive = true; 194 } 195 if (f < 0) { 196 dashes = null; 197 break; 198 } 199 } 200 if (!hasPositive || (dashes != null && dashes.length == 0)) { 201 dashes = null; 202 } 203 } 204 float dashesOffset = c.get(type.prefix + DASHES_OFFSET, 0f, Float.class); 205 Color dashesBackground = c.get(type.prefix + DASHES_BACKGROUND_COLOR, null, Color.class); 206 if (dashesBackground != null) { 207 pAlpha = Utils.color_float2int(c.get(type.prefix + DASHES_BACKGROUND_OPACITY, null, Float.class)); 208 if (pAlpha != null) { 209 alpha = pAlpha; 210 } 211 dashesBackground = new Color(dashesBackground.getRed(), dashesBackground.getGreen(), 212 dashesBackground.getBlue(), alpha); 213 } 214 215 Integer cap = null; 216 Keyword capKW = c.get(type.prefix + LINECAP, null, Keyword.class); 217 if (capKW != null) { 218 if ("none".equals(capKW.val)) { 219 cap = BasicStroke.CAP_BUTT; 220 } else if ("round".equals(capKW.val)) { 221 cap = BasicStroke.CAP_ROUND; 222 } else if ("square".equals(capKW.val)) { 223 cap = BasicStroke.CAP_SQUARE; 224 } 225 } 226 if (cap == null) { 227 cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND; 228 } 229 230 Integer join = null; 231 Keyword joinKW = c.get(type.prefix + LINEJOIN, null, Keyword.class); 232 if (joinKW != null) { 233 if ("round".equals(joinKW.val)) { 234 join = BasicStroke.JOIN_ROUND; 235 } else if ("miter".equals(joinKW.val)) { 236 join = BasicStroke.JOIN_MITER; 237 } else if ("bevel".equals(joinKW.val)) { 238 join = BasicStroke.JOIN_BEVEL; 239 } 240 } 241 if (join == null) { 242 join = BasicStroke.JOIN_ROUND; 243 } 244 245 float miterlimit = c.get(type.prefix + MITERLIMIT, 10f, Float.class); 246 if (miterlimit < 1f) { 247 miterlimit = 10f; 248 } 249 250 BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset); 251 BasicStroke dashesLine = null; 252 253 if (dashes != null && dashesBackground != null) { 254 float[] dashes2 = new float[dashes.length]; 255 System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1); 256 dashes2[0] = dashes[dashes.length-1]; 257 dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset); 258 } 259 260 return new LineElemStyle(c, type.default_major_z_index, line, color, dashesLine, dashesBackground, offset, realWidth); 261 } 262 263 @Override 264 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter, 265 boolean selected, boolean outermember, boolean member) { 266 Way w = (Way)primitive; 267 /* show direction arrows, if draw.segment.relevant_directions_only is not set, 268 the way is tagged with a direction key 269 (even if the tag is negated as in oneway=false) or the way is selected */ 270 boolean showOrientation = !isModifier && (selected || paintSettings.isShowDirectionArrow()) && !paintSettings.isUseRealWidth(); 271 boolean showOneway = !isModifier && !selected && 272 !paintSettings.isUseRealWidth() && 273 paintSettings.isShowOnewayArrow() && w.hasDirectionKeys(); 274 boolean onewayReversed = w.reversedDirection(); 275 /* head only takes over control if the option is true, 276 the direction should be shown at all and not only because it's selected */ 277 boolean showOnlyHeadArrowOnly = showOrientation && !selected && paintSettings.isShowHeadArrowOnly(); 278 Node lastN; 279 280 Color myDashedColor = dashesBackground; 281 BasicStroke myLine = line, myDashLine = dashesLine; 282 if (realWidth > 0 && paintSettings.isUseRealWidth() && !showOrientation) { 283 float myWidth = (int) (100 / (float) (painter.getCircum() / realWidth)); 284 if (myWidth < line.getLineWidth()) { 285 myWidth = line.getLineWidth(); 286 } 287 myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(), 288 line.getMiterLimit(), line.getDashArray(), line.getDashPhase()); 289 if (dashesLine != null) { 290 myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(), 291 dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase()); 292 } 293 } 294 295 Color myColor = color; 296 if (selected) { 297 myColor = paintSettings.getSelectedColor(color.getAlpha()); 298 } else if (member || outermember) { 299 myColor = paintSettings.getRelationSelectedColor(color.getAlpha()); 300 } else if(w.isDisabled()) { 301 myColor = paintSettings.getInactiveColor(); 302 myDashedColor = paintSettings.getInactiveColor(); 303 } 304 305 painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, offset, showOrientation, 306 showOnlyHeadArrowOnly, showOneway, onewayReversed); 307 308 if(paintSettings.isShowOrderNumber() && !painter.isInactiveMode()) { 309 int orderNumber = 0; 310 lastN = null; 311 for(Node n : w.getNodes()) { 312 if(lastN != null) { 313 orderNumber++; 314 painter.drawOrderNumber(lastN, n, orderNumber, myColor); 315 } 316 lastN = n; 317 } 318 } 319 } 320 321 @Override 322 public boolean isProperLineStyle() { 323 return !isModifier; 324 } 325 326 @Override 327 public boolean equals(Object obj) { 328 if (obj == null || getClass() != obj.getClass()) 329 return false; 330 if (!super.equals(obj)) 331 return false; 332 final LineElemStyle other = (LineElemStyle) obj; 333 return Objects.equals(line, other.line) && 334 Objects.equals(color, other.color) && 335 Objects.equals(dashesLine, other.dashesLine) && 336 Objects.equals(dashesBackground, other.dashesBackground) && 337 offset == other.offset && 338 realWidth == other.realWidth; 339 } 340 341 @Override 342 public int hashCode() { 343 int hash = super.hashCode(); 344 hash = 29 * hash + line.hashCode(); 345 hash = 29 * hash + color.hashCode(); 346 hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0); 347 hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0); 348 hash = 29 * hash + Float.floatToIntBits(offset); 349 hash = 29 * hash + Float.floatToIntBits(realWidth); 350 return hash; 351 } 352 353 @Override 354 public String toString() { 355 return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() + 356 " realWidth=" + realWidth + " color=" + Utils.toString(color) + 357 " dashed=" + Arrays.toString(line.getDashArray()) + 358 (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) + 359 " dashedColor=" + Utils.toString(dashesBackground) + 360 " linejoin=" + linejoinToString(line.getLineJoin()) + 361 " linecap=" + linecapToString(line.getEndCap()) + 362 (offset == 0 ? "" : " offset=" + offset) + 363 '}'; 364 } 365 366 public String linejoinToString(int linejoin) { 367 switch (linejoin) { 368 case BasicStroke.JOIN_BEVEL: return "bevel"; 369 case BasicStroke.JOIN_ROUND: return "round"; 370 case BasicStroke.JOIN_MITER: return "miter"; 371 default: return null; 372 } 373 } 374 375 public String linecapToString(int linecap) { 376 switch (linecap) { 377 case BasicStroke.CAP_BUTT: return "none"; 378 case BasicStroke.CAP_ROUND: return "round"; 379 case BasicStroke.CAP_SQUARE: return "square"; 380 default: return null; 381 } 382 } 383}