Statistics
| Revision:

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

History | View | Annotate | Download (8.45 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 9641 2007-01-10 16:42:08Z jaume $
45
 * $Log$
46
 * Revision 1.2  2007-01-10 16:39:41  jaume
47
 * ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
48
 *
49
 * Revision 1.1  2007/01/10 16:31:36  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.6  2006/12/04 17:13:39  fjp
53
 * *** empty log message ***
54
 *
55
 * Revision 1.5  2006/11/14 11:10:27  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.4  2006/11/09 18:39:05  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.3  2006/11/08 10:56:47  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.2  2006/11/06 17:08:45  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.1  2006/10/31 16:16:34  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.4  2006/10/30 19:30:35  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.3  2006/10/29 23:53:49  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.2  2006/10/26 16:27:33  jaume
77
 * support for composite marker symbols (not tested)
78
 *
79
 * Revision 1.1  2006/10/25 10:50:41  jaume
80
 * movement of classes and gui stuff
81
 *
82
 * Revision 1.3  2006/10/24 19:54:16  jaume
83
 * added IPersistence
84
 *
85
 * Revision 1.2  2006/10/24 08:02:51  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.1  2006/10/18 07:54:06  jaume
89
 * *** empty log message ***
90
 *
91
 *
92
 */
93
package com.iver.cit.gvsig.fmap.core.symbols;
94

    
95
import java.awt.Color;
96
import java.awt.Font;
97
import java.awt.Graphics2D;
98
import java.awt.Point;
99
import java.awt.Rectangle;
100
import java.awt.Shape;
101
import java.awt.geom.AffineTransform;
102
import java.awt.geom.Point2D;
103

    
104
import javax.print.attribute.PrintRequestAttributeSet;
105

    
106
import org.apache.batik.ext.awt.geom.PathLength;
107

    
108
import com.iver.cit.gvsig.fmap.DriverException;
109
import com.iver.cit.gvsig.fmap.core.FPoint2D;
110
import com.iver.cit.gvsig.fmap.core.FShape;
111
import com.iver.cit.gvsig.fmap.core.SymbolFactory;
112
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
113
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
114
import com.iver.utiles.StringUtilities;
115
import com.iver.utiles.XMLEntity;
116
import com.vividsolutions.jts.geom.Geometry;
117

    
118

    
119
// TODO quitar batik-util.jar del classpath de extGraph cuando movamos esto a FMap
120
/**
121
 * Symbol that manages symbols from a TrueType font source
122
 * @author jaume dominguez faus - jaume.dominguez@iver.es
123
 */
124
public class CharacterMarkerSymbol extends AbstractMarkerSymbol {
125
        private Font font = new Font("Arial", Font.PLAIN, 20);
126
        private int symbol;
127
        private ISymbol selectionSymbol;
128

    
129
        /**
130
         * Creates a new instance of CharacterMarker with default values
131
         *
132
         */
133
        public CharacterMarkerSymbol() {
134
                super();
135
        }
136

    
137
        /**
138
         * Creates a new instance of CharacterMarker specifying the marker source
139
         * font, the character code corresponding to the symbol, and the color that
140
         * will be used in rendering time.
141
         *
142
         * @param font -
143
         *            src Font
144
         * @param charCode -
145
         *            character code of the symbol for this font
146
         * @param color -
147
         *            color to be used in when rendering.
148
         */
149
        public CharacterMarkerSymbol(Font font, int charCode, Color color) {
150
                super();
151
                this.font = font;
152
                symbol = charCode;
153
                this.color = color;
154
        }
155

    
156
        public Font getFont() {
157
                return font;
158
        }
159

    
160
        public void setFont(Font font) {
161
                this.font = font;
162
        }
163

    
164
        public ISymbol getSymbolForSelection() {
165
                if (selectionSymbol == null) {
166
                        XMLEntity xml = getXMLEntity();
167
                        xml.putProperty("color", StringUtilities.color2String(Color.YELLOW));
168
                        selectionSymbol = SymbolFactory.createFromXML(xml, getDescription() + " version for selection.");
169
                }
170
                return selectionSymbol;
171
        }
172

    
173
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
174
                g.setFont(getFont());
175
                g.setColor(color);
176
                g.rotate(getRotation());
177
                Point2D p = null;
178
                switch (shp.getShapeType()) {
179
                case FShape.POINT:
180
                        p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
181
                                        .getY());
182
                        break;
183
                case FShape.LINE:
184
                        PathLength pathLen = new PathLength(shp);
185
                        float midDistance = pathLen.lengthOfPath() / 2;
186
                        p = pathLen.pointAtLength(midDistance);
187
                        break;
188
                case FShape.POLYGON:
189
                        Geometry geom = FConverter.java2d_to_jts(shp);
190
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
191
                        p = new Point2D.Double(centroid.getX(), centroid.getY());
192
                }
193
                // p = affineTransform.transform(p, null);
194

    
195
                char[] text = new char[] { (char) symbol };
196
                g.drawChars(text, 0, text.length, (int) p.getX(), (int) p.getY());
197

    
198
                if (subSymbols!=null) {
199
                        for (int i = 0; i < subSymbols.length; i++) {
200
                                subSymbols[i].draw(g, affineTransform, shp);
201
                        }
202
                }
203

    
204
        }
