Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / fmap / core / symbols / CharacterMarker.java @ 8432

History | View | Annotate | Download (7.21 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: CharacterMarker.java 8432 2006-10-30 19:30:35Z jaume $
45
 * $Log$
46
 * Revision 1.4  2006-10-30 19:30:35  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.3  2006/10/29 23:53:49  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.2  2006/10/26 16:27:33  jaume
53
 * support for composite marker symbols (not tested)
54
 *
55
 * Revision 1.1  2006/10/25 10:50:41  jaume
56
 * movement of classes and gui stuff
57
 *
58
 * Revision 1.3  2006/10/24 19:54:16  jaume
59
 * added IPersistence
60
 *
61
 * Revision 1.2  2006/10/24 08:02:51  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.1  2006/10/18 07:54:06  jaume
65
 * *** empty log message ***
66
 *
67
 *
68
 */
69
package com.iver.cit.gvsig.fmap.core.symbols;
70

    
71
import java.awt.Color;
72
import java.awt.Font;
73
import java.awt.Graphics2D;
74
import java.awt.Point;
75
import java.awt.Rectangle;
76
import java.awt.Shape;
77
import java.awt.geom.AffineTransform;
78
import java.awt.geom.Point2D;
79

    
80
import org.apache.batik.ext.awt.geom.PathLength;
81

    
82
import com.iver.cit.gvsig.fmap.core.FPoint2D;
83
import com.iver.cit.gvsig.fmap.core.FShape;
84
import com.iver.cit.gvsig.fmap.core.ISymbol;
85
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
86
import com.iver.utiles.StringUtilities;
87
import com.iver.utiles.XMLEntity;
88

    
89

    
90
// TODO quitar batik-util.jar del classpath de extGraph cuando movamos esto a FMap
91
/**
92
 * Symbol that manages symbols from a TrueType font source
93
 * @author jaume dominguez faus - jaume.dominguez@iver.es
94
 */
95
public class CharacterMarker extends AbstractMarker {
96
        private Font font;
97
        private Color color = Color.BLACK;
98
        private int symbol;
99

    
100
        /**
101
         * Creates a new instance of CharacterMarker with default values
102
         *
103
         */
104
        public CharacterMarker() {
105
                super();
106
        }
107

    
108
        /**
109
         * Creates a new instance of CharacterMarker specifying the marker source
110
         * font, the character code corresponding to the symbol, and the color that
111
         * will be used in rendering time.
112
         *
113
         * @param font -
114
         *            src Font
115
         * @param charCode -
116
         *            character code of the symbol for this font
117
         * @param color -
118
         *            color to be used in when rendering.
119
         */
120
        public CharacterMarker(Font font, int charCode, Color color) {
121
                super();
122
                this.font = font;
123
                symbol = charCode;
124
                this.color = color;
125
        }
126

    
127
        public Font getFont() {
128
                if (font == null) {
129
                        font = new Font("Arial", Font.PLAIN, 20);
130
                }
131
                return font;
132
        }
133

    
134
        public void setFont(Font font) {
135
                this.font = font;
136
        }
137

    
138
        public ISymbol getSymbolForSelection() {
139
                // TODO Auto-generated method stub
140
                throw new Error("Not yet implemented!");
141
        }
142

    
143
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
144
                g.setFont(getFont());
145
                g.setColor(color);
146
                g.rotate(getRotation());
147
                Point2D p = null;
148
                switch (shp.getShapeType()) {
149
                case FShape.POINT:
150
                        p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
151
                                        .getY());
152
                        // p = affineTransform.transform(p, null);
153
                        break;
154
                case FShape.LINE:
155
                        PathLength pathLen = new PathLength(shp);
156
                        float midDistance = pathLen.lengthOfPath() / 2;
157
                        p = pathLen.pointAtLength(midDistance);
158
                        // p = affineTransform.transform(p, null);
159
                        break;
160
                case FShape.POLYGON:
161
                        /*Geometry geom = FConverter.java2d_to_jts(shp);
162
                        Point centroid = geom.getCentroid();
163
                        p = new Point2D.Double(centroid.getX(), centroid.getY());
164
                        */
165
                }
166

    
167
                char[] text = new char[] { (char) symbol };
168
                g.drawChars(text, 0, text.length, (int) p.getX(), (int) p.getY());
169
        }
