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

Last change on this file since 31031 was 31031, checked in by malcolmh, 11 years ago

cope with missing nodes

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