source: osm/applications/editors/josm/plugins/smed2/src/render/Renderer.java@ 30339

Last change on this file since 30339 was 30339, checked in by malcolmh, 12 years ago

save

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