source: osm/applications/editors/josm/plugins/smed2/src/render/Renderer.java@ 30186

Last change on this file since 30186 was 30186, checked in by malcolmh, 11 years ago

save

File size: 23.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 render;
11
12import java.awt.*;
13import java.awt.font.*;
14import java.awt.geom.*;
15import java.awt.image.*;
16import java.util.*;
17
18import s57.S57val.*;
19import s57.S57map;
20import s57.S57map.*;
21import s57.S57map.Area;
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, 0.138 };
29
30 static final EnumMap<ColCOL, Color> bodyColours = new EnumMap<ColCOL, Color>(ColCOL.class);
31 static {
32 bodyColours.put(ColCOL.COL_UNK, new Color(0, true));
33 bodyColours.put(ColCOL.COL_WHT, new Color(0xffffff));
34 bodyColours.put(ColCOL.COL_BLK, new Color(0x000000));
35 bodyColours.put(ColCOL.COL_RED, new Color(0xd40000));
36 bodyColours.put(ColCOL.COL_GRN, new Color(0x00d400));
37 bodyColours.put(ColCOL.COL_BLU, Color.blue);
38 bodyColours.put(ColCOL.COL_YEL, new Color(0xffd400));
39 bodyColours.put(ColCOL.COL_GRY, Color.gray);
40 bodyColours.put(ColCOL.COL_BRN, new Color(0x8b4513));
41 bodyColours.put(ColCOL.COL_AMB, new Color(0xfbf00f));
42 bodyColours.put(ColCOL.COL_VIO, new Color(0xee82ee));
43 bodyColours.put(ColCOL.COL_ORG, Color.orange);
44 bodyColours.put(ColCOL.COL_MAG, new Color(0xf000f0));
45 bodyColours.put(ColCOL.COL_PNK, Color.pink);
46 }
47
48 static final EnumMap<ColPAT, Patt> pattMap = new EnumMap<ColPAT, Patt>(ColPAT.class);
49 static {
50 pattMap.put(ColPAT.PAT_UNKN, Patt.Z);
51 pattMap.put(ColPAT.PAT_HORI, Patt.H);
52 pattMap.put(ColPAT.PAT_VERT, Patt.V);
53 pattMap.put(ColPAT.PAT_DIAG, Patt.D);
54 pattMap.put(ColPAT.PAT_BRDR, Patt.B);
55 pattMap.put(ColPAT.PAT_SQUR, Patt.S);
56 pattMap.put(ColPAT.PAT_CROS, Patt.C);
57 pattMap.put(ColPAT.PAT_SALT, Patt.X);
58 pattMap.put(ColPAT.PAT_STRP, Patt.H);
59 }
60
61 public enum LabelStyle { NONE, RRCT, RECT, ELPS, CIRC, VCLR, PCLR, HCLR }
62
63 static MapContext context;
64 static S57map map;
65 static double sScale;
66 static Graphics2D g2;
67 static int zoom;
68
69 public static void reRender(Graphics2D g, int z, double factor, S57map m, MapContext c) {
70 g2 = g;
71 zoom = z;
72 context = c;
73 map = m;
74 sScale = symbolScale[zoom] * factor;
75 if (map != null) {
76 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
77 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
78 g2.setStroke(new BasicStroke(0, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
79 Rules.rules();
80 }
81 }
82
83 public static void symbol(Feature feature, Symbol symbol) {
84 Point2D point = context.getPoint(feature.centre);
85 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, null);
86 }
87 public static void symbol(Feature feature, Symbol symbol, Scheme scheme) {
88 Point2D point = context.getPoint(feature.centre);
89 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, null);
90 }
91 public static void symbol(Feature feature, Symbol symbol, Delta delta) {
92 Point2D point = context.getPoint(feature.centre);
93 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), null, delta);
94 }
95 public static void symbol(Feature feature, Symbol symbol, Scheme scheme, Delta delta) {
96 Point2D point = context.getPoint(feature.centre);
97 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), scheme, delta);
98 }
99
100 public static void cluster(Feature feature, ArrayList<Symbol> symbols) {
101 Rectangle2D.Double bbox = null;
102 if (symbols.size() > 4) {
103 for (Instr instr : symbols.get(0)) {
104 if (instr.type == Prim.BBOX) {
105 bbox = (Rectangle2D.Double) instr.params;
106 break;
107 }
108 }
109 if (bbox == null) return;
110 }
111 switch (symbols.size()) {
112 case 1:
113 symbol(feature, symbols.get(0), new Delta(Handle.CC, new AffineTransform()));
114 break;
115 case 2:
116 symbol(feature, symbols.get(0), new Delta(Handle.RC, new AffineTransform()));
117 symbol(feature, symbols.get(1), new Delta(Handle.LC, new AffineTransform()));
118 break;
119 case 3:
120 symbol(feature, symbols.get(0), new Delta(Handle.BC, new AffineTransform()));
121 symbol(feature, symbols.get(1), new Delta(Handle.TR, new AffineTransform()));
122 symbol(feature, symbols.get(2), new Delta(Handle.TL, new AffineTransform()));
123 break;
124 case 4:
125 symbol(feature, symbols.get(0), new Delta(Handle.BR, new AffineTransform()));
126 symbol(feature, symbols.get(1), new Delta(Handle.BL, new AffineTransform()));
127 symbol(feature, symbols.get(2), new Delta(Handle.TR, new AffineTransform()));
128 symbol(feature, symbols.get(3), new Delta(Handle.TL, new AffineTransform()));
129 break;
130 case 5:
131 symbol(feature, symbols.get(0), new Delta(Handle.BR, new AffineTransform()));
132 symbol(feature, symbols.get(1), new Delta(Handle.BL, new AffineTransform()));
133 symbol(feature, symbols.get(2), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
134 symbol(feature, symbols.get(3), new Delta(Handle.TC, new AffineTransform()));
135 symbol(feature, symbols.get(4), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
136 break;
137 case 6:
138 symbol(feature, symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
139 symbol(feature, symbols.get(1), new Delta(Handle.BC, new AffineTransform()));
140 symbol(feature, symbols.get(2), new Delta(Handle.BL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
141 symbol(feature, symbols.get(3), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
142 symbol(feature, symbols.get(4), new Delta(Handle.TC, new AffineTransform()));
143 symbol(feature, symbols.get(5), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
144 break;
145 case 7:
146 symbol(feature, symbols.get(0), new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
147 symbol(feature, symbols.get(1), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
148 symbol(feature, symbols.get(2), new Delta(Handle.CC, new AffineTransform()));
149 symbol(feature, symbols.get(3), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
150 symbol(feature, symbols.get(4), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
151 symbol(feature, symbols.get(5), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
152 symbol(feature, symbols.get(6), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
153 break;
154 case 8:
155 symbol(feature, symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
156 symbol(feature, symbols.get(1), new Delta(Handle.BL, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
157 symbol(feature, symbols.get(2), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
158 symbol(feature, symbols.get(3), new Delta(Handle.CC, new AffineTransform()));
159 symbol(feature, symbols.get(4), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
160 symbol(feature, symbols.get(5), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
161 symbol(feature, symbols.get(6), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
162 symbol(feature, symbols.get(7), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
163 break;
164 case 9:
165 symbol(feature, symbols.get(0), new Delta(Handle.BR, AffineTransform.getTranslateInstance(-bbox.width/2, -bbox.height/2)));
166 symbol(feature, symbols.get(1), new Delta(Handle.BC, AffineTransform.getTranslateInstance(0, -bbox.height/2)));
167 symbol(feature, symbols.get(2), new Delta(Handle.BL, AffineTransform.getTranslateInstance(bbox.width/2, -bbox.height/2)));
168 symbol(feature, symbols.get(3), new Delta(Handle.RC, AffineTransform.getTranslateInstance(-bbox.width/2, 0)));
169 symbol(feature, symbols.get(4), new Delta(Handle.CC, new AffineTransform()));
170 symbol(feature, symbols.get(5), new Delta(Handle.LC, AffineTransform.getTranslateInstance(bbox.width/2, 0)));
171 symbol(feature, symbols.get(6), new Delta(Handle.TR, AffineTransform.getTranslateInstance(-bbox.width/2, bbox.height/2)));
172 symbol(feature, symbols.get(7), new Delta(Handle.TC, AffineTransform.getTranslateInstance(0, bbox.height/2)));
173 symbol(feature, symbols.get(8), new Delta(Handle.TL, AffineTransform.getTranslateInstance(bbox.width/2, bbox.height/2)));
174 break;
175 }
176 }
177
178 private static Rectangle2D.Double symbolSize(Symbol symbol) {
179 Symbol ssymb = symbol;
180 while (ssymb != null) {
181 for (Instr item : symbol) {
182 if (item.type == Prim.BBOX) {
183 return (Rectangle2D.Double) item.params;
184 }
185 if (item.type == Prim.SYMB) {
186 ssymb = ((SubSymbol) item.params).instr;
187 break;
188 }
189 }
190 if (ssymb == symbol)
191 break;
192 }
193 return null;
194 }
195
196 public static void lineSymbols(Feature feature, Symbol prisymb, double space, Symbol secsymb, Symbol tersymb, int ratio, Color col) {
197 Area area;
198 switch (feature.flag) {
199 case LINE:
200 Edge edge = map.edges.get(feature.refs);
201 area = map.new Area();
202 area.add(map.new Bound(map.new Side(edge, true), true));
203 break;
204 case AREA:
205 area = map.areas.get(feature.refs);
206 break;
207 default:
208 return;
209 }
210 Rectangle2D.Double prect = symbolSize(prisymb);
211 Rectangle2D.Double srect = symbolSize(secsymb);
212 Rectangle2D.Double trect = symbolSize(tersymb);
213 if (srect == null)
214 ratio = 0;
215 if (prect != null) {
216 double psize = Math.abs(prect.getY()) * sScale;
217 double ssize = (srect != null) ? Math.abs(srect.getY()) * sScale : 0;
218 double tsize = (trect != null) ? Math.abs(srect.getY()) * sScale : 0;
219 Point2D prev = new Point2D.Double();
220 Point2D next = new Point2D.Double();
221 Point2D curr = new Point2D.Double();
222 Point2D succ = new Point2D.Double();
223 boolean gap = true;
224 boolean piv = false;
225 double len = 0;
226 double angle = 0;
227 int stcount = ratio;
228 boolean stflag = false;
229 Symbol symbol = prisymb;
230 for (Bound bound : area) {
231 BoundIterator bit = map.new BoundIterator(bound);
232 boolean first = true;
233 while (bit.hasNext()) {
234 prev = next;
235 next = context.getPoint(bit.next());
236 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
237 piv = true;
238 if (first) {
239 curr = succ = next;
240 gap = (space > 0);
241 stcount = ratio - 1;
242 symbol = prisymb;
243 len = gap ? psize * space * 0.5 : psize;
244 first = false;
245 } else {
246 while (curr.distance(next) >= len) {
247 if (piv) {
248 double rem = len;
249 double s = prev.distance(next);
250 double p = curr.distance(prev);
251 if ((s > 0) && (p > 0)) {
252 double n = curr.distance(next);
253 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
254 double phi = Math.asin(p / len * Math.sin(theta));
255 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
256 }
257 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
258 piv = false;
259 } else {
260 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
261 }
262 if (!gap) {
263 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Scheme(col),
264 new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))));
265 }
266 if (space > 0)
267 gap = !gap;
268 curr = succ;
269 len = gap ? (psize * space) : (--stcount == 0) ? (stflag ? tsize : ssize) : psize;
270 if (stcount == 0) {
271 symbol = stflag ? tersymb : secsymb;
272 if (trect != null) stflag = !stflag;
273 stcount = ratio;
274 } else {
275 symbol = prisymb;
276 }
277 }
278 }
279 }
280 }
281 }
282 }
283
284 public static void lineVector(Feature feature, LineStyle style) {
285 Path2D.Double p = new Path2D.Double();
286 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
287 Point2D point;
288 switch (feature.flag) {
289 case LINE:
290 EdgeIterator eit = map.new EdgeIterator(map.edges.get(feature.refs), true);
291 point = context.getPoint(eit.next());
292 p.moveTo(point.getX(), point.getY());
293 while (eit.hasNext()) {
294 point = context.getPoint(eit.next());
295 p.lineTo(point.getX(), point.getY());
296 }
297 break;
298 case AREA:
299 for (Bound bound : map.areas.get(feature.refs)) {
300 BoundIterator bit = map.new BoundIterator(bound);
301 point = context.getPoint(bit.next());
302 p.moveTo(point.getX(), point.getY());
303 while (bit.hasNext()) {
304 point = context.getPoint(bit.next());
305 p.lineTo(point.getX(), point.getY());
306 }
307 }
308 break;
309 }
310 if (style.line != null) {
311 if (style.dash != null) {
312 float[] dash = new float[style.dash.length];
313 System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
314 for (int i = 0; i < style.dash.length; i++) {
315 dash[i] *= (float) sScale;
316 }
317 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
318 } else {
319 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
320 }
321 g2.setPaint(style.line);
322 g2.draw(p);
323 }
324 if (style.fill != null) {
325 g2.setPaint(style.fill);
326 g2.fill(p);
327 }
328 }
329
330 public static void lineCircle(Feature feature, LineStyle style, double radius, UniHLU units) {
331 switch (units) {
332 case HLU_FEET:
333 radius /= 6076;
334 break;
335 case HLU_KMTR:
336 radius /= 1.852;
337 break;
338 case HLU_HMTR:
339 radius /= 18.52;
340 break;
341 case HLU_SMIL:
342 radius /= 1.15078;
343 break;
344 case HLU_NMIL:
345 break;
346 default:
347 radius /= 1852;
348 break;
349 }
350 radius *= context.mile(feature);
351 Symbol circle = new Symbol();
352 if (style.fill != null) {
353 circle.add(new Instr(Prim.FILL, style.fill));
354 circle.add(new Instr(Prim.RSHP, new Ellipse2D.Double(-radius,-radius,radius*2,radius*2)));
355 }
356 circle.add(new Instr(Prim.FILL, style.line));
357 circle.add(new Instr(Prim.STRK, new BasicStroke(style.width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, style.dash, 0)));
358 circle.add(new Instr(Prim.ELPS, new Ellipse2D.Double(-radius,-radius,radius*2,radius*2)));
359 Point2D point = context.getPoint(feature.centre);
360 Symbols.drawSymbol(g2, circle, 1, point.getX(), point.getY(), null, null);
361 }
362
363
364 public static void fillPattern(Feature feature, BufferedImage image) {
365 Path2D.Double p = new Path2D.Double();
366 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
367 Point2D point;
368 switch (feature.flag) {
369 case POINT:
370 point = context.getPoint(feature.centre);
371 g2.drawImage(image, new AffineTransformOp(AffineTransform.getScaleInstance(sScale, sScale), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
372 (int)(point.getX() - (50 * sScale)), (int)(point.getY() - (50 * sScale)));
373 break;
374 case AREA:
375 for (Bound bound : map.areas.get(feature.refs)) {
376 BoundIterator bit = map.new BoundIterator(bound);
377 point = context.getPoint(bit.next());
378 p.moveTo(point.getX(), point.getY());
379 while (bit.hasNext()) {
380 point = context.getPoint(bit.next());
381 p.lineTo(point.getX(), point.getY());
382 }
383 }
384 g2.setPaint(new TexturePaint(image, new Rectangle(0, 0, 1 + (int)(100 * sScale), 1 + (int)(100 * sScale))));
385 g2.fill(p);
386 break;
387 }
388 }
389
390 public static void labelText(Feature feature, String str, Font font, Color tc) {
391 labelText(feature, str, font, tc, LabelStyle.NONE, null, null, null);
392 }
393 public static void labelText(Feature feature, String str, Font font, Color tc, Delta delta) {
394 labelText(feature, str, font, tc, LabelStyle.NONE, null, null, delta);
395 }
396 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg) {
397 labelText(feature, str, font, tc, style, fg, null, null);
398 }
399 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Color bg) {
400 labelText(feature, str, font, tc, style, fg, bg, null);
401 }
402 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Delta delta) {
403 labelText(feature, str, font, tc, style, fg, null, delta);
404 }
405 public static void labelText(Feature feature, String str, Font font, Color tc, LabelStyle style, Color fg, Color bg, Delta delta) {
406 if (delta == null) delta = new Delta(Handle.CC);
407 if (bg == null) bg = new Color(0x00000000, true);
408 if ((str == null) || (str.isEmpty())) str = " ";
409 FontRenderContext frc = g2.getFontRenderContext();
410 GlyphVector gv = font.deriveFont((float)(font.getSize())).createGlyphVector(frc, str.equals(" ") ? "!" : str);
411 Rectangle2D bounds = gv.getVisualBounds();
412 double width = bounds.getWidth();
413 double height = bounds.getHeight();
414 Symbol label = new Symbol();
415 double lx, ly, tx, ty;
416 switch (style) {
417 case RRCT:
418 width += height * 1.0;
419 height *= 1.5;
420 if (width < height) width = height;
421 lx = -width / 2;
422 ly = -height / 2;
423 tx = lx + (height * 0.34);
424 ty = ly + (height * 0.17);
425 label.add(new Instr(Prim.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
426 label.add(new Instr(Prim.FILL, bg));
427 label.add(new Instr(Prim.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
428 label.add(new Instr(Prim.FILL, fg));
429 label.add(new Instr(Prim.STRK, new BasicStroke(1 + (int)(height/10), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
430 label.add(new Instr(Prim.RRCT, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
431 break;
432 case VCLR:
433 width += height * 1.0;
434 height *= 2.0;
435 if (width < height) width = height;
436 lx = -width / 2;
437 ly = -height / 2;
438 tx = lx + (height * 0.27);
439 ty = ly + (height * 0.25);
440 label.add(new Instr(Prim.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
441 label.add(new Instr(Prim.FILL, bg));
442 label.add(new Instr(Prim.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
443 label.add(new Instr(Prim.FILL, fg));
444 int sw = 1 + (int)(height/10);
445 double po = sw / 2;
446 label.add(new Instr(Prim.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
447 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));
448 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));
449 label.add(new Instr(Prim.PLIN, p));
450 break;
451 case PCLR:
452 width += height * 1.0;
453 height *= 2.0;
454 if (width < height) width = height;
455 lx = -width / 2;
456 ly = -height / 2;
457 tx = lx + (height * 0.27);
458 ty = ly + (height * 0.25);
459 label.add(new Instr(Prim.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
460 label.add(new Instr(Prim.FILL, bg));
461 label.add(new Instr(Prim.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
462 label.add(new Instr(Prim.FILL, fg));
463 sw = 1 + (int)(height/10);
464 po = sw / 2;
465 label.add(new Instr(Prim.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
466 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));
467 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));
468 label.add(new Instr(Prim.PLIN, p));
469 label.add(new Instr(Prim.SYMB, new Symbols.SubSymbol(Areas.CableFlash, 1, 0, 0, null, new Delta(Handle.CC, new AffineTransform(0,-1,1,0,-width/2,0)))));
470 label.add(new Instr(Prim.SYMB, new Symbols.SubSymbol(Areas.CableFlash, 1, 0, 0, null, new Delta(Handle.CC, new AffineTransform(0,-1,1,0,width/2,0)))));
471 break;
472 case HCLR:
473 width += height * 1.5;
474 height *= 1.5;
475 if (width < height) width = height;
476 lx = -width / 2;
477 ly = -height / 2;
478 tx = lx + (height * 0.5);
479 ty = ly + (height * 0.17);
480 label.add(new Instr(Prim.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
481 label.add(new Instr(Prim.FILL, bg));
482 label.add(new Instr(Prim.RSHP, new RoundRectangle2D.Double(lx,ly,width,height,height,height)));
483 label.add(new Instr(Prim.FILL, fg));
484 sw = 1 + (int)(height/10);
485 double vo = height / 4;
486 label.add(new Instr(Prim.STRK, new BasicStroke(sw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)));
487 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);
488 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);
489 label.add(new Instr(Prim.PLIN, p));
490 break;
491 default:
492 lx = -width / 2;
493 ly = -height / 2;
494 tx = lx;
495 ty = ly;
496 label.add(new Instr(Prim.BBOX, new Rectangle2D.Double(lx,ly,width,height)));
497 break;
498 }
499 label.add(new Instr(Prim.TEXT, new Caption(str, font, tc, new Delta(Handle.TL, AffineTransform.getTranslateInstance(tx, ty)))));
500 Point2D point = context.getPoint(feature.centre);
501 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, delta);
502 }
503
504 public static void lineText(Feature feature, String str, Font font, Color colour, double offset, double dy) {
505 Area area;
506 switch (feature.flag) {
507 case LINE:
508 Edge edge = map.edges.get(feature.refs);
509 area = map.new Area();
510 area.add(map.new Bound(map.new Side(edge, true), true));
511 break;
512 case AREA:
513 area = map.areas.get(feature.refs);
514 break;
515 default:
516 return;
517 }
518// Rectangle prect = symbolSize(prisymb);
519 if (!str.isEmpty()) {
520 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
521 FontRenderContext frc = g2.getFontRenderContext();
522 GlyphVector gv = font.deriveFont((float)(font.getSize()*sScale)).createGlyphVector(frc, str);
523// double psize = Math.abs(prect.getX());
524 Point2D prev = new Point2D.Double();
525 Point2D next = new Point2D.Double();
526 Point2D curr = new Point2D.Double();
527 Point2D succ = new Point2D.Double();
528 boolean piv = false;
529 double len = 0;
530 double angle = 0;
531 for (Bound bound : area) {
532 BoundIterator bit = map.new BoundIterator(bound);
533 boolean first = true;
534 while (bit.hasNext()) {
535 prev = next;
536 next = context.getPoint(bit.next());
537 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
538 piv = true;
539 if (first) {
540 curr = succ = next;
541// len = psize;
542 first = false;
543 } else {
544 while (curr.distance(next) >= len) {
545 if (piv) {
546 double rem = len;
547 double s = prev.distance(next);
548 double p = curr.distance(prev);
549 if ((s > 0) && (p > 0)) {
550 double n = curr.distance(next);
551 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
552 double phi = Math.asin(p / len * Math.sin(theta));
553 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
554 }
555 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
556 piv = false;
557 } else {
558 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
559 }
560// 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);
561 curr = succ;
562// len = psize;
563 }
564 }
565 }
566 }
567 }
568 }
569}
Note: See TracBrowser for help on using the repository browser.