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

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

save

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