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

Last change on this file since 32090 was 32090, checked in by malcolmh, 9 years ago

add RADRFLs

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