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

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

clip to map bounds

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