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

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

save

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