Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / CharacterMarkerSymbol.java @ 12043

History | View | Annotate | Download (10.4 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
 *
44
 * $Id: CharacterMarkerSymbol.java 12043 2007-06-07 06:58:18Z jaume $
45
 * $Log$
46
 * Revision 1.21  2007-06-07 06:50:40  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.20  2007/05/29 15:46:37  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.19  2007/05/28 15:36:42  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.18  2007/05/17 09:32:06  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.17  2007/05/09 16:07:26  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.16  2007/05/09 11:05:28  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.15  2007/05/08 08:47:40  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.14  2007/04/26 11:41:00  jaume
68
 * attempting to let defining size in world units
69
 *
70
 * Revision 1.13  2007/04/20 07:11:11  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.12  2007/04/19 16:01:27  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.11  2007/04/19 14:21:30  jaume
77
 * *** empty log message ***
78
 *
79
 * Revision 1.10  2007/03/26 14:24:13  jaume
80
 * implemented Print
81
 *
82
 * Revision 1.9  2007/03/21 11:37:00  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.8  2007/03/21 11:02:17  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.7  2007/03/09 11:20:56  jaume
89
 * Advanced symbology (start committing)
90
 *
91
 * Revision 1.5.2.8  2007/02/21 07:34:09  jaume
92
 * labeling starts working
93
 *
94
 * Revision 1.5.2.7  2007/02/16 10:54:12  jaume
95
 * multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
96
 *
97
 * Revision 1.5.2.6  2007/02/15 16:23:44  jaume
98
 * *** empty log message ***
99
 *
100
 * Revision 1.5.2.5  2007/02/14 09:58:37  jaume
101
 * *** empty log message ***
102
 *
103
 * Revision 1.5.2.4  2007/02/12 15:15:20  jaume
104
 * refactored interval legend and added graduated symbol legend
105
 *
106
 * Revision 1.5.2.3  2007/02/09 07:47:04  jaume
107
 * Isymbol moved
108
 *
109
 * Revision 1.5.2.2  2007/02/05 14:59:04  jaume
110
 * *** empty log message ***
111
 *
112
 * Revision 1.5.2.1  2007/01/30 18:10:45  jaume
113
 * start commiting labeling stuff
114
 *
115
 * Revision 1.5  2007/01/25 16:25:23  jaume
116
 * *** empty log message ***
117
 *
118
 * Revision 1.4  2007/01/24 17:58:22  jaume
119
 * new features and architecture error fixes
120
 *
121
 * Revision 1.3  2007/01/16 11:50:44  jaume
122
 * *** empty log message ***
123
 *
124
 * Revision 1.2  2007/01/10 16:39:41  jaume
125
 * ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
126
 *
127
 * Revision 1.1  2007/01/10 16:31:36  jaume
128
 * *** empty log message ***
129
 *
130
 * Revision 1.6  2006/12/04 17:13:39  fjp
131
 * *** empty log message ***
132
 *
133
 * Revision 1.5  2006/11/14 11:10:27  jaume
134
 * *** empty log message ***
135
 *
136
 * Revision 1.4  2006/11/09 18:39:05  jaume
137
 * *** empty log message ***
138
 *
139
 * Revision 1.3  2006/11/08 10:56:47  jaume
140
 * *** empty log message ***
141
 *
142
 * Revision 1.2  2006/11/06 17:08:45  jaume
143
 * *** empty log message ***
144
 *
145
 * Revision 1.1  2006/10/31 16:16:34  jaume
146
 * *** empty log message ***
147
 *
148
 * Revision 1.4  2006/10/30 19:30:35  jaume
149
 * *** empty log message ***
150
 *
151
 * Revision 1.3  2006/10/29 23:53:49  jaume
152
 * *** empty log message ***
153
 *
154
 * Revision 1.2  2006/10/26 16:27:33  jaume
155
 * support for composite marker symbols (not tested)
156
 *
157
 * Revision 1.1  2006/10/25 10:50:41  jaume
158
 * movement of classes and gui stuff
159
 *
160
 * Revision 1.3  2006/10/24 19:54:16  jaume
161
 * added IPersistence
162
 *
163
 * Revision 1.2  2006/10/24 08:02:51  jaume
164
 * *** empty log message ***
165
 *
166
 * Revision 1.1  2006/10/18 07:54:06  jaume
167
 * *** empty log message ***
168
 *
169
 *
170
 */
171
package com.iver.cit.gvsig.fmap.core.symbols;
172

    
173
import java.awt.Color;
174
import java.awt.Font;
175
import java.awt.Graphics2D;
176
import java.awt.Point;
177
import java.awt.Rectangle;
178
import java.awt.RenderingHints;
179
import java.awt.Shape;
180
import java.awt.font.FontRenderContext;
181
import java.awt.font.GlyphVector;
182
import java.awt.geom.AffineTransform;
183
import java.awt.geom.Point2D;
184

    
185
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
186
import com.iver.cit.gvsig.fmap.core.FPoint2D;
187
import com.iver.cit.gvsig.fmap.core.FShape;
188
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
189
import com.iver.cit.gvsig.fmap.core.styles.IMask;
190
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
191
import com.iver.utiles.StringUtilities;
192
import com.iver.utiles.XMLEntity;
193

    
194

    
195
/**
196
 * Symbol that manages symbols from a TrueType font source
197
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
198
 */
199
public class CharacterMarkerSymbol extends CartographicMarkerSymbol {
200
        private Font font = new Font("Arial", Font.PLAIN, 20);
201
        private int unicode;
202
        private ISymbol selectionSymbol;
203

    
204
        /**
205
         * Creates a new instance of CharacterMarker with default values
206
         *
207
         */
208
        public CharacterMarkerSymbol() {
209
                super();
210
        }
211

    
212
        /**
213
         * Creates a new instance of CharacterMarker specifying the marker source
214
         * font, the character code corresponding to the symbol, and the color that
215
         * will be used in rendering time.
216
         *
217
         * @param font -
218
         *            src Font
219
         * @param charCode -
220
         *            character code of the symbol for this font
221
         * @param color -
222
         *            color to be used in when rendering.
223
         */
224
        public CharacterMarkerSymbol(Font font, int charCode, Color color) {
225
                super();
226
                this.font = font;
227
                unicode = charCode;
228
                setColor(color);
229
        }
230

    
231
        /**
232
         * @return
233
         * @uml.property  name="font"
234
         */
235
        public Font getFont() {
236
                return font;
237
        }
238

    
239
        /**
240
         * @param font
241
         * @uml.property  name="font"
242
         */
243
        public void setFont(Font font) {
244
                this.font = font;
245
        }
246

    
247
        public ISymbol getSymbolForSelection() {
248
                if (selectionSymbol == null) {
249
                        XMLEntity xml = getXMLEntity();
250
                        xml.putProperty("color", FSymbol.getSelectionColor());
251
                        selectionSymbol = SymbologyFactory.createSymbolFromXML(xml, getDescription() + " version for selection.");
252
                }
253
                return selectionSymbol;
254
        }
255

    
256
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
257
                g.setFont(getFont());
258
                g.setColor(getColor());
259
                RenderingHints old = g.getRenderingHints();
260
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
261
                double theta = getRotation();
262
                int xOffset = (int) getOffset().getX(); // * multiplicador d'unitats;
263
                int yOffset = (int) getOffset().getY(); // * multiplicador d'unitats
264
                Point2D p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
265
                                .getY());
266

    
267
                double size = getSize();
268

    
269

    
270
                g.translate((int) (p.getX() + xOffset), (int) (p.getY()  + yOffset));
271
                if (theta != 0)        g.rotate(theta);
272

    
273
                char[] text = new char[] { (char) unicode };
274

    
275
                IMask mask = getMask();
276
                if (mask != null) {
277
                        FontRenderContext frc = g.getFontRenderContext();
278

    
279
                        GlyphVector gv = font.createGlyphVector(frc, text );
280

    
281
                        Shape markerShape = gv.getOutline(0, 0);
282
                        mask.
283
                                 getFillSymbol().
284
                                         draw(g, null, mask.getHaloShape(markerShape));
285

    
286
                }
287
                g.drawChars(text, 0, text.length, - (int) (size*0.4), (int) (size*0.4));
288

    
289

    
290
                if (theta!=0) g.rotate(-theta);
291
                g.translate(-(int) (p.getX() + xOffset), - (int) (p.getY() + yOffset));
292
                g.setRenderingHints(old);
293
        }
