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

Last change on this file since 31846 was 31532, checked in by malcolmh, 9 years ago

update

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