170

    
171
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
172
                        Shape shp) {
173
                // TODO Auto-generated method stub
174
                throw new Error("Not yet implemented!");
175
        }
176

    
177
        public int getOnePointRgb() {
178
                return color.getRGB();
179
        }
180

    
181
        public XMLEntity getXMLEntity() {
182
                XMLEntity xml = new XMLEntity();
183

    
184
                // the class name
185
                xml.putProperty("className", getClassName());
186

    
187
                // color
188
                xml.putProperty("color", StringUtilities.color2String(color));
189

    
190
                // font
191
                xml.putProperty("font", font.getFontName());
192

    
193
                // font style
194
                xml.putProperty("fontStyle", font.getStyle());
195

    
196
                // symbol code
197
                xml.putProperty("symbolCode", symbol);
198

    
199
                // description
200
                xml.putProperty("desc", desc);
201

    
202
                // is shape visible
203
                xml.putProperty("isShapeVisible", isShapeVisible);
204

    
205
                // x offset
206
                xml.putProperty("xOffset", getOffset().getX());
207

    
208
                // y offset
209
                xml.putProperty("yOffset", getOffset().getY());
210

    
211
                // rotation
212
                xml.putProperty("rotation", rotation);
213
                return xml;
214
        }
215

    
216
        public int getSymbolType() {
217
                return CHARACTER_MARKER;
218
        }
219

    
220
        public void drawInsideRectangle(Graphics2D g,
221
                        AffineTransform scaleInstance, Rectangle r) {
222
                g.rotate(getRotation());
223
                // Sirve de algo el scaleInstance???
224
                // g.setFont(font.deriveFont(scaleInstance));
225
                double h = r.getHeight();
226
                Font myFont = font.deriveFont(
227
                                (float) (h * FConstant.FONT_HEIGHT_SCALE_FACTOR)).deriveFont(
228
                                                scaleInstance);
229
                g.setFont(myFont);
230
                g.setColor(color);
231
                r.y = (int) h;
232

    
233
                // center character in case the rectangle is not square
234
                int x = r.x;
235
                int y = r.y;
236
                int width = (int) r.getHeight();
237
                int height = (int) r.getWidth();
238
                int diff = (width-height);
239
                if (diff<0) {
240
                        x -= diff /2;
241
                } else {
242
                        y += diff /2;
243
                }
244

    
245
                // and apply the offset
246
                x = x + (int) offset.getX(); // TODO scale offset
247
                y = y + (int) offset.getY(); // TODO scale offset
248
                char[] text = new char[] { (char) symbol };
249
                g.drawChars(text, 0, text.length, x, y);
250

    
251
                // finally, draw the subsymbols if any
252
                if (subSymbols!=null) {
253
                        for (int i = 0; i < subSymbols.length; i++) {
254
                                subSymbols[i].drawInsideRectangle(g, scaleInstance, r);
255
                        }
256
                }
257
        }
258

    
259
        public void setCharacter(int symbol) {
260
                this.symbol = symbol;
261
        }
262

    
263
        public String getClassName() {
264
                return this.getClass().getName();
265
        }
266

    
267
        public void setXMLEntity(XMLEntity xml) {
268
                color = StringUtilities.string2Color(xml.getStringProperty("color"));
269
                Point p = new Point();
270
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
271

    
272
                desc = xml.getStringProperty("desc");
273
                font = new Font(xml.getStringProperty("font"),
274
                                xml.getIntProperty("fontStyle"),
275
                                100 /* or whatever*/);
276
                isShapeVisible = xml.getBooleanProperty("isShapeVisible");
277
                symbol = xml.getIntProperty("symbolCode");
278
                offset = p;
279
                rotation = xml.getDoubleProperty("rotation");
280

    
281
        }
282
}