294

    
295
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
296
                        Shape shp) {
297
                return 0;
298
        }
299

    
300
        public XMLEntity getXMLEntity() {
301
                XMLEntity xml = new XMLEntity();
302

    
303
                // the class name
304
                xml.putProperty("className", getClassName());
305

    
306
                // color
307
                xml.putProperty("color", StringUtilities.color2String(getColor()));
308

    
309
                // font
310
                xml.putProperty("font", font.getFontName());
311

    
312
                // font style
313
                xml.putProperty("fontStyle", font.getStyle());
314

    
315
                // marker size
316
                xml.putProperty("size", font.getSize());
317

    
318
                // symbol code
319
                xml.putProperty("symbolCode", unicode);
320

    
321
                // description
322
                xml.putProperty("desc", getDescription());
323

    
324
                // is shape visible
325
                xml.putProperty("isShapeVisible", isShapeVisible());
326

    
327
                // x offset
328
                xml.putProperty("xOffset", getOffset().getX());
329

    
330
                // y offset
331
                xml.putProperty("yOffset", getOffset().getY());
332

    
333
                // rotation
334
                xml.putProperty("rotation", getRotation());
335

    
336
                // measure unit
337
                xml.putProperty("unit", getUnit());
338

    
339
                // reference system
340
                xml.putProperty("referenceSystem", getReferenceSystem());
341

    
342
                return xml;
343
        }
