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

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

save

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