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.Polygon;
|
---|
15 | import java.awt.Rectangle;
|
---|
16 | import java.awt.RenderingHints;
|
---|
17 | import java.awt.Shape;
|
---|
18 | import java.awt.TexturePaint;
|
---|
19 | import java.awt.font.FontRenderContext;
|
---|
20 | import java.awt.font.GlyphVector;
|
---|
21 | import java.awt.font.LineMetrics;
|
---|
22 | import java.awt.geom.AffineTransform;
|
---|
23 | import java.awt.geom.GeneralPath;
|
---|
24 | import java.awt.geom.Path2D;
|
---|
25 | import java.awt.geom.Point2D;
|
---|
26 | import java.awt.geom.Rectangle2D;
|
---|
27 | import java.util.ArrayList;
|
---|
28 | import java.util.Collection;
|
---|
29 | import java.util.Collections;
|
---|
30 | import java.util.Iterator;
|
---|
31 | import java.util.List;
|
---|
32 | import java.util.concurrent.Callable;
|
---|
33 | import java.util.concurrent.ExecutionException;
|
---|
34 | import java.util.concurrent.ExecutorService;
|
---|
35 | import java.util.concurrent.Future;
|
---|
36 |
|
---|
37 | import javax.swing.AbstractButton;
|
---|
38 | import javax.swing.FocusManager;
|
---|
39 |
|
---|
40 | import org.openstreetmap.josm.Main;
|
---|
41 | import org.openstreetmap.josm.data.Bounds;
|
---|
42 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
43 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
44 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
45 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
46 | import org.openstreetmap.josm.data.osm.Node;
|
---|
47 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
48 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
49 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
50 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
51 | import org.openstreetmap.josm.data.osm.Way;
|
---|
52 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
53 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
54 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
|
---|
55 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.PolyData;
|
---|
56 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
|
---|
57 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
58 | import org.openstreetmap.josm.gui.mappaint.AreaElemStyle;
|
---|
59 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle;
|
---|
60 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.HorizontalTextAlignment;
|
---|
61 | import org.openstreetmap.josm.gui.mappaint.BoxTextElemStyle.VerticalTextAlignment;
|
---|
62 | import org.openstreetmap.josm.gui.mappaint.ElemStyle;
|
---|
63 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
64 | import org.openstreetmap.josm.gui.mappaint.MapImage;
|
---|
65 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
66 | import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
|
---|
67 | import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.Symbol;
|
---|
68 | import org.openstreetmap.josm.gui.mappaint.RepeatImageElemStyle.LineImageAlignment;
|
---|
69 | import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
|
---|
70 | import org.openstreetmap.josm.gui.mappaint.TextElement;
|
---|
71 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
72 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
|
---|
73 | import org.openstreetmap.josm.tools.CompositeList;
|
---|
74 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
75 | import org.openstreetmap.josm.tools.Pair;
|
---|
76 | import org.openstreetmap.josm.tools.Utils;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * A map renderer which renders a map according to style rules in a set of style sheets.
|
---|
80 | * @since 486
|
---|
81 | */
|
---|
82 | public class StyledMapRenderer extends AbstractMapRenderer {
|
---|
83 |
|
---|
84 | private static final Pair<Integer, ExecutorService> THREAD_POOL =
|
---|
85 | Utils.newThreadPool("mappaint.StyledMapRenderer.style_creation.numberOfThreads");
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Iterates over a list of Way Nodes and returns screen coordinates that
|
---|
89 | * represent a line that is shifted by a certain offset perpendicular
|
---|
90 | * to the way direction.
|
---|
91 | *
|
---|
92 | * There is no intention, to handle consecutive duplicate Nodes in a
|
---|
93 | * perfect way, but it is should not throw an exception.
|
---|
94 | */
|
---|
95 | private class OffsetIterator implements Iterator<Point> {
|
---|
96 |
|
---|
97 | private List<Node> nodes;
|
---|
98 | private float offset;
|
---|
99 | private int idx;
|
---|
100 |
|
---|
101 | private Point prev = null;
|
---|
102 | /* 'prev0' is a point that has distance 'offset' from 'prev' and the
|
---|
103 | * line from 'prev' to 'prev0' is perpendicular to the way segment from
|
---|
104 | * 'prev' to the next point.
|
---|
105 | */
|
---|
106 | private int x_prev0, y_prev0;
|
---|
107 |
|
---|
108 | public OffsetIterator(List<Node> nodes, float offset) {
|
---|
109 | this.nodes = nodes;
|
---|
110 | this.offset = offset;
|
---|
111 | idx = 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public boolean hasNext() {
|
---|
116 | return idx < nodes.size();
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public Point next() {
|
---|
121 | if (Math.abs(offset) < 0.1f) return nc.getPoint(nodes.get(idx++));
|
---|
122 |
|
---|
123 | Point current = nc.getPoint(nodes.get(idx));
|
---|
124 |
|
---|
125 | if (idx == nodes.size() - 1) {
|
---|
126 | ++idx;
|
---|
127 | if (prev != null) {
|
---|
128 | return new Point(x_prev0 + current.x - prev.x, y_prev0 + current.y - prev.y);
|
---|
129 | } else {
|
---|
130 | return current;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | Point next = nc.getPoint(nodes.get(idx+1));
|
---|
135 |
|
---|
136 | int dx_next = next.x - current.x;
|
---|
137 | int dy_next = next.y - current.y;
|
---|
138 | double len_next = Math.sqrt(dx_next*dx_next + dy_next*dy_next);
|
---|
139 |
|
---|
140 | if (len_next == 0) {
|
---|
141 | len_next = 1; // value does not matter, because dy_next and dx_next is 0
|
---|
142 | }
|
---|
143 |
|
---|
144 | int x_current0 = current.x + (int) Math.round(offset * dy_next / len_next);
|
---|
145 | int y_current0 = current.y - (int) Math.round(offset * dx_next / len_next);
|
---|
146 |
|
---|
147 | if (idx==0) {
|
---|
148 | ++idx;
|
---|
149 | prev = current;
|
---|
150 | x_prev0 = x_current0;
|
---|
151 | y_prev0 = y_current0;
|
---|
152 | return new Point(x_current0, y_current0);
|
---|
153 | } else {
|
---|
154 | int dx_prev = current.x - prev.x;
|
---|
155 | int dy_prev = current.y - prev.y;
|
---|
156 |
|
---|
157 | // determine intersection of the lines parallel to the two
|
---|
158 | // segments
|
---|
159 | int det = dx_next*dy_prev - dx_prev*dy_next;
|
---|
160 |
|
---|
161 | if (det == 0) {
|
---|
162 | ++idx;
|
---|
163 | prev = current;
|
---|
164 | x_prev0 = x_current0;
|
---|
165 | y_prev0 = y_current0;
|
---|
166 | return new Point(x_current0, y_current0);
|
---|
167 | }
|
---|
168 |
|
---|
169 | int m = dx_next*(y_current0 - y_prev0) - dy_next*(x_current0 - x_prev0);
|
---|
170 |
|
---|
171 | int cx_ = x_prev0 + Math.round((float)m * dx_prev / det);
|
---|
172 | int cy_ = y_prev0 + Math.round((float)m * dy_prev / det);
|
---|
173 | ++idx;
|
---|
174 | prev = current;
|
---|
175 | x_prev0 = x_current0;
|
---|
176 | y_prev0 = y_current0;
|
---|
177 | return new Point(cx_, cy_);
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override
|
---|
182 | public void remove() {
|
---|
183 | throw new UnsupportedOperationException();
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | private static class StyleRecord implements Comparable<StyleRecord> {
|
---|
188 | final ElemStyle style;
|
---|
189 | final OsmPrimitive osm;
|
---|
190 | final int flags;
|
---|
191 |
|
---|
192 | public StyleRecord(ElemStyle style, OsmPrimitive osm, int flags) {
|
---|
193 | this.style = style;
|
---|
194 | this.osm = osm;
|
---|
195 | this.flags = flags;
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public int compareTo(StyleRecord other) {
|
---|
200 | if ((this.flags & FLAG_DISABLED) != 0 && (other.flags & FLAG_DISABLED) == 0)
|
---|
201 | return -1;
|
---|
202 | if ((this.flags & FLAG_DISABLED) == 0 && (other.flags & FLAG_DISABLED) != 0)
|
---|
203 | return 1;
|
---|
204 |
|
---|
205 | int d0 = Float.compare(this.style.major_z_index, other.style.major_z_index);
|
---|
206 | if (d0 != 0)
|
---|
207 | return d0;
|
---|
208 |
|
---|
209 | // selected on top of member of selected on top of unselected
|
---|
210 | // FLAG_DISABLED bit is the same at this point
|
---|
211 | if (this.flags > other.flags)
|
---|
212 | return 1;
|
---|
213 | if (this.flags < other.flags)
|
---|
214 | return -1;
|
---|
215 |
|
---|
216 | int dz = Float.compare(this.style.z_index, other.style.z_index);
|
---|
217 | if (dz != 0)
|
---|
218 | return dz;
|
---|
219 |
|
---|
220 | // simple node on top of icons and shapes
|
---|
221 | if (this.style == NodeElemStyle.SIMPLE_NODE_ELEMSTYLE && other.style != NodeElemStyle.SIMPLE_NODE_ELEMSTYLE)
|
---|
222 | return 1;
|
---|
223 | if (this.style != NodeElemStyle.SIMPLE_NODE_ELEMSTYLE && other.style == NodeElemStyle.SIMPLE_NODE_ELEMSTYLE)
|
---|
224 | return -1;
|
---|
225 |
|
---|
226 | // newer primitives to the front
|
---|
227 | long id = this.osm.getUniqueId() - other.osm.getUniqueId();
|
---|
228 | if (id > 0)
|
---|
229 | return 1;
|
---|
230 | if (id < 0)
|
---|
231 | return -1;
|
---|
232 |
|
---|
233 | return Float.compare(this.style.object_z_index, other.style.object_z_index);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | private static Boolean IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG = null;
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Check, if this System has the GlyphVector double translation bug.
|
---|
241 | *
|
---|
242 | * With this bug, <code>gv.setGlyphTransform(i, trfm)</code> has a different
|
---|
243 | * effect than on most other systems, namely the translation components
|
---|
244 | * ("m02" & "m12", {@link AffineTransform}) appear to be twice as large, as
|
---|
245 | * they actually are. The rotation is unaffected (scale & shear not tested
|
---|
246 | * so far).
|
---|
247 | *
|
---|
248 | * This bug has only been observed on Mac OS X, see #7841.
|
---|
249 | *
|
---|
250 | * @return true, if the GlyphVector double translation bug is present on
|
---|
251 | * this System
|
---|
252 | */
|
---|
253 | public static boolean isGlyphVectorDoubleTranslationBug() {
|
---|
254 | if (IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG != null)
|
---|
255 | return IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG;
|
---|
256 | FontRenderContext frc = new FontRenderContext(null, false, false);
|
---|
257 | Font font = new Font("Dialog", Font.PLAIN, 12);
|
---|
258 | GlyphVector gv = font.createGlyphVector(frc, "x");
|
---|
259 | gv.setGlyphTransform(0, AffineTransform.getTranslateInstance(1000, 1000));
|
---|
260 | Shape shape = gv.getGlyphOutline(0);
|
---|
261 | // x is about 1000 on normal stystems and about 2000 when the bug occurs
|
---|
262 | int x = shape.getBounds().x;
|
---|
263 | IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG = x > 1500;
|
---|
264 | return IS_GLYPH_VECTOR_DOUBLE_TRANSLATION_BUG;
|
---|
265 | }
|
---|
266 |
|
---|
267 | private double circum;
|
---|
268 |
|
---|
269 | private MapPaintSettings paintSettings;
|
---|
270 |
|
---|
271 | private Color relationSelectedColor;
|
---|
272 | private Color highlightColorTransparent;
|
---|
273 |
|
---|
274 | private static final int FLAG_NORMAL = 0;
|
---|
275 | private static final int FLAG_DISABLED = 1;
|
---|
276 | private static final int FLAG_MEMBER_OF_SELECTED = 2;
|
---|
277 | private static final int FLAG_SELECTED = 4;
|
---|
278 |
|
---|
279 | private static final double PHI = Math.toRadians(20);
|
---|
280 | private static final double cosPHI = Math.cos(PHI);
|
---|
281 | private static final double sinPHI = Math.sin(PHI);
|
---|
282 |
|
---|
283 | private Collection<WaySegment> highlightWaySegments;
|
---|
284 |
|
---|
285 | // highlight customization fields
|
---|
286 | private int highlightLineWidth;
|
---|
287 | private int highlightPointRadius;
|
---|
288 | private int widerHighlight;
|
---|
289 | private int highlightStep;
|
---|
290 |
|
---|
291 | //flag that activate wider highlight mode
|
---|
292 | private boolean useWiderHighlight;
|
---|
293 |
|
---|
294 | private boolean useStrokes;
|
---|
295 | private boolean showNames;
|
---|
296 | private boolean showIcons;
|
---|
297 | private boolean isOutlineOnly;
|
---|
298 |
|
---|
299 | private Font orderFont;
|
---|
300 |
|
---|
301 | private boolean leftHandTraffic;
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Constructs a new {@code StyledMapRenderer}.
|
---|
305 | *
|
---|
306 | * @param g the graphics context. Must not be null.
|
---|
307 | * @param nc the map viewport. Must not be null.
|
---|
308 | * @param isInactiveMode if true, the paint visitor shall render OSM objects such that they
|
---|
309 | * look inactive. Example: rendering of data in an inactive layer using light gray as color only.
|
---|
310 | * @throws IllegalArgumentException thrown if {@code g} is null
|
---|
311 | * @throws IllegalArgumentException thrown if {@code nc} is null
|
---|
312 | */
|
---|
313 | public StyledMapRenderer(Graphics2D g, NavigatableComponent nc, boolean isInactiveMode) {
|
---|
314 | super(g, nc, isInactiveMode);
|
---|
315 |
|
---|
316 | if (nc!=null) {
|
---|
317 | Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
|
---|
318 | useWiderHighlight = !(focusOwner instanceof AbstractButton || focusOwner == nc);
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | private Polygon buildPolygon(Point center, int radius, int sides) {
|
---|
323 | return buildPolygon(center, radius, sides, 0.0);
|
---|
324 | }
|
---|
325 |
|
---|
326 | private Polygon buildPolygon(Point center, int radius, int sides, double rotation) {
|
---|
327 | Polygon polygon = new Polygon();
|
---|
328 | for (int i = 0; i < sides; i++) {
|
---|
329 | double angle = ((2 * Math.PI / sides) * i) - rotation;
|
---|
330 | int x = (int) Math.round(center.x + radius * Math.cos(angle));
|
---|
331 | int y = (int) Math.round(center.y + radius * Math.sin(angle));
|
---|
332 | polygon.addPoint(x, y);
|
---|
333 | }
|
---|
334 | return polygon;
|
---|
335 | }
|
---|
336 |
|
---|
337 | private void displaySegments(GeneralPath path, GeneralPath orientationArrows, GeneralPath onewayArrows, GeneralPath onewayArrowsCasing,
|
---|
338 | Color color, BasicStroke line, BasicStroke dashes, Color dashedColor) {
|
---|
339 | g.setColor(isInactiveMode ? inactiveColor : color);
|
---|
340 | if (useStrokes) {
|
---|
341 | g.setStroke(line);
|
---|
342 | }
|
---|
343 | g.draw(path);
|
---|
344 |
|
---|
345 | if(!isInactiveMode && useStrokes && dashes != null) {
|
---|
346 | g.setColor(dashedColor);
|
---|
347 | g.setStroke(dashes);
|
---|
348 | g.draw(path);
|
---|
349 | }
|
---|
350 |
|
---|
351 | if (orientationArrows != null) {
|
---|
352 | g.setColor(isInactiveMode ? inactiveColor : color);
|
---|
353 | g.setStroke(new BasicStroke(line.getLineWidth(), line.getEndCap(), BasicStroke.JOIN_MITER, line.getMiterLimit()));
|
---|
354 | g.draw(orientationArrows);
|
---|
355 | }
|
---|
356 |
|
---|
357 | if (onewayArrows != null) {
|
---|
358 | g.setStroke(new BasicStroke(1, line.getEndCap(), BasicStroke.JOIN_MITER, line.getMiterLimit()));
|
---|
359 | g.fill(onewayArrowsCasing);
|
---|
360 | g.setColor(isInactiveMode ? inactiveColor : backgroundColor);
|
---|
361 | g.fill(onewayArrows);
|
---|
362 | }
|
---|
363 |
|
---|
364 | if (useStrokes) {
|
---|
365 | g.setStroke(new BasicStroke());
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Displays text at specified position including its halo, if applicable.
|
---|
371 | *
|
---|
372 | * @param gv Text's glyphs to display. If {@code null}, use text from {@code s} instead.
|
---|
373 | * @param s text to display if {@code gv} is {@code null}
|
---|
374 | * @param x X position
|
---|
375 | * @param y Y position
|
---|
376 | * @param disabled {@code true} if element is disabled (filtered out)
|
---|
377 | * @param text text style to use
|
---|
378 | */
|
---|
379 | private void displayText(GlyphVector gv, String s, int x, int y, boolean disabled, TextElement text) {
|
---|
380 | if (isInactiveMode || disabled) {
|
---|
381 | g.setColor(inactiveColor);
|
---|
382 | if (gv != null) {
|
---|
383 | g.drawGlyphVector(gv, x, y);
|
---|
384 | } else {
|
---|
385 | g.setFont(text.font);
|
---|
386 | g.drawString(s, x, y);
|
---|
387 | }
|
---|
388 | } else if (text.haloRadius != null) {
|
---|
389 | g.setStroke(new BasicStroke(2*text.haloRadius, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
|
---|
390 | g.setColor(text.haloColor);
|
---|
391 | if (gv == null) {
|
---|
392 | FontRenderContext frc = g.getFontRenderContext();
|
---|
393 | gv = text.font.createGlyphVector(frc, s);
|
---|
394 | }
|
---|
395 | Shape textOutline = gv.getOutline(x, y);
|
---|
396 | g.draw(textOutline);
|
---|
397 | g.setStroke(new BasicStroke());
|
---|
398 | g.setColor(text.color);
|
---|
399 | g.fill(textOutline);
|
---|
400 | } else {
|
---|
401 | g.setColor(text.color);
|
---|
402 | if (gv != null) {
|
---|
403 | g.drawGlyphVector(gv, x, y);
|
---|
404 | } else {
|
---|
405 | g.setFont(text.font);
|
---|
406 | g.drawString(s, x, y);
|
---|
407 | }
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, MapImage fillImage, TextElement text) {
|
---|
412 |
|
---|
413 | Shape area = path.createTransformedShape(nc.getAffineTransform());
|
---|
414 |
|
---|
415 | if (!isOutlineOnly) {
|
---|
416 | if (fillImage == null) {
|
---|
417 | if (isInactiveMode) {
|
---|
418 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.33f));
|
---|
419 | }
|
---|
420 | g.setColor(color);
|
---|
421 | g.fill(area);
|
---|
422 | } else {
|
---|
423 | TexturePaint texture = new TexturePaint(fillImage.getImage(),
|
---|
424 | new Rectangle(0, 0, fillImage.getWidth(), fillImage.getHeight()));
|
---|
425 | g.setPaint(texture);
|
---|
426 | Float alpha = Utils.color_int2float(fillImage.alpha);
|
---|
427 | if (alpha != 1f) {
|
---|
428 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
|
---|
429 | }
|
---|
430 | g.fill(area);
|
---|
431 | g.setPaintMode();
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | drawAreaText(osm, text, area);
|
---|
436 | }
|
---|
437 |
|
---|
438 | private void drawAreaText(OsmPrimitive osm, TextElement text, Shape area) {
|
---|
439 | if (text != null && isShowNames()) {
|
---|
440 | // abort if we can't compose the label to be rendered
|
---|
441 | if (text.labelCompositionStrategy == null) return;
|
---|
442 | String name = text.labelCompositionStrategy.compose(osm);
|
---|
443 | if (name == null) return;
|
---|
444 |
|
---|
445 | Rectangle pb = area.getBounds();
|
---|
446 | FontMetrics fontMetrics = g.getFontMetrics(orderFont); // if slow, use cache
|
---|
447 | Rectangle2D nb = fontMetrics.getStringBounds(name, g); // if slow, approximate by strlen()*maxcharbounds(font)
|
---|
448 |
|
---|
449 | // Using the Centroid is Nicer for buildings like: +--------+
|
---|
450 | // but this needs to be fast. As most houses are | 42 |
|
---|
451 | // boxes anyway, the center of the bounding box +---++---+
|
---|
452 | // will have to do. ++
|
---|
453 | // Centroids are not optimal either, just imagine a U-shaped house.
|
---|
454 |
|
---|
455 | // quick check to see if label box is smaller than primitive box
|
---|
456 | if (pb.width >= nb.getWidth() && pb.height >= nb.getHeight()) {
|
---|
457 |
|
---|
458 | final double w = pb.width - nb.getWidth();
|
---|
459 | final double h = pb.height - nb.getHeight();
|
---|
460 |
|
---|
461 | final int x2 = pb.x + (int)(w/2.0);
|
---|
462 | final int y2 = pb.y + (int)(h/2.0);
|
---|
463 |
|
---|
464 | final int nbw = (int) nb.getWidth();
|
---|
465 | final int nbh = (int) nb.getHeight();
|
---|
466 |
|
---|
467 | Rectangle centeredNBounds = new Rectangle(x2, y2, nbw, nbh);
|
---|
468 |
|
---|
469 | // slower check to see if label is displayed inside primitive shape
|
---|
470 | boolean labelOK = area.contains(centeredNBounds);
|
---|
471 | if (!labelOK) {
|
---|
472 | // if center position (C) is not inside osm shape, try naively some other positions as follows:
|
---|
473 | final int x1 = pb.x + (int)( w/4.0);
|
---|
474 | final int x3 = pb.x + (int)(3*w/4.0);
|
---|
475 | final int y1 = pb.y + (int)( h/4.0);
|
---|
476 | final int y3 = pb.y + (int)(3*h/4.0);
|
---|
477 | // +-----------+
|
---|
478 | // | 5 1 6 |
|
---|
479 | // | 4 C 2 |
|
---|
480 | // | 8 3 7 |
|
---|
481 | // +-----------+
|
---|
482 | Rectangle[] candidates = new Rectangle[] {
|
---|
483 | new Rectangle(x2, y1, nbw, nbh),
|
---|
484 | new Rectangle(x3, y2, nbw, nbh),
|
---|
485 | new Rectangle(x2, y3, nbw, nbh),
|
---|
486 | new Rectangle(x1, y2, nbw, nbh),
|
---|
487 | new Rectangle(x1, y1, nbw, nbh),
|
---|
488 | new Rectangle(x3, y1, nbw, nbh),
|
---|
489 | new Rectangle(x3, y3, nbw, nbh),
|
---|
490 | new Rectangle(x1, y3, nbw, nbh)
|
---|
491 | };
|
---|
492 | // Dumb algorithm to find a better placement. We could surely find a smarter one but it should
|
---|
493 | // solve most of building issues with only few calculations (8 at most)
|
---|
494 | for (int i = 0; i < candidates.length && !labelOK; i++) {
|
---|
495 | centeredNBounds = candidates[i];
|
---|
496 | labelOK = area.contains(centeredNBounds);
|
---|
497 | }
|
---|
498 | }
|
---|
499 | if (labelOK) {
|
---|
500 | Font defaultFont = g.getFont();
|
---|
501 | int x = (int)(centeredNBounds.getMinX() - nb.getMinX());
|
---|
502 | int y = (int)(centeredNBounds.getMinY() - nb.getMinY());
|
---|
503 | displayText(null, name, x, y, osm.isDisabled(), text);
|
---|
504 | g.setFont(defaultFont);
|
---|
505 | } else if (Main.isDebugEnabled()) {
|
---|
506 | Main.debug("Couldn't find a correct label placement for "+osm+" / "+name);
|
---|
507 | }
|
---|
508 | }
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | public void drawArea(Relation r, Color color, MapImage fillImage, TextElement text) {
|
---|
513 | Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, r);
|
---|
514 | if (!r.isDisabled() && !multipolygon.getOuterWays().isEmpty()) {
|
---|
515 | for (PolyData pd : multipolygon.getCombinedPolygons()) {
|
---|
516 | Path2D.Double p = pd.get();
|
---|
517 | if (!isAreaVisible(p)) {
|
---|
518 | continue;
|
---|
519 | }
|
---|
520 | drawArea(r, p,
|
---|
521 | pd.selected ? paintSettings.getRelationSelectedColor(color.getAlpha()) : color,
|
---|
522 | fillImage, text);
|
---|
523 | }
|
---|
524 | }
|
---|
525 | }
|
---|
526 |
|
---|
527 | public void drawArea(Way w, Color color, MapImage fillImage, TextElement text) {
|
---|
528 | drawArea(w, getPath(w), color, fillImage, text);
|
---|
529 | }
|
---|
530 |
|
---|
531 | public void drawBoxText(Node n, BoxTextElemStyle bs) {
|
---|
532 | if (!isShowNames() || bs == null)
|
---|
533 | return;
|
---|
534 |
|
---|
535 | Point p = nc.getPoint(n);
|
---|
536 | TextElement text = bs.text;
|
---|
537 | String s = text.labelCompositionStrategy.compose(n);
|
---|
538 | if (s == null) return;
|
---|
539 |
|
---|
540 | Font defaultFont = g.getFont();
|
---|
541 | g.setFont(text.font);
|
---|
542 |
|
---|
543 | int x = p.x + text.xOffset;
|
---|
544 | int y = p.y + text.yOffset;
|
---|
545 | /**
|
---|
546 | *
|
---|
547 | * left-above __center-above___ right-above
|
---|
548 | * left-top| |right-top
|
---|
549 | * | |
|
---|
550 | * left-center| center-center |right-center
|
---|
551 | * | |
|
---|
552 | * left-bottom|_________________|right-bottom
|
---|
553 | * left-below center-below right-below
|
---|
554 | *
|
---|
555 | */
|
---|
556 | Rectangle box = bs.getBox();
|
---|
557 | if (bs.hAlign == HorizontalTextAlignment.RIGHT) {
|
---|
558 | x += box.x + box.width + 2;
|
---|
559 | } else {
|
---|
560 | FontRenderContext frc = g.getFontRenderContext();
|
---|
561 | Rectangle2D bounds = text.font.getStringBounds(s, frc);
|
---|
562 | int textWidth = (int) bounds.getWidth();
|
---|
563 | if (bs.hAlign == HorizontalTextAlignment.CENTER) {
|
---|
564 | x -= textWidth / 2;
|
---|
565 | } else if (bs.hAlign == HorizontalTextAlignment.LEFT) {
|
---|
566 | x -= - box.x + 4 + textWidth;
|
---|
567 | } else throw new AssertionError();
|
---|
568 | }
|
---|
569 |
|
---|
570 | if (bs.vAlign == VerticalTextAlignment.BOTTOM) {
|
---|
571 | y += box.y + box.height;
|
---|
572 | } else {
|
---|
573 | FontRenderContext frc = g.getFontRenderContext();
|
---|
574 | LineMetrics metrics = text.font.getLineMetrics(s, frc);
|
---|
575 | if (bs.vAlign == VerticalTextAlignment.ABOVE) {
|
---|
576 | y -= - box.y + metrics.getDescent();
|
---|
577 | } else if (bs.vAlign == VerticalTextAlignment.TOP) {
|
---|
578 | y -= - box.y - metrics.getAscent();
|
---|
579 | } else if (bs.vAlign == VerticalTextAlignment.CENTER) {
|
---|
580 | y += (metrics.getAscent() - metrics.getDescent()) / 2;
|
---|
581 | } else if (bs.vAlign == VerticalTextAlignment.BELOW) {
|
---|
582 | y += box.y + box.height + metrics.getAscent() + 2;
|
---|
583 | } else throw new AssertionError();
|
---|
584 | }
|
---|
585 | displayText(null, s, x, y, n.isDisabled(), text);
|
---|
586 | g.setFont(defaultFont);
|
---|
587 | }
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Draw an image along a way repeatedly.
|
---|
591 | *
|
---|
592 | * @param way the way
|
---|
593 | * @param pattern the image
|
---|
594 | * @param offset offset from the way
|
---|
595 | * @param spacing spacing between two images
|
---|
596 | * @param phase initial spacing
|
---|
597 | * @param align alignment of the image. The top, center or bottom edge
|
---|
598 | * can be aligned with the way.
|
---|
599 | */
|
---|
600 | public void drawRepeatImage(Way way, Image pattern, float offset, float spacing, float phase, LineImageAlignment align) {
|
---|
601 | final int imgWidth = pattern.getWidth(null);
|
---|
602 | final double repeat = imgWidth + spacing;
|
---|
603 | final int imgHeight = pattern.getHeight(null);
|
---|
604 |
|
---|
605 | Point lastP = null;
|
---|
606 | double currentWayLength = phase % repeat;
|
---|
607 | if (currentWayLength < 0) {
|
---|
608 | currentWayLength += repeat;
|
---|
609 | }
|
---|
610 |
|
---|
611 | int dy1, dy2;
|
---|
612 | switch (align) {
|
---|
613 | case TOP:
|
---|
614 | dy1 = 0;
|
---|
615 | dy2 = imgHeight;
|
---|
616 | break;
|
---|
617 | case CENTER:
|
---|
618 | dy1 = - imgHeight / 2;
|
---|
619 | dy2 = imgHeight + dy1;
|
---|
620 | break;
|
---|
621 | case BOTTOM:
|
---|
622 | dy1 = -imgHeight;
|
---|
623 | dy2 = 0;
|
---|
624 | break;
|
---|
625 | default:
|
---|
626 | throw new AssertionError();
|
---|
627 | }
|
---|
628 |
|
---|
629 | OffsetIterator it = new OffsetIterator(way.getNodes(), offset);
|
---|
630 | while (it.hasNext()) {
|
---|
631 | Point thisP = it.next();
|
---|
632 |
|
---|
633 | if (lastP != null) {
|
---|
634 | final double segmentLength = thisP.distance(lastP);
|
---|
635 |
|
---|
636 | final double dx = thisP.x - lastP.x;
|
---|
637 | final double dy = thisP.y - lastP.y;
|
---|
638 |
|
---|
639 | // pos is the position from the beginning of the current segment
|
---|
640 | // where an image should be painted
|
---|
641 | double pos = repeat - (currentWayLength % repeat);
|
---|
642 |
|
---|
643 | AffineTransform saveTransform = g.getTransform();
|
---|
644 | g.translate(lastP.x, lastP.y);
|
---|
645 | g.rotate(Math.atan2(dy, dx));
|
---|
646 |
|
---|
647 | // draw the rest of the image from the last segment in case it
|
---|
648 | // is cut off
|
---|
649 | if (pos > spacing) {
|
---|
650 | // segment is too short for a complete image
|
---|
651 | if (pos > segmentLength + spacing) {
|
---|
652 | g.drawImage(pattern, 0, dy1, (int) segmentLength, dy2,
|
---|
653 | (int) (repeat - pos), 0,
|
---|
654 | (int) (repeat - pos + segmentLength), imgHeight, null);
|
---|
655 | // rest of the image fits fully on the current segment
|
---|
656 | } else {
|
---|
657 | g.drawImage(pattern, 0, dy1, (int) (pos - spacing), dy2,
|
---|
658 | (int) (repeat - pos), 0, imgWidth, imgHeight, null);
|
---|
659 | }
|
---|
660 | }
|
---|
661 | // draw remaining images for this segment
|
---|
662 | while (pos < segmentLength) {
|
---|
663 | // cut off at the end?
|
---|
664 | if (pos + imgWidth > segmentLength) {
|
---|
665 | g.drawImage(pattern, (int) pos, dy1, (int) segmentLength, dy2,
|
---|
666 | 0, 0, (int) segmentLength - (int) pos, imgHeight, null);
|
---|
667 | } else {
|
---|
668 | g.drawImage(pattern, (int) pos, dy1, nc);
|
---|
669 | }
|
---|
670 | pos += repeat;
|
---|
671 | }
|
---|
672 | g.setTransform(saveTransform);
|
---|
673 |
|
---|
674 | currentWayLength += segmentLength;
|
---|
675 | }
|
---|
676 | lastP = thisP;
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | @Override
|
---|
681 | public void drawNode(Node n, Color color, int size, boolean fill) {
|
---|
682 | if(size <= 0 && !n.isHighlighted())
|
---|
683 | return;
|
---|
684 |
|
---|
685 | Point p = nc.getPoint(n);
|
---|
686 |
|
---|
687 | if(n.isHighlighted()) {
|
---|
688 | drawPointHighlight(p, size);
|
---|
689 | }
|
---|
690 |
|
---|
691 | if (size > 1) {
|
---|
692 | if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
|
---|
693 | int radius = size / 2;
|
---|
694 |
|
---|
695 | if (isInactiveMode || n.isDisabled()) {
|
---|
696 | g.setColor(inactiveColor);
|
---|
697 | } else {
|
---|
698 | g.setColor(color);
|
---|
699 | }
|
---|
700 | if (fill) {
|
---|
701 | g.fillRect(p.x-radius-1, p.y-radius-1, size + 1, size + 1);
|
---|
702 | } else {
|
---|
703 | g.drawRect(p.x-radius-1, p.y-radius-1, size, size);
|
---|
704 | }
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | public void drawNodeIcon(Node n, Image img, float alpha, boolean selected, boolean member) {
|
---|
709 | Point p = nc.getPoint(n);
|
---|
710 |
|
---|
711 | final int w = img.getWidth(null), h=img.getHeight(null);
|
---|
712 | if(n.isHighlighted()) {
|
---|
713 | drawPointHighlight(p, Math.max(w, h));
|
---|
714 | }
|
---|
715 |
|
---|
716 | if (alpha != 1f) {
|
---|
717 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
|
---|
718 | }
|
---|
719 | g.drawImage(img, p.x-w/2, p.y-h/2, nc);
|
---|
720 | g.setPaintMode();
|
---|
721 | if (selected || member)
|
---|
722 | {
|
---|
723 | Color color;
|
---|
724 | if (isInactiveMode || n.isDisabled()) {
|
---|
725 | color = inactiveColor;
|
---|
726 | } else if (selected) {
|
---|
727 | color = selectedColor;
|
---|
728 | } else {
|
---|
729 | color = relationSelectedColor;
|
---|
730 | }
|
---|
731 | g.setColor(color);
|
---|
732 | g.drawRect(p.x-w/2-2, p.y-h/2-2, w+4, h+4);
|
---|
733 | }
|
---|
734 | }
|
---|
735 |
|
---|
736 | public void drawNodeSymbol(Node n, Symbol s, Color fillColor, Color strokeColor) {
|
---|
737 | Point p = nc.getPoint(n);
|
---|
738 | int radius = s.size / 2;
|
---|
739 |
|
---|
740 | if(n.isHighlighted()) {
|
---|
741 | drawPointHighlight(p, s.size);
|
---|
742 | }
|
---|
743 |
|
---|
744 | if (fillColor != null) {
|
---|
745 | g.setColor(fillColor);
|
---|
746 | switch (s.symbol) {
|
---|
747 | case SQUARE:
|
---|
748 | g.fillRect(p.x - radius, p.y - radius, s.size, s.size);
|
---|
749 | break;
|
---|
750 | case CIRCLE:
|
---|
751 | g.fillOval(p.x - radius, p.y - radius, s.size, s.size);
|
---|
752 | break;
|
---|
753 | case TRIANGLE:
|
---|
754 | g.fillPolygon(buildPolygon(p, radius, 3, Math.PI / 2));
|
---|
755 | break;
|
---|
756 | case PENTAGON:
|
---|
757 | g.fillPolygon(buildPolygon(p, radius, 5, Math.PI / 2));
|
---|
758 | break;
|
---|
759 | case HEXAGON:
|
---|
760 | g.fillPolygon(buildPolygon(p, radius, 6));
|
---|
761 | break;
|
---|
762 | case HEPTAGON:
|
---|
763 | g.fillPolygon(buildPolygon(p, radius, 7, Math.PI / 2));
|
---|
764 | break;
|
---|
765 | case OCTAGON:
|
---|
766 | g.fillPolygon(buildPolygon(p, radius, 8, Math.PI / 8));
|
---|
767 | break;
|
---|
768 | case NONAGON:
|
---|
769 | g.fillPolygon(buildPolygon(p, radius, 9, Math.PI / 2));
|
---|
770 | break;
|
---|
771 | case DECAGON:
|
---|
772 | g.fillPolygon(buildPolygon(p, radius, 10));
|
---|
773 | break;
|
---|
774 | default:
|
---|
775 | throw new AssertionError();
|
---|
776 | }
|
---|
777 | }
|
---|
778 | if (s.stroke != null) {
|
---|
779 | g.setStroke(s.stroke);
|
---|
780 | g.setColor(strokeColor);
|
---|
781 | switch (s.symbol) {
|
---|
782 | case SQUARE:
|
---|
783 | g.drawRect(p.x - radius, p.y - radius, s.size - 1, s.size - 1);
|
---|
784 | break;
|
---|
785 | case CIRCLE:
|
---|
786 | g.drawOval(p.x - radius, p.y - radius, s.size - 1, s.size - 1);
|
---|
787 | break;
|
---|
788 | case TRIANGLE:
|
---|
789 | g.drawPolygon(buildPolygon(p, radius, 3, Math.PI / 2));
|
---|
790 | break;
|
---|
791 | case PENTAGON:
|
---|
792 | g.drawPolygon(buildPolygon(p, radius, 5, Math.PI / 2));
|
---|
793 | break;
|
---|
794 | case HEXAGON:
|
---|
795 | g.drawPolygon(buildPolygon(p, radius, 6));
|
---|
796 | break;
|
---|
797 | case HEPTAGON:
|
---|
798 | g.drawPolygon(buildPolygon(p, radius, 7, Math.PI / 2));
|
---|
799 | break;
|
---|
800 | case OCTAGON:
|
---|
801 | g.drawPolygon(buildPolygon(p, radius, 8, Math.PI / 8));
|
---|
802 | break;
|
---|
803 | case NONAGON:
|
---|
804 | g.drawPolygon(buildPolygon(p, radius, 9, Math.PI / 2));
|
---|
805 | break;
|
---|
806 | case DECAGON:
|
---|
807 | g.drawPolygon(buildPolygon(p, radius, 10));
|
---|
808 | break;
|
---|
809 | default:
|
---|
810 | throw new AssertionError();
|
---|
811 | }
|
---|
812 | g.setStroke(new BasicStroke());
|
---|
813 | }
|
---|
814 | }
|
---|
815 |
|
---|
816 | /**
|
---|
817 | * Draw a number of the order of the two consecutive nodes within the
|
---|
818 | * parents way
|
---|
819 | */
|
---|
820 | public void drawOrderNumber(Node n1, Node n2, int orderNumber, Color clr) {
|
---|
821 | Point p1 = nc.getPoint(n1);
|
---|
822 | Point p2 = nc.getPoint(n2);
|
---|
823 | StyledMapRenderer.this.drawOrderNumber(p1, p2, orderNumber, clr);
|
---|
824 | }
|
---|
825 |
|
---|
826 | /**
|
---|
827 | * highlights a given GeneralPath using the settings from BasicStroke to match the line's
|
---|
828 | * style. Width of the highlight is hard coded.
|
---|
829 | * @param path
|
---|
830 | * @param line
|
---|
831 | */
|
---|
832 | private void drawPathHighlight(GeneralPath path, BasicStroke line) {
|
---|
833 | if(path == null)
|
---|
834 | return;
|
---|
835 | g.setColor(highlightColorTransparent);
|
---|
836 | float w = (line.getLineWidth() + highlightLineWidth);
|
---|
837 | if (useWiderHighlight) w+=widerHighlight;
|
---|
838 | while(w >= line.getLineWidth()) {
|
---|
839 | g.setStroke(new BasicStroke(w, line.getEndCap(), line.getLineJoin(), line.getMiterLimit()));
|
---|
840 | g.draw(path);
|
---|
841 | w -= highlightStep;
|
---|
842 | }
|
---|
843 | }
|
---|
844 | /**
|
---|
845 | * highlights a given point by drawing a rounded rectangle around it. Give the
|
---|
846 | * size of the object you want to be highlighted, width is added automatically.
|
---|
847 | */
|
---|
848 | private void drawPointHighlight(Point p, int size) {
|
---|
849 | g.setColor(highlightColorTransparent);
|
---|
850 | int s = size + highlightPointRadius;
|
---|
851 | if (useWiderHighlight) s+=widerHighlight;
|
---|
852 | while(s >= size) {
|
---|
853 | int r = (int) Math.floor(s/2);
|
---|
854 | g.fillRoundRect(p.x-r, p.y-r, s, s, r, r);
|
---|
855 | s -= highlightStep;
|
---|
856 | }
|
---|
857 | }
|
---|
858 |
|
---|
859 | public void drawRestriction(Image img, Point pVia, double vx, double vx2, double vy, double vy2, double angle, boolean selected) {
|
---|
860 | // rotate image with direction last node in from to, and scale down image to 16*16 pixels
|
---|
861 | Image smallImg = ImageProvider.createRotatedImage(img, angle, new Dimension(16, 16));
|
---|
862 | int w = smallImg.getWidth(null), h=smallImg.getHeight(null);
|
---|
863 | g.drawImage(smallImg, (int)(pVia.x+vx+vx2)-w/2, (int)(pVia.y+vy+vy2)-h/2, nc);
|
---|
864 |
|
---|
865 | if (selected) {
|
---|
866 | g.setColor(isInactiveMode ? inactiveColor : relationSelectedColor);
|
---|
867 | g.drawRect((int)(pVia.x+vx+vx2)-w/2-2,(int)(pVia.y+vy+vy2)-h/2-2, w+4, h+4);
|
---|
868 | }
|
---|
869 | }
|
---|
870 |
|
---|
871 | public void drawRestriction(Relation r, MapImage icon) {
|
---|
872 | Way fromWay = null;
|
---|
873 | Way toWay = null;
|
---|
874 | OsmPrimitive via = null;
|
---|
875 |
|
---|
876 | /* find the "from", "via" and "to" elements */
|
---|
877 | for (RelationMember m : r.getMembers()) {
|
---|
878 | if(m.getMember().isIncomplete())
|
---|
879 | return;
|
---|
880 | else {
|
---|
881 | if(m.isWay()) {
|
---|
882 | Way w = m.getWay();
|
---|
883 | if(w.getNodesCount() < 2) {
|
---|
884 | continue;
|
---|
885 | }
|
---|
886 |
|
---|
887 | switch(m.getRole()) {
|
---|
888 | case "from":
|
---|
889 | if(fromWay == null) {
|
---|
890 | fromWay = w;
|
---|
891 | }
|
---|
892 | break;
|
---|
893 | case "to":
|
---|
894 | if(toWay == null) {
|
---|
895 | toWay = w;
|
---|
896 | }
|
---|
897 | break;
|
---|
898 | case "via":
|
---|
899 | if(via == null) {
|
---|
900 | via = w;
|
---|
901 | }
|
---|
902 | }
|
---|
903 | } else if(m.isNode()) {
|
---|
904 | Node n = m.getNode();
|
---|
905 | if("via".equals(m.getRole()) && via == null) {
|
---|
906 | via = n;
|
---|
907 | }
|
---|
908 | }
|
---|
909 | }
|
---|
910 | }
|
---|
911 |
|
---|
912 | if (fromWay == null || toWay == null || via == null)
|
---|
913 | return;
|
---|
914 |
|
---|
915 | Node viaNode;
|
---|
916 | if(via instanceof Node)
|
---|
917 | {
|
---|
918 | viaNode = (Node) via;
|
---|
919 | if(!fromWay.isFirstLastNode(viaNode))
|
---|
920 | return;
|
---|
921 | }
|
---|
922 | else
|
---|
923 | {
|
---|
924 | Way viaWay = (Way) via;
|
---|
925 | Node firstNode = viaWay.firstNode();
|
---|
926 | Node lastNode = viaWay.lastNode();
|
---|
927 | Boolean onewayvia = false;
|
---|
928 |
|
---|
929 | String onewayviastr = viaWay.get("oneway");
|
---|
930 | if(onewayviastr != null)
|
---|
931 | {
|
---|
932 | if("-1".equals(onewayviastr)) {
|
---|
933 | onewayvia = true;
|
---|
934 | Node tmp = firstNode;
|
---|
935 | firstNode = lastNode;
|
---|
936 | lastNode = tmp;
|
---|
937 | } else {
|
---|
938 | onewayvia = OsmUtils.getOsmBoolean(onewayviastr);
|
---|
939 | if (onewayvia == null) {
|
---|
940 | onewayvia = false;
|
---|
941 | }
|
---|
942 | }
|
---|
943 | }
|
---|
944 |
|
---|
945 | if(fromWay.isFirstLastNode(firstNode)) {
|
---|
946 | viaNode = firstNode;
|
---|
947 | } else if (!onewayvia && fromWay.isFirstLastNode(lastNode)) {
|
---|
948 | viaNode = lastNode;
|
---|
949 | } else
|
---|
950 | return;
|
---|
951 | }
|
---|
952 |
|
---|
953 | /* find the "direct" nodes before the via node */
|
---|
954 | Node fromNode;
|
---|
955 | if(fromWay.firstNode() == via) {
|
---|
956 | fromNode = fromWay.getNode(1);
|
---|
957 | } else {
|
---|
958 | fromNode = fromWay.getNode(fromWay.getNodesCount()-2);
|
---|
959 | }
|
---|
960 |
|
---|
961 | Point pFrom = nc.getPoint(fromNode);
|
---|
962 | Point pVia = nc.getPoint(viaNode);
|
---|
963 |
|
---|
964 | /* starting from via, go back the "from" way a few pixels
|
---|
965 | (calculate the vector vx/vy with the specified length and the direction
|
---|
966 | away from the "via" node along the first segment of the "from" way)
|
---|
967 | */
|
---|
968 | double distanceFromVia=14;
|
---|
969 | double dx = (pFrom.x >= pVia.x) ? (pFrom.x - pVia.x) : (pVia.x - pFrom.x);
|
---|
970 | double dy = (pFrom.y >= pVia.y) ? (pFrom.y - pVia.y) : (pVia.y - pFrom.y);
|
---|
971 |
|
---|
972 | double fromAngle;
|
---|
973 | if(dx == 0.0) {
|
---|
974 | fromAngle = Math.PI/2;
|
---|
975 | } else {
|
---|
976 | fromAngle = Math.atan(dy / dx);
|
---|
977 | }
|
---|
978 | double fromAngleDeg = Math.toDegrees(fromAngle);
|
---|
979 |
|
---|
980 | double vx = distanceFromVia * Math.cos(fromAngle);
|
---|
981 | double vy = distanceFromVia * Math.sin(fromAngle);
|
---|
982 |
|
---|
983 | if(pFrom.x < pVia.x) {
|
---|
984 | vx = -vx;
|
---|
985 | }
|
---|
986 | if(pFrom.y < pVia.y) {
|
---|
987 | vy = -vy;
|
---|
988 | }
|
---|
989 |
|
---|
990 | /* go a few pixels away from the way (in a right angle)
|
---|
991 | (calculate the vx2/vy2 vector with the specified length and the direction
|
---|
992 | 90degrees away from the first segment of the "from" way)
|
---|
993 | */
|
---|
994 | double distanceFromWay=10;
|
---|
995 | double vx2 = 0;
|
---|
996 | double vy2 = 0;
|
---|
997 | double iconAngle = 0;
|
---|
998 |
|
---|
999 | if(pFrom.x >= pVia.x && pFrom.y >= pVia.y) {
|
---|
1000 | if(!leftHandTraffic) {
|
---|
1001 | vx2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg - 90));
|
---|
1002 | vy2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg - 90));
|
---|
1003 | } else {
|
---|
1004 | vx2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg + 90));
|
---|
1005 | vy2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg + 90));
|
---|
1006 | }
|
---|
1007 | iconAngle = 270+fromAngleDeg;
|
---|
1008 | }
|
---|
1009 | if(pFrom.x < pVia.x && pFrom.y >= pVia.y) {
|
---|
1010 | if(!leftHandTraffic) {
|
---|
1011 | vx2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg));
|
---|
1012 | vy2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg));
|
---|
1013 | } else {
|
---|
1014 | vx2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg + 180));
|
---|
1015 | vy2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg + 180));
|
---|
1016 | }
|
---|
1017 | iconAngle = 90-fromAngleDeg;
|
---|
1018 | }
|
---|
1019 | if(pFrom.x < pVia.x && pFrom.y < pVia.y) {
|
---|
1020 | if(!leftHandTraffic) {
|
---|
1021 | vx2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg + 90));
|
---|
1022 | vy2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg + 90));
|
---|
1023 | } else {
|
---|
1024 | vx2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg - 90));
|
---|
1025 | vy2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg - 90));
|
---|
1026 | }
|
---|
1027 | iconAngle = 90+fromAngleDeg;
|
---|
1028 | }
|
---|
1029 | if(pFrom.x >= pVia.x && pFrom.y < pVia.y) {
|
---|
1030 | if(!leftHandTraffic) {
|
---|
1031 | vx2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg + 180));
|
---|
1032 | vy2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg + 180));
|
---|
1033 | } else {
|
---|
1034 | vx2 = distanceFromWay * Math.sin(Math.toRadians(fromAngleDeg));
|
---|
1035 | vy2 = distanceFromWay * Math.cos(Math.toRadians(fromAngleDeg));
|
---|
1036 | }
|
---|
1037 | iconAngle = 270-fromAngleDeg;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | drawRestriction(isInactiveMode || r.isDisabled() ? icon.getDisabled() : icon.getImage(),
|
---|
1041 | pVia, vx, vx2, vy, vy2, iconAngle, r.isSelected());
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | public void drawTextOnPath(Way way, TextElement text) {
|
---|
1045 | if (way == null || text == null)
|
---|
1046 | return;
|
---|
1047 | String name = text.getString(way);
|
---|
1048 | if (name == null || name.isEmpty())
|
---|
1049 | return;
|
---|
1050 |
|
---|
1051 | FontMetrics fontMetrics = g.getFontMetrics(text.font);
|
---|
1052 | Rectangle2D rec = fontMetrics.getStringBounds(name, g);
|
---|
1053 |
|
---|
1054 | Rectangle bounds = g.getClipBounds();
|
---|
1055 |
|
---|
1056 | Polygon poly = new Polygon();
|
---|
1057 | Point lastPoint = null;
|
---|
1058 | Iterator<Node> it = way.getNodes().iterator();
|
---|
1059 | double pathLength = 0;
|
---|
1060 | long dx, dy;
|
---|
1061 |
|
---|
1062 | // find half segments that are long enough to draw text on
|
---|
1063 | // (don't draw text over the cross hair in the center of each segment)
|
---|
1064 | List<Double> longHalfSegmentSart = new ArrayList<>(); // start point of half segment (as length along the way)
|
---|
1065 | List<Double> longHalfSegmentEnd = new ArrayList<>(); // end point of half segment (as length along the way)
|
---|
1066 | List<Double> longHalfsegmentQuality = new ArrayList<>(); // quality factor (off screen / partly on screen / fully on screen)
|
---|
1067 |
|
---|
1068 | while (it.hasNext()) {
|
---|
1069 | Node n = it.next();
|
---|
1070 | Point p = nc.getPoint(n);
|
---|
1071 | poly.addPoint(p.x, p.y);
|
---|
1072 |
|
---|
1073 | if(lastPoint != null) {
|
---|
1074 | dx = p.x - lastPoint.x;
|
---|
1075 | dy = p.y - lastPoint.y;
|
---|
1076 | double segmentLength = Math.sqrt(dx*dx + dy*dy);
|
---|
1077 | if (segmentLength > 2*(rec.getWidth()+4)) {
|
---|
1078 | Point center = new Point((lastPoint.x + p.x)/2, (lastPoint.y + p.y)/2);
|
---|
1079 | double q = 0;
|
---|
1080 | if (bounds != null) {
|
---|
1081 | if (bounds.contains(lastPoint) && bounds.contains(center)) {
|
---|
1082 | q = 2;
|
---|
1083 | } else if (bounds.contains(lastPoint) || bounds.contains(center)) {
|
---|
1084 | q = 1;
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 | longHalfSegmentSart.add(pathLength);
|
---|
1088 | longHalfSegmentEnd.add(pathLength + segmentLength / 2);
|
---|
1089 | longHalfsegmentQuality.add(q);
|
---|
1090 |
|
---|
1091 | q = 0;
|
---|
1092 | if (bounds != null) {
|
---|
1093 | if (bounds.contains(center) && bounds.contains(p)) {
|
---|
1094 | q = 2;
|
---|
1095 | } else if (bounds.contains(center) || bounds.contains(p)) {
|
---|
1096 | q = 1;
|
---|
1097 | }
|
---|
1098 | }
|
---|
1099 | longHalfSegmentSart.add(pathLength + segmentLength / 2);
|
---|
1100 | longHalfSegmentEnd.add(pathLength + segmentLength);
|
---|
1101 | longHalfsegmentQuality.add(q);
|
---|
1102 | }
|
---|
1103 | pathLength += segmentLength;
|
---|
1104 | }
|
---|
1105 | lastPoint = p;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | if (rec.getWidth() > pathLength)
|
---|
1109 | return;
|
---|
1110 |
|
---|
1111 | double t1, t2;
|
---|
1112 |
|
---|
1113 | if (!longHalfSegmentSart.isEmpty()) {
|
---|
1114 | if (way.getNodesCount() == 2) {
|
---|
1115 | // For 2 node ways, the two half segments are exactly
|
---|
1116 | // the same size and distance from the center.
|
---|
1117 | // Prefer the first one for consistency.
|
---|
1118 | longHalfsegmentQuality.set(0, longHalfsegmentQuality.get(0) + 0.5);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | // find the long half segment that is closest to the center of the way
|
---|
1122 | // candidates with higher quality value are preferred
|
---|
1123 | double bestStart = Double.NaN;
|
---|
1124 | double bestEnd = Double.NaN;
|
---|
1125 | double bestDistanceToCenter = Double.MAX_VALUE;
|
---|
1126 | double bestQuality = -1;
|
---|
1127 | for (int i=0; i<longHalfSegmentSart.size(); i++) {
|
---|
1128 | double start = longHalfSegmentSart.get(i);
|
---|
1129 | double end = longHalfSegmentEnd.get(i);
|
---|
1130 | double dist = Math.abs(0.5 * (end + start) - 0.5 * pathLength);
|
---|
1131 | if (longHalfsegmentQuality.get(i) > bestQuality || (dist < bestDistanceToCenter && longHalfsegmentQuality.get(i) == bestQuality)) {
|
---|
1132 | bestStart = start;
|
---|
1133 | bestEnd = end;
|
---|
1134 | bestDistanceToCenter = dist;
|
---|
1135 | bestQuality = longHalfsegmentQuality.get(i);
|
---|
1136 | }
|
---|
1137 | }
|
---|
1138 | double remaining = bestEnd - bestStart - rec.getWidth(); // total space left and right from the text
|
---|
1139 | // The space left and right of the text should be distributed 20% - 80% (towards the center),
|
---|
1140 | // but the smaller space should not be less than 7 px.
|
---|
1141 | // However, if the total remaining space is less than 14 px, then distribute it evenly.
|
---|
1142 | double smallerSpace = Math.min(Math.max(0.2 * remaining, 7), 0.5 * remaining);
|
---|
1143 | if ((bestEnd + bestStart)/2 < pathLength/2) {
|
---|
1144 | t2 = bestEnd - smallerSpace;
|
---|
1145 | t1 = t2 - rec.getWidth();
|
---|
1146 | } else {
|
---|
1147 | t1 = bestStart + smallerSpace;
|
---|
1148 | t2 = t1 + rec.getWidth();
|
---|
1149 | }
|
---|
1150 | } else {
|
---|
1151 | // doesn't fit into one half-segment -> just put it in the center of the way
|
---|
1152 | t1 = pathLength/2 - rec.getWidth()/2;
|
---|
1153 | t2 = pathLength/2 + rec.getWidth()/2;
|
---|
1154 | }
|
---|
1155 | t1 /= pathLength;
|
---|
1156 | t2 /= pathLength;
|
---|
1157 |
|
---|
1158 | double[] p1 = pointAt(t1, poly, pathLength);
|
---|
1159 | double[] p2 = pointAt(t2, poly, pathLength);
|
---|
1160 |
|
---|
1161 | if (p1 == null || p2 == null)
|
---|
1162 | return;
|
---|
1163 |
|
---|
1164 | double angleOffset;
|
---|
1165 | double offsetSign;
|
---|
1166 | double tStart;
|
---|
1167 |
|
---|
1168 | if (p1[0] < p2[0] &&
|
---|
1169 | p1[2] < Math.PI/2 &&
|
---|
1170 | p1[2] > -Math.PI/2) {
|
---|
1171 | angleOffset = 0;
|
---|
1172 | offsetSign = 1;
|
---|
1173 | tStart = t1;
|
---|
1174 | } else {
|
---|
1175 | angleOffset = Math.PI;
|
---|
1176 | offsetSign = -1;
|
---|
1177 | tStart = t2;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | FontRenderContext frc = g.getFontRenderContext();
|
---|
1181 | GlyphVector gv = text.font.createGlyphVector(frc, name);
|
---|
1182 |
|
---|
1183 | for (int i=0; i<gv.getNumGlyphs(); ++i) {
|
---|
1184 | Rectangle2D rect = gv.getGlyphLogicalBounds(i).getBounds2D();
|
---|
1185 | double t = tStart + offsetSign * (rect.getX() + rect.getWidth()/2) / pathLength;
|
---|
1186 | double[] p = pointAt(t, poly, pathLength);
|
---|
1187 | if (p != null) {
|
---|
1188 | AffineTransform trfm = AffineTransform.getTranslateInstance(p[0] - rect.getX(), p[1]);
|
---|
1189 | trfm.rotate(p[2]+angleOffset);
|
---|
1190 | double off = -rect.getY() - rect.getHeight()/2 + text.yOffset;
|
---|
1191 | trfm.translate(-rect.getWidth()/2, off);
|
---|
1192 | if (isGlyphVectorDoubleTranslationBug()) {
|
---|
1193 | // scale the translation components by one half
|
---|
1194 | AffineTransform tmp = AffineTransform.getTranslateInstance(-0.5 * trfm.getTranslateX(), -0.5 * trfm.getTranslateY());
|
---|
1195 | tmp.concatenate(trfm);
|
---|
1196 | trfm = tmp;
|
---|
1197 | }
|
---|
1198 | gv.setGlyphTransform(i, trfm);
|
---|
1199 | }
|
---|
1200 | }
|
---|
1201 | displayText(gv, null, 0, 0, way.isDisabled(), text);
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | /**
|
---|
1205 | * draw way
|
---|
1206 | * @param showOrientation show arrows that indicate the technical orientation of
|
---|
1207 | * the way (defined by order of nodes)
|
---|
1208 | * @param showOneway show symbols that indicate the direction of the feature,
|
---|
1209 | * e.g. oneway street or waterway
|
---|
1210 | * @param onewayReversed for oneway=-1 and similar
|
---|
1211 | */
|
---|
1212 | public void drawWay(Way way, Color color, BasicStroke line, BasicStroke dashes, Color dashedColor, float offset,
|
---|
1213 | boolean showOrientation, boolean showHeadArrowOnly,
|
---|
1214 | boolean showOneway, boolean onewayReversed) {
|
---|
1215 |
|
---|
1216 | GeneralPath path = new GeneralPath();
|
---|
1217 | GeneralPath orientationArrows = showOrientation ? new GeneralPath() : null;
|
---|
1218 | GeneralPath onewayArrows = showOneway ? new GeneralPath() : null;
|
---|
1219 | GeneralPath onewayArrowsCasing = showOneway ? new GeneralPath() : null;
|
---|
1220 | Rectangle bounds = g.getClipBounds();
|
---|
1221 | if (bounds != null) {
|
---|
1222 | // avoid arrow heads at the border
|
---|
1223 | bounds.grow(100, 100);
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | double wayLength = 0;
|
---|
1227 | Point lastPoint = null;
|
---|
1228 | boolean initialMoveToNeeded = true;
|
---|
1229 | List<Node> wayNodes = way.getNodes();
|
---|
1230 | if (wayNodes.size() < 2) return;
|
---|
1231 |
|
---|
1232 | // only highlight the segment if the way itself is not highlighted
|
---|
1233 | if (!way.isHighlighted()) {
|
---|
1234 | GeneralPath highlightSegs = null;
|
---|
1235 | for (WaySegment ws : highlightWaySegments) {
|
---|
1236 | if (ws.way != way || ws.lowerIndex < offset) {
|
---|
1237 | continue;
|
---|
1238 | }
|
---|
1239 | if(highlightSegs == null) {
|
---|
1240 | highlightSegs = new GeneralPath();
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | Point p1 = nc.getPoint(ws.getFirstNode());
|
---|
1244 | Point p2 = nc.getPoint(ws.getSecondNode());
|
---|
1245 | highlightSegs.moveTo(p1.x, p1.y);
|
---|
1246 | highlightSegs.lineTo(p2.x, p2.y);
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | drawPathHighlight(highlightSegs, line);
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | Iterator<Point> it = new OffsetIterator(wayNodes, offset);
|
---|
1253 | while (it.hasNext()) {
|
---|
1254 | Point p = it.next();
|
---|
1255 | if (lastPoint != null) {
|
---|
1256 | Point p1 = lastPoint;
|
---|
1257 | Point p2 = p;
|
---|
1258 |
|
---|
1259 | /**
|
---|
1260 | * Do custom clipping to work around openjdk bug. It leads to
|
---|
1261 | * drawing artefacts when zooming in a lot. (#4289, #4424)
|
---|
1262 | * (Looks like int overflow.)
|
---|
1263 | */
|
---|
1264 | LineClip clip = new LineClip(p1, p2, bounds);
|
---|
1265 | if (clip.execute()) {
|
---|
1266 | if (!p1.equals(clip.getP1())) {
|
---|
1267 | p1 = clip.getP1();
|
---|
1268 | path.moveTo(p1.x, p1.y);
|
---|
1269 | } else if (initialMoveToNeeded) {
|
---|
1270 | initialMoveToNeeded = false;
|
---|
1271 | path.moveTo(p1.x, p1.y);
|
---|
1272 | }
|
---|
1273 | p2 = clip.getP2();
|
---|
1274 | path.lineTo(p2.x, p2.y);
|
---|
1275 |
|
---|
1276 | /* draw arrow */
|
---|
1277 | if (showHeadArrowOnly ? !it.hasNext() : showOrientation) {
|
---|
1278 | final double segmentLength = p1.distance(p2);
|
---|
1279 | if (segmentLength != 0.0) {
|
---|
1280 | final double l = (10. + line.getLineWidth()) / segmentLength;
|
---|
1281 |
|
---|
1282 | final double sx = l * (p1.x - p2.x);
|
---|
1283 | final double sy = l * (p1.y - p2.y);
|
---|
1284 |
|
---|
1285 | orientationArrows.moveTo (p2.x + cosPHI * sx - sinPHI * sy, p2.y + sinPHI * sx + cosPHI * sy);
|
---|
1286 | orientationArrows.lineTo(p2.x, p2.y);
|
---|
1287 | orientationArrows.lineTo (p2.x + cosPHI * sx + sinPHI * sy, p2.y - sinPHI * sx + cosPHI * sy);
|
---|
1288 | }
|
---|
1289 | }
|
---|
1290 | if (showOneway) {
|
---|
1291 | final double segmentLength = p1.distance(p2);
|
---|
1292 | if (segmentLength != 0.0) {
|
---|
1293 | final double nx = (p2.x - p1.x) / segmentLength;
|
---|
1294 | final double ny = (p2.y - p1.y) / segmentLength;
|
---|
1295 |
|
---|
1296 | final double interval = 60;
|
---|
1297 | // distance from p1
|
---|
1298 | double dist = interval - (wayLength % interval);
|
---|
1299 |
|
---|
1300 | while (dist < segmentLength) {
|
---|
1301 | for (int i=0; i<2; ++i) {
|
---|
1302 | float onewaySize = i == 0 ? 3f : 2f;
|
---|
1303 | GeneralPath onewayPath = i == 0 ? onewayArrowsCasing : onewayArrows;
|
---|
1304 |
|
---|
1305 | // scale such that border is 1 px
|
---|
1306 | final double fac = - (onewayReversed ? -1 : 1) * onewaySize * (1 + sinPHI) / (sinPHI * cosPHI);
|
---|
1307 | final double sx = nx * fac;
|
---|
1308 | final double sy = ny * fac;
|
---|
1309 |
|
---|
1310 | // Attach the triangle at the incenter and not at the tip.
|
---|
1311 | // Makes the border even at all sides.
|
---|
1312 | final double x = p1.x + nx * (dist + (onewayReversed ? -1 : 1) * (onewaySize / sinPHI));
|
---|
1313 | final double y = p1.y + ny * (dist + (onewayReversed ? -1 : 1) * (onewaySize / sinPHI));
|
---|
1314 |
|
---|
1315 | onewayPath.moveTo(x, y);
|
---|
1316 | onewayPath.lineTo (x + cosPHI * sx - sinPHI * sy, y + sinPHI * sx + cosPHI * sy);
|
---|
1317 | onewayPath.lineTo (x + cosPHI * sx + sinPHI * sy, y - sinPHI * sx + cosPHI * sy);
|
---|
1318 | onewayPath.lineTo(x, y);
|
---|
1319 | }
|
---|
1320 | dist += interval;
|
---|
1321 | }
|
---|
1322 | }
|
---|
1323 | wayLength += segmentLength;
|
---|
1324 | }
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 | lastPoint = p;
|
---|
1328 | }
|
---|
1329 | if(way.isHighlighted()) {
|
---|
1330 | drawPathHighlight(path, line);
|
---|
1331 | }
|
---|
1332 | displaySegments(path, orientationArrows, onewayArrows, onewayArrowsCasing, color, line, dashes, dashedColor);
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | public double getCircum() {
|
---|
1336 | return circum;
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | @Override
|
---|
1340 | public void getColors() {
|
---|
1341 | super.getColors();
|
---|
1342 | this.relationSelectedColor = PaintColors.RELATIONSELECTED.get();
|
---|
1343 | this.highlightColorTransparent = new Color(highlightColor.getRed(), highlightColor.getGreen(), highlightColor.getBlue(), 100);
|
---|
1344 | this.backgroundColor = PaintColors.getBackgroundColor();
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | @Override
|
---|
1348 | protected void getSettings(boolean virtual) {
|
---|
1349 | super.getSettings(virtual);
|
---|
1350 | paintSettings = MapPaintSettings.INSTANCE;
|
---|
1351 |
|
---|
1352 | circum = nc.getDist100Pixel();
|
---|
1353 |
|
---|
1354 | leftHandTraffic = Main.pref.getBoolean("mappaint.lefthandtraffic", false);
|
---|
1355 |
|
---|
1356 | useStrokes = paintSettings.getUseStrokesDistance() > circum;
|
---|
1357 | showNames = paintSettings.getShowNamesDistance() > circum;
|
---|
1358 | showIcons = paintSettings.getShowIconsDistance() > circum;
|
---|
1359 | isOutlineOnly = paintSettings.isOutlineOnly();
|
---|
1360 | orderFont = new Font(Main.pref.get("mappaint.font", "Droid Sans"), Font.PLAIN, Main.pref.getInteger("mappaint.fontsize", 8));
|
---|
1361 |
|
---|
1362 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
---|
1363 | Main.pref.getBoolean("mappaint.use-antialiasing", true) ?
|
---|
1364 | RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
|
---|
1365 |
|
---|
1366 | highlightLineWidth = Main.pref.getInteger("mappaint.highlight.width", 4);
|
---|
1367 | highlightPointRadius = Main.pref.getInteger("mappaint.highlight.radius", 7);
|
---|
1368 | widerHighlight = Main.pref.getInteger("mappaint.highlight.bigger-increment", 5);
|
---|
1369 | highlightStep = Main.pref.getInteger("mappaint.highlight.step", 4);
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | private Path2D.Double getPath(Way w) {
|
---|
1373 | Path2D.Double path = new Path2D.Double();
|
---|
1374 | boolean initial = true;
|
---|
1375 | for (Node n : w.getNodes()) {
|
---|
1376 | EastNorth p = n.getEastNorth();
|
---|
1377 | if (p != null) {
|
---|
1378 | if (initial) {
|
---|
1379 | path.moveTo(p.getX(), p.getY());
|
---|
1380 | initial = false;
|
---|
1381 | } else {
|
---|
1382 | path.lineTo(p.getX(), p.getY());
|
---|
1383 | }
|
---|
1384 | }
|
---|
1385 | }
|
---|
1386 | return path;
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | private boolean isAreaVisible(Path2D.Double area) {
|
---|
1390 | Rectangle2D bounds = area.getBounds2D();
|
---|
1391 | if (bounds.isEmpty()) return false;
|
---|
1392 | Point2D p = nc.getPoint2D(new EastNorth(bounds.getX(), bounds.getY()));
|
---|
1393 | if (p.getX() > nc.getWidth()) return false;
|
---|
1394 | if (p.getY() < 0) return false;
|
---|
1395 | p = nc.getPoint2D(new EastNorth(bounds.getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight()));
|
---|
1396 | if (p.getX() < 0) return false;
|
---|
1397 | if (p.getY() > nc.getHeight()) return false;
|
---|
1398 | return true;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | public boolean isInactiveMode() {
|
---|
1402 | return isInactiveMode;
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | public boolean isShowIcons() {
|
---|
1406 | return showIcons;
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | public boolean isShowNames() {
|
---|
1410 | return showNames;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | private double[] pointAt(double t, Polygon poly, double pathLength) {
|
---|
1414 | double totalLen = t * pathLength;
|
---|
1415 | double curLen = 0;
|
---|
1416 | long dx, dy;
|
---|
1417 | double segLen;
|
---|
1418 |
|
---|
1419 | // Yes, it is inefficient to iterate from the beginning for each glyph.
|
---|
1420 | // Can be optimized if it turns out to be slow.
|
---|
1421 | for (int i = 1; i < poly.npoints; ++i) {
|
---|
1422 | dx = poly.xpoints[i] - poly.xpoints[i-1];
|
---|
1423 | dy = poly.ypoints[i] - poly.ypoints[i-1];
|
---|
1424 | segLen = Math.sqrt(dx*dx + dy*dy);
|
---|
1425 | if (totalLen > curLen + segLen) {
|
---|
1426 | curLen += segLen;
|
---|
1427 | continue;
|
---|
1428 | }
|
---|
1429 | return new double[] {
|
---|
1430 | poly.xpoints[i-1]+(totalLen - curLen)/segLen*dx,
|
---|
1431 | poly.ypoints[i-1]+(totalLen - curLen)/segLen*dy,
|
---|
1432 | Math.atan2(dy, dx)};
|
---|
1433 | }
|
---|
1434 | return null;
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | private class ComputeStyleListWorker implements Callable<List<StyleRecord>>, Visitor {
|
---|
1438 | private final List<? extends OsmPrimitive> input;
|
---|
1439 | private final int from;
|
---|
1440 | private final int to;
|
---|
1441 | private final List<StyleRecord> output;
|
---|
1442 | private final DataSet data;
|
---|
1443 |
|
---|
1444 | private final ElemStyles styles = MapPaintStyles.getStyles();
|
---|
1445 |
|
---|
1446 | private final boolean drawArea = circum <= Main.pref.getInteger("mappaint.fillareas", 10000000);
|
---|
1447 | private final boolean drawMultipolygon = drawArea && Main.pref.getBoolean("mappaint.multipolygon", true);
|
---|
1448 | private final boolean drawRestriction = Main.pref.getBoolean("mappaint.restriction", true);
|
---|
1449 |
|
---|
1450 | /**
|
---|
1451 | * Constructs a new {@code ComputeStyleListWorker}.
|
---|
1452 | * @param input the primitives to process
|
---|
1453 | * @param from first index of <code>input</code> to use
|
---|
1454 | * @param to last index + 1
|
---|
1455 | * @param output the list of styles to which styles will be added
|
---|
1456 | * @param data the data set
|
---|
1457 | */
|
---|
1458 | public ComputeStyleListWorker(final List<? extends OsmPrimitive> input, int from, int to, List<StyleRecord> output, DataSet data) {
|
---|
1459 | this.input = input;
|
---|
1460 | this.from = from;
|
---|
1461 | this.to = to;
|
---|
1462 | this.output = output;
|
---|
1463 | this.data = data;
|
---|
1464 | this.styles.setDrawMultipolygon(drawMultipolygon);
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 | @Override
|
---|
1468 | public List<StyleRecord> call() throws Exception {
|
---|
1469 | MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().lock();
|
---|
1470 | try {
|
---|
1471 | for (int i = from; i<to; i++) {
|
---|
1472 | OsmPrimitive osm = input.get(i);
|
---|
1473 | if (osm.isDrawable()) {
|
---|
1474 | osm.accept(this);
|
---|
1475 | }
|
---|
1476 | }
|
---|
1477 | return output;
|
---|
1478 | } finally {
|
---|
1479 | MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().unlock();
|
---|
1480 | }
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | @Override
|
---|
1484 | public void visit(Node n) {
|
---|
1485 | if (n.isDisabled()) {
|
---|
1486 | add(n, FLAG_DISABLED);
|
---|
1487 | } else if (data.isSelected(n)) {
|
---|
1488 | add(n, FLAG_SELECTED);
|
---|
1489 | } else if (n.isMemberOfSelected()) {
|
---|
1490 | add(n, FLAG_MEMBER_OF_SELECTED);
|
---|
1491 | } else {
|
---|
1492 | add(n, FLAG_NORMAL);
|
---|
1493 | }
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | @Override
|
---|
1497 | public void visit(Way w) {
|
---|
1498 | if (w.isDisabled()) {
|
---|
1499 | add(w, FLAG_DISABLED);
|
---|
1500 | } else if (data.isSelected(w)) {
|
---|
1501 | add(w, FLAG_SELECTED);
|
---|
1502 | } else if (w.isMemberOfSelected()) {
|
---|
1503 | add(w, FLAG_MEMBER_OF_SELECTED);
|
---|
1504 | } else {
|
---|
1505 | add(w, FLAG_NORMAL);
|
---|
1506 | }
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | @Override
|
---|
1510 | public void visit(Relation r) {
|
---|
1511 | if (r.isDisabled()) {
|
---|
1512 | add(r, FLAG_DISABLED);
|
---|
1513 | } else if (data.isSelected(r)) {
|
---|
1514 | add(r, FLAG_SELECTED);
|
---|
1515 | } else {
|
---|
1516 | add(r, FLAG_NORMAL);
|
---|
1517 | }
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | @Override
|
---|
1521 | public void visit(Changeset cs) {
|
---|
1522 | throw new UnsupportedOperationException();
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | public void add(Node osm, int flags) {
|
---|
1526 | StyleList sl = styles.get(osm, circum, nc);
|
---|
1527 | for (ElemStyle s : sl) {
|
---|
1528 | output.add(new StyleRecord(s, osm, flags));
|
---|
1529 | }
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | public void add(Relation osm, int flags) {
|
---|
1533 | StyleList sl = styles.get(osm, circum, nc);
|
---|
1534 | for (ElemStyle s : sl) {
|
---|
1535 | if (drawMultipolygon && drawArea && s instanceof AreaElemStyle && (flags & FLAG_DISABLED) == 0) {
|
---|
1536 | output.add(new StyleRecord(s, osm, flags));
|
---|
1537 | } else if (drawRestriction && s instanceof NodeElemStyle) {
|
---|
1538 | output.add(new StyleRecord(s, osm, flags));
|
---|
1539 | }
|
---|
1540 | }
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 | public void add(Way osm, int flags) {
|
---|
1544 | StyleList sl = styles.get(osm, circum, nc);
|
---|
1545 | for (ElemStyle s : sl) {
|
---|
1546 | if (!(drawArea && (flags & FLAG_DISABLED) == 0) && s instanceof AreaElemStyle) {
|
---|
1547 | continue;
|
---|
1548 | }
|
---|
1549 | output.add(new StyleRecord(s, osm, flags));
|
---|
1550 | }
|
---|
1551 | }
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | private class ConcurrentTasksHelper {
|
---|
1555 |
|
---|
1556 | private final List<StyleRecord> allStyleElems;
|
---|
1557 | private final DataSet data;
|
---|
1558 |
|
---|
1559 | public ConcurrentTasksHelper(List<StyleRecord> allStyleElems, DataSet data) {
|
---|
1560 | this.allStyleElems = allStyleElems;
|
---|
1561 | this.data = data;
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | void process(List<? extends OsmPrimitive> prims) {
|
---|
1565 | final List<ComputeStyleListWorker> tasks = new ArrayList<>();
|
---|
1566 | final int bucketsize = Math.max(100, prims.size()/THREAD_POOL.a/3);
|
---|
1567 | final int noBuckets = (prims.size() + bucketsize - 1) / bucketsize;
|
---|
1568 | final boolean singleThread = THREAD_POOL.a == 1 || noBuckets == 1;
|
---|
1569 | for (int i=0; i<noBuckets; i++) {
|
---|
1570 | int from = i*bucketsize;
|
---|
1571 | int to = Math.min((i+1)*bucketsize, prims.size());
|
---|
1572 | List<StyleRecord> target = singleThread ? allStyleElems : new ArrayList<StyleRecord>(to - from);
|
---|
1573 | tasks.add(new ComputeStyleListWorker(prims, from, to, target, data));
|
---|
1574 | }
|
---|
1575 | if (singleThread) {
|
---|
1576 | try {
|
---|
1577 | for (ComputeStyleListWorker task : tasks) {
|
---|
1578 | task.call();
|
---|
1579 | }
|
---|
1580 | } catch (Exception ex) {
|
---|
1581 | throw new RuntimeException(ex);
|
---|
1582 | }
|
---|
1583 | } else if (!tasks.isEmpty()) {
|
---|
1584 | try {
|
---|
1585 | for (Future<List<StyleRecord>> future : THREAD_POOL.b.invokeAll(tasks)) {
|
---|
1586 | allStyleElems.addAll(future.get());
|
---|
1587 | }
|
---|
1588 | } catch (InterruptedException | ExecutionException ex) {
|
---|
1589 | throw new RuntimeException(ex);
|
---|
1590 | }
|
---|
1591 | }
|
---|
1592 | }
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | @Override
|
---|
1596 | public void render(final DataSet data, boolean renderVirtualNodes, Bounds bounds) {
|
---|
1597 | BBox bbox = bounds.toBBox();
|
---|
1598 | getSettings(renderVirtualNodes);
|
---|
1599 |
|
---|
1600 | data.getReadLock().lock();
|
---|
1601 | try {
|
---|
1602 | highlightWaySegments = data.getHighlightedWaySegments();
|
---|
1603 |
|
---|
1604 | long timeStart=0, timePhase1=0, timeFinished;
|
---|
1605 | if (Main.isTraceEnabled()) {
|
---|
1606 | timeStart = System.currentTimeMillis();
|
---|
1607 | System.err.print("BENCHMARK: rendering ");
|
---|
1608 | Main.debug(null);
|
---|
1609 | }
|
---|
1610 |
|
---|
1611 | List<Node> nodes = data.searchNodes(bbox);
|
---|
1612 | List<Way> ways = data.searchWays(bbox);
|
---|
1613 | List<Relation> relations = data.searchRelations(bbox);
|
---|
1614 |
|
---|
1615 | final List<StyleRecord> allStyleElems = new ArrayList<>(nodes.size()+ways.size()+relations.size());
|
---|
1616 |
|
---|
1617 | ConcurrentTasksHelper helper = new ConcurrentTasksHelper(allStyleElems, data);
|
---|
1618 |
|
---|
1619 | // Need to process all relations first.
|
---|
1620 | // Reason: Make sure, ElemStyles.getStyleCacheWithRange is
|
---|
1621 | // not called for the same primitive in parallel threads.
|
---|
1622 | // (Could be synchronized, but try to avoid this for
|
---|
1623 | // performance reasons.)
|
---|
1624 | helper.process(relations);
|
---|
1625 | helper.process(new CompositeList<>(nodes, ways));
|
---|
1626 |
|
---|
1627 | if (Main.isTraceEnabled()) {
|
---|
1628 | timePhase1 = System.currentTimeMillis();
|
---|
1629 | System.err.print("phase 1 (calculate styles): " + (timePhase1 - timeStart) + " ms");
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 | Collections.sort(allStyleElems); // TODO: try parallel sort when switching to Java 8
|
---|
1633 |
|
---|
1634 | for (StyleRecord r : allStyleElems) {
|
---|
1635 | r.style.paintPrimitive(
|
---|
1636 | r.osm,
|
---|
1637 | paintSettings,
|
---|
1638 | StyledMapRenderer.this,
|
---|
1639 | (r.flags & FLAG_SELECTED) != 0,
|
---|
1640 | (r.flags & FLAG_MEMBER_OF_SELECTED) != 0
|
---|
1641 | );
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 | if (Main.isTraceEnabled()) {
|
---|
1645 | timeFinished = System.currentTimeMillis();
|
---|
1646 | System.err.println("; phase 2 (draw): " + (timeFinished - timePhase1) + " ms; total: " + (timeFinished - timeStart) + " ms" +
|
---|
1647 | " (scale: " + circum + " zoom level: " + Selector.GeneralSelector.scale2level(circum) + ")");
|
---|
1648 | }
|
---|
1649 |
|
---|
1650 | drawVirtualNodes(data, bbox);
|
---|
1651 | } finally {
|
---|
1652 | data.getReadLock().unlock();
|
---|
1653 | }
|
---|
1654 | }
|
---|
1655 | }
|
---|