344

    
345
        public int getSymbolType() {
346
                return FShape.POINT;
347
        }
348

    
349
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
350
                draw(g, scaleInstance, new FPoint2D(r.getCenterX(), r.getCenterY()));
351
        }
352

    
353
        public void setUnicode(int symbol) {
354
                this.unicode = symbol;
355
        }
356

    
357
        public int getUnicode() {
358
                return unicode;
359
        }
360

    
361
        public String getClassName() {
362
                return this.getClass().getName();
363
        }
364

    
365
        public void setXMLEntity(XMLEntity xml) {
366
                setColor(StringUtilities.string2Color(xml.getStringProperty("color")));
367
                Point p = new Point();
368
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
369

    
370
                setDescription(xml.getStringProperty("desc"));
371
                setUnit(xml.getIntProperty("unit"));
372
                font = new Font(xml.getStringProperty("font"),
373
                                xml.getIntProperty("fontStyle"),
374
                                (int) xml.getFloatProperty("size"));
375
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
376
                unicode = xml.getIntProperty("symbolCode");
377
                setOffset(p);
378
                setRotation(xml.getDoubleProperty("rotation"));
379

    
380
        }
381

    
382
        public void print(Graphics2D g, AffineTransform at, FShape shape) throws ReadDriverException {
383
                // TODO Implement it
384
                throw new Error("Not yet implemented!");
385

    
386
        }
387

    
388
        public double getSize() {
389
                return font.getSize();
390
        }
391

    
392
        public void setSize(double size) {
393
                if (size == 0) {
394
                        try {
395
                                Double.parseDouble("setSize com size="+size);
396
                        }catch(Exception e) {
397
                                e.printStackTrace();
398
                        }
399
                }
400
                font = new Font(font.getName(), font.getStyle(), (int) size);
401
        }
402

    
403
}