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

Last change on this file since 32088 was 31063, checked in by malcolmh, 10 years ago

[SeaChart] update

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