source: osm/applications/editors/josm/plugins/seachart/src/render/Renderer.java@ 35391

Last change on this file since 35391 was 35391, checked in by malcolmh, 6 years ago

update jchart & renderer

File size: 36.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package render;
3
4import java.awt.BasicStroke;
5import java.awt.Color;
6import java.awt.Font;
7import java.awt.Graphics2D;
8import java.awt.Rectangle;
9import java.awt.RenderingHints;
10import java.awt.TexturePaint;
11import java.awt.font.FontRenderContext;
12import java.awt.font.GlyphVector;
13import java.awt.geom.AffineTransform;
14import java.awt.geom.Arc2D;
15import java.awt.geom.Ellipse2D;
16import java.awt.geom.GeneralPath;
17import java.awt.geom.Line2D;
18import java.awt.geom.Path2D;
19import java.awt.geom.Point2D;
20import java.awt.geom.Rectangle2D;
21import java.awt.geom.RoundRectangle2D;
22import java.awt.image.AffineTransformOp;
23import java.awt.image.BufferedImage;
24import java.util.ArrayList;
25
26import s57.S57map;
27import s57.S57map.GeomIterator;
28import s57.S57map.Pflag;
29import s57.S57map.Snode;
30import s57.S57val.UniHLU;
31import symbols.Areas;
32import symbols.Symbols;
33import symbols.Symbols.Caption;
34import symbols.Symbols.Delta;
35import symbols.Symbols.Form;
36import symbols.Symbols.Handle;
37import symbols.Symbols.Instr;
38import symbols.Symbols.LineStyle;
39import symbols.Symbols.Scheme;
40import symbols.Symbols.SubSymbol;
41import symbols.Symbols.Symbol;
42
43/**
44 * @author Malcolm Herring
45 */
46public final class Renderer {
47 private Renderer() {
48 // Hide default constructor for utilities classes
49 }
50
51 public static final double[] symbolScale = {
52 256.0, 128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.61, 0.372, 0.227, 0.138, 0.0843, 0.0514, 0.0313, 0.0191, 0.0117, 0.007};
53
54 public enum LabelStyle { NONE, RRCT, RECT, ELPS, CIRC, VCLR, PCLR, HCLR }
55
56 static ChartContext context;
57 static S57map map;
58 static double sScale;
59 static Graphics2D g2;
60 static int zoom;
61
62 public static void reRender(Graphics2D g, Rectangle rect, int z, double factor, S57map m, ChartContext c) {
63 g2 = g;
64 zoom = z;
65 context = c;
66 map = m;
67 sScale = symbolScale[zoom] * factor;
68 if (map != null) {
69 if (context.clip()) {
70 Point2D tl = context.getPoint(new Snode(map.bounds.maxlat, map.bounds.minlon));
71 Point2D br = context.getPoint(new Snode(map.bounds.minlat, map.bounds.maxlon));
72 g2.clip(new Rectangle2D.Double(tl.getX(), tl.getY(), (br.getX() - tl.getX()), (br.getY() - tl.getY())));
73 }
74 g2.setBackground(context.background(map));
75 g2.clearRect(rect.x, rect.y, rect.width, rect.height);
76 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
77 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
78 g2.setStroke(new BasicStroke(0, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
79 do {} while (!Rules.rules());
80 }
81 grid();
82 }
83
84 public static void symbol(Symbol symbol) {
85 Point2D point = context.getPoint(Rules.feature.geom.centre);
86 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, null);
87 }
88
89 public static void symbol(Symbol symbol, Scheme scheme) {
90 Point2D point = context.getPoint(Rules.feature.geom.centre);
91 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, null);
92 }
93
94 public static void symbol(Symbol symbol, Delta delta) {
95 Point2D point = context.getPoint(Rules.feature.geom.centre);
96 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, delta);
97 }
98
99 public static void symbol(Symbol symbol, Scheme scheme, Delta delta) {
100 Point2D point = context.getPoint(Rules.feature.geom.centre);
101 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, delta);
102 }
103
104 public static void cluster(ArrayList<Symbol> symbols) {
105 Rectangle2D.Double bbox = null;
106 if (symbols.size() > 4) {
107 for (Instr instr : symbols.get(0)) {
108 if (instr.type == Form.BBOX) {
109 bbox = (Rectangle2D.Double) instr.params;
110 break;
111 }
112 }
113 if (bbox == null) return;
114 }
115 switch (symbols.size()) {
116 case 1:
117 symbol(symbols.get(0), new Delta(Handle.CC, new AffineTransform()));
118 break;
119 case 2:
120 symbol(symbols.get(0), new Delta(Handle.RC, new AffineTransform()));
121 symbol(symbols.get(1), new Delta(Handle.LC, new AffineTransform()));
122 break;
123 case 3:
124 symbol(symbols.get(0), new Delta(Handle.BC, new AffineTransform()));
125 symbol(symbols.get(1), new Delta(Handle.TR, new AffineTransform()));
126 symbol(symbols.get(2), new Delta(Handle.TL, new AffineTransform()));
127 break;
128 case 4:
129 symbol(symbols.get(0), new Delta(Handle.BR, new AffineTransform()));
130 symbol(symbols.get(1), new Delta(Handle.BL, new AffineTransform()));
131 symbol(symbols.get(2), new Delta(Handle.TR, new AffineTransform()));
132 symbol(symbols.get(3), new Delta(Handle.TL, new AffineTransform()));
133 break;
134 case 5:
135 symbol(symbols.get(0), new Delta(Handle.BR, new AffineTransform()));
136 symbol(symbols.get(1), new Delta(Handle.BL, new AffineTransform()));
137 symbol(symbols.get(2), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
138 symbol(symbols.get(3), new Delta(Handle.TC, new AffineTransform()));
139 symbol(symbols.get(4), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
140 break;
141 case 6:
142 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
143 symbol(symbols.get(1), new Delta(Handle.BC, new AffineTransform()));
144 symbol(symbols.get(2), new Delta(Handle.BL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
145 symbol(symbols.get(3), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
146 symbol(symbols.get(4), new Delta(Handle.TC, new AffineTransform()));
147 symbol(symbols.get(5), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
148 break;
149 case 7:
150 symbol(symbols.get(0), new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
151 symbol(symbols.get(1), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
152 symbol(symbols.get(2), new Delta(Handle.CC, new AffineTransform()));
153 symbol(symbols.get(3), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
154 symbol(symbols.get(4), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
155 symbol(symbols.get(5), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
156 symbol(symbols.get(6), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
157 break;
158 case 8:
159 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
160 symbol(symbols.get(1), new Delta(Handle.BL, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
161 symbol(symbols.get(2), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
162 symbol(symbols.get(3), new Delta(Handle.CC, new AffineTransform()));
163 symbol(symbols.get(4), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
164 symbol(symbols.get(5), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
165 symbol(symbols.get(6), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
166 symbol(symbols.get(7), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
167 break;
168 case 9:
169 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(-bbox.width/2, -bbox.height/2)));
170 symbol(symbols.get(1), new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
171 symbol(symbols.get(2), new Delta(Handle.BL, AffineTransform.getTranslateInstance(bbox.width/2, -bbox.height/2)));
172 symbol(symbols.get(3), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
173 symbol(symbols.get(4), new Delta(Handle.CC, new AffineTransform()));
174 symbol(symbols.get(5), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
175 symbol(symbols.get(6), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
176 symbol(symbols.get(7), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
177 symbol(symbols.get(8), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
178 break;
179 }
180 }
181
182 private static Rectangle2D.Double symbolSize(Symbol symbol) {
183 Symbol ssymb = symbol;
184 while (ssymb != null) {
185 for (Instr item : symbol) {
186 if (item.type == Form.BBOX) {
187 return (Rectangle2D.Double) item.params;
188 }
189 if (item.type == Form.SYMB) {
190 ssymb = ((SubSymbol) item.params).instr;
191 break;
192 }
193 }
194 if (ssymb == symbol)
195 break;
196 }
197 return null;
198 }
199
200 public static void lineSymbols(Symbol prisymb, double space, Symbol secsymb, Symbol tersymb, int ratio, Color col) {
201 if ((Rules.feature.geom.prim == Pflag.NOSP) || (Rules.feature.geom.prim == Pflag.POINT))
202 return;
203 Rectangle2D.Double prect = symbolSize(prisymb);
204 Rectangle2D.Double srect = symbolSize(secsymb);
205 Rectangle2D.Double trect = symbolSize(tersymb);
206 if (srect == null)
207 ratio = 0;
208 if (prect != null) {
209 double psize = Math.abs(prect.getY()) * sScale;
210 double ssize = (srect != null) ? Math.abs(srect.getY()) * sScale : 0;
211 double tsize = (trect != null) ? Math.abs(srect.getY()) * sScale : 0;
212 Point2D prev = new Point2D.Double();
213 Point2D next = new Point2D.Double();
214 Point2D curr = new Point2D.Double();
215 Point2D succ = new Point2D.Double();
216 boolean gap = true;
217 boolean piv = false;
218 double len = 0;
219 double angle = 0;
220 int stcount = ratio;
221 boolean stflag = false;
222 Symbol symbol = prisymb;
223 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
224 while (git.hasComp()) {
225 git.nextComp();
226 boolean first = true;
227 while (git.hasEdge()) {
228 git.nextEdge();
229 while (git.hasNode()) {
230 Snode node = git.next();
231 if (node == null) continue;
232 prev = next;
233 next = context.getPoint(node);
234 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
235 piv = true;
236 if (first) {
237 curr = succ = next;
238 gap = (space > 0);
239 stcount = ratio - 1;
240 symbol = prisymb;
241 len = gap ? psize * space * 0.5 : psize;
242 first = false;
243 } else {
244 while (curr.distance(next) >= len) {
245 if (piv) {
246 double rem = len;
247 double s = prev.distance(next);
248 double p = curr.distance(prev);
249 if ((s > 0) && (p > 0)) {
250 double n = curr.distance(next);
251 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
252 double phi = Math.asin(p / len * Math.sin(theta));
253 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
254 }
255 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
256 piv = false;
257 } else {
258 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
259 }
260 if (!gap) {
261 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Scheme(col),
262 new Delta(Handle.BC, AffineTransform.getRotateInstance(
263 Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))));
264 }
265 if (space > 0)
266 gap = !gap;
267 curr = succ;
268 len = gap ? (psize * space) : (--stcount == 0) ? (stflag ? tsize : ssize) : psize;
269 if (stcount == 0) {
270 symbol = stflag ? tersymb : secsymb;
271 if (trect != null)
272 stflag = !stflag;
273 stcount = ratio;
274 } else {
275 symbol = prisymb;
276 }
277 }
278 }
279 }
280 }
281 }
282 }
283 }
284
285 public static void lineVector(LineStyle style) {
286 Path2D.Double p = new Path2D.Double();
287 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
288 Point2D point;
289 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
290 while (git.hasComp()) {
291 git.nextComp();
292 boolean first = true;
293 while (git.hasEdge()) {
294 git.nextEdge();
295 point = context.getPoint(git.next());
296 if (first) {
297 p.moveTo(point.getX(), point.getY());
298 first = false;
299 } else {
300 p.lineTo(point.getX(), point.getY());
301 }
302 while (git.hasNode()) {
303 Snode node = git.next();
304 if (node == null) continue;
305 point = context.getPoint(node);
306 p.lineTo(point.getX(), point.getY());
307 }
308 }
309 }
310 if ((style.fill != null) && (Rules.feature.geom.prim == Pflag.AREA)) {
311 g2.setPaint(style.fill);
312 g2.fill(p);
313 }
314 if (style.line != null) {
315 if (style.dash != null) {
316 float[] dash = new float[style.dash.length];
317 System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
318 for (int i = 0; i < style.dash.length; i++) {
319 dash[i] *= (float) sScale;
320 }
321 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
322 } else {
323 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
324 }
325 g2.setPaint(style.line);
326 g2.draw(p);
327 }
328 }
329
330 public static void grid() {
331 if (context.grid() > 0) {
332 LineStyle style = new LineStyle(Color.black, (float)2.0);
333 double nspan = 60 * Math.toDegrees(map.bounds.maxlon - map.bounds.minlon) / 5.0;
334 double mult = 1.0;
335 if (nspan < 1.0) {
336 do {
337 nspan *= 10.0;
338 mult *= 10.0;
339 } while (nspan < 1.0);
340 } else if (nspan > 10.0){
341 do {
342 nspan /= 10.0;
343 mult /= 10.0;
344 } while (nspan > 10.0);
345 }
346 if (nspan < 2.0) nspan = 1.0;
347 else if (nspan < 5.0) nspan = 2.0;
348 else nspan = 5.0;
349 nspan = nspan / mult / 60.0;
350 double left = Math.toDegrees(map.bounds.minlon) + 180.0;
351 left = Math.ceil(left / nspan);
352 left = Math.toRadians((left * nspan) - 180.0);
353 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
354 Path2D.Double p = new Path2D.Double();
355 for (double lon = left; lon < map.bounds.maxlon; lon += Math.toRadians(nspan)) {
356 Point2D point = context.getPoint(new Snode(map.bounds.maxlat, lon));
357 p.moveTo(point.getX(), point.getY());
358 point = context.getPoint(new Snode(map.bounds.minlat, lon));
359 p.lineTo(point.getX(), point.getY());
360 double deg = Math.toDegrees(lon);
361 String ew = (deg < 0) ? "W" : "E";
362 deg = Math.abs(deg);
363 String dstr = String.format("%03d°", (int)Math.floor(deg));
364 double min = (deg - Math.floor(deg)) * 60.0;
365 String mstr = String.format("%05.2f'%s", min, ew);
366 Symbol label = new Symbol();
367 if (point.getX() > 500.0) {
368 label.add(new Instr(Form.TEXT, new Caption(dstr, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.BR, AffineTransform.getTranslateInstance(-10, -20)))));
369 label.add(new Instr(Form.TEXT, new Caption(mstr, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.BL, AffineTransform.getTranslateInstance(20, 0)))));
370 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, null);
371 }
372 }
373 g2.setPaint(style.line);
374 g2.draw(p);
375 double tspan = 60 * Math.toDegrees(map.bounds.maxlat - map.bounds.minlat) / 5.0;
376 mult = 1.0;
377 if (tspan < 1.0) {
378 do {
379 tspan *= 10.0;
380 mult *= 10.0;
381 } while (tspan < 1.0);
382 } else if (tspan > 10.0){
383 do {
384 tspan /= 10.0;
385 mult /= 10.0;
386 } while (tspan > 10.0);
387 }
388 if (tspan < 2.0) tspan = 1.0;
389 else if (tspan < 5.0) tspan = 2.0;
390 else tspan = 5.0;
391 tspan = tspan / mult / 60.0;
392 double bottom = Math.toDegrees(map.bounds.minlat) + 90.0;
393 bottom = Math.ceil(bottom / tspan);
394 bottom = Math.toRadians((bottom * tspan) - 90.0);
395 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
396 p = new Path2D.Double();
397 for (double lat = bottom; lat < map.bounds.maxlat; lat += Math.toRadians(tspan)) {
398 Point2D point = context.getPoint(new Snode(lat, map.bounds.maxlon));
399 p.moveTo(point.getX(), point.getY());
400 point = context.getPoint(new Snode(lat, map.bounds.minlon));
401 p.lineTo(point.getX(), point.getY());
402 double deg = Math.toDegrees(lat);
403 String ns = (deg < 0) ? "S" : "N";
404 deg = Math.abs(deg);
405 String dstr = String.format("%02d°%s", (int)Math.floor(deg), ns);
406 double min = (deg - Math.floor(deg)) * 60.0;
407 String mstr = String.format("%05.2f'", min);
408 Symbol label = new Symbol();
409 label.add(new Instr(Form.TEXT, new Caption(dstr, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.BL, AffineTransform.getTranslateInstance(10, -10)))));
410 label.add(new Instr(Form.TEXT, new Caption(mstr, new Font("Arial", Font.PLAIN, 40), Color.black, new Delta(Handle.BL, AffineTransform.getTranslateInstance(0, 50)))));
411 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, null);
412 }
413 g2.setPaint(style.line);
414 g2.draw(p);
415 }
416 }
417
418 public static void lineCircle(LineStyle style, double radius, UniHLU units) {
419 switch (units) {
420 case HLU_FEET:
421 radius /= 6076;
422 break;
423 case HLU_KMTR:
424 radius /= 1.852;
425 break;
426 case HLU_HMTR:
427 radius /= 18.52;
428 break;
429 case HLU_SMIL:
430 radius /= 1.15078;
431 break;
432 case HLU_NMIL:
433 break;
434 default:
435 radius /= 1852;
436 break;
437 }
438 radius *= context.mile(Rules.feature);
439 Symbol circle = new Symbol();
440 if (style.fill != null) {
441 circle.add(new Instr(Form.FILL, style.fill));
442 circle.add(new Instr(Form.RSHP, new Ellipse2D.Double(-radius, -radius, radius*2, radius*2)));
443 }
444 circle.add(new Instr(Form.FILL, style.line));
445 circle.add(new Instr(Form.STRK, new BasicStroke(style.width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, style.dash, 0)));
446 circle.add(new Instr(Form.ELPS, new Ellipse2D.Double(-radius, -radius, radius*2, radius*2)));
447 Point2D point = context.getPoint(Rules.feature.geom.centre);
448 Symbols.drawSymbol(g2, circle, 1, point.getX(), point.getY(), null, null);
449 }
450
451 public static void fillPattern(BufferedImage image) {
452 Path2D.Double p = new Path2D.Double();
453 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
454 Point2D point;
455 switch (Rules.feature.geom.prim) {
456 case POINT:
457 point = context.getPoint(Rules.feature.geom.centre);
458 g2.drawImage(image, new AffineTransformOp(AffineTransform.getScaleInstance(sScale, sScale), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
459 (int) (point.getX() - (50 * sScale)), (int) (point.getY() - (50 * sScale)));
460 break;
461 case AREA:
462 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
463 while (git.hasComp()) {
464 git.nextComp();
465 boolean newComp = true;
466 while (git.hasEdge()) {
467 git.nextEdge();
468 point = context.getPoint(git.next());
469 if (newComp) {
470 p.moveTo(point.getX(), point.getY());
471 newComp = false;
472 } else {
473 p.lineTo(point.getX(), point.getY());
474 }
475 while (git.hasNode()) {
476 Snode node = git.next();
477 if (node == null) continue;
478 point = context.getPoint(node);
479 p.lineTo(point.getX(), point.getY());
480 }
481 }
482 }
483 g2.setPaint(new TexturePaint(image, new Rectangle(0, 0, 1 + (int) (300 * sScale), 1 + (int) (300 * sScale))));
484 g2.fill(p);
485 break;
486 default:
487 break;
488 }
489 }
490
491 public static void labelText(String str, Font font, Color tc) {
492 labelText(str, font, tc, LabelStyle.NONE, null, null, null);
493 }
494
495 public static void labelText(String str, Font font, Color tc, Delta delta) {
496 labelText(str, font, tc, LabelStyle.NONE, null, null, delta);
497 }
498
499 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg) {
500 labelText(str, font, tc, style, fg, null, null);
501 }
502
503 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Color bg) {
504 labelText(str, font, tc, style, fg, bg, null);
505 }
506
507 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Delta delta) {
508 labelText(str, font, tc, style, fg, null, delta);
509 }
510
511 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Color bg, Delta delta) {
512 if (delta == null) delta = new Delta(Handle.CC);
513 if (bg == null) bg = new Color(0x00000000, true);
514 if (str == null || str.isEmpty()) str = " ";
515 FontRenderContext frc = g2.getFontRenderContext();
516 GlyphVector gv = font.deriveFont((float) font.getSize()).createGlyphVector(frc, str.equals(" ") ? "M" : str);
517 Rectangle2D bounds = gv.getVisualBounds();
518 double width = bounds.getWidth();
519 double height = bounds.getHeight();
520 Symbol label = new Symbol();
521 double lx, ly, tx, ty;
522 switch (style) {
523 case RRCT:
524 width += height * 1.0;
525 height *= 1.5;
526 if (width < height) width = height;
527 lx = -width / 2;
528 ly = -height / 2;
529 tx = lx + (height * 0.34);
530 ty = ly + (height * 0.17);
531 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
532 label.add(new Instr(Form.FILL, bg));
533 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
534 label.add(new Instr(Form.FILL, fg));
535 label.add(new Instr(Form.STRK, new BasicStroke(1 + (int) (height/10), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
536 label.add(new Instr(Form.RRCT, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
537 break;
538 case VCLR:
539 width += height * 1.0;
540 height *= 2.0;
541 if (width < height) width = height;
542 lx = -width / 2;
543 ly = -height / 2;
544 tx = lx + (height * 0.27);
545 ty = ly + (height * 0.25);
546 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
547 label.add(new Instr(Form.FILL, bg));
548 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
549 label.add(new Instr(Form.FILL, fg));
550 int sw = 1 + (int) (height/10);
551 double po = sw / 2;
552 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
553 Path2D.Double p = new Path2D.Double(); p.moveTo(-height*0.2, -ly-po);
554 p.lineTo(height*0.2, -ly-po); p.moveTo(0, -ly-po); p.lineTo(0, -ly-po-(height*0.15));
555 p.moveTo(-height*0.2, ly+po); p.lineTo((height*0.2), ly+po); p.moveTo(0, ly+po); p.lineTo(0, ly+po+(height*0.15));
556 label.add(new Instr(Form.PLIN, p));
557 break;
558 case PCLR:
559 width += height * 1.0;
560 height *= 2.0;
561 if (width < height) width = height;
562 lx = -width / 2;
563 ly = -height / 2;
564 tx = lx + (height * 0.27);
565 ty = ly + (height * 0.25);
566 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
567 label.add(new Instr(Form.FILL, bg));
568 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
569 label.add(new Instr(Form.FILL, fg));
570 sw = 1 + (int) (height/10);
571 po = sw / 2;
572 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
573 p = new Path2D.Double();
574 p.moveTo(-height*0.2, -ly-po);
575 p.lineTo(height*0.2, -ly-po);
576 p.moveTo(0, -ly-po);
577 p.lineTo(0, -ly-po-(height*0.15));
578 p.moveTo(-height*0.2, ly+po);
579 p.lineTo((height*0.2), ly+po);
580 p.moveTo(0, ly+po);
581 p.lineTo(0, ly+po+(height*0.15));
582 label.add(new Instr(Form.PLIN, p));
583 label.add(new Instr(Form.SYMB, new Symbols.SubSymbol(Areas.CableFlash, 1, 0, 0, null,
584 new Delta(Handle.CC, new AffineTransform(0, -1, 1, 0, -width/2, 0)))));
585 label.add(new Instr(Form.SYMB, new Symbols.SubSymbol(Areas.CableFlash, 1, 0, 0, null,
586 new Delta(Handle.CC, new AffineTransform(0, -1, 1, 0, width/2, 0)))));
587 break;
588 case HCLR:
589 width += height * 1.5;
590 height *= 1.5;
591 if (width < height) width = height;
592 lx = -width / 2;
593 ly = -height / 2;
594 tx = lx + (height * 0.5);
595 ty = ly + (height * 0.17);
596 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
597 label.add(new Instr(Form.FILL, bg));
598 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
599 label.add(new Instr(Form.FILL, fg));
600 sw = 1 + (int) (height/10);
601 double vo = height / 4;
602 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
603 p = new Path2D.Double();
604 p.moveTo(-width*0.4-sw, -ly-vo);
605 p.lineTo(-width*0.4-sw, ly+vo);
606 p.moveTo(-width*0.4-sw, 0);
607 p.lineTo(-width*0.4+sw, 0);
608 p.moveTo(width*0.4+sw, -ly-vo);
609 p.lineTo(width*0.4+sw, ly+vo);
610 p.moveTo(width*0.4-sw, 0);
611 p.lineTo(width*0.4+sw, 0);
612 label.add(new Instr(Form.PLIN, p));
613 break;
614 default:
615 lx = -width / 2;
616 ly = -height / 2;
617 tx = lx;
618 ty = ly;
619 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
620 break;
621 }
622 label.add(new Instr(Form.TEXT, new Caption(str, font, tc, new Delta(Handle.TL, AffineTransform.getTranslateInstance(tx, ty)))));
623 Point2D point = context.getPoint(Rules.feature.geom.centre);
624 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, delta);
625 }
626
627 public static void lineText(String str, Font font, Color colour, double dy) {
628 if (!str.isEmpty()) {
629 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
630 g2.setPaint(colour);
631 FontRenderContext frc = g2.getFontRenderContext();
632 GlyphVector gv = font.deriveFont(font.getSize2D() * (float) sScale).createGlyphVector(frc, str);
633 double width = gv.getVisualBounds().getWidth();
634 double height = gv.getVisualBounds().getHeight();
635 double offset = (Rules.feature.geom.length * context.mile(Rules.feature) - width) / 2;
636 if (offset > 0) {
637 Point2D before = null;
638 Point2D after = null;
639 ArrayList<Point2D> between = new ArrayList<>();
640 Point2D prev = null;
641 Point2D next = null;
642 double length = 0;
643 double lb = 0;
644 double la = 0;
645 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
646 if (git.hasComp()) {
647 git.nextComp();
648 while (git.hasEdge()) {
649 git.nextEdge();
650 while (git.hasNode()) {
651 Snode node = git.next();
652 if (node == null)
653 continue;
654 prev = next;
655 next = context.getPoint(node);
656 if (prev != null)
657 length += Math.sqrt(Math.pow((next.getX() - prev.getX()), 2) + Math.pow((next.getY() - prev.getY()), 2));
658 if (length < offset) {
659 before = next;
660 lb = la = length;
661 } else if (after == null) {
662 if (length > (offset + width)) {
663 after = next;
664 la = length;
665 break;
666 } else {
667 between.add(next);
668 }
669 }
670 }
671 if (after != null)
672 break;
673 }
674 }
675 if (after != null) {
676 double angle = Math.atan2((after.getY() - before.getY()), (after.getX() - before.getX()));
677 double rotate = Math.abs(angle) < (Math.PI / 2) ? angle : angle + Math.PI;
678 Point2D mid = new Point2D.Double((before.getX() + after.getX()) / 2, (before.getY() + after.getY()) / 2);
679 Point2D centre = context.getPoint(Rules.feature.geom.centre);
680 AffineTransform pos = AffineTransform.getTranslateInstance(-dy * Math.sin(rotate), dy * Math.cos(rotate));
681 pos.rotate(rotate);
682 pos.translate((mid.getX() - centre.getX()), (mid.getY() - centre.getY()));
683 Symbol label = new Symbol();
684 label.add(new Instr(Form.BBOX, new Rectangle2D.Double((-width / 2), -height, width, height)));
685 label.add(new Instr(Form.TEXT, new Caption(str, font, colour, new Delta(Handle.BC))));
686 Symbols.drawSymbol(g2, label, sScale, centre.getX(), centre.getY(), null, new Delta(Handle.BC, pos));
687 }
688 }
689 }
690 }
691
692 public static void lightSector(Color col1, Color col2, double radius, double s1, double s2, Double dir, String str) {
693 double mid = (((s1 + s2) / 2) + (s1 > s2 ? 180 : 0)) % 360;
694 g2.setStroke(new BasicStroke((float) (3.0 * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1,
695 new float[] {20 * (float) sScale, 20 * (float) sScale}, 0));
696 g2.setPaint(Color.black);
697 Point2D.Double centre = (Point2D.Double) context.getPoint(Rules.feature.geom.centre);
698 double radial = radius * context.mile(Rules.feature);
699 if (dir != null) {
700 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(dir)),
701 centre.y + radial * Math.cos(Math.toRadians(dir))));
702 } else {
703 if ((s1 != 0.0) || (s2 != 360.0)) {
704 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(s1)),
705 centre.y + radial * Math.cos(Math.toRadians(s1))));
706 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(s2)),
707 centre.y + radial * Math.cos(Math.toRadians(s2))));
708 }
709 }
710 double arcWidth = 10.0 * sScale;
711 g2.setStroke(new BasicStroke((float) arcWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1));
712 g2.setPaint(col1);
713 g2.draw(new Arc2D.Double(centre.x - radial, centre.y - radial, 2 * radial, 2 * radial, -(s1 + 90),
714 ((s1 < s2) ? (s1 - s2) : (s1 - s2 - 360)), Arc2D.OPEN));
715 if (col2 != null) {
716 g2.setPaint(col2);
717 g2.draw(new Arc2D.Double(centre.x - radial + arcWidth, centre.y - radial + arcWidth, 2 * (radial - arcWidth),
718 2 * (radial - arcWidth), -(s1 + 90), ((s1 < s2) ? (s1 - s2) : (s1 - s2 - 360)), Arc2D.OPEN));
719 }
720 if (str != null && !str.isEmpty()) {
721 Font font = new Font("Arial", Font.PLAIN, 40);
722 double arc = (s2 > s1) ? (s2 - s1) : (s2 - s1 + 360);
723 double awidth = (Math.toRadians(arc) * radial);
724 boolean hand = ((mid > 270) || (mid < 90));
725 double phi = Math.toRadians(mid);
726 radial += 30 * sScale;
727 AffineTransform at = AffineTransform.getTranslateInstance(-radial * Math.sin(phi) / sScale, radial * Math.cos(phi) / sScale);
728 if ((font.getSize() * sScale * str.length()) < awidth) {
729 at.rotate(Math.toRadians(mid + (hand ? 0 : 180)));
730 labelText(str, font, Color.black, new Delta(Handle.CC, at));
731 } else if ((font.getSize() * sScale) < awidth) {
732 hand = (mid < 180);
733 at.rotate(Math.toRadians(mid + (hand ? -90 : 90)));
734 labelText(str, font, Color.black, hand ? new Delta(Handle.RC, at) : new Delta(Handle.LC, at));
735 }
736 if (dir != null) {
737 font = new Font("Arial", Font.PLAIN, 30);
738 str = dir + "°";
739 hand = (dir > 180);
740 phi = Math.toRadians(dir + (hand ? -0.5 : 0.5));
741 radial -= 70 * sScale;
742 at = AffineTransform.getTranslateInstance(-radial * Math.sin(phi) / sScale, radial * Math.cos(phi) / sScale);
743 at.rotate(Math.toRadians(dir + (hand ? 90 : -90)));
744 labelText(str, font, Color.black, hand ? new Delta(Handle.BR, at) : new Delta(Handle.BL, at));
745 }
746 }
747 }
748}
Note: See TracBrowser for help on using the repository browser.