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

Last change on this file since 29204 was 29204, checked in by malcolmh, 13 years ago

save

File size: 8.8 KB
Line 
1/* Copyright 2012 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.Rectangle;
17import java.awt.font.TextLayout;
18import java.awt.geom.*;
19import java.util.ArrayList;
20import java.util.EnumMap;
21
22import s57.S57val.*;
23
24public class Symbols {
25
26 public enum Prim {
27 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
28 }
29
30 public enum Handle {
31 CC, TL, TR, TC, LC, RC, BL, BR, BC
32 }
33
34 public static final double symbolScale[] = { 256.0, 128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.61, 0.372, 0.227, 0.138,
35 0.0843, 0.0514, 0.0313, 0.0191, 0.0117, 0.007, 0.138 };
36
37 public static final double textScale[] = { 256.0, 128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.5556, 0.3086, 0.1714, 0.0953,
38 0.0529, 0.0294, 0.0163, 0.0091, 0.0050, 0.0028, 0.0163 };
39
40 private static final EnumMap<ColCOL, Color> bodyColours = new EnumMap<ColCOL, Color>(ColCOL.class);
41 static {
42 bodyColours.put(ColCOL.COL_UNK, new Color(0, true));
43 bodyColours.put(ColCOL.COL_WHT, new Color(0xffffff));
44 bodyColours.put(ColCOL.COL_BLK, new Color(0x000000));
45 bodyColours.put(ColCOL.COL_RED, new Color(0xd40000));
46 bodyColours.put(ColCOL.COL_GRN, new Color(0x00d400));
47 bodyColours.put(ColCOL.COL_BLU, Color.blue);
48 bodyColours.put(ColCOL.COL_YEL, new Color(0xffd400));
49 bodyColours.put(ColCOL.COL_GRY, Color.gray);
50 bodyColours.put(ColCOL.COL_BRN, new Color(0x8b4513));
51 bodyColours.put(ColCOL.COL_AMB, new Color(0xfbf00f));
52 bodyColours.put(ColCOL.COL_VIO, new Color(0xee82ee));
53 bodyColours.put(ColCOL.COL_ORG, Color.orange);
54 bodyColours.put(ColCOL.COL_MAG, new Color(0xf000f0));
55 bodyColours.put(ColCOL.COL_PNK, Color.pink);
56 }
57
58 public static class Instr {
59 public Prim type;
60 public Object params;
61
62 public Instr(Prim itype, Object iparams) {
63 type = itype;
64 params = iparams;
65 }
66 }
67
68 public static class Delta {
69 public Handle h;
70 public AffineTransform t;
71
72 public Delta(Handle ih, AffineTransform it) {
73 h = ih;
74 t = it;
75 }
76 }
77
78 public static class Scheme {
79 public ArrayList<ColPAT> pat;
80 public ArrayList<ColCOL> col;
81
82 public Scheme(ArrayList<ColPAT> ipat, ArrayList<ColCOL> icol) {
83 pat = ipat;
84 col = icol;
85 }
86 }
87
88 public static class Caption {
89 public String string;
90 public Font font;
91 public Color colour;
92 public Delta dd;
93
94 public Caption(String istr, Font ifont, Color icolour, Delta idd) {
95 string = istr;
96 font = ifont;
97 colour = icolour;
98 dd = idd;
99 }
100 }
101
102 public static class LineStyle {
103 public Color line;
104 public float width;
105 public float[] dash;
106 public Color fill;
107
108 public LineStyle(Color iline, float iwidth, float[] idash, Color ifill) {
109 line = iline;
110 width = iwidth;
111 dash = idash;
112 fill = ifill;
113 }
114 }
115
116 public static class Symbol extends ArrayList<Instr> {
117
118 public Symbol() {
119 super();
120 }
121 }
122
123 public static class SubSymbol {
124 Symbol instr;
125 double scale;
126 double x;
127 double y;
128 Delta delta;
129 Scheme scheme;
130
131 public SubSymbol(Symbol iinstr, double iscale, double ix, double iy, Delta idelta, Scheme ischeme) {
132 instr = iinstr;
133 scale = iscale;
134 x = ix;
135 y = iy;
136 delta = idelta;
137 scheme = ischeme;
138 }
139 }
140
141 public static void drawSymbol(Graphics2D g2, Symbol symbol, double scale, double x, double y, Delta dd, Scheme cs) {
142 int pn = 0;
143 int cn = 0;
144 if (cs != null) {
145 pn = cs.pat.size();
146 cn = cs.col.size() - ((pn != 0) ? pn - 1 : 0);
147 }
148 AffineTransform savetr = g2.getTransform();
149 g2.translate(x, y);
150 g2.scale(scale, scale);
151 if (symbol != null) {
152 for (Instr item : symbol) {
153 switch (item.type) {
154 case BBOX:
155 Rectangle bbox = (Rectangle) item.params;
156 double dx = 0.0;
157 double dy = 0.0;
158 if (dd != null) {
159 g2.transform(dd.t);
160 switch (dd.h) {
161 case CC:
162 dx -= bbox.x + (bbox.width / 2.0);
163 dy -= bbox.y + (bbox.height / 2.0);
164 break;
165 case TL:
166 dx -= bbox.x;
167 dy -= bbox.y;
168 break;
169 case TR:
170 dx -= bbox.x + bbox.width;
171 dy -= bbox.y;
172 break;
173 case TC:
174 dx -= bbox.x + (bbox.width / 2.0);
175 dy -= bbox.y;
176 break;
177 case LC:
178 dx -= bbox.x;
179 dy -= bbox.y + (bbox.height / 2.0);
180 break;
181 case RC:
182 dx -= bbox.x + bbox.width;
183 dy -= bbox.y + (bbox.height / 2.0);
184 break;
185 case BL:
186 dx -= bbox.x;
187 dy -= bbox.y + bbox.height;
188 break;
189 case BR:
190 dx -= bbox.x + bbox.width;
191 dy -= bbox.y + bbox.height;
192 break;
193 case BC:
194 dx -= bbox.x + (bbox.width / 2.0);
195 dy -= bbox.y + bbox.height;
196 break;
197 }
198 g2.translate(dx, dy);
199 }
200 break;
201 case COLR:
202 if ((cs != null) && (cs.col != null)) {
203 for (Instr patch : (Symbol) item.params) {
204 switch (patch.type) {
205 case P1:
206 if (cn > 0) {
207 g2.setPaint(bodyColours.get(cs.col.get(0)));
208 g2.fill((Path2D.Double) patch.params);
209 }
210 break;
211 case P2:
212 if (cn > 0) {
213 if (cn > 1) {
214 g2.setPaint(bodyColours.get(cs.col.get(1)));
215 } else {
216 g2.setPaint(bodyColours.get(cs.col.get(0)));
217 }
218 g2.fill((Path2D.Double) patch.params);
219 }
220 break;
221 case H2:
222 if ((cn > 1) && (cs.pat.get(0) == ColPAT.PAT_HORI)) {
223 g2.setPaint(bodyColours.get(cs.col.get(cs.col.size() - pn)));
224 g2.fill((Path2D.Double) patch.params);
225 }
226 break;
227 case H3:
228 if ((cn == 3) && (cs.pat.get(0) == ColPAT.PAT_HORI)) {
229 g2.setPaint(bodyColours.get(cs.col.get(1)));
230 g2.fill((Path2D.Double) patch.params);
231 }
232 break;
233 case H4:
234 if ((cn == 4) && (cs.pat.get(0) == ColPAT.PAT_HORI)) {
235 g2.setPaint(bodyColours.get(cs.col.get(1)));
236 g2.fill((Path2D.Double) patch.params);
237 }
238 break;
239 case H5:
240 if ((cn == 4) && (cs.pat.get(0) == ColPAT.PAT_HORI)) {
241 g2.setPaint(bodyColours.get(cs.col.get(2)));
242 g2.fill((Path2D.Double) patch.params);
243 }
244 break;
245 case V2:
246 if ((cn > 1) && (cs.pat.get(0) == ColPAT.PAT_VERT)) {
247 g2.setPaint(bodyColours.get(cs.col.get(cs.col.size() - pn)));
248 g2.fill((Path2D.Double) patch.params);
249 }
250 break;
251 }
252 }
253 }
254 break;
255 case STRK:
256 g2.setStroke((BasicStroke) item.params);
257 break;
258 case FILL:
259 g2.setPaint((Color) item.params);
260 break;
261 case LINE:
262 g2.draw((Line2D.Double) item.params);
263 break;
264 case RECT:
265 g2.draw((Rectangle2D.Double) item.params);
266 break;
267 case RRCT:
268 g2.draw((RoundRectangle2D.Double) item.params);
269 break;
270 case ELPS:
271 g2.draw((Ellipse2D.Double) item.params);
272 break;
273 case EARC:
274 g2.draw((Arc2D.Double) item.params);
275 break;
276 case PLIN:
277 g2.draw((Path2D.Double) item.params);
278 break;
279 case PGON:
280 g2.fill((Path2D.Double) item.params);
281 break;
282 case RSHP:
283 g2.fill((RectangularShape) item.params);
284 break;
285 case SYMB:
286 SubSymbol s = (SubSymbol) item.params;
287 drawSymbol(g2, s.instr, s.scale, s.x, s.y, s.delta, s.scheme);
288 break;
289 case TEXT:
290 Caption c = (Caption) item.params;
291 g2.setPaint(c.colour);
292 TextLayout layout = new TextLayout(c.string, c.font, g2.getFontRenderContext());
293 Rectangle2D bb = layout.getBounds();
294 dx = 0;
295 dy = 0;
296 if (c.dd != null) {
297 g2.transform(c.dd.t);
298 switch (c.dd.h) {
299 case CC:
300 dx -= bb.getX() + (bb.getWidth() / 2.0);
301 dy -= bb.getY() + (bb.getHeight() / 2.0);
302 break;
303 case TL:
304 dx -= bb.getX();
305 dy -= bb.getY();
306 break;
307 case TR:
308 dx -= bb.getX() + bb.getWidth();
309 dy -= bb.getY();
310 break;
311 case TC:
312 dx -= bb.getX() + (bb.getWidth() / 2.0);
313 dy -= bb.getY();
314 break;
315 case LC:
316 dx -= bb.getX();
317 dy -= bb.getY() + (bb.getHeight() / 2.0);
318 break;
319 case RC:
320 dx -= bb.getX() + bb.getWidth();
321 dy -= bb.getY() + (bb.getHeight() / 2.0);
322 break;
323 case BL:
324 dx -= bb.getX();
325 dy -= bb.getY() + bb.getHeight();
326 break;
327 case BR:
328 dx -= bb.getX() + bb.getWidth();
329 dy -= bb.getY() + bb.getHeight();
330 break;
331 case BC:
332 dx -= bb.getX() + (bb.getWidth() / 2.0);
333 dy -= bb.getY() + bb.getHeight();
334 break;
335 }
336 }
337 layout.draw(g2, (float)dx, (float)dy);
338 break;
339 }
340 }
341 }
342 g2.setTransform(savetr);
343 }
344}
Note: See TracBrowser for help on using the repository browser.