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

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

update jicons

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