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

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

save

File size: 7.1 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, FONT, 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 Caption {
88 String str;;
89 float x;
90 float y;
91
92 public Caption(String istr, float ix, float iy) {
93 str = istr;
94 x = ix;
95 y = iy;
96 }
97 }
98
99 public static class Symbol {
100 ArrayList<Instr> instr;
101 double scale;
102 double x;
103 double y;
104 Delta delta;
105 Scheme scheme;
106
107 public Symbol(ArrayList<Instr> iinstr, double iscale, double ix, double iy, Delta idelta, Scheme ischeme) {
108 instr = iinstr;
109 scale = iscale;
110 x = ix;
111 y = iy;
112 delta = idelta;
113 scheme = ischeme;
114 }
115 }
116
117 public static void drawSymbol(Graphics2D g2, ArrayList<Instr> symbol, double scale, double x, double y, Delta dd, Scheme cs) {
118 int pn = 0;
119 int cn = 0;
120 if (cs != null) {
121 pn = cs.pat.size();
122 cn = cs.col.size() - ((pn != 0) ? pn - 1 : 0);
123 }
124 AffineTransform savetr = g2.getTransform();
125 g2.translate(x, y);
126 g2.scale(scale, scale);
127 if (symbol != null) {
128 for (Instr item : symbol) {
129 switch (item.type) {
130 case BBOX:
131 Rectangle bbox = (Rectangle) item.params;
132 double dx = 0.0;
133 double dy = 0.0;
134 if (dd != null) {
135 g2.transform(dd.t);
136 switch (dd.h) {
137 case CC:
138 dx = bbox.x + (bbox.width / 2.0);
139 dy = bbox.y + (bbox.height / 2.0);
140 break;
141 case TL:
142 dx = bbox.x;
143 dy = bbox.y;
144 break;
145 case TR:
146 dx = bbox.x + bbox.width;
147 dy = bbox.y;
148 break;
149 case TC:
150 dx = bbox.x + (bbox.width / 2.0);
151 dy = bbox.y;
152 break;
153 case LC:
154 dx = bbox.x;
155 dy = bbox.y + (bbox.height / 2.0);
156 break;
157 case RC:
158 dx = bbox.x + bbox.width;
159 dy = bbox.y + (bbox.height / 2.0);
160 break;
161 case BL:
162 dx = bbox.x;
163 dy = bbox.y + bbox.height;
164 break;
165 case BR:
166 dx = bbox.x + bbox.width;
167 dy = bbox.y + bbox.height;
168 break;
169 case BC:
170 dx = bbox.x + (bbox.width / 2.0);
171 dy = bbox.y + bbox.height;
172 break;
173 }
174 g2.translate(-dx, -dy);
175 }
176 break;
177 case COLR:
178 if ((cs != null) && (cs.col != null)) {
179 for (Instr patch : (ArrayList<Instr>) item.params) {
180 switch (patch.type) {
181 case P1:
182 if (cn > 0) {
183 g2.setPaint(bodyColours.get(cs.col.get(0)));
184 g2.fill((Path2D.Double) patch.params);
185 }
186 break;
187 case P2:
188 if (cn > 0) {
189 if (cn > 1) {
190 g2.setPaint(bodyColours.get(cs.col.get(1)));
191 } else {
192 g2.setPaint(bodyColours.get(cs.col.get(0)));
193 }
194 g2.fill((Path2D.Double) patch.params);
195 }
196 break;
197 case H2:
198 if ((cn > 1) && (cs.pat.get(0) == ColPAT.PAT_HORI)) {
199 g2.setPaint(bodyColours.get(cs.col.get(cs.col.size() - pn)));
200 g2.fill((Path2D.Double) patch.params);
201 }
202 break;
203 case H3:
204 if ((cn == 3) && (cs.pat.get(0) == ColPAT.PAT_HORI)) {
205 g2.setPaint(bodyColours.get(cs.col.get(1)));
206 g2.fill((Path2D.Double) patch.params);
207 }
208 break;
209 case H4:
210 if ((cn == 4) && (cs.pat.get(0) == ColPAT.PAT_HORI)) {
211 g2.setPaint(bodyColours.get(cs.col.get(1)));
212 g2.fill((Path2D.Double) patch.params);
213 }
214 break;
215 case H5:
216 if ((cn == 4) && (cs.pat.get(0) == ColPAT.PAT_HORI)) {
217 g2.setPaint(bodyColours.get(cs.col.get(2)));
218 g2.fill((Path2D.Double) patch.params);
219 }
220 break;
221 case V2:
222 if ((cn > 1) && (cs.pat.get(0) == ColPAT.PAT_VERT)) {
223 g2.setPaint(bodyColours.get(cs.col.get(cs.col.size() - pn)));
224 g2.fill((Path2D.Double) patch.params);
225 }
226 break;
227 }
228 }
229 }
230 break;
231 case STRK:
232 g2.setStroke((BasicStroke) item.params);
233 break;
234 case FILL:
235 g2.setPaint((Color) item.params);
236 break;
237 case LINE:
238 g2.draw((Line2D.Double) item.params);
239 break;
240 case RECT:
241 g2.draw((Rectangle2D.Double) item.params);
242 break;
243 case RRCT:
244 g2.draw((RoundRectangle2D.Double) item.params);
245 break;
246 case ELPS:
247 g2.draw((Ellipse2D.Double) item.params);
248 break;
249 case EARC:
250 g2.draw((Arc2D.Double) item.params);
251 break;
252 case PLIN:
253 g2.draw((Path2D.Double) item.params);
254 break;
255 case PGON:
256 g2.fill((Path2D.Double) item.params);
257 break;
258 case RSHP:
259 g2.fill((RectangularShape) item.params);
260 break;
261 case SYMB:
262 Symbol s = (Symbol) item.params;
263 drawSymbol(g2, s.instr, s.scale, s.x, s.y, s.delta, s.scheme);
264 break;
265 case FONT:
266 g2.setFont((Font) item.params);
267 break;
268 case TEXT:
269 Caption c = (Caption) item.params;
270 g2.drawString(c.str, c.x, c.y);
271 break;
272 }
273 }
274 }
275 g2.setTransform(savetr);
276 }
277}
Note: See TracBrowser for help on using the repository browser.