1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor.paint;
|
---|
3 |
|
---|
4 | import java.awt.AlphaComposite;
|
---|
5 | import java.awt.BasicStroke;
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.FontMetrics;
|
---|
11 | import java.awt.Graphics2D;
|
---|
12 | import java.awt.Image;
|
---|
13 | import java.awt.Point;
|
---|
14 | import java.awt.Rectangle;
|
---|
15 | import java.awt.RenderingHints;
|
---|
16 | import java.awt.Shape;
|
---|
17 | import java.awt.TexturePaint;
|
---|
18 | import java.awt.font.FontRenderContext;
|
---|
19 | import java.awt.font.GlyphVector;
|
---|
20 | import java.awt.font.LineMetrics;
|
---|
21 | import java.awt.font.TextLayout;
|
---|
22 | import java.awt.geom.AffineTransform;
|
---|
23 | import java.awt.geom.Path2D;
|
---|
24 | import java.awt.geom.Point2D;
|
---|
25 | import java.awt.geom.Rectangle2D;
|
---|
26 | import java.awt.geom.RoundRectangle2D;
|
---|
27 | import java.awt.image.BufferedImage;
|
---|
28 | import java.util.ArrayList;
|
---|
29 | import java.util.Collection;
|
---|
30 | import java.util.Collections;
|
---|
31 | import java.util.Comparator;
|
---|
32 | import java.util.HashMap;
|
---|
33 | import java.util.Iterator;
|
---|
34 | import java.util.List;
|
---|
35 | import java.util.Map;
|
---|
36 | import java.util.Optional;
|
---|
37 | import java.util.concurrent.ForkJoinPool;
|
---|
38 | import java.util.concurrent.ForkJoinTask;
|
---|
39 | import java.util.concurrent.RecursiveTask;
|
---|
40 | import java.util.function.BiConsumer;
|
---|
41 | import java.util.function.Supplier;
|
---|
42 | import java.util.stream.Collectors;
|
---|
43 |
|
---|
44 | import javax.swing.AbstractButton;
|
---|
45 | import javax.swing.FocusManager;
|
---|
46 |
|
---|
47 | import org.openstreetmap.josm.Main;
|
---|
48 | import org.openstreetmap.josm.data.Bounds;
|
---|
49 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
50 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
51 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
52 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
53 | import org.openstreetmap.josm.data.osm.Node;
|
---|
54 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
55 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
56 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
57 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
58 | import org.openstreetmap.josm.data.osm.Way;
|
---|
59 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
60 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
61 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
|
---|
62 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.PolyData;
|
---|
63 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
|
---|
64 | import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
|
---|
65 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
66 | import org.openstreetmap.josm.gui.draw.MapViewPath;
|
---|
67 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
68 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
69 | import org.openstreetmap.josm.gui.mappaint.StyleElementList;
|
---|
70 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
71 | import org.openstreetmap.josm.gui.mappaint.styleelement.AreaElement;
|
---|
72 | import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement;
|
---|
73 | import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.HorizontalTextAlignment;
|
---|
74 | import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.VerticalTextAlignment;
|
---|
75 | import org.openstreetmap.josm.gui.mappaint.styleelement.MapImage;
|
---|
76 | import org.openstreetmap.josm.gui.mappaint.styleelement.NodeElement;
|
---|
77 | import org.openstreetmap.josm.gui.mappaint.styleelement.RepeatImageElement.LineImageAlignment;
|
---|
78 | import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;
|
---|
79 | import org.openstreetmap.josm.gui.mappaint.styleelement.Symbol;
|
---|
80 | import org.openstreetmap.josm.gui.mappaint.styleelement.TextLabel;
|
---|
81 | import org.openstreetmap.josm.tools.CompositeList;
|
---|
82 | import org.openstreetmap.josm.tools.Geometry;
|
---|
83 | import org.openstreetmap.josm.tools.Geometry.AreaAndPerimeter;
|
---|
84 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
85 | import org.openstreetmap.josm.tools.Utils;
|
---|
86 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * A map renderer which renders a map according to style rules in a set of style sheets.
|
---|
90 | * @since 486
|
---|
91 | */
|
---|
92 | public class StyledMapRenderer extends AbstractMapRenderer {
|
---|
93 |
|
---|
94 | private static final ForkJoinPool THREAD_POOL =
|
---|
95 | Utils.newForkJoinPool("mappaint.StyledMapRenderer.style_creation.numberOfThreads", "styled-map-renderer-%d", Thread.NORM_PRIORITY);
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * This stores a style and a primitive that should be painted with that style.
|
---|
99 | */
|
---|
100 | public static class StyleRecord implements Comparable<StyleRecord> {
|
---|
101 | private final StyleElement style;
|
---|
102 | private final OsmPrimitive osm;
|
---|
103 | private final int flags;
|
---|
104 |
|
---|
105 | StyleRecord(StyleElement style, OsmPrimitive osm, int flags) {
|
---|
106 | this.style = style;
|
---|
107 | this.osm = osm;
|
---|
108 | this.flags = flags;
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public int compareTo(StyleRecord other) {
|
---|
113 | if ((this.flags & FLAG_DISABLED) != 0 && (other.flags & FLAG_DISABLED) == 0)
|
---|
114 | return -1;
|
---|
115 | if ((this.flags & FLAG_DISABLED) == 0 && (other.flags & FLAG_DISABLED) != 0)
|
---|
116 | return 1;
|
---|
117 |
|
---|
118 | int d0 = Float.compare(this.style.majorZIndex, other.style.majorZIndex);
|
---|
119 | if (d0 != 0)
|
---|
120 | return d0;
|
---|
121 |
|
---|
122 | // selected on top of member of selected on top of unselected
|
---|
123 | // FLAG_DISABLED bit is the same at this point
|
---|
124 | if (this.flags > other.flags)
|
---|
125 | return 1;
|
---|
126 | if (this.flags < other.flags)
|
---|
127 | return -1;
|
---|
128 |
|
---|
129 | int dz = Float.compare(this.style.zIndex, other.style.zIndex);
|
---|
130 | if (dz != 0)
|
---|
131 | return dz;
|
---|
132 |
|
---|
133 | // simple node on top of icons and shapes
|
---|
134 | if (NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(this.style) && !NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(other.style))
|
---|
135 | return 1;
|
---|
136 | if (!NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(this.style) && NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(other.style))
|
---|
137 | return -1;
|
---|
138 |
|
---|
139 | // newer primitives to the front
|
---|
140 | long id = this.osm.getUniqueId() - other.osm.getUniqueId();
|
---|
141 | if (id > 0)
|
---|
142 | return 1;
|
---|
143 | if (id < 0)
|
---|
144 | return -1;
|
---|
145 |
|
---|
146 | return Float.compare(this.style.objectZIndex, other.style.objectZIndex);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Get the style for this style element.
|
---|
151 | * @return The style
|
---|
152 | */
|
---|
153 | public StyleElement getStyle() {
|
---|
154 | return style;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Paints the primitive with the style.
|
---|
159 | * @param paintSettings The settings to use.
|
---|
160 | * @param painter The painter to paint the style.
|
---|
161 | */
|
---|
162 | public void paintPrimitive(MapPaintSettings paintSettings, StyledMapRenderer painter) {
|
---|
163 | style.paintPrimitive(
|
---|
164 | osm,
|
---|
165 | paintSettings,
|
---|
166 | painter,
|
---|
167 | (flags & FLAG_SELECTED) != 0,
|
---|
168 | (flags & FLAG_OUTERMEMBER_OF_SELECTED) != 0,
|
---|
169 | (flags & FLAG_MEMBER_OF_SELECTED) != 0
|
---|
170 | );
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public String toString() {
|
---|
175 | return "StyleRecord [style=" + style + ", osm=" + osm + ", flags=" + flags + "]";
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | private static Map<Font, Boolean> IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG = new HashMap<>();
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Check, if this System has the GlyphVector double translation bug.
|
---|
183 | *
|
---|
184 | * With this bug, <code>gv.setGlyphTransform(i, trfm)</code> has a different
|
---|
185 | * effect than on most other systems, namely the translation components
|
---|
186 | * ("m02" & "m12", {@link AffineTransform}) appear to be twice as large, as
|
---|
187 | * they actually are. The rotation is unaffected (scale & shear not tested
|
---|
188 | * so far).
|
---|
189 | *
|
---|
190 | * This bug has only been observed on Mac OS X, see #7841.
|
---|
191 | *
|
---|
192 | * After switch to Java 7, this test is a false positive on Mac OS X (see #10446),
|
---|
193 | * i.e. it returns true, but the real rendering code does not require any special
|
---|
194 | * handling.
|
---|
195 | * It hasn't been further investigated why the test reports a wrong result in
|
---|
196 | * this case, but the method has been changed to simply return false by default.
|
---|
197 | * (This can be changed with a setting in the advanced preferences.)
|
---|
198 | *
|
---|
199 | * @param font The font to check.
|
---|
200 | * @return false by default, but depends on the value of the advanced
|
---|
201 | * preference glyph-bug=false|true|auto, where auto is the automatic detection
|
---|
202 | * method which apparently no longer gives a useful result for Java 7.
|
---|
203 | */
|
---|
204 | public static boolean isGlyphVectorDoubleTranslationBug(Font font) {
|
---|
205 | Boolean cached = IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG.get(font);
|
---|
206 | if (cached != null)
|
---|
207 | return cached;
|
---|
208 | String overridePref = Main.pref.get("glyph-bug", "auto");
|
---|
209 | if ("auto".equals(overridePref)) {
|
---|
210 | FontRenderContext frc = new FontRenderContext(null, false, false);
|
---|
211 | GlyphVector gv = font.createGlyphVector(frc, "x");
|
---|
212 | gv.setGlyphTransform(0, AffineTransform.getTranslateInstance(1000, 1000));
|
---|
213 | Shape shape = gv.getGlyphOutline(0);
|
---|
214 | if (Main.isTraceEnabled()) {
|
---|
215 | Main.trace("#10446: shape: "+shape.getBounds());
|
---|
216 | }
|
---|
217 | // x is about 1000 on normal stystems and about 2000 when the bug occurs
|
---|
218 | int x = shape.getBounds().x;
|
---|
219 | boolean isBug = x > 1500;
|
---|
220 | IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG.put(font, isBug);
|
---|
221 | return isBug;
|
---|
222 | } else {
|
---|
223 | boolean override = Boolean.parseBoolean(overridePref);
|
---|
224 | IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG.put(font, override);
|
---|
225 | return override;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private double circum;
|
---|
230 | private double scale;
|
---|
231 |
|
---|
232 | private MapPaintSettings paintSettings;
|
---|
233 |
|
---|
234 | private Color highlightColorTransparent;
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Flags used to store the primitive state along with the style. This is the normal style.
|
---|
238 | * <p>
|
---|
239 | * Not used in any public interfaces.
|
---|
240 | */
|
---|
241 | private static final int FLAG_NORMAL = 0;
|
---|
242 | /**
|
---|
243 | * A primitive with {@link OsmPrimitive#isDisabled()}
|
---|
244 | */
|
---|
245 | private static final int FLAG_DISABLED = 1;
|
---|
246 | /**
|
---|
247 | * A primitive with {@link OsmPrimitive#isMemberOfSelected()}
|
---|
248 | */
|
---|
249 | private static final int FLAG_MEMBER_OF_SELECTED = 2;
|
---|
250 | /**
|
---|
251 | * A primitive with {@link OsmPrimitive#isSelected()}
|
---|
252 | */
|
---|
253 | private static final int FLAG_SELECTED = 4;
|
---|
254 | /**
|
---|
255 | * A primitive with {@link OsmPrimitive#isOuterMemberOfSelected()}
|
---|
256 | */
|
---|
257 | private static final int FLAG_OUTERMEMBER_OF_SELECTED = 8;
|
---|
258 |
|
---|
259 | private static final double PHI = Math.toRadians(20);
|
---|
260 | private static final double cosPHI = Math.cos(PHI);
|
---|
261 | private static final double sinPHI = Math.sin(PHI);
|
---|
262 |
|
---|
263 | private Collection<WaySegment> highlightWaySegments;
|
---|
264 |
|
---|
265 | // highlight customization fields
|
---|
266 | private int highlightLineWidth;
|
---|
267 | private int highlightPointRadius;
|
---|
268 | private int widerHighlight;
|
---|
269 | private int highlightStep;
|
---|
270 |
|
---|
271 | //flag that activate wider highlight mode
|
---|
272 | private boolean useWiderHighlight;
|
---|
273 |
|
---|
274 | private boolean useStrokes;
|
---|
275 | private boolean showNames;
|
---|
276 | private boolean showIcons;
|
---|
277 | private boolean isOutlineOnly;
|
---|
278 |
|
---|
279 | private Font orderFont;
|
---|
280 |
|
---|
281 | private boolean leftHandTraffic;
|
---|
282 | private Object antialiasing;
|
---|
283 |
|
---|
284 | private Supplier<RenderBenchmarkCollector> benchmarkFactory = RenderBenchmarkCollector.defaultBenchmarkSupplier();
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Constructs a new {@code StyledMapRenderer}.
|
---|
288 | *
|
---|
289 | * @param g the graphics context. Must not be null.
|
---|
290 | * @param nc the map viewport. Must not be null.
|
---|
291 | * @param isInactiveMode if true, the paint visitor shall render OSM objects such that they
|
---|
292 | * look inactive. Example: rendering of data in an inactive layer using light gray as color only.
|
---|
293 | * @throws IllegalArgumentException if {@code g} is null
|
---|
294 | * @throws IllegalArgumentException if {@code nc} is null
|
---|
295 | */
|
---|
296 | public StyledMapRenderer(Graphics2D g, NavigatableComponent nc, boolean isInactiveMode) {
|
---|
297 | super(g, nc, isInactiveMode);
|
---|
298 |
|
---|
299 | if (nc != null) {
|
---|
300 | Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
|
---|
301 | useWiderHighlight = !(focusOwner instanceof AbstractButton || focusOwner == nc);
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | private void displaySegments(MapViewPath path, Path2D orientationArrows, Path2D onewayArrows, Path2D onewayArrowsCasing,
|
---|
306 | Color color, BasicStroke line, BasicStroke dashes, Color dashedColor) {
|
---|
307 | g.setColor(isInactiveMode ? inactiveColor : color);
|
---|
308 | if (useStrokes) {
|
---|
309 | g.setStroke(line);
|
---|
310 | }
|
---|
311 | g.draw(path.computeClippedLine(g.getStroke()));
|
---|
312 |
|
---|
313 | if (!isInactiveMode && useStrokes && dashes != null) {
|
---|
314 | g.setColor(dashedColor);
|
---|
315 | g.setStroke(dashes);
|
---|
316 | g.draw(path.computeClippedLine(dashes));
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (orientationArrows != null) {
|
---|
320 | g.setColor(isInactiveMode ? inactiveColor : color);
|
---|
321 | g.setStroke(new BasicStroke(line.getLineWidth(), line.getEndCap(), BasicStroke.JOIN_MITER, line.getMiterLimit()));
|
---|
322 | g.draw(orientationArrows);
|
---|
323 | }
|
---|
324 |
|
---|
325 | if (onewayArrows != null) {
|
---|
326 | g.setStroke(new BasicStroke(1, line.getEndCap(), BasicStroke.JOIN_MITER, line.getMiterLimit()));
|
---|
327 | g.fill(onewayArrowsCasing);
|
---|
328 | g.setColor(isInactiveMode ? inactiveColor : backgroundColor);
|
---|
329 | g.fill(onewayArrows);
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (useStrokes) {
|
---|
333 | g.setStroke(new BasicStroke());
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Displays text at specified position including its halo, if applicable.
|
---|
339 | *
|
---|
340 | * @param gv Text's glyphs to display. If {@code null}, use text from {@code s} instead.
|
---|
341 | * @param s text to display if {@code gv} is {@code null}
|
---|
342 | * @param x X position
|
---|
343 | * @param y Y position
|
---|
344 | * @param disabled {@code true} if element is disabled (filtered out)
|
---|
345 | * @param text text style to use
|
---|
346 | */
|
---|
347 | private void displayText(GlyphVector gv, String s, int x, int y, boolean disabled, TextLabel text) {
|
---|
348 | if (gv == null && s.isEmpty()) return;
|
---|
349 | if (isInactiveMode || disabled) {
|
---|
350 | g.setColor(inactiveColor);
|
---|
351 | if (gv != null) {
|
---|
352 | g.drawGlyphVector(gv, x, y);
|
---|
353 | } else {
|
---|
354 | g.setFont(text.font);
|
---|
355 | g.drawString(s, x, y);
|
---|
356 | }
|
---|
357 | } else if (text.haloRadius != null) {
|
---|
358 | g.setStroke(new BasicStroke(2*text.haloRadius, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
|
---|
359 | g.setColor(text.haloColor);
|
---|
360 | Shape textOutline;
|
---|
361 | if (gv == null) {
|
---|
362 | FontRenderContext frc = g.getFontRenderContext();
|
---|
363 | TextLayout tl = new TextLayout(s, text.font, frc);
|
---|
364 | textOutline = tl.getOutline(AffineTransform.getTranslateInstance(x, y));
|
---|
365 | } else {
|
---|
366 | textOutline = gv.getOutline(x, y);
|
---|
367 | }
|
---|
368 | g.draw(textOutline);
|
---|
369 | g.setStroke(new BasicStroke());
|
---|
370 | g.setColor(text.color);
|
---|
371 | g.fill(textOutline);
|
---|
372 | } else {
|
---|
373 | g.setColor(text.color);
|
---|
374 | if (gv != null) {
|
---|
375 | g.drawGlyphVector(gv, x, y);
|
---|
376 | } else {
|
---|
377 | g.setFont(text.font);
|
---|
378 | g.drawString(s, x, y);
|
---|
379 | }
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Worker function for drawing areas.
|
---|
385 | *
|
---|
386 | * @param osm the primitive
|
---|
387 | * @param path the path object for the area that should be drawn; in case
|
---|
388 | * of multipolygons, this can path can be a complex shape with one outer
|
---|
389 | * polygon and one or more inner polygons
|
---|
390 | * @param color The color to fill the area with.
|
---|
391 | * @param fillImage The image to fill the area with. Overrides color.
|
---|
392 | * @param extent if not null, area will be filled partially; specifies, how
|
---|
393 | * far to fill from the boundary towards the center of the area;
|
---|
394 | * if null, area will be filled completely
|
---|
395 | * @param pfClip clipping area for partial fill (only needed for unclosed
|
---|
396 | * polygons)
|
---|
397 | * @param disabled If this should be drawn with a special disabled style.
|
---|
398 | * @param text The text to write on the area.
|
---|
399 | */
|
---|
400 | protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color,
|
---|
401 | MapImage fillImage, Float extent, Path2D.Double pfClip, boolean disabled, TextLabel text) {
|
---|
402 |
|
---|
403 | Shape area = path.createTransformedShape(mapState.getAffineTransform());
|
---|
404 |
|
---|
405 | if (color.getAlpha() == 0) {
|
---|
406 | // skip drawing
|
---|
407 | } else if (!isOutlineOnly) {
|
---|
408 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
|
---|
409 | if (fillImage == null) {
|
---|
410 | if (isInactiveMode) {
|
---|
411 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.33f));
|
---|
412 | }
|
---|
413 | g.setColor(color);
|
---|
414 | if (extent == null) {
|
---|
415 | g.fill(area);
|
---|
416 | } else {
|
---|
417 | Shape oldClip = g.getClip();
|
---|
418 | Shape clip = area;
|
---|
419 | if (pfClip != null) {
|
---|
420 | clip = pfClip.createTransformedShape(mapState.getAffineTransform());
|
---|
421 | }
|
---|
422 | g.clip(clip);
|
---|
423 | g.setStroke(new BasicStroke(2 * extent, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 4));
|
---|
424 | g.draw(area);
|
---|
425 | g.setClip(oldClip);
|
---|
426 | }
|
---|
427 | } else {
|
---|
428 | TexturePaint texture = new TexturePaint(fillImage.getImage(disabled),
|
---|
429 | new Rectangle(0, 0, fillImage.getWidth(), fillImage.getHeight()));
|
---|
430 | g.setPaint(texture);
|
---|
431 | Float alpha = fillImage.getAlphaFloat();
|
---|
432 | if (!Utils.equalsEpsilon(alpha, 1f)) {
|
---|
433 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
|
---|
434 | }
|
---|
435 | if (extent == null) {
|
---|
436 | g.fill(area);
|
---|
437 | } else {
|
---|
438 | Shape oldClip = g.getClip();
|
---|
439 | BasicStroke stroke = new BasicStroke(2 * extent, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
|
---|
440 | g.clip(stroke.createStrokedShape(area));
|
---|
441 | Shape fill = area;
|
---|
442 | if (pfClip != null) {
|
---|
443 | fill = pfClip.createTransformedShape(mapState.getAffineTransform());
|
---|
444 | }
|
---|
445 | g.fill(fill);
|
---|
446 | g.setClip(oldClip);
|
---|
447 | }
|
---|
448 | g.setPaintMode();
|
---|
449 | }
|
---|
450 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing);
|
---|
451 | }
|
---|
452 |
|
---|
453 | drawAreaText(osm, text, area);
|
---|
454 | }
|
---|
455 |
|
---|
456 | private void drawAreaText(OsmPrimitive osm, TextLabel text, Shape area) {
|
---|
457 | if (text != null && isShowNames()) {
|
---|
458 | // abort if we can't compose the label to be rendered
|
---|
459 | if (text.labelCompositionStrategy == null) return;
|
---|
460 | String name = text.labelCompositionStrategy.compose(osm);
|
---|
461 | if (name == null || name.isEmpty()) return;
|
---|
462 |
|
---|
463 | FontMetrics fontMetrics = g.getFontMetrics(orderFont); // if slow, use cache
|
---|
464 | Rectangle2D nb = fontMetrics.getStringBounds(name, g); // if slow, approximate by strlen()*maxcharbounds(font)
|
---|
465 |
|
---|
466 | Rectangle centeredNBounds = findLabelPlacement(area, nb);
|
---|
467 | if (centeredNBounds != null) {
|
---|
468 | Font defaultFont = g.getFont();
|
---|
469 | int x = (int) (centeredNBounds.getMinX() - nb.getMinX());
|
---|
470 | int y = (int) (centeredNBounds.getMinY() - nb.getMinY());
|
---|
471 | displayText(null, name, x, y, osm.isDisabled(), text);
|
---|
472 | g.setFont(defaultFont);
|
---|
473 | } else if (Main.isTraceEnabled()) {
|
---|
474 | Main.trace("Couldn't find a correct label placement for "+osm+" / "+name);
|
---|
475 | }
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Finds the correct position of a label / icon inside the area.
|
---|
481 | * @param area The area to search in
|
---|
482 | * @param nb The bounding box of the thing we are searching a place for.
|
---|
483 | * @return The position as rectangle with the same dimension as nb. <code>null</code> if none was found.
|
---|
484 | */
|
---|
485 | private Rectangle findLabelPlacement(Shape area, Rectangle2D nb) {
|
---|
486 | // Using the Centroid is Nicer for buildings like: +--------+
|
---|
487 | // but this needs to be fast. As most houses are | 42 |
|
---|
488 | // boxes anyway, the center of the bounding box +---++---+
|
---|
489 | // will have to do. ++
|
---|
490 | // Centroids are not optimal either, just imagine a U-shaped house.
|
---|
491 |
|
---|
492 | Rectangle pb = area.getBounds();
|
---|
493 |
|
---|
494 | // quick check to see if label box is smaller than primitive box
|
---|
495 | if (pb.width < nb.getWidth() || pb.height < nb.getHeight()) {
|
---|
496 | return null;
|
---|
497 | }
|
---|
498 |
|
---|
499 | final double w = pb.width - nb.getWidth();
|
---|
500 | final double h = pb.height - nb.getHeight();
|
---|
501 |
|
---|
502 | final int x2 = pb.x + (int) (w/2.0);
|
---|
503 | final int y2 = pb.y + (int) (h/2.0);
|
---|
504 |
|
---|
505 | final int nbw = (int) nb.getWidth();
|
---|
506 | final int nbh = (int) nb.getHeight();
|
---|
507 |
|
---|
508 | Rectangle centeredNBounds = new Rectangle(x2, y2, nbw, nbh);
|
---|
509 |
|
---|
510 | // slower check to see if label is displayed inside primitive shape
|
---|
511 | if (area.contains(centeredNBounds)) {
|
---|
512 | return centeredNBounds;
|
---|
513 | }
|
---|
514 |
|
---|
515 | // if center position (C) is not inside osm shape, try naively some other positions as follows:
|
---|
516 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
517 | final int x1 = pb.x + (int) (w/4.0);
|
---|
518 | final int x3 = pb.x + (int) (3*w/4.0);
|
---|
519 | final int y1 = pb.y + (int) (h/4.0);
|
---|
520 | final int y3 = pb.y + (int) (3*h/4.0);
|
---|
521 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
522 | // +-----------+
|
---|
523 | // | 5 1 6 |
|
---|
524 | // | 4 C 2 |
|
---|
525 | // | 8 3 7 |
|
---|
526 | // +-----------+
|
---|
527 | Rectangle[] candidates = new Rectangle[] {
|
---|
528 | new Rectangle(x2, y1, nbw, nbh),
|
---|
529 | new Rectangle(x3, y2, nbw, nbh),
|
---|
530 | new Rectangle(x2, y3, nbw, nbh),
|
---|
531 | new Rectangle(x1, y2, nbw, nbh),
|
---|
532 | new Rectangle(x1, y1, nbw, nbh),
|
---|
533 | new Rectangle(x3, y1, nbw, nbh),
|
---|
534 | new Rectangle(x3, y3, nbw, nbh),
|
---|
535 | new Rectangle(x1, y3, nbw, nbh)
|
---|
536 | };
|
---|
537 | // Dumb algorithm to find a better placement. We could surely find a smarter one but it should
|
---|
538 | // solve most of building issues with only few calculations (8 at most)
|
---|
539 | for (int i = 0; i < candidates.length; i++) {
|
---|
540 | centeredNBounds = candidates[i];
|
---|
541 | if (area.contains(centeredNBounds)) {
|
---|
542 | return centeredNBounds;
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 | // none found
|
---|
547 | return null;
|
---|
548 | }
|
---|
549 |
|
---|
550 | /**
|
---|
551 | * Draws a multipolygon area.
|
---|
552 | * @param r The multipolygon relation
|
---|
553 | * @param color The color to fill the area with.
|
---|
554 | * @param fillImage The image to fill the area with. Overrides color.
|
---|
555 | * @param extent if not null, area will be filled partially; specifies, how
|
---|
556 | * far to fill from the boundary towards the center of the area;
|
---|
557 | * if null, area will be filled completely
|
---|
558 | * @param extentThreshold if not null, determines if the partial filled should
|
---|
559 | * be replaced by plain fill, when it covers a certain fraction of the total area
|
---|
560 | * @param disabled If this should be drawn with a special disabled style.
|
---|
561 | * @param text The text to write on the area.
|
---|
562 | */
|
---|
563 | public void drawArea(Relation r, Color color, MapImage fillImage, Float extent, Float extentThreshold, boolean disabled, TextLabel text) {
|
---|
564 | Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, r);
|
---|
565 | if (!r.isDisabled() && !multipolygon.getOuterWays().isEmpty()) {
|
---|
566 | for (PolyData pd : multipolygon.getCombinedPolygons()) {
|
---|
567 | Path2D.Double p = pd.get();
|
---|
568 | Path2D.Double pfClip = null;
|
---|
569 | if (!isAreaVisible(p)) {
|
---|
570 | continue;
|
---|
571 | }
|
---|
572 | if (extent != null) {
|
---|
573 | if (!usePartialFill(pd.getAreaAndPerimeter(null), extent, extentThreshold)) {
|
---|
574 | extent = null;
|
---|
575 | } else if (!pd.isClosed()) {
|
---|
576 | pfClip = getPFClip(pd, extent * scale);
|
---|
577 | }
|
---|
578 | }
|
---|
579 | drawArea(r, p,
|
---|
580 | pd.isSelected() ? paintSettings.getRelationSelectedColor(color.getAlpha()) : color,
|
---|
581 | fillImage, extent, pfClip, disabled, text);
|
---|
582 | }
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Draws an area defined by a way. They way does not need to be closed, but it should.
|
---|
588 | * @param w The way.
|
---|
589 | * @param color The color to fill the area with.
|
---|
590 | * @param fillImage The image to fill the area with. Overrides color.
|
---|
591 | * @param extent if not null, area will be filled partially; specifies, how
|
---|
592 | * far to fill from the boundary towards the center of the area;
|
---|
593 | * if null, area will be filled completely
|
---|
594 | * @param extentThreshold if not null, determines if the partial filled should
|
---|
595 | * be replaced by plain fill, when it covers a certain fraction of the total area
|
---|
596 | * @param disabled If this should be drawn with a special disabled style.
|
---|
597 | * @param text The text to write on the area.
|
---|
598 | */
|
---|
599 | public void drawArea(Way w, Color color, MapImage fillImage, Float extent, Float extentThreshold, boolean disabled, TextLabel text) {
|
---|
600 | Path2D.Double pfClip = null;
|
---|
601 | if (extent != null) {
|
---|
602 | if (!usePartialFill(Geometry.getAreaAndPerimeter(w.getNodes()), extent, extentThreshold)) {
|
---|
603 | extent = null;
|
---|
604 | } else if (!w.isClosed()) {
|
---|
605 | pfClip = getPFClip(w, extent * scale);
|
---|
606 | }
|
---|
607 | }
|
---|
608 | drawArea(w, getPath(w), color, fillImage, extent, pfClip, disabled, text);
|
---|
609 | }
|
---|
610 |
|
---|
611 | /**
|
---|
612 | * Determine, if partial fill should be turned off for this object, because
|
---|
613 | * only a small unfilled gap in the center of the area would be left.
|
---|
614 | *
|
---|
615 | * This is used to get a cleaner look for urban regions with many small
|
---|
616 | * areas like buildings, etc.
|
---|
617 | * @param ap the area and the perimeter of the object
|
---|
618 | * @param extent the "width" of partial fill
|
---|
619 | * @param threshold when the partial fill covers that much of the total
|
---|
620 | * area, the partial fill is turned off; can be greater than 100% as the
|
---|
621 | * covered area is estimated as <code>perimeter * extent</code>
|
---|
622 | * @return true, if the partial fill should be used, false otherwise
|
---|
623 | */
|
---|
624 | private boolean usePartialFill(AreaAndPerimeter ap, float extent, Float threshold) {
|
---|
625 | if (threshold == null) return true;
|
---|
626 | return ap.getPerimeter() * extent * scale < threshold * ap.getArea();
|
---|
627 | }
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * Draw a text onto a node
|
---|
631 | * @param n The node to draw the text on
|
---|
632 | * @param bs The text and it's alignment.
|
---|
633 | */
|
---|
634 | public void drawBoxText(Node n, BoxTextElement bs) {
|
---|
635 | if (!isShowNames() || bs == null)
|
---|
636 | return;
|
---|
637 |
|
---|
638 | MapViewPoint p = mapState.getPointFor(n);
|
---|
639 | TextLabel text = bs.text;
|
---|
640 | String s = text.labelCompositionStrategy.compose(n);
|
---|
641 | if (s == null) return;
|
---|
642 |
|
---|
643 | Font defaultFont = g.getFont();
|
---|
644 | g.setFont(text.font);
|
---|
645 |
|
---|
646 | int x = (int) (Math.round(p.getInViewX()) + text.xOffset);
|
---|
647 | int y = (int) (Math.round(p.getInViewY()) + text.yOffset);
|
---|
648 | /**
|
---|
649 | *
|
---|
650 | * left-above __center-above___ right-above
|
---|
651 | * left-top| |right-top
|
---|
652 | * | |
|
---|
653 | * left-center| center-center |right-center
|
---|
654 | * | |
|
---|
655 | * left-bottom|_________________|right-bottom
|
---|
656 | * left-below center-below right-below
|
---|
657 | *
|
---|
658 | */
|
---|
659 | Rectangle box = bs.getBox();
|
---|
660 | if (bs.hAlign == HorizontalTextAlignment.RIGHT) {
|
---|
661 | x += box.x + box.width + 2;
|
---|
662 | } else {
|
---|
663 | FontRenderContext frc = g.getFontRenderContext();
|
---|
664 | Rectangle2D bounds = text.font.getStringBounds(s, frc);
|
---|
665 | int textWidth = (int) bounds.getWidth();
|
---|
666 | if (bs.hAlign == HorizontalTextAlignment.CENTER) {
|
---|
667 | x -= textWidth / 2;
|
---|
668 | } else if (bs.hAlign == HorizontalTextAlignment.LEFT) {
|
---|
669 | x -= -box.x + 4 + textWidth;
|
---|
670 | } else throw new AssertionError();
|
---|
671 | }
|
---|
672 |
|
---|
673 | if (bs.vAlign == VerticalTextAlignment.BOTTOM) {
|
---|
674 | y += box.y + box.height;
|
---|
675 | } else {
|
---|
676 | FontRenderContext frc = g.getFontRenderContext();
|
---|
677 | LineMetrics metrics = text.font.getLineMetrics(s, frc);
|
---|
678 | if (bs.vAlign == VerticalTextAlignment.ABOVE) {
|
---|
679 | y -= -box.y + (int) metrics.getDescent();
|
---|
680 | } else if (bs.vAlign == VerticalTextAlignment.TOP) {
|
---|
681 | y -= -box.y - (int) metrics.getAscent();
|
---|
682 | } else if (bs.vAlign == VerticalTextAlignment.CENTER) {
|
---|
683 | y += (int) ((metrics.getAscent() - metrics.getDescent()) / 2);
|
---|
684 | } else if (bs.vAlign == VerticalTextAlignment.BELOW) {
|
---|
685 | y += box.y + box.height + (int) metrics.getAscent() + 2;
|
---|
686 | } else throw new AssertionError();
|
---|
687 | }
|
---|
688 | displayText(null, s, x, y, n.isDisabled(), text);
|
---|
689 | g.setFont(defaultFont);
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Draw an image along a way repeatedly.
|
---|
694 | *
|
---|
695 | * @param way the way
|
---|
696 | * @param pattern the image
|
---|
697 | * @param disabled If this should be drawn with a special disabled style.
|
---|
698 | * @param offset offset from the way
|
---|
699 | * @param spacing spacing between two images
|
---|
700 | * @param phase initial spacing
|
---|
701 | * @param align alignment of the image. The top, center or bottom edge can be aligned with the way.
|
---|
702 | */
|
---|
703 | public void drawRepeatImage(Way way, MapImage pattern, boolean disabled, double offset, double spacing, double phase,
|
---|
704 | LineImageAlignment align) {
|
---|
705 | final int imgWidth = pattern.getWidth();
|
---|
706 | final double repeat = imgWidth + spacing;
|
---|
707 | final int imgHeight = pattern.getHeight();
|
---|
708 |
|
---|
709 | int dy1 = (int) ((align.getAlignmentOffset() - .5) * imgHeight);
|
---|
710 | int dy2 = dy1 + imgHeight;
|
---|
711 |
|
---|
712 | OffsetIterator it = new OffsetIterator(mapState, way.getNodes(), offset);
|
---|
713 | MapViewPath path = new MapViewPath(mapState);
|
---|
714 | if (it.hasNext()) {
|
---|
715 | path.moveTo(it.next());
|
---|
716 | }
|
---|
717 | while (it.hasNext()) {
|
---|
718 | path.lineTo(it.next());
|
---|
719 | }
|
---|
720 |
|
---|
721 | double startOffset = phase % repeat;
|
---|
722 | if (startOffset < 0) {
|
---|
723 | startOffset += repeat;
|
---|
724 | }
|
---|
725 |
|
---|
726 | BufferedImage image = pattern.getImage(disabled);
|
---|
727 |
|
---|
728 | path.visitClippedLine(startOffset, repeat, (inLineOffset, start, end, startIsOldEnd) -> {
|
---|
729 | final double segmentLength = start.distanceToInView(end);
|
---|
730 | if (segmentLength < 0.1) {
|
---|
731 | // avoid odd patterns when zoomed out.
|
---|
732 | return;
|
---|
733 | }
|
---|
734 | if (segmentLength > repeat * 500) {
|
---|
735 | // simply skip drawing so many images - something must be wrong.
|
---|
736 | return;
|
---|
737 | }
|
---|
738 | AffineTransform saveTransform = g.getTransform();
|
---|
739 | g.translate(start.getInViewX(), start.getInViewY());
|
---|
740 | double dx = end.getInViewX() - start.getInViewX();
|
---|
741 | double dy = end.getInViewY() - start.getInViewY();
|
---|
742 | g.rotate(Math.atan2(dy, dx));
|
---|
743 |
|
---|
744 | // The start of the next image
|
---|
745 | double imageStart = -(inLineOffset % repeat);
|
---|
746 |
|
---|
747 | while (imageStart < segmentLength) {
|
---|
748 | int x = (int) imageStart;
|
---|
749 | int sx1 = Math.max(0, -x);
|
---|
750 | int sx2 = imgWidth - Math.max(0, x + imgWidth - (int) Math.ceil(segmentLength));
|
---|
751 | g.drawImage(image, x + sx1, dy1, x + sx2, dy2, sx1, 0, sx2, imgHeight, null);
|
---|
752 | imageStart += repeat;
|
---|
753 | }
|
---|
754 |
|
---|
755 | g.setTransform(saveTransform);
|
---|
756 | });
|
---|
757 | }
|
---|
758 |
|
---|
759 | @Override
|
---|
760 | public void drawNode(Node n, Color color, int size, boolean fill) {
|
---|
761 | if (size <= 0 && !n.isHighlighted())
|
---|
762 | return;
|
---|
763 |
|
---|
764 | MapViewPoint p = mapState.getPointFor(n);
|
---|
765 |
|
---|
766 | if (n.isHighlighted()) {
|
---|
767 | drawPointHighlight(p.getInView(), size);
|
---|
768 | }
|
---|
769 |
|
---|
770 | if (size > 1 && p.isInView()) {
|
---|
771 | int radius = size / 2;
|
---|
772 |
|
---|
773 | if (isInactiveMode || n.isDisabled()) {
|
---|
774 | g.setColor(inactiveColor);
|
---|
775 | } else {
|
---|
776 | g.setColor(color);
|
---|
777 | }
|
---|
778 | Rectangle2D rect = new Rectangle2D.Double(p.getInViewX()-radius-1, p.getInViewY()-radius-1, size + 1, size + 1);
|
---|
779 | if (fill) {
|
---|
780 | g.fill(rect);
|
---|
781 | } else {
|
---|
782 | g.draw(rect);
|
---|
783 | }
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | /**
|
---|
788 | * Draw the icon for a given node.
|
---|
789 | * @param n The node
|
---|
790 | * @param img The icon to draw at the node position
|
---|
791 | * @param disabled {@code} true to render disabled version, {@code false} for the standard version
|
---|
792 | * @param selected {@code} true to render it as selected, {@code false} otherwise
|
---|
793 | * @param member {@code} true to render it as a relation member, {@code false} otherwise
|
---|
794 | * @param theta the angle of rotation in radians
|
---|
795 | */
|
---|
796 | public void drawNodeIcon(Node n, MapImage img, boolean disabled, boolean selected, boolean member, double theta) {
|
---|
797 | MapViewPoint p = mapState.getPointFor(n);
|
---|
798 |
|
---|
799 | int w = img.getWidth();
|
---|
800 | int h = img.getHeight();
|
---|
801 | if (n.isHighlighted()) {
|
---|
802 | drawPointHighlight(p.getInView(), Math.max(w, h));
|
---|
803 | }
|
---|
804 |
|
---|
805 | drawIcon(p, img, disabled, selected, member, theta, (g, r) -> {
|
---|
806 | Color color = getSelectionHintColor(disabled, selected);
|
---|
807 | g.setColor(color);
|
---|
808 | g.draw(r);
|
---|
809 | });
|
---|
810 | }
|
---|
811 |
|
---|
812 |
|
---|
813 | /**
|
---|
814 | * Draw the icon for a given area. The icon is drawn around the lat/lon center of the area.
|
---|
815 | * @param primitive The node
|
---|
816 | * @param img The icon to draw at the node position
|
---|
817 | * @param disabled {@code} true to render disabled version, {@code false} for the standard version
|
---|
818 | * @param selected {@code} true to render it as selected, {@code false} otherwise
|
---|
819 | * @param member {@code} true to render it as a relation member, {@code false} otherwise
|
---|
820 | * @param theta the angle of rotation in radians
|
---|
821 | * @since 11670
|
---|
822 | */
|
---|
823 | public void drawAreaIcon(OsmPrimitive primitive, MapImage img, boolean disabled, boolean selected, boolean member, double theta) {
|
---|
824 | BBox bbox = null;
|
---|
825 | if (primitive instanceof Way) {
|
---|
826 | bbox = primitive.getBBox();
|
---|
827 | } else if (primitive instanceof Relation) {
|
---|
828 | Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, (Relation) primitive);
|
---|
829 | if (multipolygon != null) {
|
---|
830 | BBox collect = new BBox();
|
---|
831 | multipolygon.getOuterPolygons().forEach(p -> p.getNodes().forEach(n -> collect.add(n.getCoor())));
|
---|
832 | bbox = collect;
|
---|
833 | }
|
---|
834 | }
|
---|
835 |
|
---|
836 | if (bbox != null && bbox.isValid()) {
|
---|
837 | MapViewPoint p = mapState.getPointFor(bbox.getCenter());
|
---|
838 | drawIcon(p, img, disabled, selected, member, theta, (g, r) -> {
|
---|
839 | if (useStrokes) {
|
---|
840 | g.setStroke(new BasicStroke(2));
|
---|
841 | }
|
---|
842 | // only draw a minor highlighting, so that users do not confuse this for a point.
|
---|
843 | Color color = getSelectionHintColor(disabled, selected);
|
---|
844 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), (int) (color.getAlpha() * .2));
|
---|
845 | g.setColor(color);
|
---|
846 | g.draw(r);
|
---|
847 | });
|
---|
848 | }
|
---|
849 | }
|
---|
850 |
|
---|
851 | private void drawIcon(MapViewPoint p, MapImage img, boolean disabled, boolean selected, boolean member, double theta,
|
---|
852 | BiConsumer<Graphics2D, Rectangle2D> selectionDrawer) {
|
---|
853 | float alpha = img.getAlphaFloat();
|
---|
854 |
|
---|
855 | Graphics2D temporaryGraphics = (Graphics2D) g.create();
|
---|
856 | if (!Utils.equalsEpsilon(alpha, 1f)) {
|
---|
857 | temporaryGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
|
---|
858 | }
|
---|
859 |
|
---|
860 | double x = Math.round(p.getInViewX());
|
---|
861 | double y = Math.round(p.getInViewY());
|
---|
862 | temporaryGraphics.translate(x, y);
|
---|
863 | temporaryGraphics.rotate(theta);
|
---|
864 | int drawX = -img.getWidth() / 2 + img.offsetX;
|
---|
865 | int drawY = -img.getHeight() / 2 + img.offsetY;
|
---|
866 | temporaryGraphics.drawImage(img.getImage(disabled), drawX, drawY, nc);
|
---|
867 | if (selected || member) {
|
---|
868 | selectionDrawer.accept(temporaryGraphics, new Rectangle2D.Double(drawX - 2, drawY - 2, img.getWidth() + 4, img.getHeight() + 4));
|
---|
869 | }
|
---|
870 | }
|
---|
871 |
|
---|
872 | private Color getSelectionHintColor(boolean disabled, boolean selected) {
|
---|
873 | Color color;
|
---|
874 | if (disabled) {
|
---|
875 | color = inactiveColor;
|
---|
876 | } else if (selected) {
|
---|
877 | color = selectedColor;
|
---|
878 | } else {
|
---|
879 | color = relationSelectedColor;
|
---|
880 | }
|
---|
881 | return color;
|
---|
882 | }
|
---|
883 |
|
---|
884 | /**
|
---|
885 | * Draw the symbol and possibly a highlight marking on a given node.
|
---|
886 | * @param n The position to draw the symbol on
|
---|
887 | * @param s The symbol to draw
|
---|
888 | * @param fillColor The color to fill the symbol with
|
---|
889 | * @param strokeColor The color to use for the outer corner of the symbol
|
---|
890 | */
|
---|
891 | public void drawNodeSymbol(Node n, Symbol s, Color fillColor, Color strokeColor) {
|
---|
892 | MapViewPoint p = mapState.getPointFor(n);
|
---|
893 |
|
---|
894 | if (n.isHighlighted()) {
|
---|
895 | drawPointHighlight(p.getInView(), s.size);
|
---|
896 | }
|
---|
897 |
|
---|
898 | if (fillColor != null || strokeColor != null) {
|
---|
899 | Shape shape = s.buildShapeAround(p.getInViewX(), p.getInViewY());
|
---|
900 |
|
---|
901 | if (fillColor != null) {
|
---|
902 | g.setColor(fillColor);
|
---|
903 | g.fill(shape);
|
---|
904 | }
|
---|
905 | if (s.stroke != null) {
|
---|
906 | g.setStroke(s.stroke);
|
---|
907 | g.setColor(strokeColor);
|
---|
908 | g.draw(shape);
|
---|
909 | g.setStroke(new BasicStroke());
|
---|
910 | }
|
---|
911 | }
|
---|
912 | }
|
---|
913 |
|
---|
914 | /**
|
---|
915 | * Draw a number of the order of the two consecutive nodes within the
|
---|
916 | * parents way
|
---|
917 | *
|
---|
918 | * @param n1 First node of the way segment.
|
---|
919 | * @param n2 Second node of the way segment.
|
---|
920 | * @param orderNumber The number of the segment in the way.
|
---|
921 | * @param clr The color to use for drawing the text.
|
---|
922 | */
|
---|
923 | public void drawOrderNumber(Node n1, Node n2, int orderNumber, Color clr) {
|
---|
924 | MapViewPoint p1 = mapState.getPointFor(n1);
|
---|
925 | MapViewPoint p2 = mapState.getPointFor(n2);
|
---|
926 | drawOrderNumber(p1, p2, orderNumber, clr);
|
---|
927 | }
|
---|
928 |
|
---|
929 | /**
|
---|
930 | * highlights a given GeneralPath using the settings from BasicStroke to match the line's
|
---|
931 | * style. Width of the highlight is hard coded.
|
---|
932 | * @param path path to draw
|
---|
933 | * @param line line style
|
---|
934 | */
|
---|
935 | private void drawPathHighlight(MapViewPath path, BasicStroke line) {
|
---|
936 | if (path == null)
|
---|
937 | return;
|
---|
938 | g.setColor(highlightColorTransparent);
|
---|
939 | float w = line.getLineWidth() + highlightLineWidth;
|
---|
940 | if (useWiderHighlight) w += widerHighlight;
|
---|
941 | while (w >= line.getLineWidth()) {
|
---|
942 | g.setStroke(new BasicStroke(w, line.getEndCap(), line.getLineJoin(), line.getMiterLimit()));
|
---|
943 | g.draw(path);
|
---|
944 | w -= highlightStep;
|
---|
945 | }
|
---|
946 | }
|
---|
947 |
|
---|
948 | /**
|
---|
949 | * highlights a given point by drawing a rounded rectangle around it. Give the
|
---|
950 | * size of the object you want to be highlighted, width is added automatically.
|
---|
951 | * @param p point
|
---|
952 | * @param size highlight size
|
---|
953 | */
|
---|
954 | private void drawPointHighlight(Point2D p, int size) {
|
---|
955 | g.setColor(highlightColorTransparent);
|
---|
956 | int s = size + highlightPointRadius;
|
---|
957 | if (useWiderHighlight) s += widerHighlight;
|
---|
958 | while (s >= size) {
|
---|
959 | int r = (int) Math.floor(s/2d);
|
---|
960 | g.fill(new RoundRectangle2D.Double(p.getX()-r, p.getY()-r, s, s, r, r));
|
---|
961 | s -= highlightStep;
|
---|
962 | }
|
---|
963 | }
|
---|
964 |
|
---|
965 | public void drawRestriction(Image img, Point pVia, double vx, double vx2, double vy, double vy2, double angle, boolean selected) {
|
---|
966 | // rotate image with direction last node in from to, and scale down image to 16*16 pixels
|
---|
967 | Image smallImg = ImageProvider.createRotatedImage(img, angle, new Dimension(16, 16));
|
---|
968 | int w = smallImg.getWidth(null), h = smallImg.getHeight(null);
|
---|
969 | g.drawImage(smallImg, (int) (pVia.x+vx+vx2)-w/2, (int) (pVia.y+vy+vy2)-h/2, nc);
|
---|
970 |
|
---|
971 | if (selected) {
|
---|
972 | g.setColor(isInactiveMode ? inactiveColor : relationSelectedColor);
|
---|
973 | g.drawRect((int) (pVia.x+vx+vx2)-w/2-2, (int) (pVia.y+vy+vy2)-h/2-2, w+4, h+4);
|
---|
974 | }
|
---|
975 | }
|
---|
976 |
|
---|
977 | public void drawRestriction(Relation r, MapImage icon, boolean disabled) {
|
---|
978 | Way fromWay = null;
|
---|
979 | Way toWay = null;
|
---|
980 | OsmPrimitive via = null;
|
---|
981 |
|
---|
982 | /* find the "from", "via" and "to" elements */
|
---|
983 | for (RelationMember m : r.getMembers()) {
|
---|
984 | if (m.getMember().isIncomplete())
|
---|
985 | return;
|
---|
986 | else {
|
---|
987 | if (m.isWay()) {
|
---|
988 | Way w = m.getWay();
|
---|
989 | if (w.getNodesCount() < 2) {
|
---|
990 | continue;
|
---|
991 | }
|
---|
992 |
|
---|
993 | switch(m.getRole()) {
|
---|
994 | case "from":
|
---|
995 | if (fromWay == null) {
|
---|
996 | fromWay = w;
|
---|
997 | }
|
---|
998 | break;
|
---|
999 | case "to":
|
---|
1000 | if (toWay == null) {
|
---|
1001 | toWay = w;
|
---|
1002 | }
|
---|
1003 | break;
|
---|
1004 | case "via":
|
---|
1005 | if (via == null) {
|
---|
1006 | via = w;
|
---|
1007 | }
|
---|
1008 | break;
|
---|
1009 | default: // Do nothing
|
---|
1010 | }
|
---|
1011 | } else if (m.isNode()) {
|
---|
1012 | Node n = m.getNode();
|
---|
1013 | if (via == null && "via".equals(m.getRole())) {
|
---|
1014 | via = n;
|
---|
1015 | }
|
---|
1016 | }
|
---|
1017 | }
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | if (fromWay == null || toWay == null || via == null)
|
---|
1021 | return;
|
---|
1022 |
|
---|
1023 | Node viaNode;
|
---|
1024 | if (via instanceof Node) {
|
---|
1025 | viaNode = (Node) via;
|
---|
1026 | if (!fromWay.isFirstLastNode(viaNode))
|
---|
1027 | return;
|
---|
1028 | } else {
|
---|
1029 | Way viaWay = (Way) via;
|
---|
1030 | Node firstNode = viaWay.firstNode();
|
---|
1031 | Node lastNode = viaWay.lastNode();
|
---|
1032 | Boolean onewayvia = Boolean.FALSE;
|
---|
1033 |
|
---|
1034 | String onewayviastr = viaWay.get("oneway");
|
---|
1035 | if (onewayviastr != null) {
|
---|
1036 | if ("-1".equals(onewayviastr)) {
|
---|
1037 | onewayvia = Boolean.TRUE;
|
---|
1038 | Node tmp = firstNode;
|
---|
1039 | firstNode = lastNode;
|
---|
1040 | lastNode = tmp;
|
---|
1041 | } else {
|
---|
1042 | onewayvia = Optional.ofNullable(OsmUtils.getOsmBoolean(onewayviastr)).orElse(Boolean.FALSE);
|
---|
1043 | }
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | if (fromWay.isFirstLastNode(firstNode)) {
|
---|
1047 | viaNode = firstNode;
|
---|
1048 | } else if (!onewayvia && fromWay.isFirstLastNode(lastNode)) {
|
---|
1049 | viaNode = lastNode;
|
---|
1050 | } else
|
---|
1051 | return;
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | /* find the "direct" nodes before the via node */
|
---|
1055 | Node fromNode;
|
---|
1056 | if (fromWay.firstNode() == via) {
|
---|
1057 | fromNode = fromWay.getNode(1);
|
---|
1058 | } else {
|
---|
1059 | fromNode = fromWay.getNode(fromWay.getNodesCount()-2);
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | Point pFrom = nc.getPoint(fromNode);
|
---|
1063 | Point pVia = nc.getPoint(viaNode);
|
---|
1064 |
|
---|
1065 | /* starting from via, go back the "from" way a few pixels
|
---|
1066 | (calculate the vector vx/vy with the specified length and the direction
|
---|
1067 | away from the "via" node along the first segment of the "from" way)
|
---|
1068 | */
|
---|
1069 | double distanceFromVia = 14;
|
---|
1070 | double dx = pFrom.x >= pVia.x ? pFrom.x - pVia.x : pVia.x - pFrom.x;
|
---|
1071 | double dy = pFrom.y >= pVia.y ? pFrom.y - pVia.y : pVia.y - pFrom.y;
|
---|
1072 |
|
---|
1073 | double fromAngle;
|
---|
1074 | if (dx == 0) {
|
---|
1075 | fromAngle = Math.PI/2;
|
---|
1076 | } else {
|
---|
1077 | fromAngle = Math.atan(dy / dx);
|
---|
1078 | }
|
---|
1079 | double fromAngleDeg = Math.toDegrees(fromAngle);
|
---|
1080 |
|
---|
1081 | double vx = distanceFromVia * Math.cos(fromAngle);
|
---|
1082 | double vy = distanceFromVia * Math.sin(fromAngle);
|
---|
1083 |
|
---|
1084 | if (pFrom.x < pVia.x) {
|
---|
1085 | vx = -vx;
|
---|
1086 | }
|
---|
1087 | if (pFrom.y < pVia.y) {
|
---|
1088 | vy = -vy;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | /* go a few pixels away from the way (in a right angle)
|
---|
1092 | (calculate the vx2/vy2 vector with the specified length and the direction
|
---|
1093 | 90degrees away from the first segment of the "from" way)
|
---|
1094 | */
|
---|
1095 | double distanceFromWay = 10;
|
---|
1096 | double vx2 = 0;
|
---|
1097 | double vy2 = 0;
|
---|
1098 | double iconAngle = 0;
|
---|
1099 |
|
---|
1100 | if (pFrom.x >= pVia.x && pFrom.y >= pVia.y) {
|
---|
1101 | if (!leftHandTraffic) {
|
---|
1102 | vx2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg - 90));
|
---|
1103 | vy2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg - 90));
|
---|
1104 | } else {
|
---|
1105 | vx2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg + 90));
|
---|
1106 | vy2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg + 90));
|
---|
1107 | }
|
---|
1108 | iconAngle = 270+fromAngleDeg;
|
---|
1109 | }
|
---|
1110 | if (pFrom.x < pVia.x && pFrom.y >= pVia.y) {
|
---|
1111 | if (!leftHandTraffic) {
|
---|
1112 | vx2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg));
|
---|
1113 | vy2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg));
|
---|
1114 | } else {
|
---|
1115 | vx2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg + 180));
|
---|
1116 | vy2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg + 180));
|
---|
1117 | }
|
---|
1118 | iconAngle = 90-fromAngleDeg;
|
---|
1119 | }
|
---|
1120 | if (pFrom.x < pVia.x && pFrom.y < pVia.y) {
|
---|
1121 | if (!leftHandTraffic) {
|
---|
1122 | vx2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg + 90));
|
---|
1123 | vy2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg + 90));
|
---|
1124 | } else {
|
---|
1125 | vx2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg - 90));
|
---|
1126 | vy2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg - 90));
|
---|
1127 | }
|
---|
1128 | iconAngle = 90+fromAngleDeg;
|
---|
1129 | }
|
---|
1130 | if (pFrom.x >= pVia.x && pFrom.y < pVia.y) {
|
---|
1131 | if (!leftHandTraffic) {
|
---|
1132 | vx2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg + 180));
|
---|
1133 | vy2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg + 180));
|
---|
1134 | } else {
|
---|
1135 | vx2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg));
|
---|
1136 | vy2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg));
|
---|
1137 | }
|
---|
1138 | iconAngle = 270-fromAngleDeg;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | drawRestriction(icon.getImage(disabled),
|
---|
1142 | pVia, vx, vx2, vy, vy2, iconAngle, r.isSelected());
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | /**
|
---|
1146 | * A half segment that can be used to place text on it. Used in the drawTextOnPath algorithm.
|
---|
1147 | * @author Michael Zangl
|
---|
1148 | */
|
---|
1149 | private static class HalfSegment {
|
---|
1150 | /**
|
---|
1151 | * start point of half segment (as length along the way)
|
---|
1152 | */
|
---|
1153 | final double start;
|
---|
1154 | /**
|
---|
1155 | * end point of half segment (as length along the way)
|
---|
1156 | */
|
---|
1157 | final double end;
|
---|
1158 | /**
|
---|
1159 | * quality factor (off screen / partly on screen / fully on screen)
|
---|
1160 | */
|
---|
1161 | final double quality;
|
---|
1162 |
|
---|
1163 | /**
|
---|
1164 | * Create a new half segment
|
---|
1165 | * @param start The start along the way
|
---|
1166 | * @param end The end of the segment
|
---|
1167 | * @param quality A quality factor.
|
---|
1168 | */
|
---|
1169 | HalfSegment(double start, double end, double quality) {
|
---|
1170 | super();
|
---|
1171 | this.start = start;
|
---|
1172 | this.end = end;
|
---|
1173 | this.quality = quality;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | @Override
|
---|
1177 | public String toString() {
|
---|
1178 | return "HalfSegment [start=" + start + ", end=" + end + ", quality=" + quality + "]";
|
---|
1179 | }
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | /**
|
---|
1183 | * Draws a text along a given way.
|
---|
1184 | * @param way The way to draw the text on.
|
---|
1185 | * @param text The text definition (font/.../text content) to draw.
|
---|
1186 | */
|
---|
1187 | public void drawTextOnPath(Way way, TextLabel text) {
|
---|
1188 | if (way == null || text == null)
|
---|
1189 | return;
|
---|
1190 | String name = text.getString(way);
|
---|
1191 | if (name == null || name.isEmpty())
|
---|
1192 | return;
|
---|
1193 |
|
---|
1194 | FontMetrics fontMetrics = g.getFontMetrics(text.font);
|
---|
1195 | Rectangle2D rec = fontMetrics.getStringBounds(name, g);
|
---|
1196 |
|
---|
1197 | Rectangle bounds = g.getClipBounds();
|
---|
1198 |
|
---|
1199 | List<MapViewPoint> points = way.getNodes().stream().map(mapState::getPointFor).collect(Collectors.toList());
|
---|
1200 |
|
---|
1201 | // find half segments that are long enough to draw text on (don't draw text over the cross hair in the center of each segment)
|
---|
1202 | List<HalfSegment> longHalfSegment = new ArrayList<>();
|
---|
1203 |
|
---|
1204 | double pathLength = computePath(2 * (rec.getWidth() + 4), bounds, points, longHalfSegment);
|
---|
1205 |
|
---|
1206 | if (rec.getWidth() > pathLength)
|
---|
1207 | return;
|
---|
1208 |
|
---|
1209 | double t1, t2;
|
---|
1210 |
|
---|
1211 | if (!longHalfSegment.isEmpty()) {
|
---|
1212 | // find the segment with the best quality. If there are several with best quality, the one close to the center is prefered.
|
---|
1213 | Optional<HalfSegment> besto = longHalfSegment.stream().max(
|
---|
1214 | Comparator.comparingDouble(segment ->
|
---|
1215 | segment.quality - 1e-5 * Math.abs(0.5 * (segment.end + segment.start) - 0.5 * pathLength)
|
---|
1216 | ));
|
---|
1217 | if (!besto.isPresent())
|
---|
1218 | throw new IllegalStateException("Unable to find the segment with the best quality for " + way);
|
---|
1219 | HalfSegment best = besto.get();
|
---|
1220 | double remaining = best.end - best.start - rec.getWidth(); // total space left and right from the text
|
---|
1221 | // The space left and right of the text should be distributed 20% - 80% (towards the center),
|
---|
1222 | // but the smaller space should not be less than 7 px.
|
---|
1223 | // However, if the total remaining space is less than 14 px, then distribute it evenly.
|
---|
1224 | double smallerSpace = Math.min(Math.max(0.2 * remaining, 7), 0.5 * remaining);
|
---|
1225 | if ((best.end + best.start)/2 < pathLength/2) {
|
---|
1226 | t2 = best.end - smallerSpace;
|
---|
1227 | t1 = t2 - rec.getWidth();
|
---|
1228 | } else {
|
---|
1229 | t1 = best.start + smallerSpace;
|
---|
1230 | t2 = t1 + rec.getWidth();
|
---|
1231 | }
|
---|
1232 | } else {
|
---|
1233 | // doesn't fit into one half-segment -> just put it in the center of the way
|
---|
1234 | t1 = pathLength/2 - rec.getWidth()/2;
|
---|
1235 | t2 = pathLength/2 + rec.getWidth()/2;
|
---|
1236 | }
|
---|
1237 | t1 /= pathLength;
|
---|
1238 | t2 /= pathLength;
|
---|
1239 |
|
---|
1240 | double[] p1 = pointAt(t1, points, pathLength);
|
---|
1241 | double[] p2 = pointAt(t2, points, pathLength);
|
---|
1242 |
|
---|
1243 | if (p1 == null || p2 == null)
|
---|
1244 | return;
|
---|
1245 |
|
---|
1246 | double angleOffset;
|
---|
1247 | double offsetSign;
|
---|
1248 | double tStart;
|
---|
1249 |
|
---|
1250 | if (p1[0] < p2[0] &&
|
---|
1251 | p1[2] < Math.PI/2 &&
|
---|
1252 | p1[2] > -Math.PI/2) {
|
---|
1253 | angleOffset = 0;
|
---|
1254 | offsetSign = 1;
|
---|
1255 | tStart = t1;
|
---|
1256 | } else {
|
---|
1257 | angleOffset = Math.PI;
|
---|
1258 | offsetSign = -1;
|
---|
1259 | tStart = t2;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | List<GlyphVector> gvs = Utils.getGlyphVectorsBidi(name, text.font, g.getFontRenderContext());
|
---|
1263 | double gvOffset = 0;
|
---|
1264 | for (GlyphVector gv : gvs) {
|
---|
1265 | double gvWidth = gv.getLogicalBounds().getBounds2D().getWidth();
|
---|
1266 | for (int i = 0; i < gv.getNumGlyphs(); ++i) {
|
---|
1267 | Rectangle2D rect = gv.getGlyphLogicalBounds(i).getBounds2D();
|
---|
1268 | double t = tStart + offsetSign * (gvOffset + rect.getX() + rect.getWidth()/2) / pathLength;
|
---|
1269 | double[] p = pointAt(t, points, pathLength);
|
---|
1270 | if (p != null) {
|
---|
1271 | AffineTransform trfm = AffineTransform.getTranslateInstance(p[0] - rect.getX(), p[1]);
|
---|
1272 | trfm.rotate(p[2]+angleOffset);
|
---|
1273 | double off = -rect.getY() - rect.getHeight()/2 + text.yOffset;
|
---|
1274 | trfm.translate(-rect.getWidth()/2, off);
|
---|
1275 | if (isGlyphVectorDoubleTranslationBug(text.font)) {
|
---|
1276 | // scale the translation components by one half
|
---|
1277 | AffineTransform tmp = AffineTransform.getTranslateInstance(-0.5 * trfm.getTranslateX(), -0.5 * trfm.getTranslateY());
|
---|
1278 | tmp.concatenate(trfm);
|
---|
1279 | trfm = tmp;
|
---|
1280 | }
|
---|
1281 | gv.setGlyphTransform(i, trfm);
|
---|
1282 | }
|
---|
1283 | }
|
---|
1284 | displayText(gv, null, 0, 0, way.isDisabled(), text);
|
---|
1285 | gvOffset += gvWidth;
|
---|
1286 | }
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | private static double computePath(double minSegmentLength, Rectangle bounds, List<MapViewPoint> points,
|
---|
1290 | List<HalfSegment> longHalfSegment) {
|
---|
1291 | MapViewPoint lastPoint = points.get(0);
|
---|
1292 | double pathLength = 0;
|
---|
1293 | for (MapViewPoint p : points.subList(1, points.size())) {
|
---|
1294 | double segmentLength = p.distanceToInView(lastPoint);
|
---|
1295 | if (segmentLength > minSegmentLength) {
|
---|
1296 | Point2D center = new Point2D.Double((lastPoint.getInViewX() + p.getInViewX())/2, (lastPoint.getInViewY() + p.getInViewY())/2);
|
---|
1297 | double q = computeQuality(bounds, lastPoint, center);
|
---|
1298 | // prefer the first one for quality equality.
|
---|
1299 | longHalfSegment.add(new HalfSegment(pathLength, pathLength + segmentLength / 2, q));
|
---|
1300 |
|
---|
1301 | q = 0;
|
---|
1302 | if (bounds != null) {
|
---|
1303 | if (bounds.contains(center) && bounds.contains(p.getInView())) {
|
---|
1304 | q = 2;
|
---|
1305 | } else if (bounds.contains(center) || bounds.contains(p.getInView())) {
|
---|
1306 | q = 1;
|
---|
1307 | }
|
---|
1308 | }
|
---|
1309 | longHalfSegment.add(new HalfSegment(pathLength + segmentLength / 2, pathLength + segmentLength, q));
|
---|
1310 | }
|
---|
1311 | pathLength += segmentLength;
|
---|
1312 | lastPoint = p;
|
---|
1313 | }
|
---|
1314 | return pathLength;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | private static double computeQuality(Rectangle bounds, MapViewPoint p1, Point2D p2) {
|
---|
1318 | double q = 0;
|
---|
1319 | if (bounds != null) {
|
---|
1320 | if (bounds.contains(p1.getInView())) {
|
---|
1321 | q += 1;
|
---|
1322 | }
|
---|
1323 | if (bounds.contains(p2)) {
|
---|
1324 | q += 1;
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 | return q;
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | /**
|
---|
1331 | * draw way. This method allows for two draw styles (line using color, dashes using dashedColor) to be passed.
|
---|
1332 | * @param way The way to draw
|
---|
1333 | * @param color The base color to draw the way in
|
---|
1334 | * @param line The line style to use. This is drawn using color.
|
---|
1335 | * @param dashes The dash style to use. This is drawn using dashedColor. <code>null</code> if unused.
|
---|
1336 | * @param dashedColor The color of the dashes.
|
---|
1337 | * @param offset The offset
|
---|
1338 | * @param showOrientation show arrows that indicate the technical orientation of
|
---|
1339 | * the way (defined by order of nodes)
|
---|
1340 | * @param showHeadArrowOnly True if only the arrow at the end of the line but not those on the segments should be displayed.
|
---|
1341 | * @param showOneway show symbols that indicate the direction of the feature,
|
---|
1342 | * e.g. oneway street or waterway
|
---|
1343 | * @param onewayReversed for oneway=-1 and similar
|
---|
1344 | */
|
---|
1345 | public void drawWay(Way way, Color color, BasicStroke line, BasicStroke dashes, Color dashedColor, float offset,
|
---|
1346 | boolean showOrientation, boolean showHeadArrowOnly,
|
---|
1347 | boolean showOneway, boolean onewayReversed) {
|
---|
1348 |
|
---|
1349 | MapViewPath path = new MapViewPath(mapState);
|
---|
1350 | MapViewPath orientationArrows = showOrientation ? new MapViewPath(mapState) : null;
|
---|
1351 | MapViewPath onewayArrows;
|
---|
1352 | MapViewPath onewayArrowsCasing;
|
---|
1353 | Rectangle bounds = g.getClipBounds();
|
---|
1354 | if (bounds != null) {
|
---|
1355 | // avoid arrow heads at the border
|
---|
1356 | bounds.grow(100, 100);
|
---|
1357 | }
|
---|
1358 |
|
---|
1359 | List<Node> wayNodes = way.getNodes();
|
---|
1360 | if (wayNodes.size() < 2) return;
|
---|
1361 |
|
---|
1362 | // only highlight the segment if the way itself is not highlighted
|
---|
1363 | if (!way.isHighlighted() && highlightWaySegments != null) {
|
---|
1364 | MapViewPath highlightSegs = null;
|
---|
1365 | for (WaySegment ws : highlightWaySegments) {
|
---|
1366 | if (ws.way != way || ws.lowerIndex < offset) {
|
---|
1367 | continue;
|
---|
1368 | }
|
---|
1369 | if (highlightSegs == null) {
|
---|
1370 | highlightSegs = new MapViewPath(mapState);
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | highlightSegs.moveTo(ws.getFirstNode());
|
---|
1374 | highlightSegs.lineTo(ws.getSecondNode());
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | drawPathHighlight(highlightSegs, line);
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | MapViewPoint lastPoint = null;
|
---|
1381 | Iterator<MapViewPoint> it = new OffsetIterator(mapState, wayNodes, offset);
|
---|
1382 | boolean initialMoveToNeeded = true;
|
---|
1383 | while (it.hasNext()) {
|
---|
1384 | MapViewPoint p = it.next();
|
---|
1385 | if (lastPoint != null) {
|
---|
1386 | MapViewPoint p1 = lastPoint;
|
---|
1387 | MapViewPoint p2 = p;
|
---|
1388 |
|
---|
1389 | if (initialMoveToNeeded) {
|
---|
1390 | initialMoveToNeeded = false;
|
---|
1391 | path.moveTo(p1);
|
---|
1392 | }
|
---|
1393 | path.lineTo(p2);
|
---|
1394 |
|
---|
1395 | /* draw arrow */
|
---|
1396 | if (showHeadArrowOnly ? !it.hasNext() : showOrientation) {
|
---|
1397 | //TODO: Cache
|
---|
1398 | ArrowPaintHelper drawHelper = new ArrowPaintHelper(PHI, 10 + line.getLineWidth());
|
---|
1399 | drawHelper.paintArrowAt(orientationArrows, p2, p1);
|
---|
1400 | }
|
---|
1401 | }
|
---|
1402 | lastPoint = p;
|
---|
1403 | }
|
---|
1404 | if (showOneway) {
|
---|
1405 | onewayArrows = new MapViewPath(mapState);
|
---|
1406 | onewayArrowsCasing = new MapViewPath(mapState);
|
---|
1407 | double interval = 60;
|
---|
1408 |
|
---|
1409 | path.visitClippedLine(0, 60, (inLineOffset, start, end, startIsOldEnd) -> {
|
---|
1410 | double segmentLength = start.distanceToInView(end);
|
---|
1411 | if (segmentLength > 0.001) {
|
---|
1412 | final double nx = (end.getInViewX() - start.getInViewX()) / segmentLength;
|
---|
1413 | final double ny = (end.getInViewY() - start.getInViewY()) / segmentLength;
|
---|
1414 |
|
---|
1415 | // distance from p1
|
---|
1416 | double dist = interval - (inLineOffset % interval);
|
---|
1417 |
|
---|
1418 | while (dist < segmentLength) {
|
---|
1419 | appenOnewayPath(onewayReversed, start, nx, ny, dist, 3d, onewayArrowsCasing);
|
---|
1420 | appenOnewayPath(onewayReversed, start, nx, ny, dist, 2d, onewayArrows);
|
---|
1421 | dist += interval;
|
---|
1422 | }
|
---|
1423 | }
|
---|
1424 | });
|
---|
1425 | } else {
|
---|
1426 | onewayArrows = null;
|
---|
1427 | onewayArrowsCasing = null;
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | if (way.isHighlighted()) {
|
---|
1431 | drawPathHighlight(path, line);
|
---|
1432 | }
|
---|
1433 | displaySegments(path, orientationArrows, onewayArrows, onewayArrowsCasing, color, line, dashes, dashedColor);
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | private static void appenOnewayPath(boolean onewayReversed, MapViewPoint p1, double nx, double ny, double dist,
|
---|
1437 | double onewaySize, Path2D onewayPath) {
|
---|
1438 | // scale such that border is 1 px
|
---|
1439 | final double fac = -(onewayReversed ? -1 : 1) * onewaySize * (1 + sinPHI) / (sinPHI * cosPHI);
|
---|
1440 | final double sx = nx * fac;
|
---|
1441 | final double sy = ny * fac;
|
---|
1442 |
|
---|
1443 | // Attach the triangle at the incenter and not at the tip.
|
---|
1444 | // Makes the border even at all sides.
|
---|
1445 | final double x = p1.getInViewX() + nx * (dist + (onewayReversed ? -1 : 1) * (onewaySize / sinPHI));
|
---|
1446 | final double y = p1.getInViewY() + ny * (dist + (onewayReversed ? -1 : 1) * (onewaySize / sinPHI));
|
---|
1447 |
|
---|
1448 | onewayPath.moveTo(x, y);
|
---|
1449 | onewayPath.lineTo(x + cosPHI * sx - sinPHI * sy, y + sinPHI * sx + cosPHI * sy);
|
---|
1450 | onewayPath.lineTo(x + cosPHI * sx + sinPHI * sy, y - sinPHI * sx + cosPHI * sy);
|
---|
1451 | onewayPath.lineTo(x, y);
|
---|
1452 | }
|
---|
1453 |
|
---|
1454 | /**
|
---|
1455 | * Gets the "circum". This is the distance on the map in meters that 100 screen pixels represent.
|
---|
1456 | * @return The "circum"
|
---|
1457 | */
|
---|
1458 | public double getCircum() {
|
---|
1459 | return circum;
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | @Override
|
---|
1463 | public void getColors() {
|
---|
1464 | super.getColors();
|
---|
1465 | this.highlightColorTransparent = new Color(highlightColor.getRed(), highlightColor.getGreen(), highlightColor.getBlue(), 100);
|
---|
1466 | this.backgroundColor = PaintColors.getBackgroundColor();
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | @Override
|
---|
1470 | public void getSettings(boolean virtual) {
|
---|
1471 | super.getSettings(virtual);
|
---|
1472 | paintSettings = MapPaintSettings.INSTANCE;
|
---|
1473 |
|
---|
1474 | circum = nc.getDist100Pixel();
|
---|
1475 | scale = nc.getScale();
|
---|
1476 |
|
---|
1477 | leftHandTraffic = Main.pref.getBoolean("mappaint.lefthandtraffic", false);
|
---|
1478 |
|
---|
1479 | useStrokes = paintSettings.getUseStrokesDistance() > circum;
|
---|
1480 | showNames = paintSettings.getShowNamesDistance() > circum;
|
---|
1481 | showIcons = paintSettings.getShowIconsDistance() > circum;
|
---|
1482 | isOutlineOnly = paintSettings.isOutlineOnly();
|
---|
1483 | orderFont = new Font(Main.pref.get("mappaint.font", "Droid Sans"), Font.PLAIN, Main.pref.getInteger("mappaint.fontsize", 8));
|
---|
1484 |
|
---|
1485 | antialiasing = Main.pref.getBoolean("mappaint.use-antialiasing", true) ?
|
---|
1486 | RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF;
|
---|
1487 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing);
|
---|
1488 |
|
---|
1489 | Object textAntialiasing;
|
---|
1490 | switch (Main.pref.get("mappaint.text-antialiasing", "default")) {
|
---|
1491 | case "on":
|
---|
1492 | textAntialiasing = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
|
---|
1493 | break;
|
---|
1494 | case "off":
|
---|
1495 | textAntialiasing = RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
|
---|
1496 | break;
|
---|
1497 | case "gasp":
|
---|
1498 | textAntialiasing = RenderingHints.VALUE_TEXT_ANTIALIAS_GASP;
|
---|
1499 | break;
|
---|
1500 | case "lcd-hrgb":
|
---|
1501 | textAntialiasing = RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB;
|
---|
1502 | break;
|
---|
1503 | case "lcd-hbgr":
|
---|
1504 | textAntialiasing = RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR;
|
---|
1505 | break;
|
---|
1506 | case "lcd-vrgb":
|
---|
1507 | textAntialiasing = RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB;
|
---|
1508 | break;
|
---|
1509 | case "lcd-vbgr":
|
---|
1510 | textAntialiasing = RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR;
|
---|
1511 | break;
|
---|
1512 | default:
|
---|
1513 | textAntialiasing = RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT;
|
---|
1514 | }
|
---|
1515 | g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, textAntialiasing);
|
---|
1516 |
|
---|
1517 | highlightLineWidth = Main.pref.getInteger("mappaint.highlight.width", 4);
|
---|
1518 | highlightPointRadius = Main.pref.getInteger("mappaint.highlight.radius", 7);
|
---|
1519 | widerHighlight = Main.pref.getInteger("mappaint.highlight.bigger-increment", 5);
|
---|
1520 | highlightStep = Main.pref.getInteger("mappaint.highlight.step", 4);
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | private static Path2D.Double getPath(Way w) {
|
---|
1524 | Path2D.Double path = new Path2D.Double();
|
---|
1525 | boolean initial = true;
|
---|
1526 | for (Node n : w.getNodes()) {
|
---|
1527 | EastNorth p = n.getEastNorth();
|
---|
1528 | if (p != null) {
|
---|
1529 | if (initial) {
|
---|
1530 | path.moveTo(p.getX(), p.getY());
|
---|
1531 | initial = false;
|
---|
1532 | } else {
|
---|
1533 | path.lineTo(p.getX(), p.getY());
|
---|
1534 | }
|
---|
1535 | }
|
---|
1536 | }
|
---|
1537 | if (w.isClosed()) {
|
---|
1538 | path.closePath();
|
---|
1539 | }
|
---|
1540 | return path;
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 | private static Path2D.Double getPFClip(Way w, double extent) {
|
---|
1544 | Path2D.Double clip = new Path2D.Double();
|
---|
1545 | buildPFClip(clip, w.getNodes(), extent);
|
---|
1546 | return clip;
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | private static Path2D.Double getPFClip(PolyData pd, double extent) {
|
---|
1550 | Path2D.Double clip = new Path2D.Double();
|
---|
1551 | clip.setWindingRule(Path2D.WIND_EVEN_ODD);
|
---|
1552 | buildPFClip(clip, pd.getNodes(), extent);
|
---|
1553 | for (PolyData pdInner : pd.getInners()) {
|
---|
1554 | buildPFClip(clip, pdInner.getNodes(), extent);
|
---|
1555 | }
|
---|
1556 | return clip;
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | /**
|
---|
1560 | * Fix the clipping area of unclosed polygons for partial fill.
|
---|
1561 | *
|
---|
1562 | * The current algorithm for partial fill simply strokes the polygon with a
|
---|
1563 | * large stroke width after masking the outside with a clipping area.
|
---|
1564 | * This works, but for unclosed polygons, the mask can crop the corners at
|
---|
1565 | * both ends (see #12104).
|
---|
1566 | *
|
---|
1567 | * This method fixes the clipping area by sort of adding the corners to the
|
---|
1568 | * clip outline.
|
---|
1569 | *
|
---|
1570 | * @param clip the clipping area to modify (initially empty)
|
---|
1571 | * @param nodes nodes of the polygon
|
---|
1572 | * @param extent the extent
|
---|
1573 | */
|
---|
1574 | private static void buildPFClip(Path2D.Double clip, List<Node> nodes, double extent) {
|
---|
1575 | boolean initial = true;
|
---|
1576 | for (Node n : nodes) {
|
---|
1577 | EastNorth p = n.getEastNorth();
|
---|
1578 | if (p != null) {
|
---|
1579 | if (initial) {
|
---|
1580 | clip.moveTo(p.getX(), p.getY());
|
---|
1581 | initial = false;
|
---|
1582 | } else {
|
---|
1583 | clip.lineTo(p.getX(), p.getY());
|
---|
1584 | }
|
---|
1585 | }
|
---|
1586 | }
|
---|
1587 | if (nodes.size() >= 3) {
|
---|
1588 | EastNorth fst = nodes.get(0).getEastNorth();
|
---|
1589 | EastNorth snd = nodes.get(1).getEastNorth();
|
---|
1590 | EastNorth lst = nodes.get(nodes.size() - 1).getEastNorth();
|
---|
1591 | EastNorth lbo = nodes.get(nodes.size() - 2).getEastNorth();
|
---|
1592 |
|
---|
1593 | EastNorth cLst = getPFDisplacedEndPoint(lbo, lst, fst, extent);
|
---|
1594 | EastNorth cFst = getPFDisplacedEndPoint(snd, fst, cLst != null ? cLst : lst, extent);
|
---|
1595 | if (cLst == null && cFst != null) {
|
---|
1596 | cLst = getPFDisplacedEndPoint(lbo, lst, cFst, extent);
|
---|
1597 | }
|
---|
1598 | if (cLst != null) {
|
---|
1599 | clip.lineTo(cLst.getX(), cLst.getY());
|
---|
1600 | }
|
---|
1601 | if (cFst != null) {
|
---|
1602 | clip.lineTo(cFst.getX(), cFst.getY());
|
---|
1603 | }
|
---|
1604 | }
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | /**
|
---|
1608 | * Get the point to add to the clipping area for partial fill of unclosed polygons.
|
---|
1609 | *
|
---|
1610 | * <code>(p1,p2)</code> is the first or last way segment and <code>p3</code> the
|
---|
1611 | * opposite endpoint.
|
---|
1612 | *
|
---|
1613 | * @param p1 1st point
|
---|
1614 | * @param p2 2nd point
|
---|
1615 | * @param p3 3rd point
|
---|
1616 | * @param extent the extent
|
---|
1617 | * @return a point q, such that p1,p2,q form a right angle
|
---|
1618 | * and the distance of q to p2 is <code>extent</code>. The point q lies on
|
---|
1619 | * the same side of the line p1,p2 as the point p3.
|
---|
1620 | * Returns null if p1,p2,p3 forms an angle greater 90 degrees. (In this case
|
---|
1621 | * the corner of the partial fill would not be cut off by the mask, so an
|
---|
1622 | * additional point is not necessary.)
|
---|
1623 | */
|
---|
1624 | private static EastNorth getPFDisplacedEndPoint(EastNorth p1, EastNorth p2, EastNorth p3, double extent) {
|
---|
1625 | double dx1 = p2.getX() - p1.getX();
|
---|
1626 | double dy1 = p2.getY() - p1.getY();
|
---|
1627 | double dx2 = p3.getX() - p2.getX();
|
---|
1628 | double dy2 = p3.getY() - p2.getY();
|
---|
1629 | if (dx1 * dx2 + dy1 * dy2 < 0) {
|
---|
1630 | double len = Math.sqrt(dx1 * dx1 + dy1 * dy1);
|
---|
1631 | if (len == 0) return null;
|
---|
1632 | double dxm = -dy1 * extent / len;
|
---|
1633 | double dym = dx1 * extent / len;
|
---|
1634 | if (dx1 * dy2 - dx2 * dy1 < 0) {
|
---|
1635 | dxm = -dxm;
|
---|
1636 | dym = -dym;
|
---|
1637 | }
|
---|
1638 | return new EastNorth(p2.getX() + dxm, p2.getY() + dym);
|
---|
1639 | }
|
---|
1640 | return null;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | /**
|
---|
1644 | * Test if the area is visible
|
---|
1645 | * @param area The area, interpreted in east/north space.
|
---|
1646 | * @return true if it is visible.
|
---|
1647 | */
|
---|
1648 | private boolean isAreaVisible(Path2D.Double area) {
|
---|
1649 | Rectangle2D bounds = area.getBounds2D();
|
---|
1650 | if (bounds.isEmpty()) return false;
|
---|
1651 | MapViewPoint p = mapState.getPointFor(new EastNorth(bounds.getX(), bounds.getY()));
|
---|
1652 | if (p.getInViewX() > mapState.getViewWidth()) return false;
|
---|
1653 | if (p.getInViewY() < 0) return false;
|
---|
1654 | p = mapState.getPointFor(new EastNorth(bounds.getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight()));
|
---|
1655 | if (p.getInViewX() < 0) return false;
|
---|
1656 | if (p.getInViewY() > mapState.getViewHeight()) return false;
|
---|
1657 | return true;
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | public boolean isInactiveMode() {
|
---|
1661 | return isInactiveMode;
|
---|
1662 | }
|
---|
1663 |
|
---|
1664 | public boolean isShowIcons() {
|
---|
1665 | return showIcons;
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 | public boolean isShowNames() {
|
---|
1669 | return showNames;
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | private static double[] pointAt(double t, List<MapViewPoint> poly, double pathLength) {
|
---|
1673 | double totalLen = t * pathLength;
|
---|
1674 | double curLen = 0;
|
---|
1675 | double dx, dy;
|
---|
1676 | double segLen;
|
---|
1677 |
|
---|
1678 | // Yes, it is inefficient to iterate from the beginning for each glyph.
|
---|
1679 | // Can be optimized if it turns out to be slow.
|
---|
1680 | for (int i = 1; i < poly.size(); ++i) {
|
---|
1681 | dx = poly.get(i).getInViewX() - poly.get(i - 1).getInViewX();
|
---|
1682 | dy = poly.get(i).getInViewY() - poly.get(i - 1).getInViewY();
|
---|
1683 | segLen = Math.sqrt(dx*dx + dy*dy);
|
---|
1684 | if (totalLen > curLen + segLen) {
|
---|
1685 | curLen += segLen;
|
---|
1686 | continue;
|
---|
1687 | }
|
---|
1688 | return new double[] {
|
---|
1689 | poly.get(i - 1).getInViewX() + (totalLen - curLen) / segLen * dx,
|
---|
1690 | poly.get(i - 1).getInViewY() + (totalLen - curLen) / segLen * dy,
|
---|
1691 | Math.atan2(dy, dx)};
|
---|
1692 | }
|
---|
1693 | return null;
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 | /**
|
---|
1697 | * Computes the flags for a given OSM primitive.
|
---|
1698 | * @param primitive The primititve to compute the flags for.
|
---|
1699 | * @param checkOuterMember <code>true</code> if we should also add {@link #FLAG_OUTERMEMBER_OF_SELECTED}
|
---|
1700 | * @return The flag.
|
---|
1701 | */
|
---|
1702 | public static int computeFlags(OsmPrimitive primitive, boolean checkOuterMember) {
|
---|
1703 | if (primitive.isDisabled()) {
|
---|
1704 | return FLAG_DISABLED;
|
---|
1705 | } else if (primitive.isSelected()) {
|
---|
1706 | return FLAG_SELECTED;
|
---|
1707 | } else if (checkOuterMember && primitive.isOuterMemberOfSelected()) {
|
---|
1708 | return FLAG_OUTERMEMBER_OF_SELECTED;
|
---|
1709 | } else if (primitive.isMemberOfSelected()) {
|
---|
1710 | return FLAG_MEMBER_OF_SELECTED;
|
---|
1711 | } else {
|
---|
1712 | return FLAG_NORMAL;
|
---|
1713 | }
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 | private static class ComputeStyleListWorker extends RecursiveTask<List<StyleRecord>> implements Visitor {
|
---|
1717 | private final transient List<? extends OsmPrimitive> input;
|
---|
1718 | private final transient List<StyleRecord> output;
|
---|
1719 |
|
---|
1720 | private final transient ElemStyles styles = MapPaintStyles.getStyles();
|
---|
1721 | private final int directExecutionTaskSize;
|
---|
1722 | private final double circum;
|
---|
1723 | private final NavigatableComponent nc;
|
---|
1724 |
|
---|
1725 | private final boolean drawArea;
|
---|
1726 | private final boolean drawMultipolygon;
|
---|
1727 | private final boolean drawRestriction;
|
---|
1728 |
|
---|
1729 | /**
|
---|
1730 | * Constructs a new {@code ComputeStyleListWorker}.
|
---|
1731 | * @param circum distance on the map in meters that 100 screen pixels represent
|
---|
1732 | * @param nc navigatable component
|
---|
1733 | * @param input the primitives to process
|
---|
1734 | * @param output the list of styles to which styles will be added
|
---|
1735 | * @param directExecutionTaskSize the threshold deciding whether to subdivide the tasks
|
---|
1736 | */
|
---|
1737 | ComputeStyleListWorker(double circum, NavigatableComponent nc,
|
---|
1738 | final List<? extends OsmPrimitive> input, List<StyleRecord> output, int directExecutionTaskSize) {
|
---|
1739 | this.circum = circum;
|
---|
1740 | this.nc = nc;
|
---|
1741 | this.input = input;
|
---|
1742 | this.output = output;
|
---|
1743 | this.directExecutionTaskSize = directExecutionTaskSize;
|
---|
1744 | this.drawArea = circum <= Main.pref.getInteger("mappaint.fillareas", 10_000_000);
|
---|
1745 | this.drawMultipolygon = drawArea && Main.pref.getBoolean("mappaint.multipolygon", true);
|
---|
1746 | this.drawRestriction = Main.pref.getBoolean("mappaint.restriction", true);
|
---|
1747 | this.styles.setDrawMultipolygon(drawMultipolygon);
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | @Override
|
---|
1751 | protected List<StyleRecord> compute() {
|
---|
1752 | if (input.size() <= directExecutionTaskSize) {
|
---|
1753 | return computeDirectly();
|
---|
1754 | } else {
|
---|
1755 | final Collection<ForkJoinTask<List<StyleRecord>>> tasks = new ArrayList<>();
|
---|
1756 | for (int fromIndex = 0; fromIndex < input.size(); fromIndex += directExecutionTaskSize) {
|
---|
1757 | final int toIndex = Math.min(fromIndex + directExecutionTaskSize, input.size());
|
---|
1758 | final List<StyleRecord> output = new ArrayList<>(directExecutionTaskSize);
|
---|
1759 | tasks.add(new ComputeStyleListWorker(circum, nc, input.subList(fromIndex, toIndex), output, directExecutionTaskSize).fork());
|
---|
1760 | }
|
---|
1761 | for (ForkJoinTask<List<StyleRecord>> task : tasks) {
|
---|
1762 | output.addAll(task.join());
|
---|
1763 | }
|
---|
1764 | return output;
|
---|
1765 | }
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | public List<StyleRecord> computeDirectly() {
|
---|
1769 | MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().lock();
|
---|
1770 | try {
|
---|
1771 | for (final OsmPrimitive osm : input) {
|
---|
1772 | acceptDrawable(osm);
|
---|
1773 | }
|
---|
1774 | return output;
|
---|
1775 | } catch (RuntimeException e) {
|
---|
1776 | throw BugReport.intercept(e).put("input-size", input.size()).put("output-size", output.size());
|
---|
1777 | } finally {
|
---|
1778 | MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().unlock();
|
---|
1779 | }
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 | private void acceptDrawable(final OsmPrimitive osm) {
|
---|
1783 | try {
|
---|
1784 | if (osm.isDrawable()) {
|
---|
1785 | osm.accept(this);
|
---|
1786 | }
|
---|
1787 | } catch (RuntimeException e) {
|
---|
1788 | throw BugReport.intercept(e).put("osm", osm);
|
---|
1789 | }
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 | @Override
|
---|
1793 | public void visit(Node n) {
|
---|
1794 | add(n, computeFlags(n, false));
|
---|
1795 | }
|
---|
1796 |
|
---|
1797 | @Override
|
---|
1798 | public void visit(Way w) {
|
---|
1799 | add(w, computeFlags(w, true));
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | @Override
|
---|
1803 | public void visit(Relation r) {
|
---|
1804 | add(r, computeFlags(r, true));
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | @Override
|
---|
1808 | public void visit(Changeset cs) {
|
---|
1809 | throw new UnsupportedOperationException();
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | public void add(Node osm, int flags) {
|
---|
1813 | StyleElementList sl = styles.get(osm, circum, nc);
|
---|
1814 | for (StyleElement s : sl) {
|
---|
1815 | output.add(new StyleRecord(s, osm, flags));
|
---|
1816 | }
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | public void add(Relation osm, int flags) {
|
---|
1820 | StyleElementList sl = styles.get(osm, circum, nc);
|
---|
1821 | for (StyleElement s : sl) {
|
---|
1822 | if (drawMultipolygon && drawArea && s instanceof AreaElement && (flags & FLAG_DISABLED) == 0) {
|
---|
1823 | output.add(new StyleRecord(s, osm, flags));
|
---|
1824 | } else if (drawRestriction && s instanceof NodeElement) {
|
---|
1825 | output.add(new StyleRecord(s, osm, flags));
|
---|
1826 | }
|
---|
1827 | }
|
---|
1828 | }
|
---|
1829 |
|
---|
1830 | public void add(Way osm, int flags) {
|
---|
1831 | StyleElementList sl = styles.get(osm, circum, nc);
|
---|
1832 | for (StyleElement s : sl) {
|
---|
1833 | if (!(drawArea && (flags & FLAG_DISABLED) == 0) && s instanceof AreaElement) {
|
---|
1834 | continue;
|
---|
1835 | }
|
---|
1836 | output.add(new StyleRecord(s, osm, flags));
|
---|
1837 | }
|
---|
1838 | }
|
---|
1839 | }
|
---|
1840 |
|
---|
1841 | /**
|
---|
1842 | * Sets the factory that creates the benchmark data receivers.
|
---|
1843 | * @param benchmarkFactory The factory.
|
---|
1844 | * @since 10697
|
---|
1845 | */
|
---|
1846 | public void setBenchmarkFactory(Supplier<RenderBenchmarkCollector> benchmarkFactory) {
|
---|
1847 | this.benchmarkFactory = benchmarkFactory;
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | @Override
|
---|
1851 | public void render(final DataSet data, boolean renderVirtualNodes, Bounds bounds) {
|
---|
1852 | RenderBenchmarkCollector benchmark = benchmarkFactory.get();
|
---|
1853 | BBox bbox = bounds.toBBox();
|
---|
1854 | getSettings(renderVirtualNodes);
|
---|
1855 |
|
---|
1856 | data.getReadLock().lock();
|
---|
1857 | try {
|
---|
1858 | highlightWaySegments = data.getHighlightedWaySegments();
|
---|
1859 |
|
---|
1860 | benchmark.renderStart(circum);
|
---|
1861 |
|
---|
1862 | List<Node> nodes = data.searchNodes(bbox);
|
---|
1863 | List<Way> ways = data.searchWays(bbox);
|
---|
1864 | List<Relation> relations = data.searchRelations(bbox);
|
---|
1865 |
|
---|
1866 | final List<StyleRecord> allStyleElems = new ArrayList<>(nodes.size()+ways.size()+relations.size());
|
---|
1867 |
|
---|
1868 | // Need to process all relations first.
|
---|
1869 | // Reason: Make sure, ElemStyles.getStyleCacheWithRange is not called for the same primitive in parallel threads.
|
---|
1870 | // (Could be synchronized, but try to avoid this for performance reasons.)
|
---|
1871 | THREAD_POOL.invoke(new ComputeStyleListWorker(circum, nc, relations, allStyleElems,
|
---|
1872 | Math.max(20, relations.size() / THREAD_POOL.getParallelism() / 3)));
|
---|
1873 | THREAD_POOL.invoke(new ComputeStyleListWorker(circum, nc, new CompositeList<>(nodes, ways), allStyleElems,
|
---|
1874 | Math.max(100, (nodes.size() + ways.size()) / THREAD_POOL.getParallelism() / 3)));
|
---|
1875 |
|
---|
1876 | if (!benchmark.renderSort()) {
|
---|
1877 | return;
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 | Collections.sort(allStyleElems); // TODO: try parallel sort when switching to Java 8
|
---|
1881 |
|
---|
1882 | if (!benchmark.renderDraw(allStyleElems)) {
|
---|
1883 | return;
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 | for (StyleRecord record : allStyleElems) {
|
---|
1887 | paintRecord(record);
|
---|
1888 | }
|
---|
1889 |
|
---|
1890 | drawVirtualNodes(data, bbox);
|
---|
1891 |
|
---|
1892 | benchmark.renderDone();
|
---|
1893 | } catch (RuntimeException e) {
|
---|
1894 | throw BugReport.intercept(e)
|
---|
1895 | .put("data", data)
|
---|
1896 | .put("circum", circum)
|
---|
1897 | .put("scale", scale)
|
---|
1898 | .put("paintSettings", paintSettings)
|
---|
1899 | .put("renderVirtualNodes", renderVirtualNodes);
|
---|
1900 | } finally {
|
---|
1901 | data.getReadLock().unlock();
|
---|
1902 | }
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 | private void paintRecord(StyleRecord record) {
|
---|
1906 | try {
|
---|
1907 | record.paintPrimitive(paintSettings, this);
|
---|
1908 | } catch (RuntimeException e) {
|
---|
1909 | throw BugReport.intercept(e).put("record", record);
|
---|
1910 | }
|
---|
1911 | }
|
---|
1912 | }
|
---|