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

Last change on this file since 35685 was 35685, checked in by malcolmh, 4 years ago

null exception fix + misc updates

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