Statistics
| Revision:

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

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

    
155
import java.awt.Color;
156
import java.awt.Font;
157
import java.awt.Graphics2D;
158
import java.awt.Point;
159
import java.awt.Rectangle;
160
import java.awt.RenderingHints;
161
import java.awt.Shape;
162
import java.awt.geom.AffineTransform;
163
import java.awt.geom.Point2D;
164

    
165
import org.apache.batik.ext.awt.geom.PathLength;
166

    
167
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
168
import com.iver.cit.gvsig.fmap.core.FPoint2D;
169
import com.iver.cit.gvsig.fmap.core.FShape;
170
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
171
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
172
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
173
import com.iver.utiles.StringUtilities;
174
import com.iver.utiles.XMLEntity;
175
import com.vividsolutions.jts.geom.Geometry;
176

    
177

    
178
/**
179
 * Symbol that manages symbols from a TrueType font source
180
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
181
 */
182
public class CharacterMarkerSymbol extends CartographicMarkerSymbol {
183
        private Font font = new Font("Arial", Font.PLAIN, 20);
184
        private int unicode;
185
        private ISymbol selectionSymbol;
186

    
187
        /**
188
         * Creates a new instance of CharacterMarker with default values
189
         *
190
         */
191
        public CharacterMarkerSymbol() {
192
                super();
193
        }
194

    
195
        /**
196
         * Creates a new instance of CharacterMarker specifying the marker source
197
         * font, the character code corresponding to the symbol, and the color that
198
         * will be used in rendering time.
199
         *
200
         * @param font -
201
         *            src Font
202
         * @param charCode -
203
         *            character code of the symbol for this font
204
         * @param color -
205
         *            color to be used in when rendering.
206
         */
207
        public CharacterMarkerSymbol(Font font, int charCode, Color color) {
208
                super();
209
                this.font = font;
210
                unicode = charCode;
211
                setColor(color);
212
        }
213

    
214
        /**
215
         * @return
216
         * @uml.property  name="font"
217
         */
218
        public Font getFont() {
219
                return font;
220
        }
221

    
222
        /**
223
         * @param font
224
         * @uml.property  name="font"
225
         */
226
        public void setFont(Font font) {
227
                this.font = font;
228
        }
229

    
230
        public ISymbol getSymbolForSelection() {
231
                if (selectionSymbol == null) {
232
                        XMLEntity xml = getXMLEntity();
233
                        xml.putProperty("color", FSymbol.getSelectionColor());
234
                        selectionSymbol = SymbologyFactory.createSymbolFromXML(xml, getDescription() + " version for selection.");
235
                }
236
                return selectionSymbol;
237
        }
238

    
239
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
240
                double unitFactor = getScaleX();
241
//                if (unitFactor > 0) {
242
//                        // disctance units
243
//                        System.err.println("affine scale = "+affineTransform.getScaleX()+", unitFactor="+unitFactor+", FONT_HEIGHT_SCALE"+FConstant.FONT_HEIGHT_SCALE_FACTOR+"::"+affineTransform.getScaleX()*unitFactor*FConstant.FONT_HEIGHT_SCALE_FACTOR);
244
//                        g.setFont(getFont().deriveFont((float)
245
//                                        (affineTransform.getScaleX()*unitFactor*FConstant.FONT_HEIGHT_SCALE_FACTOR)));
246
//                } else {
247
                        g.setFont(getFont());
248
//                }
249

    
250
                g.setColor(getColor());
251
                double theta = getRotation();
252
                int xOffset = (int) getOffset().getX(); // * multiplicador d'unitats;
253
                int yOffset = (int) getOffset().getY(); // * multiplicador d'unitats
254
//                Point2D p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
255
//                                .getY());
256

    
257

    
258

    
259
                 Point2D p = null;
260
                switch (shp.getShapeType()) {
261
                case FShape.POINT:
262
                p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
263
                                .getY());
264
                break;
265
                case FShape.LINE:
266
                final PathLength pathLen = new PathLength(shp);
267
                final float midDistance = pathLen.lengthOfPath() / 2;
268
                p = pathLen.pointAtLength(midDistance);
269
                break;
270
                case FShape.POLYGON:
271
                Geometry geom = FConverter.java2d_to_jts(shp);
272
                com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
273
                p = new Point2D.Double(centroid.getX(), centroid.getY());
274
                }
