Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / CharacterMarkerSymbol.java @ 10679

History | View | Annotate | Download (9.76 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 10679 2007-03-09 11:25:01Z jaume $
45
 * $Log$
46
 * Revision 1.7  2007-03-09 11:20:56  jaume
47
 * Advanced symbology (start committing)
48
 *
49
 * Revision 1.5.2.8  2007/02/21 07:34:09  jaume
50
 * labeling starts working
51
 *
52
 * Revision 1.5.2.7  2007/02/16 10:54:12  jaume
53
 * multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
54
 *
55
 * Revision 1.5.2.6  2007/02/15 16:23:44  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.5.2.5  2007/02/14 09:58:37  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.5.2.4  2007/02/12 15:15:20  jaume
62
 * refactored interval legend and added graduated symbol legend
63
 *
64
 * Revision 1.5.2.3  2007/02/09 07:47:04  jaume
65
 * Isymbol moved
66
 *
67
 * Revision 1.5.2.2  2007/02/05 14:59:04  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.5.2.1  2007/01/30 18:10:45  jaume
71
 * start commiting labeling stuff
72
 *
73
 * Revision 1.5  2007/01/25 16:25:23  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.4  2007/01/24 17:58:22  jaume
77
 * new features and architecture error fixes
78
 *
79
 * Revision 1.3  2007/01/16 11:50:44  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.2  2007/01/10 16:39:41  jaume
83
 * ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
84
 *
85
 * Revision 1.1  2007/01/10 16:31:36  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.6  2006/12/04 17:13:39  fjp
89
 * *** empty log message ***
90
 *
91
 * Revision 1.5  2006/11/14 11:10:27  jaume
92
 * *** empty log message ***
93
 *
94
 * Revision 1.4  2006/11/09 18:39:05  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.3  2006/11/08 10:56:47  jaume
98
 * *** empty log message ***
99
 *
100
 * Revision 1.2  2006/11/06 17:08:45  jaume
101
 * *** empty log message ***
102
 *
103
 * Revision 1.1  2006/10/31 16:16:34  jaume
104
 * *** empty log message ***
105
 *
106
 * Revision 1.4  2006/10/30 19:30:35  jaume
107
 * *** empty log message ***
108
 *
109
 * Revision 1.3  2006/10/29 23:53:49  jaume
110
 * *** empty log message ***
111
 *
112
 * Revision 1.2  2006/10/26 16:27:33  jaume
113
 * support for composite marker symbols (not tested)
114
 *
115
 * Revision 1.1  2006/10/25 10:50:41  jaume
116
 * movement of classes and gui stuff
117
 *
118
 * Revision 1.3  2006/10/24 19:54:16  jaume
119
 * added IPersistence
120
 *
121
 * Revision 1.2  2006/10/24 08:02:51  jaume
122
 * *** empty log message ***
123
 *
124
 * Revision 1.1  2006/10/18 07:54:06  jaume
125
 * *** empty log message ***
126
 *
127
 *
128
 */
129
package com.iver.cit.gvsig.fmap.core.symbols;
130

    
131
import java.awt.Color;
132
import java.awt.Font;
133
import java.awt.FontMetrics;
134
import java.awt.Graphics2D;
135
import java.awt.Point;
136
import java.awt.Rectangle;
137
import java.awt.RenderingHints;
138
import java.awt.Shape;
139
import java.awt.geom.AffineTransform;
140
import java.awt.geom.Point2D;
141

    
142
import org.apache.batik.ext.awt.geom.PathLength;
143

    
144
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
145
import com.iver.cit.gvsig.fmap.core.FPoint2D;
146
import com.iver.cit.gvsig.fmap.core.FShape;
147
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
148
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
149
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
150
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
151
import com.iver.utiles.StringUtilities;
152
import com.iver.utiles.XMLEntity;
153
import com.vividsolutions.jts.geom.Geometry;
154

    
155

    
156
/**
157
 * Symbol that manages symbols from a TrueType font source
158
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
159
 */
160
public class CharacterMarkerSymbol extends CartographicMarkerSymbol {
161
        /**
162
         * @uml.property  name="font"
163
         */
164
        private Font font = new Font("Arial", Font.PLAIN, 20);
165
        private int symbol;
166
        private ISymbol selectionSymbol;
167

    
168
        /**
169
         * Creates a new instance of CharacterMarker with default values
170
         *
171
         */
172
        public CharacterMarkerSymbol() {
173
                super();
174
        }
175

    
176
        /**
177
         * Creates a new instance of CharacterMarker specifying the marker source
178
         * font, the character code corresponding to the symbol, and the color that
179
         * will be used in rendering time.
180
         *
181
         * @param font -
182
         *            src Font
183
         * @param charCode -
184
         *            character code of the symbol for this font
185
         * @param color -
186
         *            color to be used in when rendering.
187
         */
188
        public CharacterMarkerSymbol(Font font, int charCode, Color color) {
189
                super();
190
                this.font = font;
191
                symbol = charCode;
192
                setColor(color);
193
        }
194

    
195
        /**
196
         * @return
197
         * @uml.property  name="font"
198
         */
199
        public Font getFont() {
200
                return font;
201
        }
202

    
203
        /**
204
         * @param font
205
         * @uml.property  name="font"
206
         */
207
        public void setFont(Font font) {
208
                this.font = font;
209
        }
210

    
211
        public ISymbol getSymbolForSelection() {
212
                if (selectionSymbol == null) {
213
                        XMLEntity xml = getXMLEntity();
214
                        xml.putProperty("color", FSymbol.getSelectionColor());
215
                        selectionSymbol = SymbologyFactory.createSymbolFromXML(xml, getDescription() + " version for selection.");
216
                }
217
                return selectionSymbol;
218
        }
219

    
220
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
221
                g.setFont(getFont()/*.deriveFont((float) getSize())*/);
222
                g.setColor(getColor());
223
                g.rotate(getRotation());
224
                Point2D p = null;
225
                switch (shp.getShapeType()) {
226
                case FShape.POINT:
227
                        p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
228
                                        .getY());
229
                        break;
230
                case FShape.LINE:
231
                        final PathLength pathLen = new PathLength(shp);
232
                        final float midDistance = pathLen.lengthOfPath() / 2;
233
                        p = pathLen.pointAtLength(midDistance);
234
                        break;
235
                case FShape.POLYGON:
236
                        Geometry geom = FConverter.java2d_to_jts(shp);
237
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
238
                        p = new Point2D.Double(centroid.getX(), centroid.getY());
239
                }
240
                // p = affineTransform.transform(p, null);
241

    
242
                char[] text = new char[] { (char) symbol };
243
                g.drawChars(text, 0, text.length, (int) p.getX(), (int) p.getY());
244
        }
