source: osm/applications/editors/josm/plugins/smed2/src/symbols/Symbols.java@ 30325

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

save

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