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

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

checkstyle

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