245

    
246
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
247
                        Shape shp) {
248
                return 0;
249
        }
250

    
251
        public XMLEntity getXMLEntity() {
252
                XMLEntity xml = new XMLEntity();
253

    
254
                // the class name
255
                xml.putProperty("className", getClassName());
256

    
257
                // color
258
                xml.putProperty("color", StringUtilities.color2String(getColor()));
259

    
260
                // font
261
                xml.putProperty("font", font.getFontName());
262

    
263
                // font style
264
                xml.putProperty("fontStyle", font.getStyle());
265

    
266
                // symbol code
267
                xml.putProperty("symbolCode", symbol);
268

    
269
                // description
270
                xml.putProperty("desc", getDescription());
271

    
272
                // is shape visible
273
                xml.putProperty("isShapeVisible", isShapeVisible());
274

    
275
                // x offset
276
                xml.putProperty("xOffset", getOffset().getX());
277

    
278
                // y offset
279
                xml.putProperty("yOffset", getOffset().getY());
280

    
281
                // rotation
282
                xml.putProperty("rotation", getRotation());
283
                return xml;
284
        }
285

    
286
        public int getSymbolType() {
287
                return FShape.POINT;
288
        }
289

    
290
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
291
                g.rotate(getRotation());
292
                // Sirve de algo el scaleInstance???
293
                RenderingHints old = g.getRenderingHints();
294
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
295
                double h = r.getHeight();
296
                Font myFont = font.deriveFont(
297
                                (float) (h * FConstant.FONT_HEIGHT_SCALE_FACTOR)).deriveFont(
298
                                                scaleInstance);
299
                g.setFont(myFont);
300
                g.setColor(getColor());
301
                r.y = (int) h;
302

    
303
                // center character in case the rectangle is not square
304
//                FontMetrics metrics = g.getFontMetrics(font);
305
//                String text = String.valueOf( (char) symbol );
306
//
307
//                int x = (r.width - metrics.stringWidth(text)) / 2;
308
//                int y = r.height / 2;
309
//
310
//                // and apply the offset
311
//                x = x + (int) offset.getX(); // TODO scale offset
312
//                y = y + (int) offset.getY(); // TODO scale offset
313
//                g.drawString(text, x, y);
314

    
315
                int x = r.x;
316
                int y = r.y;
317
                int width = (int) r.getHeight();
318
                int height = (int) r.getWidth();
319
                int diff = (width-height);
320
                if (diff<0) {
321
                        x -= diff /2;
322
                } else {
323
                        y += diff /2;
324
                }
325

    
326
                // and apply the offset
327
                x = x + (int) getOffset().getX(); // TODO scale offset
328
                y = y + (int) getOffset().getY(); // TODO scale offset
329
                char[] text = new char[] { (char) symbol };
330
                g.drawChars(text, 0, text.length, x, y);
331
                g.setRenderingHints(old);
332
        }
333

    
334
        public void setCharacter(int symbol) {
335
                this.symbol = symbol;
336
        }
337

    
338
        public String getClassName() {
339
                return this.getClass().getName();
340
        }
341

    
342
        public void setXMLEntity(XMLEntity xml) {
343
                setColor(StringUtilities.string2Color(xml.getStringProperty("color")));
344
                Point p = new Point();
345
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
346

    
347
                setDescription(xml.getStringProperty("desc"));
348
                font = new Font(xml.getStringProperty("font"),
349
                                xml.getIntProperty("fontStyle"),
350
                                100 /* or whatever*/);
351
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
352
                symbol = xml.getIntProperty("symbolCode");
353
                setOffset(p);
354
                setRotation(xml.getDoubleProperty("rotation"));
355

    
356
        }
357

    
358
        public void print(Graphics2D g, AffineTransform at, FShape shape) throws ReadDriverException {
359
                // TODO Implement it
360
                throw new Error("Not yet implemented!");
361

    
362
        }
363

    
364
}