source: osm/applications/editors/josm/plugins/smed2/src/seamap/Renderer.java@ 30036

Last change on this file since 30036 was 30036, checked in by malcolmh, 12 years ago

save

File size: 19.3 KB
Line 
1/* Copyright 2013 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 seamap;
11
12import java.awt.*;
13import java.awt.font.*;
14import java.awt.geom.*;
15import java.awt.image.*;
16import java.util.*;
17
18import s57.S57att.*;
19import s57.S57obj.*;
20import s57.S57val.*;
21import s57.S57val;
22import seamap.SeaMap;
23import seamap.SeaMap.*;
24import seamap.SeaMap.Area;
25import symbols.Symbols;
26import symbols.Symbols.*;
27
28public class Renderer {
29
30 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, 0.138 };
31
32 public enum LabelStyle { NONE, RRCT, RECT, ELPS, CIRC, VCLR, HCLR }
33
34 static MapContext context;
35 static SeaMap map;
36 static double sScale;
37 static Graphics2D g2;
38 static int zoom;
39
40 public static void reRender(Graphics2D g, int z, double factor, SeaMap m, MapContext c) {
41 g2 = g;
42 zoom = z;
43 context = c;
44 map = m;
45 sScale = symbolScale[zoom] * factor;
46 if (map != null) {
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(map, zoom);
51 }
52 }
53
54 public static void symbol(Feature feature, Symbol symbol) {
55 Point2D point = context.getPoint(feature.centre);
56 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, null);
57 }
58
59 public static void symbol(Feature feature, Symbol symbol, Scheme scheme) {
60 Point2D point = context.getPoint(feature.centre);
61 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, null);
62 }
63
64 public static void symbol(Feature feature, Symbol symbol, Delta delta) {
65 Point2D point = context.getPoint(feature.centre);
66 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, delta);
67 }
68
69 public static void symbol(Feature feature, Symbol symbol, Scheme scheme, Delta delta) {
70 Point2D point = context.getPoint(feature.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 == Prim.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 == Prim.BBOX) {
157 return (Rectangle2D.Double) item.params;
158 }
159 if (item.type == Prim.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 Area area;
172 switch (feature.flag) {
173 case LINE:
174 Edge edge = map.edges.get(feature.refs);
175 area = map.new Area();
176 area.add(map.new Bound(map.new Side(edge, true), true));
177 break;
178 case AREA:
179 area = map.areas.get(feature.refs);
180 break;
181 default:
182 return;
183 }
184 Rectangle2D.Double prect = symbolSize(prisymb);
185 Rectangle2D.Double srect = symbolSize(secsymb);
186 Rectangle2D.Double trect = symbolSize(tersymb);
187 if (srect == null)
188 ratio = 0;
189 if (prect != null) {
190 double psize = Math.abs(prect.getY()) * sScale;
191 double ssize = (srect != null) ? Math.abs(srect.getY()) * sScale : 0;
192 double tsize = (trect != null) ? Math.abs(srect.getY()) * sScale : 0;
193 Point2D prev = new Point2D.Double();
194 Point2D next = new Point2D.Double();
195 Point2D curr = new Point2D.Double();
196 Point2D succ = new Point2D.Double();
197 boolean gap = true;
198 boolean piv = false;
199 double len = 0;
200 double angle = 0;
201 int stcount = ratio;
202 boolean stflag = false;
203 Symbol symbol = prisymb;
204 for (Bound bound : area) {
205 BoundIterator bit = map.new BoundIterator(bound);
206 boolean first = true;
207 while (bit.hasNext()) {
208 prev = next;
209 next = context.getPoint(bit.next());
210 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
211 piv = true;
212 if (first) {
213 curr = succ = next;
214 gap = (space > 0);
215 stcount = ratio - 1;
216 symbol = prisymb;
217 len = gap ? psize * space * 0.5 : psize;
218 first = false;
219 } else {
220 while (curr.distance(next) >= len) {
221 if (piv) {
222 double rem = len;
223 double s = prev.distance(next);
224 double p = curr.distance(prev);
225 if ((s > 0) && (p > 0)) {
226 double n = curr.distance(next);
227 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
228 double phi = Math.asin(p / len * Math.sin(theta));
229 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
230 }
231 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
232 piv = false;
233 } else {
234 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
235 }
236 if (!gap) {
237 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Scheme(col),
238 new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))));
239 }
240 if (space > 0)
241 gap = !gap;
242 curr = succ;
243 len = gap ? (psize * space) : (--stcount == 0) ? (stflag ? tsize : ssize) : psize;
244 if (stcount == 0) {
245 symbol = stflag ? tersymb : secsymb;
246 if (trect != null) stflag = !stflag;
247 stcount = ratio;
248 } else {
249 symbol = prisymb;
250 }
251 }
252 }
253 }
254 }
255 }
256 }
257
258 public static void lineVector(Feature feature, LineStyle style) {
259 Path2D.Double p = new Path2D.Double();
260 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
261 Point2D point;
262 switch (feature.flag) {
263 case LINE:
264 EdgeIterator eit = map.new EdgeIterator(map.edges.get(feature.refs), true);
265 point = context.getPoint(eit.next());
266 p.moveTo(point.getX(), point.getY());
267 while (eit.hasNext()) {
268 point = context.getPoint(eit.next());
269 p.lineTo(point.getX(), point.getY());
270 }
271 break;
272 case AREA:
273 for (Bound bound : map.areas.get(feature.refs)) {
274 BoundIterator bit = map.new BoundIterator(bound);
275 point = context.getPoint(bit.next());
276 p.moveTo(point.getX(), point.getY());
277 while (bit.hasNext()) {
278 point = context.getPoint(bit.next());
279 p.lineTo(point.getX(), point.getY());
280 }
281 }
282 break;
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_BUTT, BasicStroke.JOIN_ROUND));
294 }
295 g2.setPaint(style.line);
296 g2.draw(p);
297 }
298 if (style.fill != null) {
299 g2.setPaint(style.fill);
300 g2.fill(p);
301 }
302 }
303
304 public static void lineCircle(Feature feature, LineStyle style, double radius, UniHLU units) {
305 switch (units) {
306 case HLU_FEET:
307 radius /= 6076;
308 break;
309 case HLU_KMTR:
310 radius /= 1.852;
311 break;
312 case HLU_HMTR:
313 radius /= 18.52;
314 break;
315 case HLU_SMIL:
316 radius /= 1.15078;
317 break;
318 case HLU_NMIL:
319 break;
320 default:
321 radius /= 1852;
322 break;
323 }
324 radius *= context.mile(feature);
325 Symbol circle = new Symbol();
326 if (style.fill != null) {
327 circle.add(new Instr(Prim.FILL, style.fill));
328 circle.add(new Instr(Prim.RSHP, new Ellipse2D.Double(-radius,-radius,radius*2,radius*2)));
329 }
330 circle.add(new Instr(Prim.FILL, style.line));
331 circle.add(new Instr(Prim.STRK, new BasicStroke(style.width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, style.dash, 0)));
332 circle.add(new Instr(Prim.ELPS, new Ellipse2D.Double(-radius,-radius,radius*2,radius*2)));
333 Point2D point = context.getPoint(feature.centre);
334 Symbols.drawSymbol(g2, circle, 1, point.getX(), point.getY(), null, null);
335 }
336
337
338 public static void fillPattern(Feature feature, BufferedImage image) {
339 Path2D.Double p = new Path2D.Double();
340 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
341 Point2D point;
342 switch (feature.flag) {
343 case POINT:
344 point = context.getPoint(feature.centre);
345 g2.drawImage(image, new AffineTransformOp(AffineTransform.getScaleInstance(sScale, sScale), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
346 (int)(point.getX() - (50 * sScale)), (int)(point.getY() - (50 * sScale)));
347 break;
348 case AREA:
349 for (Bound bound : map.areas.get(feature.refs)) {
350 BoundIterator bit = map.new BoundIterator(bound);
351 point = context.getPoint(bit.next());
352 p.moveTo(point.getX(), point.getY());
353 while (bit.hasNext()) {
354 point = context.getPoint(bit.next());
355 p.lineTo(point.getX(), point.getY());
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 }
362 }
363
364 public static void labelText(Feature feature, String str, Font font, LabelStyle style, Color fg, Color bg, Delta delta) {
365 if (delta == null) delta = new Delta(Handle.CC);
366 if (bg == null) bg = new Color(0x00000000, true);
367 if ((str == null) || (str.isEmpty())) str = " ";
368 FontRenderContext frc = g2.getFontRenderContext();
369 GlyphVector gv = font.deriveFont((float)(font.getSize())).createGlyphVector(frc, str.equals(" ") ? "!" : str);
370 Rectangle2D bounds = gv.getVisualBounds();
371 double width = bounds.getWidth();
372 double height = bounds.getHeight();
373 Symbol label = new Symbol();
374 double lx, ly, tx, ty;
375 switch (style) {
376 case RRCT:
377 width += height * 1.0;
378 height *= 1.5;
379 if (width < height) width = height;
380 lx = -width / 2;
381 ly = -height / 2;
382 tx = lx + (height * 0.34);
383 ty = ly + (height * 0.17);
384 label.add(new Instr(Prim.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
385 label.add(new Instr(Prim.FILL, bg));
386 label.add(new Instr(Prim.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
387 label.add(new Instr(Prim.FILL, fg));
388 label.add(new Instr(Prim.STRK, new BasicStroke(1 + (int)(height/10), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
389 label.add(new Instr(Prim.RRCT, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
390 break;
391 case VCLR:
392 width += height * 1.0;
393 height *= 2.0;
394 if (width < height) width = height;
395 lx = -width / 2;
396 ly = -height / 2;
397 tx = lx + (height * 0.27);
398 ty = ly + (height * 0.25);
399 label.add(new Instr(Prim.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
400 label.add(new Instr(Prim.FILL, bg));
401 label.add(new Instr(Prim.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
402 label.add(new Instr(Prim.FILL, fg));
403 int sw = 1 + (int)(height/10);
404 double po = sw / 2;
405 label.add(new Instr(Prim.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
406 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));
407 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));
408 label.add(new Instr(Prim.PLIN, p));
409 break;
410 default:
411 lx = -width / 2;
412 ly = -height / 2;
413 tx = lx;
414 ty = ly;
415 label.add(new Instr(Prim.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
416 break;
417 }
418 label.add(new Instr(Prim.TEXT, new Caption(str, font, fg, new Delta(Handle.TL, AffineTransform.getTranslateInstance(tx, ty)))));
419 Point2D point = context.getPoint(feature.centre);
420 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, delta);
421 }
422
423 public static void lineText(Feature feature, String str, Font font, Color colour, double offset, double dy) {
424 Area area;
425 switch (feature.flag) {
426 case LINE:
427 Edge edge = map.edges.get(feature.refs);
428 area = map.new Area();
429 area.add(map.new Bound(map.new Side(edge, true), true));
430 break;
431 case AREA:
432 area = map.areas.get(feature.refs);
433 break;
434 default:
435 return;
436 }
437// Rectangle prect = symbolSize(prisymb);
438 if (!str.isEmpty()) {
439 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
440 FontRenderContext frc = g2.getFontRenderContext();
441 GlyphVector gv = font.deriveFont((float)(font.getSize()*sScale)).createGlyphVector(frc, str);
442// double psize = Math.abs(prect.getX());
443 Point2D prev = new Point2D.Double();
444 Point2D next = new Point2D.Double();
445 Point2D curr = new Point2D.Double();
446 Point2D succ = new Point2D.Double();
447 boolean gap = true;
448 boolean piv = false;
449 double len = 0;
450 double angle = 0;
451 for (Bound bound : area) {
452 BoundIterator bit = map.new BoundIterator(bound);
453 boolean first = true;
454 while (bit.hasNext()) {
455 prev = next;
456 next = context.getPoint(bit.next());
457 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
458 piv = true;
459 if (first) {
460 curr = succ = next;
461// gap = (space > 0);
462// len = gap ? psize * space * 0.5 : psize;
463 first = false;
464 } else {
465 while (curr.distance(next) >= len) {
466 if (piv) {
467 double rem = len;
468 double s = prev.distance(next);
469 double p = curr.distance(prev);
470 if ((s > 0) && (p > 0)) {
471 double n = curr.distance(next);
472 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
473 double phi = Math.asin(p / len * Math.sin(theta));
474 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
475 }
476 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
477 piv = false;
478 } else {
479 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
480 }
481 if (!gap) {
482// Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))), null);
483 }
484 gap = false;
485 curr = succ;
486// len = psize;
487 }
488 }
489 }
490 }
491 }
492 }
493}
Note: See TracBrowser for help on using the repository browser.