[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[9278] | 2 | package org.openstreetmap.josm.gui.mappaint.styleelement;
|
---|
[885] | 3 |
|
---|
[3860] | 4 | import java.awt.BasicStroke;
|
---|
[626] | 5 | import java.awt.Color;
|
---|
[3824] | 6 | import java.util.Arrays;
|
---|
[7083] | 7 | import java.util.Objects;
|
---|
[11553] | 8 | import java.util.Optional;
|
---|
[626] | 9 |
|
---|
[13919] | 10 | import org.openstreetmap.josm.data.osm.INode;
|
---|
[13662] | 11 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
[13919] | 12 | import org.openstreetmap.josm.data.osm.IWay;
|
---|
[2675] | 13 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 14 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
[5801] | 15 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
[5571] | 16 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
[9278] | 17 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
| 18 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
| 19 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
| 20 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
---|
[5214] | 21 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
|
---|
[16252] | 22 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
[12620] | 23 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2659] | 24 |
|
---|
[10374] | 25 | /**
|
---|
| 26 | * This is the style definition for a simple line.
|
---|
| 27 | */
|
---|
[9278] | 28 | public class LineElement extends StyleElement {
|
---|
[10374] | 29 | /**
|
---|
| 30 | * The default style for any untagged way.
|
---|
| 31 | */
|
---|
[9278] | 32 | public static final LineElement UNTAGGED_WAY = createSimpleLineStyle(null, false);
|
---|
[3824] | 33 |
|
---|
[12303] | 34 | /**
|
---|
| 35 | * The stroke used to paint the line
|
---|
| 36 | */
|
---|
[12082] | 37 | private final BasicStroke line;
|
---|
[12303] | 38 | /**
|
---|
| 39 | * The color of the line. Should not be accessed directly
|
---|
| 40 | */
|
---|
[1169] | 41 | public Color color;
|
---|
[12303] | 42 |
|
---|
| 43 | /**
|
---|
| 44 | * The stroke used to paint the gaps between the dashes
|
---|
| 45 | */
|
---|
| 46 | private final BasicStroke dashesLine;
|
---|
| 47 | /**
|
---|
| 48 | * The secondary color of the line that is used for the gaps in dashed lines. Should not be accessed directly
|
---|
| 49 | */
|
---|
[3860] | 50 | public Color dashesBackground;
|
---|
[12303] | 51 | /**
|
---|
| 52 | * The dash offset. Should not be accessed directly
|
---|
| 53 | */
|
---|
[5206] | 54 | public float offset;
|
---|
[12303] | 55 | /**
|
---|
| 56 | * the real width of this line in meter. Should not be accessed directly
|
---|
| 57 | */
|
---|
| 58 | public float realWidth;
|
---|
| 59 | /**
|
---|
[17333] | 60 | * A flag indicating if the direction arrows should be painted. Should not be accessed directly
|
---|
[12303] | 61 | */
|
---|
[9341] | 62 | public boolean wayDirectionArrows;
|
---|
[626] | 63 |
|
---|
[12303] | 64 | /**
|
---|
| 65 | * The type of this line
|
---|
| 66 | */
|
---|
[8087] | 67 | public enum LineType {
|
---|
[12303] | 68 | /**
|
---|
| 69 | * A normal line
|
---|
| 70 | */
|
---|
[5217] | 71 | NORMAL("", 3f),
|
---|
[12303] | 72 | /**
|
---|
| 73 | * A casing (line behind normal line, extended to the right/left)
|
---|
| 74 | */
|
---|
[5217] | 75 | CASING("casing-", 2f),
|
---|
[12303] | 76 | /**
|
---|
| 77 | * A casing, but only to the left
|
---|
| 78 | */
|
---|
[5217] | 79 | LEFT_CASING("left-casing-", 2.1f),
|
---|
[12303] | 80 | /**
|
---|
| 81 | * A casing, but only to the right
|
---|
| 82 | */
|
---|
[5217] | 83 | RIGHT_CASING("right-casing-", 2.1f);
|
---|
[5212] | 84 |
|
---|
[12303] | 85 | /**
|
---|
| 86 | * The MapCSS line prefix used
|
---|
| 87 | */
|
---|
[5217] | 88 | public final String prefix;
|
---|
[12303] | 89 | /**
|
---|
| 90 | * The major z index to use during painting
|
---|
| 91 | */
|
---|
[8346] | 92 | public final float defaultMajorZIndex;
|
---|
[5212] | 93 |
|
---|
[14095] | 94 | LineType(String prefix, float defaultMajorZIndex) {
|
---|
[5212] | 95 | this.prefix = prefix;
|
---|
[14095] | 96 | this.defaultMajorZIndex = defaultMajorZIndex;
|
---|
[5212] | 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[10001] | 100 | protected LineElement(Cascade c, float defaultMajorZindex, BasicStroke line, Color color, BasicStroke dashesLine,
|
---|
[9341] | 101 | Color dashesBackground, float offset, float realWidth, boolean wayDirectionArrows) {
|
---|
[10001] | 102 | super(c, defaultMajorZindex);
|
---|
[3860] | 103 | this.line = line;
|
---|
| 104 | this.color = color;
|
---|
| 105 | this.dashesLine = dashesLine;
|
---|
| 106 | this.dashesBackground = dashesBackground;
|
---|
[4316] | 107 | this.offset = offset;
|
---|
[3824] | 108 | this.realWidth = realWidth;
|
---|
[9341] | 109 | this.wayDirectionArrows = wayDirectionArrows;
|
---|
[1169] | 110 | }
|
---|
[626] | 111 |
|
---|
[10374] | 112 | @Override
|
---|
[13662] | 113 | public void paintPrimitive(IPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
[10374] | 114 | boolean selected, boolean outermember, boolean member) {
|
---|
| 115 | /* show direction arrows, if draw.segment.relevant_directions_only is not set,
|
---|
| 116 | the way is tagged with a direction key
|
---|
| 117 | (even if the tag is negated as in oneway=false) or the way is selected */
|
---|
| 118 | boolean showOrientation;
|
---|
| 119 | if (defaultSelectedHandling) {
|
---|
| 120 | showOrientation = !isModifier && (selected || paintSettings.isShowDirectionArrow()) && !paintSettings.isUseRealWidth();
|
---|
| 121 | } else {
|
---|
| 122 | showOrientation = wayDirectionArrows;
|
---|
| 123 | }
|
---|
| 124 | boolean showOneway = !isModifier && !selected &&
|
---|
| 125 | !paintSettings.isUseRealWidth() &&
|
---|
[11809] | 126 | paintSettings.isShowOnewayArrow() && primitive.hasDirectionKeys();
|
---|
| 127 | boolean onewayReversed = primitive.reversedDirection();
|
---|
[10374] | 128 | /* head only takes over control if the option is true,
|
---|
| 129 | the direction should be shown at all and not only because it's selected */
|
---|
| 130 | boolean showOnlyHeadArrowOnly = showOrientation && !selected && paintSettings.isShowHeadArrowOnly();
|
---|
[13919] | 131 | INode lastN;
|
---|
[10374] | 132 |
|
---|
| 133 | Color myDashedColor = dashesBackground;
|
---|
| 134 | BasicStroke myLine = line, myDashLine = dashesLine;
|
---|
| 135 | if (realWidth > 0 && paintSettings.isUseRealWidth() && !showOrientation) {
|
---|
[10378] | 136 | float myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
|
---|
[10374] | 137 | if (myWidth < line.getLineWidth()) {
|
---|
| 138 | myWidth = line.getLineWidth();
|
---|
| 139 | }
|
---|
| 140 | myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(),
|
---|
| 141 | line.getMiterLimit(), line.getDashArray(), line.getDashPhase());
|
---|
| 142 | if (dashesLine != null) {
|
---|
| 143 | myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(),
|
---|
| 144 | dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase());
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | Color myColor = color;
|
---|
| 149 | if (defaultSelectedHandling && selected) {
|
---|
| 150 | myColor = paintSettings.getSelectedColor(color.getAlpha());
|
---|
| 151 | } else if (member || outermember) {
|
---|
| 152 | myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
|
---|
[11809] | 153 | } else if (primitive.isDisabled()) {
|
---|
[10374] | 154 | myColor = paintSettings.getInactiveColor();
|
---|
| 155 | myDashedColor = paintSettings.getInactiveColor();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[13919] | 158 | if (primitive instanceof IWay) {
|
---|
| 159 | IWay<?> w = (IWay<?>) primitive;
|
---|
[11809] | 160 | painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, offset, showOrientation,
|
---|
| 161 | showOnlyHeadArrowOnly, showOneway, onewayReversed);
|
---|
[10374] | 162 |
|
---|
[11809] | 163 | if ((paintSettings.isShowOrderNumber() || (paintSettings.isShowOrderNumberOnSelectedWay() && selected))
|
---|
| 164 | && !painter.isInactiveMode()) {
|
---|
| 165 | int orderNumber = 0;
|
---|
| 166 | lastN = null;
|
---|
[13919] | 167 | for (INode n : w.getNodes()) {
|
---|
[11809] | 168 | if (lastN != null) {
|
---|
| 169 | orderNumber++;
|
---|
| 170 | painter.drawOrderNumber(lastN, n, orderNumber, myColor);
|
---|
| 171 | }
|
---|
| 172 | lastN = n;
|
---|
[10374] | 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | @Override
|
---|
| 179 | public boolean isProperLineStyle() {
|
---|
| 180 | return !isModifier;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[12303] | 183 | /**
|
---|
| 184 | * Converts a linejoin of a {@link BasicStroke} to a MapCSS string
|
---|
| 185 | * @param linejoin The linejoin
|
---|
| 186 | * @return The MapCSS string or <code>null</code> on error.
|
---|
| 187 | * @see BasicStroke#getLineJoin()
|
---|
| 188 | */
|
---|
[10374] | 189 | public String linejoinToString(int linejoin) {
|
---|
| 190 | switch (linejoin) {
|
---|
| 191 | case BasicStroke.JOIN_BEVEL: return "bevel";
|
---|
| 192 | case BasicStroke.JOIN_ROUND: return "round";
|
---|
| 193 | case BasicStroke.JOIN_MITER: return "miter";
|
---|
| 194 | default: return null;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[12303] | 198 | /**
|
---|
| 199 | * Converts a linecap of a {@link BasicStroke} to a MapCSS string
|
---|
| 200 | * @param linecap The linecap
|
---|
| 201 | * @return The MapCSS string or <code>null</code> on error.
|
---|
| 202 | * @see BasicStroke#getEndCap()
|
---|
| 203 | */
|
---|
[10374] | 204 | public String linecapToString(int linecap) {
|
---|
| 205 | switch (linecap) {
|
---|
| 206 | case BasicStroke.CAP_BUTT: return "none";
|
---|
| 207 | case BasicStroke.CAP_ROUND: return "round";
|
---|
| 208 | case BasicStroke.CAP_SQUARE: return "square";
|
---|
| 209 | default: return null;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | @Override
|
---|
| 214 | public boolean equals(Object obj) {
|
---|
| 215 | if (obj == null || getClass() != obj.getClass())
|
---|
| 216 | return false;
|
---|
| 217 | if (!super.equals(obj))
|
---|
| 218 | return false;
|
---|
| 219 | final LineElement other = (LineElement) obj;
|
---|
[17519] | 220 | return Float.compare(offset, other.offset) == 0 &&
|
---|
| 221 | Float.compare(realWidth, other.realWidth) == 0 &&
|
---|
[11452] | 222 | wayDirectionArrows == other.wayDirectionArrows &&
|
---|
| 223 | Objects.equals(line, other.line) &&
|
---|
| 224 | Objects.equals(color, other.color) &&
|
---|
| 225 | Objects.equals(dashesLine, other.dashesLine) &&
|
---|
| 226 | Objects.equals(dashesBackground, other.dashesBackground);
|
---|
[10374] | 227 | }
|
---|
| 228 |
|
---|
| 229 | @Override
|
---|
| 230 | public int hashCode() {
|
---|
| 231 | return Objects.hash(super.hashCode(), line, color, dashesBackground, offset, realWidth, wayDirectionArrows, dashesLine);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | @Override
|
---|
| 235 | public String toString() {
|
---|
| 236 | return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
|
---|
[16252] | 237 | " realWidth=" + realWidth + " color=" + ColorHelper.color2html(color) +
|
---|
[10374] | 238 | " dashed=" + Arrays.toString(line.getDashArray()) +
|
---|
| 239 | (line.getDashPhase() == 0 ? "" : " dashesOffses=" + line.getDashPhase()) +
|
---|
[16252] | 240 | " dashedColor=" + ColorHelper.color2html(dashesBackground) +
|
---|
[10374] | 241 | " linejoin=" + linejoinToString(line.getLineJoin()) +
|
---|
| 242 | " linecap=" + linecapToString(line.getEndCap()) +
|
---|
| 243 | (offset == 0 ? "" : " offset=" + offset) +
|
---|
| 244 | '}';
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /**
|
---|
[13919] | 248 | * Creates a simple line with default width.
|
---|
[10374] | 249 | * @param color The color to use
|
---|
| 250 | * @param isAreaEdge If this is an edge for an area. Edges are drawn at lower Z-Index.
|
---|
| 251 | * @return The line style.
|
---|
| 252 | */
|
---|
| 253 | public static LineElement createSimpleLineStyle(Color color, boolean isAreaEdge) {
|
---|
| 254 | MultiCascade mc = new MultiCascade();
|
---|
| 255 | Cascade c = mc.getOrCreateCascade("default");
|
---|
| 256 | c.put(WIDTH, Keyword.DEFAULT);
|
---|
| 257 | c.put(COLOR, color != null ? color : PaintColors.UNTAGGED.get());
|
---|
| 258 | c.put(OPACITY, 1f);
|
---|
| 259 | if (isAreaEdge) {
|
---|
| 260 | c.put(Z_INDEX, -3f);
|
---|
| 261 | }
|
---|
[13919] | 262 | return createLine(new Environment(new Way(), mc, "default", null));
|
---|
[10374] | 263 | }
|
---|
| 264 |
|
---|
[12303] | 265 | /**
|
---|
| 266 | * Create a line element from the given MapCSS environment
|
---|
| 267 | * @param env The environment
|
---|
| 268 | * @return The line element describing the line that should be painted, or <code>null</code> if none should be painted.
|
---|
| 269 | */
|
---|
[9278] | 270 | public static LineElement createLine(Environment env) {
|
---|
[5212] | 271 | return createImpl(env, LineType.NORMAL);
|
---|
[3836] | 272 | }
|
---|
| 273 |
|
---|
[12303] | 274 | /**
|
---|
| 275 | * Create a line element for the left casing from the given MapCSS environment
|
---|
| 276 | * @param env The environment
|
---|
| 277 | * @return The line element describing the line that should be painted, or <code>null</code> if none should be painted.
|
---|
| 278 | */
|
---|
[9278] | 279 | public static LineElement createLeftCasing(Environment env) {
|
---|
| 280 | LineElement leftCasing = createImpl(env, LineType.LEFT_CASING);
|
---|
[5206] | 281 | if (leftCasing != null) {
|
---|
| 282 | leftCasing.isModifier = true;
|
---|
| 283 | }
|
---|
| 284 | return leftCasing;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[12303] | 287 | /**
|
---|
| 288 | * Create a line element for the right casing from the given MapCSS environment
|
---|
| 289 | * @param env The environment
|
---|
| 290 | * @return The line element describing the line that should be painted, or <code>null</code> if none should be painted.
|
---|
| 291 | */
|
---|
[9278] | 292 | public static LineElement createRightCasing(Environment env) {
|
---|
| 293 | LineElement rightCasing = createImpl(env, LineType.RIGHT_CASING);
|
---|
[5206] | 294 | if (rightCasing != null) {
|
---|
| 295 | rightCasing.isModifier = true;
|
---|
| 296 | }
|
---|
| 297 | return rightCasing;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[12303] | 300 | /**
|
---|
| 301 | * Create a line element for the casing from the given MapCSS environment
|
---|
| 302 | * @param env The environment
|
---|
| 303 | * @return The line element describing the line that should be painted, or <code>null</code> if none should be painted.
|
---|
| 304 | */
|
---|
[9278] | 305 | public static LineElement createCasing(Environment env) {
|
---|
| 306 | LineElement casing = createImpl(env, LineType.CASING);
|
---|
[3848] | 307 | if (casing != null) {
|
---|
[3888] | 308 | casing.isModifier = true;
|
---|
[3848] | 309 | }
|
---|
| 310 | return casing;
|
---|
[3836] | 311 | }
|
---|
| 312 |
|
---|
[9278] | 313 | private static LineElement createImpl(Environment env, LineType type) {
|
---|
[3893] | 314 | Cascade c = env.mc.getCascade(env.layer);
|
---|
[10001] | 315 | Cascade cDef = env.mc.getCascade("default");
|
---|
[10374] | 316 | Float width = computeWidth(type, c, cDef);
|
---|
[3880] | 317 | if (width == null)
|
---|
| 318 | return null;
|
---|
| 319 |
|
---|
[10374] | 320 | float realWidth = computeRealWidth(env, type, c);
|
---|
[3860] | 321 |
|
---|
[10374] | 322 | Float offset = computeOffset(type, c, cDef, width);
|
---|
[3860] | 323 |
|
---|
[7136] | 324 | int alpha = 255;
|
---|
[5342] | 325 | Color color = c.get(type.prefix + COLOR, null, Color.class);
|
---|
[7136] | 326 | if (color != null) {
|
---|
| 327 | alpha = color.getAlpha();
|
---|
| 328 | }
|
---|
[5212] | 329 | if (type == LineType.NORMAL && color == null) {
|
---|
[5342] | 330 | color = c.get(FILL_COLOR, null, Color.class);
|
---|
[3836] | 331 | }
|
---|
| 332 | if (color == null) {
|
---|
[3848] | 333 | color = PaintColors.UNTAGGED.get();
|
---|
[3836] | 334 | }
|
---|
[3858] | 335 |
|
---|
[16252] | 336 | Integer pAlpha = ColorHelper.float2int(c.get(type.prefix + OPACITY, null, Float.class));
|
---|
[3858] | 337 | if (pAlpha != null) {
|
---|
| 338 | alpha = pAlpha;
|
---|
| 339 | }
|
---|
| 340 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
---|
| 341 |
|
---|
[7045] | 342 | float[] dashes = c.get(type.prefix + DASHES, null, float[].class, true);
|
---|
[3856] | 343 | if (dashes != null) {
|
---|
| 344 | boolean hasPositive = false;
|
---|
| 345 | for (float f : dashes) {
|
---|
| 346 | if (f > 0) {
|
---|
| 347 | hasPositive = true;
|
---|
| 348 | }
|
---|
| 349 | if (f < 0) {
|
---|
| 350 | dashes = null;
|
---|
| 351 | break;
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
[3994] | 354 | if (!hasPositive || (dashes != null && dashes.length == 0)) {
|
---|
[3856] | 355 | dashes = null;
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
[5342] | 358 | float dashesOffset = c.get(type.prefix + DASHES_OFFSET, 0f, Float.class);
|
---|
[13013] | 359 | if (dashesOffset < 0f) {
|
---|
| 360 | Logging.warn("Found negative " + DASHES_OFFSET + ": " + dashesOffset);
|
---|
| 361 | dashesOffset = 0f;
|
---|
| 362 | }
|
---|
[5342] | 363 | Color dashesBackground = c.get(type.prefix + DASHES_BACKGROUND_COLOR, null, Color.class);
|
---|
[3859] | 364 | if (dashesBackground != null) {
|
---|
[16252] | 365 | pAlpha = ColorHelper.float2int(c.get(type.prefix + DASHES_BACKGROUND_OPACITY, null, Float.class));
|
---|
[3859] | 366 | if (pAlpha != null) {
|
---|
| 367 | alpha = pAlpha;
|
---|
| 368 | }
|
---|
| 369 | dashesBackground = new Color(dashesBackground.getRed(), dashesBackground.getGreen(),
|
---|
| 370 | dashesBackground.getBlue(), alpha);
|
---|
| 371 | }
|
---|
[3836] | 372 |
|
---|
[3967] | 373 | Integer cap = null;
|
---|
[8087] | 374 | Keyword capKW = c.get(type.prefix + LINECAP, null, Keyword.class);
|
---|
[3967] | 375 | if (capKW != null) {
|
---|
[7083] | 376 | if ("none".equals(capKW.val)) {
|
---|
[3967] | 377 | cap = BasicStroke.CAP_BUTT;
|
---|
[7083] | 378 | } else if ("round".equals(capKW.val)) {
|
---|
[3967] | 379 | cap = BasicStroke.CAP_ROUND;
|
---|
[7083] | 380 | } else if ("square".equals(capKW.val)) {
|
---|
[3967] | 381 | cap = BasicStroke.CAP_SQUARE;
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 | if (cap == null) {
|
---|
[3860] | 385 | cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[3967] | 388 | Integer join = null;
|
---|
[8087] | 389 | Keyword joinKW = c.get(type.prefix + LINEJOIN, null, Keyword.class);
|
---|
[3967] | 390 | if (joinKW != null) {
|
---|
[7083] | 391 | if ("round".equals(joinKW.val)) {
|
---|
[3967] | 392 | join = BasicStroke.JOIN_ROUND;
|
---|
[7083] | 393 | } else if ("miter".equals(joinKW.val)) {
|
---|
[3967] | 394 | join = BasicStroke.JOIN_MITER;
|
---|
[7083] | 395 | } else if ("bevel".equals(joinKW.val)) {
|
---|
[3967] | 396 | join = BasicStroke.JOIN_BEVEL;
|
---|
| 397 | }
|
---|
| 398 | }
|
---|
| 399 | if (join == null) {
|
---|
[3860] | 400 | join = BasicStroke.JOIN_ROUND;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[8087] | 403 | float miterlimit = c.get(type.prefix + MITERLIMIT, 10f, Float.class);
|
---|
[3871] | 404 | if (miterlimit < 1f) {
|
---|
| 405 | miterlimit = 10f;
|
---|
| 406 | }
|
---|
[3860] | 407 |
|
---|
| 408 | BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
|
---|
| 409 | BasicStroke dashesLine = null;
|
---|
| 410 |
|
---|
| 411 | if (dashes != null && dashesBackground != null) {
|
---|
| 412 | float[] dashes2 = new float[dashes.length];
|
---|
| 413 | System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1);
|
---|
| 414 | dashes2[0] = dashes[dashes.length-1];
|
---|
| 415 | dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset);
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[9341] | 418 | boolean wayDirectionArrows = c.get(type.prefix + WAY_DIRECTION_ARROWS, env.osm.isSelected(), Boolean.class);
|
---|
| 419 |
|
---|
| 420 | return new LineElement(c, type.defaultMajorZIndex, line, color, dashesLine, dashesBackground,
|
---|
| 421 | offset, realWidth, wayDirectionArrows);
|
---|
[3836] | 422 | }
|
---|
| 423 |
|
---|
[10374] | 424 | private static Float computeWidth(LineType type, Cascade c, Cascade cDef) {
|
---|
| 425 | Float width;
|
---|
| 426 | switch (type) {
|
---|
| 427 | case NORMAL:
|
---|
| 428 | width = getWidth(c, WIDTH, getWidth(cDef, WIDTH, null));
|
---|
| 429 | break;
|
---|
| 430 | case CASING:
|
---|
| 431 | Float casingWidth = c.get(type.prefix + WIDTH, null, Float.class, true);
|
---|
| 432 | if (casingWidth == null) {
|
---|
| 433 | RelativeFloat relCasingWidth = c.get(type.prefix + WIDTH, null, RelativeFloat.class, true);
|
---|
| 434 | if (relCasingWidth != null) {
|
---|
| 435 | casingWidth = relCasingWidth.val / 2;
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 | if (casingWidth == null)
|
---|
| 439 | return null;
|
---|
[11553] | 440 | width = Optional.ofNullable(getWidth(c, WIDTH, getWidth(cDef, WIDTH, null))).orElse(0f) + 2 * casingWidth;
|
---|
[10374] | 441 | break;
|
---|
| 442 | case LEFT_CASING:
|
---|
| 443 | case RIGHT_CASING:
|
---|
| 444 | width = getWidth(c, type.prefix + WIDTH, null);
|
---|
| 445 | break;
|
---|
| 446 | default:
|
---|
| 447 | throw new AssertionError();
|
---|
[9341] | 448 | }
|
---|
[10374] | 449 | return width;
|
---|
| 450 | }
|
---|
[2675] | 451 |
|
---|
[10374] | 452 | private static float computeRealWidth(Environment env, LineType type, Cascade c) {
|
---|
| 453 | float realWidth = c.get(type.prefix + REAL_WIDTH, 0f, Float.class);
|
---|
| 454 | if (realWidth > 0 && MapPaintSettings.INSTANCE.isUseRealWidth()) {
|
---|
| 455 |
|
---|
| 456 | /* if we have a "width" tag, try use it */
|
---|
[11553] | 457 | String widthTag = Optional.ofNullable(env.osm.get("width")).orElseGet(() -> env.osm.get("est_width"));
|
---|
[10374] | 458 | if (widthTag != null) {
|
---|
| 459 | try {
|
---|
| 460 | realWidth = Float.parseFloat(widthTag);
|
---|
| 461 | } catch (NumberFormatException nfe) {
|
---|
[12620] | 462 | Logging.warn(nfe);
|
---|
[2675] | 463 | }
|
---|
| 464 | }
|
---|
| 465 | }
|
---|
[10374] | 466 | return realWidth;
|
---|
[2675] | 467 | }
|
---|
| 468 |
|
---|
[10374] | 469 | private static Float computeOffset(LineType type, Cascade c, Cascade cDef, Float width) {
|
---|
| 470 | Float offset = c.get(OFFSET, 0f, Float.class);
|
---|
| 471 | switch (type) {
|
---|
| 472 | case NORMAL:
|
---|
| 473 | break;
|
---|
| 474 | case CASING:
|
---|
| 475 | offset += c.get(type.prefix + OFFSET, 0f, Float.class);
|
---|
| 476 | break;
|
---|
| 477 | case LEFT_CASING:
|
---|
| 478 | case RIGHT_CASING:
|
---|
| 479 | Float baseWidthOnDefault = getWidth(cDef, WIDTH, null);
|
---|
| 480 | Float baseWidth = getWidth(c, WIDTH, baseWidthOnDefault);
|
---|
| 481 | if (baseWidth == null || baseWidth < 2f) {
|
---|
| 482 | baseWidth = 2f;
|
---|
| 483 | }
|
---|
| 484 | float casingOffset = c.get(type.prefix + OFFSET, 0f, Float.class);
|
---|
| 485 | casingOffset += baseWidth / 2 + width / 2;
|
---|
| 486 | /* flip sign for the right-casing-offset */
|
---|
| 487 | if (type == LineType.RIGHT_CASING) {
|
---|
| 488 | casingOffset *= -1f;
|
---|
| 489 | }
|
---|
| 490 | offset += casingOffset;
|
---|
| 491 | break;
|
---|
[4281] | 492 | }
|
---|
[10374] | 493 | return offset;
|
---|
[4281] | 494 | }
|
---|
[626] | 495 | }
|
---|