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

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

save

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