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