source: osm/applications/editors/josm/plugins/seachart/src/symbols/Symbols.java@ 33936

Last change on this file since 33936 was 32907, checked in by donvip, 8 years ago

checkstyle

File size: 16.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package symbols;
3
4import java.awt.BasicStroke;
5import java.awt.Color;
6import java.awt.Font;
7import java.awt.Graphics2D;
8import java.awt.font.TextLayout;
9import java.awt.geom.AffineTransform;
10import java.awt.geom.Arc2D;
11import java.awt.geom.Ellipse2D;
12import java.awt.geom.Line2D;
13import java.awt.geom.Path2D;
14import java.awt.geom.Rectangle2D;
15import java.awt.geom.RectangularShape;
16import java.awt.geom.RoundRectangle2D;
17import java.util.ArrayList;
18
19/**
20 * @author Malcolm Herring
21 */
22public final class Symbols {
23 private Symbols() {
24 // Hide default constructor for utilities classes
25 }
26
27 // CHECKSTYLE.OFF: LineLength
28 public static final Color Yland = new Color(0xedbc0c);
29 // public static final Color Bwater = new Color(0x78acd2);
30 public static final Color Bwater = new Color(0x9bc5e3);
31 public static final Color Gdries = new Color(0x689868);
32 public static final Color Mline = new Color(0x9a6078);
33 public static final Color Msymb = new Color(0xa30075);
34 public static final Color Mtss = new Color(0xc0c480ff, true);
35
36 public enum Form {
37 BBOX, STRK, COLR, FILL, LINE, RECT, RRCT, ELPS, EARC, PLIN, PGON, RSHP, TEXT, SYMB, N1, N2, P1, P2, H2, H3, H4, H5, V2, V3, D2, D3, D4, B1, S2, S3, S4, C2, X2
38 }
39
40 public enum Patt {
41 Z, H, V, D, B, S, C, X
42 }
43
44 public enum Handle {
45 CC, TL, TR, TC, LC, RC, BL, BR, BC
46 }
47
48 public static class Instr {
49 public Form type;
50 public Object params;
51
52 public Instr(Form itype, Object iparams) {
53 type = itype;
54 params = iparams;
55 }
56 }
57
58 public static class Delta {
59 public Handle h;
60 public AffineTransform t;
61
62 public Delta(Handle ih, AffineTransform it) {
63 h = ih;
64 t = it;
65 }
66
67 public Delta(Handle ih) {
68 h = ih;
69 t = new AffineTransform();
70 }
71 }
72
73 public static class Scheme {
74 public ArrayList<Patt> pat;
75 public ArrayList<Color> col;
76
77 public Scheme(ArrayList<Color> icol) {
78 pat = new ArrayList<>();
79 col = icol;
80 }
81
82 public Scheme(ArrayList<Patt> ipat, ArrayList<Color> icol) {
83 pat = ipat;
84 col = icol;
85 }
86
87 public Scheme(Color icol) {
88 pat = new ArrayList<>();
89 col = new ArrayList<>();
90 col.add(icol);
91 }
92
93 public Scheme() {
94 pat = new ArrayList<>();
95 col = new ArrayList<>();
96 }
97 }
98
99 public static class Caption {
100 public String string;
101 public Font font;
102 public Color colour;
103 public Delta dd;
104
105 public Caption(String istr, Font ifont, Color icolour, Delta idd) {
106 string = istr;
107 font = ifont;
108 colour = icolour;
109 dd = idd;
110 }
111 }
112
113 public static class LineStyle {
114 public Color line;
115 public float width;
116 public float[] dash;
117 public Color fill;
118
119 public LineStyle(Color ifill) {
120 line = null;
121 width = 0;
122 dash = null;
123 fill = ifill;
124 }
125
126 public LineStyle(Color iline, float iwidth) {
127 line = iline;
128 width = iwidth;
129 dash = null;
130 fill = null;
131 }
132
133 public LineStyle(Color iline, float iwidth, float[] idash) {
134 line = iline;
135 width = iwidth;
136 dash = idash;
137 fill = null;
138 }
139
140 public LineStyle(Color iline, float iwidth, Color ifill) {
141 line = iline;
142 width = iwidth;
143 dash = null;
144 fill = ifill;
145 }
146
147 public LineStyle(Color iline, float iwidth, float[] idash, Color ifill) {
148 line = iline;
149 width = iwidth;
150 dash = idash;
151 fill = ifill;
152 }
153 }
154
155 public static class Symbol extends ArrayList<Instr> {
156
157 public Symbol() {
158 super();
159 }
160 }
161
162 public static class SubSymbol {
163 public Symbol instr;
164 public double scale;
165 public double x;
166 public double y;
167 public Delta delta;
168 public Scheme scheme;
169
170 public SubSymbol(Symbol iinstr, double iscale, double ix, double iy, Scheme ischeme, Delta idelta) {
171 instr = iinstr;
172 scale = iscale;
173 x = ix;
174 y = iy;
175 delta = idelta;
176 scheme = ischeme;
177 }
178 }
179
180 public static void drawSymbol(Graphics2D g2, Symbol symbol, double scale, double x, double y, Scheme cs, Delta dd) {
181 int pn = 0;
182 int cn = 0;
183 Patt bpat = Patt.Z;
184 Color bcol = null;
185 g2.setPaint(Color.black);
186 if (cs != null) {
187 if ((cs.pat.size() > 0) && (cs.col.size() > 0) && (cs.pat.get(0) == Patt.B)) {
188 bpat = (cs.pat.remove(0));
189 bcol = (cs.col.remove(0));
190 }
191 pn = cs.pat.size();
192 cn = cs.col.size() - ((pn != 0) ? pn - 1 : 0);
193 if ((pn == 0) && (cs.col.size() == 1)) {
194 g2.setPaint(cs.col.get(0));
195 }
196 }
197 AffineTransform savetr = g2.getTransform();
198 g2.translate(x, y);
199 g2.scale(scale, scale);
200 if (symbol != null) {
201 for (Instr item : symbol) {
202 switch (item.type) {
203 case BBOX:
204 Rectangle2D.Double bbox = (Rectangle2D.Double) item.params;
205 double dx = 0.0;
206 double dy = 0.0;
207 if (dd != null) {
208 g2.transform(dd.t);
209 switch (dd.h) {
210 case CC:
211 dx -= bbox.x + (bbox.width / 2.0);
212 dy -= bbox.y + (bbox.height / 2.0);
213 break;
214 case TL:
215 dx -= bbox.x;
216 dy -= bbox.y;
217 break;
218 case TR:
219 dx -= bbox.x + bbox.width;
220 dy -= bbox.y;
221 break;
222 case TC:
223 dx -= bbox.x + (bbox.width / 2.0);
224 dy -= bbox.y;
225 break;
226 case LC:
227 dx -= bbox.x;
228 dy -= bbox.y + (bbox.height / 2.0);
229 break;
230 case RC:
231 dx -= bbox.x + bbox.width;
232 dy -= bbox.y + (bbox.height / 2.0);
233 break;
234 case BL:
235 dx -= bbox.x;
236 dy -= bbox.y + bbox.height;
237 break;
238 case BR:
239 dx -= bbox.x + bbox.width;
240 dy -= bbox.y + bbox.height;
241 break;
242 case BC:
243 dx -= bbox.x + (bbox.width / 2.0);
244 dy -= bbox.y + bbox.height;
245 break;
246 }
247 g2.translate(dx, dy);
248 }
249 break;
250 case COLR:
251 if ((cs != null) && (cs.col != null)) {
252 for (Instr patch : (Symbol) item.params) {
253 switch (patch.type) {
254 case N1:
255 if (cn > 0) {
256 Symbol s = (Symbol) patch.params;
257 drawSymbol(g2, s, 1.0, 0, 0, new Scheme(cs.col.get(0)), null);
258 }
259 break;
260 case N2:
261 if (cn > 0) {
262 Symbol s = (Symbol) patch.params;
263 drawSymbol(g2, s, 1.0, 0, 0, new Scheme((cn > 1) ? cs.col.get(1) : cs.col.get(0)), null);
264 }
265 break;
266 case P1:
267 if (cn > 0) {
268 g2.setPaint(cs.col.get(0));
269 g2.fill((Path2D.Double) patch.params);
270 }
271 break;
272 case P2:
273 if (cn > 0) {
274 if (cn > 1) {
275 g2.setPaint(cs.col.get(1));
276 } else {
277 g2.setPaint(cs.col.get(0));
278 }
279 g2.fill((Path2D.Double) patch.params);
280 }
281 break;
282 case H2:
283 if ((cn > 1) && (pn > 0) && (cs.pat.get(0) == Patt.H)) {
284 g2.setPaint(cs.col.get(cs.col.size() - pn));
285 g2.fill((Path2D.Double) patch.params);
286 }
287 break;
288 case H3:
289 if ((cn == 3) && (pn > 0) && (cs.pat.get(0) == Patt.H)) {
290 g2.setPaint(cs.col.get(1));
291 g2.fill((Path2D.Double) patch.params);
292 }
293 break;
294 case H4:
295 if ((cn == 4) && (pn > 0) && (cs.pat.get(0) == Patt.H)) {
296 g2.setPaint(cs.col.get(1));
297 g2.fill((Path2D.Double) patch.params);
298 }
299 break;
300 case H5:
301 if ((cn == 4) && (pn > 0) && (cs.pat.get(0) == Patt.H)) {
302 g2.setPaint(cs.col.get(2));
303 g2.fill((Path2D.Double) patch.params);
304 }
305 break;
306 case V2:
307 if ((cn > 1) && (pn > 0) && (cs.pat.get(0) == Patt.V)) {
308 g2.setPaint(cs.col.get(cs.col.size() - pn));
309 g2.fill((Path2D.Double) patch.params);
310 }
311 break;
312 case V3:
313 if ((cn == 3) && (pn > 0) && (cs.pat.get(0) == Patt.V)) {
314 g2.setPaint(cs.col.get(1));
315 g2.fill((Path2D.Double) patch.params);
316 }
317 break;
318 case B1:
319 if (bpat == Patt.B) {
320 g2.setPaint(bcol);
321 g2.fill((Path2D.Double) patch.params);
322 }
323 break;
324 case S2:
325 if ((cn > 1) && (pn > 0) && (cs.pat.get(0) == Patt.S)) {
326 g2.setPaint(cs.col.get(1));
327 g2.fill((Path2D.Double) patch.params);
328 }
329 break;
330 case S3:
331 if ((cn > 2) && (pn > 0) && (cs.pat.get(0) == Patt.S)) {
332 g2.setPaint(cs.col.get(2));
333 g2.fill((Path2D.Double) patch.params);
334 }
335 break;
336 case S4:
337 if ((cn == 4) && (pn > 0) && (cs.pat.get(0) == Patt.S)) {
338 g2.setPaint(cs.col.get(3));
339 g2.fill((Path2D.Double) patch.params);
340 }
341 break;
342 default:
343 break;
344 }
345 }
346 }
347 break;
348 case STRK:
349 g2.setStroke((BasicStroke) item.params);
350 break;
351 case FILL:
352 g2.setPaint((Color) item.params);
353 break;
354 case LINE:
355 g2.draw((Line2D.Double) item.params);
356 break;
357 case RECT:
358 g2.draw((Rectangle2D.Double) item.params);
359 break;
360 case RRCT:
361 g2.draw((RoundRectangle2D.Double) item.params);
362 break;
363 case ELPS:
364 g2.draw((Ellipse2D.Double) item.params);
365 break;
366 case EARC:
367 g2.draw((Arc2D.Double) item.params);
368 break;
369 case PLIN:
370 g2.draw((Path2D.Double) item.params);
371 break;
372 case PGON:
373 g2.fill((Path2D.Double) item.params);
374 break;
375 case RSHP:
376 g2.fill((RectangularShape) item.params);
377 break;
378 case SYMB:
379 SubSymbol s = (SubSymbol) item.params;
380 drawSymbol(g2, s.instr, s.scale, s.x, s.y, (s.scheme != null ? s.scheme : cs), s.delta);
381 break;
382 case TEXT:
383 Caption c = (Caption) item.params;
384 g2.setPaint(c.colour);
385 TextLayout layout = new TextLayout(c.string, c.font, g2.getFontRenderContext());
386 Rectangle2D bb = layout.getBounds();
387 dx = 0;
388 dy = 0;
389 if (c.dd != null) {
390 if (c.dd.t != null) g2.transform(c.dd.t);
391 switch (c.dd.h) {
392 case CC:
393 dx -= bb.getX() + (bb.getWidth() / 2.0);
394 dy -= bb.getY() + (bb.getHeight() / 2.0);
395 break;
396 case TL:
397 dx -= bb.getX();
398 dy -= bb.getY();
399 break;
400 case TR:
401 dx -= bb.getX() + bb.getWidth();
402 dy -= bb.getY();
403 break;
404 case TC:
405 dx -= bb.getX() + (bb.getWidth() / 2.0);
406 dy -= bb.getY();
407 break;
408 case LC:
409 dx -= bb.getX();
410 dy -= bb.getY() + (bb.getHeight() / 2.0);
411 break;
412 case RC:
413 dx -= bb.getX() + bb.getWidth();
414 dy -= bb.getY() + (bb.getHeight() / 2.0);
415 break;
416 case BL:
417 dx -= bb.getX();
418 dy -= bb.getY() + bb.getHeight();
419 break;
420 case BR:
421 dx -= bb.getX() + bb.getWidth();
422 dy -= bb.getY() + bb.getHeight();
423 break;
424 case BC:
425 dx -= bb.getX() + (bb.getWidth() / 2.0);
426 dy -= bb.getY() + bb.getHeight();
427 break;
428 }
429 }
430 layout.draw(g2, (float) dx, (float) dy);
431 break;
432 default:
433 break;
434 }
435 }
436 }
437 g2.setTransform(savetr);
438 }
439}
Note: See TracBrowser for help on using the repository browser.