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

Last change on this file since 31044 was 31044, checked in by malcolmh, 10 years ago

[seachart]update & tidy directories

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, RuleSet set, 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(Symbols.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(set);
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 public static void fillPattern(Feature feature, BufferedImage image) {
330 Path2D.Double p = new Path2D.Double();
331 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
332 Point2D point;
333 switch (feature.geom.prim) {
334 case POINT:
335 point = context.getPoint(feature.geom.centre);
336 g2.drawImage(image, new AffineTransformOp(AffineTransform.getScaleInstance(sScale, sScale), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
337 (int)(point.getX() - (50 * sScale)), (int)(point.getY() - (50 * sScale)));
338 break;
339 case AREA:
340 GeomIterator git = map.new GeomIterator(feature.geom);
341 while (git.hasComp()) {
342 git.nextComp();
343 while (git.hasEdge()) {
344 git.nextEdge();
345 point = context.getPoint(git.next());
346 p.moveTo(point.getX(), point.getY());
347 while (git.hasNode()) {
348 Snode node = git.next();
349 if (node == null) continue;
350 point = context.getPoint(node);
351 p.lineTo(point.getX(), point.getY());
352 }
353 }
354 }
355 g2.setPaint(new TexturePaint(image, new Rectangle(0, 0, 1 + (int)(100 * sScale), 1 + (int)(100 * sScale))));
356 g2.fill(p);
357 break;
358 default:
359 break;
360 }
361 }
362
363 public static void labelText(Feature feature, String str, Font font, Color tc) {
364 labelText(feature, str, font, tc, LabelStyle.NONE, null, null, null);
365 }
366 public static void labelText(Feature feature, String str, Font font, Color tc, Delta delta) {
367 labelText(feature, str, font, tc, LabelStyle.NONE, null, null, delta);
368 }
369 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg) {
370 labelText(feature, str, font, tc, style, fg, null, null);
371 }
372 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Color bg) {
373 labelText(feature, str, font, tc, style, fg, bg, null);
374 }
375 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Delta delta) {
376 labelText(feature, str, font, tc, style, fg, null, delta);
377 }
378 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Color bg, Delta delta) {
379 if (delta == null) delta = new Delta(Handle.CC);
380 if (bg == null) bg = new Color(0x00000000, true);
381 if ((str == null) || (str.isEmpty())) str = " ";
382 FontRenderContext frc = g2.getFontRenderContext();
383 GlyphVector gv = font.deriveFont((float)(font.getSize())).createGlyphVector(frc, str.equals(" ") ? "!" : str);
384 Rectangle2D bounds = gv.getVisualBounds();
385 double width = bounds.getWidth();
386 double height = bounds.getHeight();
387 Symbol label = new Symbol();
388 double lx, ly, tx, ty;
389 switch (style) {
390 case RRCT:
391 width += height * 1.0;
392 height *= 1.5;
393 if (width < height) width = height;
394 lx = -width / 2;
395 ly = -height / 2;
396 tx = lx + (height * 0.34);
397 ty = ly + (height * 0.17);
398 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
399 label.add(new Instr(Form.FILL, bg));
400 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
401 label.add(new Instr(Form.FILL, fg));
402 label.add(new Instr(Form.STRK, new BasicStroke(1 + (int)(height/10), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
403 label.add(new Instr(Form.RRCT, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
404 break;
405 case VCLR:
406 width += height * 1.0;
407 height *= 2.0;
408 if (width < height) width = height;
409 lx = -width / 2;
410 ly = -height / 2;
411 tx = lx + (height * 0.27);
412 ty = ly + (height * 0.25);
413 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
414 label.add(new Instr(Form.FILL, bg));
415 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
416 label.add(new Instr(Form.FILL, fg));
417 int sw = 1 + (int)(height/10);
418 double po = sw / 2;
419 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
420 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));
421 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 label.add(new Instr(Form.PLIN, p));
423 break;
424 case PCLR:
425 width += height * 1.0;
426 height *= 2.0;
427 if (width < height) width = height;
428 lx = -width / 2;
429 ly = -height / 2;
430 tx = lx + (height * 0.27);
431 ty = ly + (height * 0.25);
432 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
433 label.add(new Instr(Form.FILL, bg));
434 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
435 label.add(new Instr(Form.FILL, fg));
436 sw = 1 + (int)(height/10);
437 po = sw / 2;
438 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
439 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));
440 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 label.add(new Instr(Form.PLIN, p));
442 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)))));
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 break;
445 case HCLR:
446 width += height * 1.5;
447 height *= 1.5;
448 if (width < height) width = height;
449 lx = -width / 2;
450 ly = -height / 2;
451 tx = lx + (height * 0.5);
452 ty = ly + (height * 0.17);
453 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
454 label.add(new Instr(Form.FILL, bg));
455 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
456 label.add(new Instr(Form.FILL, fg));
457 sw = 1 + (int)(height/10);
458 double vo = height / 4;
459 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
460 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);
461 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 label.add(new Instr(Form.PLIN, p));
463 break;
464 default:
465 lx = -width / 2;
466 ly = -height / 2;
467 tx = lx;
468 ty = ly;
469 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
470 break;
471 }
472 label.add(new Instr(Form.TEXT, new Caption(str, font, tc, new Delta(Handle.TL, AffineTransform.getTranslateInstance(tx, ty)))));
473 Point2D point = context.getPoint(feature.geom.centre);
474 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, delta);
475 }
476
477 public static void lineText(Feature feature, String str, Font font, Color colour, double offset, double dy) {
478 if (!str.isEmpty()) {
479 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
480 g2.setPaint(colour);
481 FontRenderContext frc = g2.getFontRenderContext();
482 GlyphVector gv = font.deriveFont((float)(font.getSize()*sScale)).createGlyphVector(frc, (" " + str));
483 GeneralPath path = new GeneralPath();
484 Point2D prev = new Point2D.Double();
485 Point2D next = new Point2D.Double();
486 Point2D curr = new Point2D.Double();
487 Point2D succ = new Point2D.Double();
488 boolean piv = false;
489 double angle = 0;
490 int index = 0;
491 double gwidth = offset * (feature.geom.length * context.mile(feature) - gv.getLogicalBounds().getWidth()) + gv.getGlyphMetrics(0).getAdvance();
492 GeomIterator git = map.new GeomIterator(feature.geom);
493 while (git.hasComp()) {
494 git.nextComp();
495 boolean first = true;
496 while (git.hasEdge()) {
497 git.nextEdge();
498 while (git.hasNode()) {
499 Snode node = git.next();
500 if (node == null) continue;
501 prev = next;
502 next = context.getPoint(node);
503 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
504 piv = true;
505 if (first) {
506 curr = succ = next;
507 first = false;
508 } else {
509 while (curr.distance(next) >= gwidth) {
510 if (piv) {
511 double rem = gwidth;
512 double s = prev.distance(next);
513 double p = curr.distance(prev);
514 if ((s > 0) && (p > 0)) {
515 double n = curr.distance(next);
516 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
517 double phi = Math.asin(p / gwidth * Math.sin(theta));
518 rem = gwidth * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
519 }
520 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
521 piv = false;
522 } else {
523 succ = new Point2D.Double(curr.getX() + (gwidth * Math.cos(angle)), curr.getY() + (gwidth * Math.sin(angle)));
524 }
525 Shape shape = gv.getGlyphOutline(index);
526 Point2D point = gv.getGlyphPosition(index);
527 AffineTransform at = AffineTransform.getTranslateInstance(curr.getX(), curr.getY());
528 at.rotate(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())));
529 at.translate(-point.getX(), -point.getY() + (dy * sScale));
530 path.append(at.createTransformedShape(shape), false);
531 curr = succ;
532 if (++index < gv.getNumGlyphs()) {
533 gwidth = gv.getGlyphMetrics(index).getAdvance();
534 } else {
535 g2.fill(path);
536 return;
537 }
538 }
539 }
540 }
541 }
542 }
543 }
544 }
545
546 public static void lightSector(Feature feature, Color col1, Color col2, double radius, double s1, double s2, boolean dir, String str) {
547 if ((zoom >= 16) && (radius > 0.2)) {
548 radius = 0.2 / (Math.pow(2, zoom-16));
549 }
550 double mid = (((s1 + s2) / 2) + (s1 > s2 ? 180 : 0)) % 360;
551 g2.setStroke(new BasicStroke((float) (3.0 * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, new float[] {20 * (float)sScale, 20 * (float)sScale}, 0));
552 g2.setPaint(Color.black);
553 Point2D.Double centre = (Point2D.Double) context.getPoint(feature.geom.centre);
554 double radial = radius * context.mile(feature);
555 if (dir) {
556 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))));
557 } else {
558 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))));
559 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))));
560 }
561 double arcWidth = 10.0 * sScale;
562 g2.setStroke(new BasicStroke((float)arcWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1));
563 g2.setPaint(col1);
564 g2.draw(new Arc2D.Double(centre.x - radial, centre.y - radial, 2 * radial, 2 * radial, -(s1 + 90), (s1 - s2 - 360) % 360, Arc2D.OPEN));
565 if (col2 != null) {
566 g2.setPaint(col2);
567 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));
568 }
569 if ((str != null) && (!str.isEmpty())) {
570 g2.setPaint(Color.black);
571 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
572 FontRenderContext frc = g2.getFontRenderContext();
573 Font font = new Font("Arial", Font.PLAIN, 40);
574 GeneralPath path = new GeneralPath();
575 GlyphVector gv = font.deriveFont((float)(font.getSize()*sScale)).createGlyphVector(frc, (" " + str));
576 double gwidth = gv.getLogicalBounds().getWidth();
577 boolean hand = false;
578 double offset = 0;
579 Point2D origin;
580 double arc = (s2 - s1 + 360) % 360;
581 if (dir) {
582 radial += 10 * sScale;
583 if (mid < 180) {
584 radial += gwidth;
585 hand = true;
586 }
587 origin = new Point2D.Double(centre.x - radial * Math.sin(Math.toRadians(mid)), centre.y + radial * Math.cos(Math.toRadians(mid)));
588 int length = gv.getNumGlyphs();
589 for (int i = 0; i < length; i++) {
590 Shape shape = gv.getGlyphOutline(i);
591 Point2D point = gv.getGlyphPosition(i);
592 AffineTransform at = AffineTransform.getTranslateInstance(origin.getX(), origin.getY());
593 at.rotate(Math.toRadians(mid + (hand ? -90 : 90)));
594 at.translate(-point.getX() + offset, -point.getY() + (15 * sScale));
595 path.append(at.createTransformedShape(shape), false);
596 offset += gv.getGlyphMetrics(i).getAdvance();
597 g2.fill(path);
598 }
599 } else {
600 double awidth = (Math.toRadians(arc) * radial);
601 if (gwidth < awidth) {
602 offset = 0;
603 double phi = 0;
604 if ((mid > 270) || (mid < 90)) {
605 hand = true;
606 phi = Math.toRadians(s2) - ((awidth - gwidth) / 2) /radial;
607 radial -= 20 * sScale;
608 } else {
609 phi = Math.toRadians(s1) + (((awidth - gwidth) / 2)) /radial;
610 radial += 20 * sScale;
611 }
612 origin = new Point2D.Double(centre.x - radial * Math.sin(phi), centre.y + radial * Math.cos(phi));
613 int length = gv.getNumGlyphs();
614 for (int i = 0; i < length; i++) {
615 Shape shape = gv.getGlyphOutline(i);
616 Point2D point = gv.getGlyphPosition(i);
617 AffineTransform at = AffineTransform.getTranslateInstance(origin.getX(), origin.getY());
618 at.rotate(phi + (hand ? 0 : Math.toRadians(180)));
619 at.translate(-point.getX() + offset, -point.getY());
620 path.append(at.createTransformedShape(shape), false);
621 double advance = gv.getGlyphMetrics(i).getAdvance();
622 offset += advance;
623 phi += (hand ? -0.5 : +0.5) * advance / radial;
624 g2.fill(path);
625 }
626 }
627 }
628 }
629 }
630}
Note: See TracBrowser for help on using the repository browser.