275

    
276
                g.translate((int) (p.getX() + xOffset), (int) (p.getY() + yOffset));
277
                if (theta != 0)        g.rotate(theta);
278

    
279
                char[] text = new char[] { (char) unicode };
280
                g.drawChars(text, 0, text.length, 0, 0);
281

    
282
                if (theta!=0) g.rotate(-theta);
283
                g.translate(-(int) (p.getX() + xOffset), - (int) (p.getY() + yOffset));
284

    
285
        }
286

    
287
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
288
                        Shape shp) {
289
                return 0;
290
        }
291

    
292
        public XMLEntity getXMLEntity() {
293
                XMLEntity xml = new XMLEntity();
294

    
295
                // the class name
296
                xml.putProperty("className", getClassName());
297

    
298
                // color
299
                xml.putProperty("color", StringUtilities.color2String(getColor()));
300

    
301
                // font
302
                xml.putProperty("font", font.getFontName());
303

    
304
                // font style
305
                xml.putProperty("fontStyle", font.getStyle());
306

    
307
                // marker size
308
                xml.putProperty("size", font.getSize());
309

    
310
                // symbol code
311
                xml.putProperty("symbolCode", unicode);
312

    
313
                // description
314
                xml.putProperty("desc", getDescription());
315

    
316
                // is shape visible
317
                xml.putProperty("isShapeVisible", isShapeVisible());
318

    
319
                // x offset
320
                xml.putProperty("xOffset", getOffset().getX());
321

    
322
                // y offset
323
                xml.putProperty("yOffset", getOffset().getY());
324

    
325
                // rotation
326
                xml.putProperty("rotation", getRotation());
327

    
328
                // measure unit
329
                xml.putProperty("unit", getUnit());
330

    
331
                return xml;
332
        }
333

    
334
        public int getSymbolType() {
335
                return FShape.POINT;
336
        }
337

    
338
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
339
                g.setFont(getFont());
340
                g.setColor(getColor());
341

    
342
                int xIni = (int) (r.width*0.5);
343
                int yIni = (int) (r.height*0.5);
344

    
345
                g.translate(xIni, yIni);
346
                g.rotate(getRotation());
347

    
348
                // Sirve de algo el scaleInstance???
349
                RenderingHints old = g.getRenderingHints();
350
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
351
                char[] text = new char[] { (char) unicode };
352
                g.drawChars(text, 0, text.length, (int) getOffset().getX(), (int) getOffset().getY());
353
                g.setRenderingHints(old);
354

    
355
                g.rotate(-getRotation());
356
                g.translate(-xIni, -yIni);
357

    
358
        }
359

    
360
        public void setUnicode(int symbol) {
361
                this.unicode = symbol;
362
        }
363

    
364
        public int getUnicode() {
365
                return unicode;
366
        }
367

    
368
        public String getClassName() {
369
                return this.getClass().getName();
370
        }
371

    
372
        public void setXMLEntity(XMLEntity xml) {
373
                setColor(StringUtilities.string2Color(xml.getStringProperty("color")));
374
                Point p = new Point();
375
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
376

    
377
                setDescription(xml.getStringProperty("desc"));
378
                setUnit(xml.getIntProperty("unit"));
379
                font = new Font(xml.getStringProperty("font"),
380
                                xml.getIntProperty("fontStyle"),
381
                                (int) xml.getFloatProperty("size"));
382
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
383
                unicode = xml.getIntProperty("symbolCode");
384
                setOffset(p);
385
                setRotation(xml.getDoubleProperty("rotation"));
386

    
387
        }
388

    
389
        public void print(Graphics2D g, AffineTransform at, FShape shape) throws ReadDriverException {
390
                // TODO Implement it
391
                throw new Error("Not yet implemented!");
392

    
393
        }
394

    
395
        public double getSize() {
396
                return font.getSize();
397
        }
398

    
399
        public void setSize(double size) {
400
                font = new Font(font.getName(), font.getStyle(), (int) size);
401
        }
402

    
403
}