205

    
206
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
207
                        Shape shp) {
208
                // TODO Auto-generated method stub
209
                throw new Error("Not yet implemented!");
210
        }
211

    
212
        public XMLEntity getXMLEntity() {
213
                XMLEntity xml = new XMLEntity();
214

    
215
                // the class name
216
                xml.putProperty("className", getClassName());
217

    
218
                // color
219
                xml.putProperty("color", StringUtilities.color2String(color));
220

    
221
                // font
222
                xml.putProperty("font", font.getFontName());
223

    
224
                // font style
225
                xml.putProperty("fontStyle", font.getStyle());
226

    
227
                // symbol code
228
                xml.putProperty("symbolCode", symbol);
229

    
230
                // description
231
                xml.putProperty("desc", getDescription());
232

    
233
                // is shape visible
234
                xml.putProperty("isShapeVisible", isShapeVisible());
235

    
236
                // x offset
237
                xml.putProperty("xOffset", getOffset().getX());
238

    
239
                // y offset
240
                xml.putProperty("yOffset", getOffset().getY());
241

    
242
                // rotation
243
                xml.putProperty("rotation", rotation);
244
                return xml;
245
        }
246

    
247
        public int getSymbolType() {
248
                return CHARACTER_MARKER;
249
        }
250

    
251
        public void drawInsideRectangle(Graphics2D g,
252
                        AffineTransform scaleInstance, Rectangle r) {
253
                g.rotate(getRotation());
254
                // Sirve de algo el scaleInstance???
255
                // g.setFont(font.deriveFont(scaleInstance));
256
                double h = r.getHeight();
257
                Font myFont = font.deriveFont(
258
                                (float) (h * FConstant.FONT_HEIGHT_SCALE_FACTOR)).deriveFont(
259
                                                scaleInstance);
260
                g.setFont(myFont);
261
                g.setColor(color);
262
                r.y = (int) h;
263

    
264
                // center character in case the rectangle is not square
265
                int x = r.x;
266
                int y = r.y;
267
                int width = (int) r.getHeight();
268
                int height = (int) r.getWidth();
269
                int diff = (width-height);
270
                if (diff<0) {
271
                        x -= diff /2;
272
                } else {
273
                        y += diff /2;
274
                }
275

    
276
                // and apply the offset
277
                x = x + (int) offset.getX(); // TODO scale offset
278
                y = y + (int) offset.getY(); // TODO scale offset
279
                char[] text = new char[] { (char) symbol };
280
                g.drawChars(text, 0, text.length, x, y);
281

    
282
                // finally, draw the subsymbols if any
283
                if (subSymbols!=null) {
284
                        for (int i = 0; i < subSymbols.length; i++) {
285
                                subSymbols[i].drawInsideRectangle(g, scaleInstance, r);
286
                        }
287
                }
288
        }
289

    
290
        public void setCharacter(int symbol) {
291
                this.symbol = symbol;
292
        }
293

    
294
        public String getClassName() {
295
                return this.getClass().getName();
296
        }
297

    
298
        public void setXMLEntity(XMLEntity xml) {
299
                color = StringUtilities.string2Color(xml.getStringProperty("color"));
300
                Point p = new Point();
301
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
302

    
303
                setDescription(xml.getStringProperty("desc"));
304
                font = new Font(xml.getStringProperty("font"),
305
                                xml.getIntProperty("fontStyle"),
306
                                100 /* or whatever*/);
307
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
308
                symbol = xml.getIntProperty("symbolCode");
309
                offset = p;
310
                rotation = xml.getDoubleProperty("rotation");
311

    
312
        }
313

    
314
        public void print(Graphics2D g, AffineTransform at, FShape shape) throws DriverException {
315
                // TODO Implement it
316
                throw new Error("Not yet implemented!");
317

    
318
        }
319

    
320
}