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

Last change on this file since 33195 was 33195, checked in by malcolmh, 8 years ago

[Seachart] Publish new release

File size: 32.2 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 }
82
83 public static void symbol(Symbol symbol) {
84 Point2D point = context.getPoint(Rules.feature.geom.centre);
85 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, null);
86 }
87
88 public static void symbol(Symbol symbol, Scheme scheme) {
89 Point2D point = context.getPoint(Rules.feature.geom.centre);
90 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, null);
91 }
92
93 public static void symbol(Symbol symbol, Delta delta) {
94 Point2D point = context.getPoint(Rules.feature.geom.centre);
95 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, delta);
96 }
97
98 public static void symbol(Symbol symbol, Scheme scheme, Delta delta) {
99 Point2D point = context.getPoint(Rules.feature.geom.centre);
100 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, delta);
101 }
102
103 public static void cluster(ArrayList<Symbol> symbols) {
104 Rectangle2D.Double bbox = null;
105 if (symbols.size() > 4) {
106 for (Instr instr : symbols.get(0)) {
107 if (instr.type == Form.BBOX) {
108 bbox = (Rectangle2D.Double) instr.params;
109 break;
110 }
111 }
112 if (bbox == null) return;
113 }
114 switch (symbols.size()) {
115 case 1:
116 symbol(symbols.get(0), new Delta(Handle.CC, new AffineTransform()));
117 break;
118 case 2:
119 symbol(symbols.get(0), new Delta(Handle.RC, new AffineTransform()));
120 symbol(symbols.get(1), new Delta(Handle.LC, new AffineTransform()));
121 break;
122 case 3:
123 symbol(symbols.get(0), new Delta(Handle.BC, new AffineTransform()));
124 symbol(symbols.get(1), new Delta(Handle.TR, new AffineTransform()));
125 symbol(symbols.get(2), new Delta(Handle.TL, new AffineTransform()));
126 break;
127 case 4:
128 symbol(symbols.get(0), new Delta(Handle.BR, new AffineTransform()));
129 symbol(symbols.get(1), new Delta(Handle.BL, new AffineTransform()));
130 symbol(symbols.get(2), new Delta(Handle.TR, new AffineTransform()));
131 symbol(symbols.get(3), new Delta(Handle.TL, new AffineTransform()));
132 break;
133 case 5:
134 symbol(symbols.get(0), new Delta(Handle.BR, new AffineTransform()));
135 symbol(symbols.get(1), new Delta(Handle.BL, new AffineTransform()));
136 symbol(symbols.get(2), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
137 symbol(symbols.get(3), new Delta(Handle.TC, new AffineTransform()));
138 symbol(symbols.get(4), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
139 break;
140 case 6:
141 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
142 symbol(symbols.get(1), new Delta(Handle.BC, new AffineTransform()));
143 symbol(symbols.get(2), new Delta(Handle.BL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
144 symbol(symbols.get(3), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
145 symbol(symbols.get(4), new Delta(Handle.TC, new AffineTransform()));
146 symbol(symbols.get(5), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
147 break;
148 case 7:
149 symbol(symbols.get(0), new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
150 symbol(symbols.get(1), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
151 symbol(symbols.get(2), new Delta(Handle.CC, new AffineTransform()));
152 symbol(symbols.get(3), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
153 symbol(symbols.get(4), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
154 symbol(symbols.get(5), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
155 symbol(symbols.get(6), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
156 break;
157 case 8:
158 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
159 symbol(symbols.get(1), new Delta(Handle.BL, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
160 symbol(symbols.get(2), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
161 symbol(symbols.get(3), new Delta(Handle.CC, new AffineTransform()));
162 symbol(symbols.get(4), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
163 symbol(symbols.get(5), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
164 symbol(symbols.get(6), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
165 symbol(symbols.get(7), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
166 break;
167 case 9:
168 symbol(symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(-bbox.width/2, -bbox.height/2)));
169 symbol(symbols.get(1), new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
170 symbol(symbols.get(2), new Delta(Handle.BL, AffineTransform.getTranslateInstance(bbox.width/2, -bbox.height/2)));
171 symbol(symbols.get(3), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
172 symbol(symbols.get(4), new Delta(Handle.CC, new AffineTransform()));
173 symbol(symbols.get(5), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
174 symbol(symbols.get(6), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
175 symbol(symbols.get(7), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
176 symbol(symbols.get(8), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
177 break;
178 }
179 }
180
181 private static Rectangle2D.Double symbolSize(Symbol symbol) {
182 Symbol ssymb = symbol;
183 while (ssymb != null) {
184 for (Instr item : symbol) {
185 if (item.type == Form.BBOX) {
186 return (Rectangle2D.Double) item.params;
187 }
188 if (item.type == Form.SYMB) {
189 ssymb = ((SubSymbol) item.params).instr;
190 break;
191 }
192 }
193 if (ssymb == symbol)
194 break;
195 }
196 return null;
197 }
198
199 public static void lineSymbols(Symbol prisymb, double space, Symbol secsymb, Symbol tersymb, int ratio, Color col) {
200 if ((Rules.feature.geom.prim == Pflag.NOSP) || (Rules.feature.geom.prim == Pflag.POINT))
201 return;
202 Rectangle2D.Double prect = symbolSize(prisymb);
203 Rectangle2D.Double srect = symbolSize(secsymb);
204 Rectangle2D.Double trect = symbolSize(tersymb);
205 if (srect == null)
206 ratio = 0;
207 if (prect != null) {
208 double psize = Math.abs(prect.getY()) * sScale;
209 double ssize = (srect != null) ? Math.abs(srect.getY()) * sScale : 0;
210 double tsize = (trect != null) ? Math.abs(srect.getY()) * sScale : 0;
211 Point2D prev = new Point2D.Double();
212 Point2D next = new Point2D.Double();
213 Point2D curr = new Point2D.Double();
214 Point2D succ = new Point2D.Double();
215 boolean gap = true;
216 boolean piv = false;
217 double len = 0;
218 double angle = 0;
219 int stcount = ratio;
220 boolean stflag = false;
221 Symbol symbol = prisymb;
222 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
223 while (git.hasComp()) {
224 git.nextComp();
225 boolean first = true;
226 while (git.hasEdge()) {
227 git.nextEdge();
228 while (git.hasNode()) {
229 Snode node = git.next();
230 if (node == null) continue;
231 prev = next;
232 next = context.getPoint(node);
233 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
234 piv = true;
235 if (first) {
236 curr = succ = next;
237 gap = (space > 0);
238 stcount = ratio - 1;
239 symbol = prisymb;
240 len = gap ? psize * space * 0.5 : psize;
241 first = false;
242 } else {
243 while (curr.distance(next) >= len) {
244 if (piv) {
245 double rem = len;
246 double s = prev.distance(next);
247 double p = curr.distance(prev);
248 if ((s > 0) && (p > 0)) {
249 double n = curr.distance(next);
250 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
251 double phi = Math.asin(p / len * Math.sin(theta));
252 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
253 }
254 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
255 piv = false;
256 } else {
257 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
258 }
259 if (!gap) {
260 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Scheme(col),
261 new Delta(Handle.BC, AffineTransform.getRotateInstance(
262 Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))));
263 }
264 if (space > 0)
265 gap = !gap;
266 curr = succ;
267 len = gap ? (psize * space) : (--stcount == 0) ? (stflag ? tsize : ssize) : psize;
268 if (stcount == 0) {
269 symbol = stflag ? tersymb : secsymb;
270 if (trect != null)
271 stflag = !stflag;
272 stcount = ratio;
273 } else {
274 symbol = prisymb;
275 }
276 }
277 }
278 }
279 }
280 }
281 }
282 }
283
284 public static void lineVector(LineStyle style) {
285 Path2D.Double p = new Path2D.Double();
286 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
287 Point2D point;
288 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
289 while (git.hasComp()) {
290 git.nextComp();
291 boolean first = true;
292 while (git.hasEdge()) {
293 git.nextEdge();
294 point = context.getPoint(git.next());
295 if (first) {
296 p.moveTo(point.getX(), point.getY());
297 first = false;
298 } else {
299 p.lineTo(point.getX(), point.getY());
300 }
301 while (git.hasNode()) {
302 Snode node = git.next();
303 if (node == null) continue;
304 point = context.getPoint(node);
305 p.lineTo(point.getX(), point.getY());
306 }
307 }
308 }
309 if ((style.fill != null) && (Rules.feature.geom.prim == Pflag.AREA)) {
310 g2.setPaint(style.fill);
311 g2.fill(p);
312 }
313 if (style.line != null) {
314 if (style.dash != null) {
315 float[] dash = new float[style.dash.length];
316 System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
317 for (int i = 0; i < style.dash.length; i++) {
318 dash[i] *= (float) sScale;
319 }
320 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
321 } else {
322 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
323 }
324 g2.setPaint(style.line);
325 g2.draw(p);
326 }
327 }
328
329 public static void lineCircle(LineStyle style, double radius, UniHLU units) {
330 switch (units) {
331 case HLU_FEET:
332 radius /= 6076;
333 break;
334 case HLU_KMTR:
335 radius /= 1.852;
336 break;
337 case HLU_HMTR:
338 radius /= 18.52;
339 break;
340 case HLU_SMIL:
341 radius /= 1.15078;
342 break;
343 case HLU_NMIL:
344 break;
345 default:
346 radius /= 1852;
347 break;
348 }
349 radius *= context.mile(Rules.feature);
350 Symbol circle = new Symbol();
351 if (style.fill != null) {
352 circle.add(new Instr(Form.FILL, style.fill));
353 circle.add(new Instr(Form.RSHP, new Ellipse2D.Double(-radius, -radius, radius*2, radius*2)));
354 }
355 circle.add(new Instr(Form.FILL, style.line));
356 circle.add(new Instr(Form.STRK, new BasicStroke(style.width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, style.dash, 0)));
357 circle.add(new Instr(Form.ELPS, new Ellipse2D.Double(-radius, -radius, radius*2, radius*2)));
358 Point2D point = context.getPoint(Rules.feature.geom.centre);
359 Symbols.drawSymbol(g2, circle, 1, point.getX(), point.getY(), null, null);
360 }
361
362 public static void fillPattern(BufferedImage image) {
363 Path2D.Double p = new Path2D.Double();
364 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
365 Point2D point;
366 switch (Rules.feature.geom.prim) {
367 case POINT:
368 point = context.getPoint(Rules.feature.geom.centre);
369 g2.drawImage(image, new AffineTransformOp(AffineTransform.getScaleInstance(sScale, sScale), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
370 (int) (point.getX() - (50 * sScale)), (int) (point.getY() - (50 * sScale)));
371 break;
372 case AREA:
373 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
374 while (git.hasComp()) {
375 git.nextComp();
376 boolean newComp = true;
377 while (git.hasEdge()) {
378 git.nextEdge();
379 point = context.getPoint(git.next());
380 if (newComp) {
381 p.moveTo(point.getX(), point.getY());
382 newComp = false;
383 } else {
384 p.lineTo(point.getX(), point.getY());
385 }
386 while (git.hasNode()) {
387 Snode node = git.next();
388 if (node == null) continue;
389 point = context.getPoint(node);
390 p.lineTo(point.getX(), point.getY());
391 }
392 }
393 }
394 g2.setPaint(new TexturePaint(image, new Rectangle(0, 0, 1 + (int) (300 * sScale), 1 + (int) (300 * sScale))));
395 g2.fill(p);
396 break;
397 default:
398 break;
399 }
400 }
401
402 public static void labelText(String str, Font font, Color tc) {
403 labelText(str, font, tc, LabelStyle.NONE, null, null, null);
404 }
405
406 public static void labelText(String str, Font font, Color tc, Delta delta) {
407 labelText(str, font, tc, LabelStyle.NONE, null, null, delta);
408 }
409
410 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg) {
411 labelText(str, font, tc, style, fg, null, null);
412 }
413
414 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Color bg) {
415 labelText(str, font, tc, style, fg, bg, null);
416 }
417
418 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Delta delta) {
419 labelText(str, font, tc, style, fg, null, delta);
420 }
421
422 public static void labelText(String str, Font font, Color tc, LabelStyle style, Color fg, Color bg, Delta delta) {
423 if (delta == null) delta = new Delta(Handle.CC);
424 if (bg == null) bg = new Color(0x00000000, true);
425 if ((str == null) || (str.isEmpty())) str = " ";
426 FontRenderContext frc = g2.getFontRenderContext();
427 GlyphVector gv = font.deriveFont((float) (font.getSize())).createGlyphVector(frc, str.equals(" ") ? "M" : str);
428 Rectangle2D bounds = gv.getVisualBounds();
429 double width = bounds.getWidth();
430 double height = bounds.getHeight();
431 Symbol label = new Symbol();
432 double lx, ly, tx, ty;
433 switch (style) {
434 case RRCT:
435 width += height * 1.0;
436 height *= 1.5;
437 if (width < height) width = height;
438 lx = -width / 2;
439 ly = -height / 2;
440 tx = lx + (height * 0.34);
441 ty = ly + (height * 0.17);
442 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
443 label.add(new Instr(Form.FILL, bg));
444 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
445 label.add(new Instr(Form.FILL, fg));
446 label.add(new Instr(Form.STRK, new BasicStroke(1 + (int) (height/10), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
447 label.add(new Instr(Form.RRCT, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
448 break;
449 case VCLR:
450 width += height * 1.0;
451 height *= 2.0;
452 if (width < height) width = height;
453 lx = -width / 2;
454 ly = -height / 2;
455 tx = lx + (height * 0.27);
456 ty = ly + (height * 0.25);
457 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
458 label.add(new Instr(Form.FILL, bg));
459 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
460 label.add(new Instr(Form.FILL, fg));
461 int sw = 1 + (int) (height/10);
462 double po = sw / 2;
463 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
464 Path2D.Double p = new Path2D.Double(); p.moveTo(-height*0.2, -ly-po);
465 p.lineTo(height*0.2, -ly-po); p.moveTo(0, -ly-po); p.lineTo(0, -ly-po-(height*0.15));
466 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));
467 label.add(new Instr(Form.PLIN, p));
468 break;
469 case PCLR:
470 width += height * 1.0;
471 height *= 2.0;
472 if (width < height) width = height;
473 lx = -width / 2;
474 ly = -height / 2;
475 tx = lx + (height * 0.27);
476 ty = ly + (height * 0.25);
477 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
478 label.add(new Instr(Form.FILL, bg));
479 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
480 label.add(new Instr(Form.FILL, fg));
481 sw = 1 + (int) (height/10);
482 po = sw / 2;
483 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
484 p = new Path2D.Double();
485 p.moveTo(-height*0.2, -ly-po);
486 p.lineTo(height*0.2, -ly-po);
487 p.moveTo(0, -ly-po);
488 p.lineTo(0, -ly-po-(height*0.15));
489 p.moveTo(-height*0.2, ly+po);
490 p.lineTo((height*0.2), ly+po);
491 p.moveTo(0, ly+po);
492 p.lineTo(0, ly+po+(height*0.15));
493 label.add(new Instr(Form.PLIN, p));
494 label.add(new Instr(Form.SYMB, new Symbols.SubSymbol(Areas.CableFlash, 1, 0, 0, null,
495 new Delta(Handle.CC, new AffineTransform(0, -1, 1, 0, -width/2, 0)))));
496 label.add(new Instr(Form.SYMB, new Symbols.SubSymbol(Areas.CableFlash, 1, 0, 0, null,
497 new Delta(Handle.CC, new AffineTransform(0, -1, 1, 0, width/2, 0)))));
498 break;
499 case HCLR:
500 width += height * 1.5;
501 height *= 1.5;
502 if (width < height) width = height;
503 lx = -width / 2;
504 ly = -height / 2;
505 tx = lx + (height * 0.5);
506 ty = ly + (height * 0.17);
507 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
508 label.add(new Instr(Form.FILL, bg));
509 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx, ly, width, height, height, height)));
510 label.add(new Instr(Form.FILL, fg));
511 sw = 1 + (int) (height/10);
512 double vo = height / 4;
513 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
514 p = new Path2D.Double();
515 p.moveTo(-width*0.4-sw, -ly-vo);
516 p.lineTo(-width*0.4-sw, ly+vo);
517 p.moveTo(-width*0.4-sw, 0);
518 p.lineTo(-width*0.4+sw, 0);
519 p.moveTo(width*0.4+sw, -ly-vo);
520 p.lineTo(width*0.4+sw, ly+vo);
521 p.moveTo(width*0.4-sw, 0);
522 p.lineTo(width*0.4+sw, 0);
523 label.add(new Instr(Form.PLIN, p));
524 break;
525 default:
526 lx = -width / 2;
527 ly = -height / 2;
528 tx = lx;
529 ty = ly;
530 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx, ly, width, height)));
531 break;
532 }
533 label.add(new Instr(Form.TEXT, new Caption(str, font, tc, new Delta(Handle.TL, AffineTransform.getTranslateInstance(tx, ty)))));
534 Point2D point = context.getPoint(Rules.feature.geom.centre);
535 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, delta);
536 }
537
538 public static void lineText(String str, Font font, Color colour, double dy) {
539 if (!str.isEmpty()) {
540 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
541 g2.setPaint(colour);
542 FontRenderContext frc = g2.getFontRenderContext();
543 GlyphVector gv = font.deriveFont(font.getSize2D() * (float) sScale).createGlyphVector(frc, str);
544 double width = gv.getVisualBounds().getWidth();
545 double height = gv.getVisualBounds().getHeight();
546 double offset = (Rules.feature.geom.length * context.mile(Rules.feature) - width) / 2;
547 if (offset > 0) {
548 Point2D before = null;
549 Point2D after = null;
550 ArrayList<Point2D> between = new ArrayList<>();
551 Point2D prev = null;
552 Point2D next = null;
553 double length = 0;
554 double lb = 0;
555 double la = 0;
556 GeomIterator git = map.new GeomIterator(Rules.feature.geom);
557 if (git.hasComp()) {
558 git.nextComp();
559 while (git.hasEdge()) {
560 git.nextEdge();
561 while (git.hasNode()) {
562 Snode node = git.next();
563 if (node == null)
564 continue;
565 prev = next;
566 next = context.getPoint(node);
567 if (prev != null)
568 length += Math.sqrt(Math.pow((next.getX() - prev.getX()), 2) + Math.pow((next.getY() - prev.getY()), 2));
569 if (length < offset) {
570 before = next;
571 lb = la = length;
572 } else if (after == null) {
573 if (length > (offset + width)) {
574 after = next;
575 la = length;
576 break;
577 } else {
578 between.add(next);
579 }
580 }
581 }
582 if (after != null)
583 break;
584 }
585 }
586 if (after != null) {
587 double angle = Math.atan2((after.getY() - before.getY()), (after.getX() - before.getX()));
588 double rotate = Math.abs(angle) < (Math.PI / 2) ? angle : angle + Math.PI;
589 Point2D mid = new Point2D.Double((before.getX() + after.getX()) / 2, (before.getY() + after.getY()) / 2);
590 Point2D centre = context.getPoint(Rules.feature.geom.centre);
591 AffineTransform pos = AffineTransform.getTranslateInstance(-dy * Math.sin(rotate), dy * Math.cos(rotate));
592 pos.rotate(rotate);
593 pos.translate((mid.getX() - centre.getX()), (mid.getY() - centre.getY()));
594 Symbol label = new Symbol();
595 label.add(new Instr(Form.BBOX, new Rectangle2D.Double((-width / 2), (-height), width, height)));
596 label.add(new Instr(Form.TEXT, new Caption(str, font, colour, new Delta(Handle.BC))));
597 Symbols.drawSymbol(g2, label, sScale, centre.getX(), centre.getY(), null, new Delta(Handle.BC, pos));
598 }
599 }
600 }
601 }
602
603 public static void lightSector(Color col1, Color col2, double radius, double s1, double s2, Double dir, String str) {
604 if ((zoom >= 16) && (radius > 0.2)) {
605 radius /= (Math.pow(2, zoom-15));
606 }
607 double mid = (((s1 + s2) / 2) + (s1 > s2 ? 180 : 0)) % 360;
608 g2.setStroke(new BasicStroke((float) (3.0 * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1,
609 new float[] {20 * (float) sScale, 20 * (float) sScale}, 0));
610 g2.setPaint(Color.black);
611 Point2D.Double centre = (Point2D.Double) context.getPoint(Rules.feature.geom.centre);
612 double radial = radius * context.mile(Rules.feature);
613 if (dir != null) {
614 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(dir)),
615 centre.y + radial * Math.cos(Math.toRadians(dir))));
616 } else {
617 if ((s1 != 0.0) || (s2 != 360.0)) {
618 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(s1)),
619 centre.y + radial * Math.cos(Math.toRadians(s1))));
620 g2.draw(new Line2D.Double(centre.x, centre.y, centre.x - radial * Math.sin(Math.toRadians(s2)),
621 centre.y + radial * Math.cos(Math.toRadians(s2))));
622 }
623 }
624 double arcWidth = 10.0 * sScale;
625 g2.setStroke(new BasicStroke((float) arcWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1));
626 g2.setPaint(col1);
627 g2.draw(new Arc2D.Double(centre.x - radial, centre.y - radial, 2 * radial, 2 * radial, -(s1 + 90),
628 ((s1 < s2) ? (s1 - s2) : (s1 - s2 - 360)), Arc2D.OPEN));
629 if (col2 != null) {
630 g2.setPaint(col2);
631 g2.draw(new Arc2D.Double(centre.x - radial + arcWidth, centre.y - radial + arcWidth, 2 * (radial - arcWidth),
632 2 * (radial - arcWidth), -(s1 + 90), ((s1 < s2) ? (s1 - s2) : (s1 - s2 - 360)), Arc2D.OPEN));
633 }
634 if ((str != null) && (!str.isEmpty())) {
635 Font font = new Font("Arial", Font.PLAIN, 40);
636 double arc = (s2 > s1) ? (s2 - s1) : (s2 - s1 + 360);
637 double awidth = (Math.toRadians(arc) * radial);
638 boolean hand = ((mid > 270) || (mid < 90));
639 double phi = Math.toRadians(mid);
640 radial += 30 * sScale;
641 AffineTransform at = AffineTransform.getTranslateInstance(-radial * Math.sin(phi) / sScale, radial * Math.cos(phi) / sScale);
642 if ((font.getSize() * sScale * str.length()) < awidth) {
643 at.rotate(Math.toRadians(mid + (hand ? 0 : 180)));
644 labelText(str, font, Color.black, new Delta(Handle.CC, at));
645 } else if ((font.getSize() * sScale) < awidth) {
646 hand = (mid < 180);
647 at.rotate(Math.toRadians(mid + (hand ? -90 : 90)));
648 labelText(str, font, Color.black, hand ? new Delta(Handle.RC, at) : new Delta(Handle.LC, at));
649 }
650 if (dir != null) {
651 font = new Font("Arial", Font.PLAIN, 30);
652 str = dir + "°";
653 hand = (dir > 180);
654 phi = Math.toRadians(dir + (hand ? -0.5 : 0.5));
655 radial -= 70 * sScale;
656 at = AffineTransform.getTranslateInstance(-radial * Math.sin(phi) / sScale, radial * Math.cos(phi) / sScale);
657 at.rotate(Math.toRadians(dir + (hand ? 90 : -90)));
658 labelText(str, font, Color.black, hand ? new Delta(Handle.BR, at) : new Delta(Handle.BL, at));
659 }
660 }
661 }
662}
Note: See TracBrowser for help on using the repository browser.