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

Last change on this file since 30037 was 30037, checked in by malcolmh, 12 years ago

save

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