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

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

add base map rendering

File size: 26.4 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 prev = next;
198 next = context.getPoint(git.next());
199 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
200 piv = true;
201 if (first) {
202 curr = succ = next;
203 gap = (space > 0);
204 stcount = ratio - 1;
205 symbol = prisymb;
206 len = gap ? psize * space * 0.5 : psize;
207 first = false;
208 } else {
209 while (curr.distance(next) >= len) {
210 if (piv) {
211 double rem = len;
212 double s = prev.distance(next);
213 double p = curr.distance(prev);
214 if ((s > 0) && (p > 0)) {
215 double n = curr.distance(next);
216 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
217 double phi = Math.asin(p / len * Math.sin(theta));
218 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
219 }
220 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
221 piv = false;
222 } else {
223 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
224 }
225 if (!gap) {
226 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Scheme(col),
227 new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))));
228 }
229 if (space > 0)
230 gap = !gap;
231 curr = succ;
232 len = gap ? (psize * space) : (--stcount == 0) ? (stflag ? tsize : ssize) : psize;
233 if (stcount == 0) {
234 symbol = stflag ? tersymb : secsymb;
235 if (trect != null)
236 stflag = !stflag;
237 stcount = ratio;
238 } else {
239 symbol = prisymb;
240 }
241 }
242 }
243 }
244 }
245 }
246 }
247 }
248
249 public static void lineVector(Feature feature, LineStyle style) {
250 Path2D.Double p = new Path2D.Double();
251 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
252 Point2D point;
253 GeomIterator git = map.new GeomIterator(feature.geom);
254 while (git.hasComp()) {
255 git.nextComp();
256 boolean first = true;
257 while (git.hasEdge()) {
258 git.nextEdge();
259 point = context.getPoint(git.next());
260 if (first) {
261 p.moveTo(point.getX(), point.getY());
262 first = false;
263 } else {
264 p.lineTo(point.getX(), point.getY());
265 }
266 while (git.hasNode()) {
267 point = context.getPoint(git.next());
268 p.lineTo(point.getX(), point.getY());
269 }
270 }
271 }
272 if ((style.fill != null) && (feature.geom.prim == Pflag.AREA)) {
273 g2.setPaint(style.fill);
274 g2.fill(p);
275 }
276 if (style.line != null) {
277 if (style.dash != null) {
278 float[] dash = new float[style.dash.length];
279 System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
280 for (int i = 0; i < style.dash.length; i++) {
281 dash[i] *= (float) sScale;
282 }
283 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
284 } else {
285 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
286 }
287 g2.setPaint(style.line);
288 g2.draw(p);
289 }
290 }
291
292 public static void lineCircle(Feature feature, LineStyle style, double radius, UniHLU units) {
293 switch (units) {
294 case HLU_FEET:
295 radius /= 6076;
296 break;
297 case HLU_KMTR:
298 radius /= 1.852;
299 break;
300 case HLU_HMTR:
301 radius /= 18.52;
302 break;
303 case HLU_SMIL:
304 radius /= 1.15078;
305 break;
306 case HLU_NMIL:
307 break;
308 default:
309 radius /= 1852;
310 break;
311 }
312 radius *= context.mile(feature);
313 Symbol circle = new Symbol();
314 if (style.fill != null) {
315 circle.add(new Instr(Form.FILL, style.fill));
316 circle.add(new Instr(Form.RSHP, new Ellipse2D.Double(-radius,-radius,radius*2,radius*2)));
317 }
318 circle.add(new Instr(Form.FILL, style.line));
319 circle.add(new Instr(Form.STRK, new BasicStroke(style.width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, style.dash, 0)));
320 circle.add(new Instr(Form.ELPS, new Ellipse2D.Double(-radius,-radius,radius*2,radius*2)));
321 Point2D point = context.getPoint(feature.geom.centre);
322 Symbols.drawSymbol(g2, circle, 1, point.getX(), point.getY(), null, null);
323 }
324
325
326 public static void fillPattern(Feature feature, BufferedImage image) {
327 Path2D.Double p = new Path2D.Double();
328 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
329 Point2D point;
330 switch (feature.geom.prim) {
331 case POINT:
332 point = context.getPoint(feature.geom.centre);
333 g2.drawImage(image, new AffineTransformOp(AffineTransform.getScaleInstance(sScale, sScale), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
334 (int)(point.getX() - (50 * sScale)), (int)(point.getY() - (50 * sScale)));
335 break;
336 case AREA:
337 GeomIterator git = map.new GeomIterator(feature.geom);
338 while (git.hasComp()) {
339 git.nextComp();
340 while (git.hasEdge()) {
341 git.nextEdge();
342 point = context.getPoint(git.next());
343 p.moveTo(point.getX(), point.getY());
344 while (git.hasNode()) {
345 point = context.getPoint(git.next());
346 p.lineTo(point.getX(), point.getY());
347 }
348 }
349 }
350 g2.setPaint(new TexturePaint(image, new Rectangle(0, 0, 1 + (int)(100 * sScale), 1 + (int)(100 * sScale))));
351 g2.fill(p);
352 break;
353 default:
354 break;
355 }
356 }
357
358 public static void labelText(Feature feature, String str, Font font, Color tc) {
359 labelText(feature, str, font, tc, LabelStyle.NONE, null, null, null);
360 }
361 public static void labelText(Feature feature, String str, Font font, Color tc, Delta delta) {
362 labelText(feature, str, font, tc, LabelStyle.NONE, null, null, delta);
363 }
364 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg) {
365 labelText(feature, str, font, tc, style, fg, null, null);
366 }
367 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Color bg) {
368 labelText(feature, str, font, tc, style, fg, bg, null);
369 }
370 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Delta delta) {
371 labelText(feature, str, font, tc, style, fg, null, delta);
372 }
373 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Color bg, Delta delta) {
374 if (delta == null) delta = new Delta(Handle.CC);
375 if (bg == null) bg = new Color(0x00000000, true);
376 if ((str == null) || (str.isEmpty())) str = " ";
377 FontRenderContext frc = g2.getFontRenderContext();
378 GlyphVector gv = font.deriveFont((float)(font.getSize())).createGlyphVector(frc, str.equals(" ") ? "!" : str);
379 Rectangle2D bounds = gv.getVisualBounds();
380 double width = bounds.getWidth();
381 double height = bounds.getHeight();
382 Symbol label = new Symbol();
383 double lx, ly, tx, ty;
384 switch (style) {
385 case RRCT:
386 width += height * 1.0;
387 height *= 1.5;
388 if (width < height) width = height;
389 lx = -width / 2;
390 ly = -height / 2;
391 tx = lx + (height * 0.34);
392 ty = ly + (height * 0.17);
393 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
394 label.add(new Instr(Form.FILL, bg));
395 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
396 label.add(new Instr(Form.FILL, fg));
397 label.add(new Instr(Form.STRK, new BasicStroke(1 + (int)(height/10), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
398 label.add(new Instr(Form.RRCT, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
399 break;
400 case VCLR:
401 width += height * 1.0;
402 height *= 2.0;
403 if (width < height) width = height;
404 lx = -width / 2;
405 ly = -height / 2;
406 tx = lx + (height * 0.27);
407 ty = ly + (height * 0.25);
408 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
409 label.add(new Instr(Form.FILL, bg));
410 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
411 label.add(new Instr(Form.FILL, fg));
412 int sw = 1 + (int)(height/10);
413 double po = sw / 2;
414 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
415 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));
416 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));
417 label.add(new Instr(Form.PLIN, p));
418 break;
419 case PCLR:
420 width += height * 1.0;
421 height *= 2.0;
422 if (width < height) width = height;
423 lx = -width / 2;
424 ly = -height / 2;
425 tx = lx + (height * 0.27);
426 ty = ly + (height * 0.25);
427 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
428 label.add(new Instr(Form.FILL, bg));
429 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
430 label.add(new Instr(Form.FILL, fg));
431 sw = 1 + (int)(height/10);
432 po = sw / 2;
433 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
434 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));
435 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));
436 label.add(new Instr(Form.PLIN, p));
437 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)))));
438 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)))));
439 break;
440 case HCLR:
441 width += height * 1.5;
442 height *= 1.5;
443 if (width < height) width = height;
444 lx = -width / 2;
445 ly = -height / 2;
446 tx = lx + (height * 0.5);
447 ty = ly + (height * 0.17);
448 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
449 label.add(new Instr(Form.FILL, bg));
450 label.add(new Instr(Form.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
451 label.add(new Instr(Form.FILL, fg));
452 sw = 1 + (int)(height/10);
453 double vo = height / 4;
454 label.add(new Instr(Form.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
455 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);
456 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);
457 label.add(new Instr(Form.PLIN, p));
458 break;
459 default:
460 lx = -width / 2;
461 ly = -height / 2;
462 tx = lx;
463 ty = ly;
464 label.add(new Instr(Form.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
465 break;
466 }
467 label.add(new Instr(Form.TEXT, new Caption(str, font, tc, new Delta(Handle.TL, AffineTransform.getTranslateInstance(tx, ty)))));
468 Point2D point = context.getPoint(feature.geom.centre);
469 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, delta);
470 }
471
472 public static void lineText(Feature feature, String str, Font font, Color colour, double offset, double dy) {
473 if (!str.isEmpty()) {
474 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
475 g2.setPaint(colour);
476 FontRenderContext frc = g2.getFontRenderContext();
477 GlyphVector gv = font.deriveFont((float)(font.getSize()*sScale)).createGlyphVector(frc, (" " + str));
478 GeneralPath path = new GeneralPath();
479 Point2D prev = new Point2D.Double();
480 Point2D next = new Point2D.Double();
481 Point2D curr = new Point2D.Double();
482 Point2D succ = new Point2D.Double();
483 boolean piv = false;
484 double angle = 0;
485 int index = 0;
486 double gwidth = offset * (feature.geom.length * context.mile(feature) - gv.getLogicalBounds().getWidth()) + gv.getGlyphMetrics(0).getAdvance();
487 GeomIterator git = map.new GeomIterator(feature.geom);
488 while (git.hasComp()) {
489 git.nextComp();
490 boolean first = true;
491 while (git.hasEdge()) {
492 git.nextEdge();
493 while (git.hasNode()) {
494 prev = next;
495 next = context.getPoint(git.next());
496 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
497 piv = true;
498 if (first) {
499 curr = succ = next;
500 first = false;
501 } else {
502 while (curr.distance(next) >= gwidth) {
503 if (piv) {
504 double rem = gwidth;
505 double s = prev.distance(next);
506 double p = curr.distance(prev);
507 if ((s > 0) && (p > 0)) {
508 double n = curr.distance(next);
509 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
510 double phi = Math.asin(p / gwidth * Math.sin(theta));
511 rem = gwidth * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
512 }
513 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
514 piv = false;
515 } else {
516 succ = new Point2D.Double(curr.getX() + (gwidth * Math.cos(angle)), curr.getY() + (gwidth * Math.sin(angle)));
517 }
518 Shape shape = gv.getGlyphOutline(index);
519 Point2D point = gv.getGlyphPosition(index);
520 AffineTransform at = AffineTransform.getTranslateInstance(curr.getX(), curr.getY());
521 at.rotate(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())));
522 at.translate(-point.getX(), -point.getY() + (dy * sScale));
523 path.append(at.createTransformedShape(shape), false);
524 curr = succ;
525 if (++index < gv.getNumGlyphs()) {
526 gwidth = gv.getGlyphMetrics(index).getAdvance();
527 } else {
528 g2.fill(path);
529 return;
530 }
531 }
532 }
533 }
534 }
535 }
536 }
537 }
538
539 public static void lightSector(Feature feature, Color col1, Color col2, double radius, double s1, double s2, boolean dir, String str) {
540 if ((zoom >= 16) && (radius > 0.2)) {
541 radius = 0.2 / (Math.pow(2, zoom-16));
542 }
543 double mid = (((s1 + s2) / 2) + (s1 > s2 ? 180 : 0)) % 360;
544 g2.setStroke(new BasicStroke((float) (3.0 * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, new float[] {20 * (float)sScale, 20 * (float)sScale}, 0));
545 g2.setPaint(Color.black);
546 Point2D.Double centre = (Point2D.Double) context.getPoint(feature.geom.centre);
547 double radial = radius * context.mile(feature);
548 if (dir) {
549 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))));
550 } else {
551 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))));
552 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))));
553 }
554 double arcWidth = 10.0 * sScale;
555 g2.setStroke(new BasicStroke((float)arcWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1));
556 g2.setPaint(col1);
557 g2.draw(new Arc2D.Double(centre.x - radial, centre.y - radial, 2 * radial, 2 * radial, -(s1 + 90), (s1 - s2 - 360) % 360, Arc2D.OPEN));
558 if (col2 != null) {
559 g2.setPaint(col2);
560 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));
561 }
562 if ((str != null) && (!str.isEmpty())) {
563 g2.setPaint(Color.black);
564 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
565 FontRenderContext frc = g2.getFontRenderContext();
566 Font font = new Font("Arial", Font.PLAIN, 40);
567 GeneralPath path = new GeneralPath();
568 GlyphVector gv = font.deriveFont((float)(font.getSize()*sScale)).createGlyphVector(frc, (" " + str));
569 double gwidth = gv.getLogicalBounds().getWidth();
570 boolean hand = false;
571 double offset = 0;
572 Point2D origin;
573 double arc = (s2 - s1 + 360) % 360;
574 if (dir) {
575 radial += 10 * sScale;
576 if (mid < 180) {
577 radial += gwidth;
578 hand = true;
579 }
580 origin = new Point2D.Double(centre.x - radial * Math.sin(Math.toRadians(mid)), centre.y + radial * Math.cos(Math.toRadians(mid)));
581 int length = gv.getNumGlyphs();
582 for (int i = 0; i < length; i++) {
583 Shape shape = gv.getGlyphOutline(i);
584 Point2D point = gv.getGlyphPosition(i);
585 AffineTransform at = AffineTransform.getTranslateInstance(origin.getX(), origin.getY());
586 at.rotate(Math.toRadians(mid + (hand ? -90 : 90)));
587 at.translate(-point.getX() + offset, -point.getY() + (15 * sScale));
588 path.append(at.createTransformedShape(shape), false);
589 offset += gv.getGlyphMetrics(i).getAdvance();
590 g2.fill(path);
591 }
592 } else {
593 double awidth = (Math.toRadians(arc) * radial);
594 if (gwidth < awidth) {
595 offset = 0;
596 double phi = 0;
597 if ((mid > 270) || (mid < 90)) {
598 hand = true;
599 phi = Math.toRadians(s2) - ((awidth - gwidth) / 2) /radial;
600 radial -= 20 * sScale;
601 } else {
602 phi = Math.toRadians(s1) + (((awidth - gwidth) / 2)) /radial;
603 radial += 20 * sScale;
604 }
605 origin = new Point2D.Double(centre.x - radial * Math.sin(phi), centre.y + radial * Math.cos(phi));
606 int length = gv.getNumGlyphs();
607 for (int i = 0; i < length; i++) {
608 Shape shape = gv.getGlyphOutline(i);
609 Point2D point = gv.getGlyphPosition(i);
610 AffineTransform at = AffineTransform.getTranslateInstance(origin.getX(), origin.getY());
611 at.rotate(phi + (hand ? 0 : Math.toRadians(180)));
612 at.translate(-point.getX() + offset, -point.getY());
613 path.append(at.createTransformedShape(shape), false);
614 double advance = gv.getGlyphMetrics(i).getAdvance();
615 offset += advance;
616 phi += (hand ? -0.5 : +0.5) * advance / radial;
617 g2.fill(path);
618 }
619 }
620 }
621 }
622 }
623}
Note: See TracBrowser for help on using the repository browser.