Statistics
| Revision:

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

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

    
185
import java.awt.Color;
186
import java.awt.Font;
187
import java.awt.Graphics2D;
188
import java.awt.Point;
189
import java.awt.RenderingHints;
190
import java.awt.Shape;
191
import java.awt.font.FontRenderContext;
192
import java.awt.font.GlyphVector;
193
import java.awt.geom.AffineTransform;
194
import java.awt.geom.Point2D;
195
import java.awt.image.BufferedImage;
196

    
197
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
198
import com.iver.cit.gvsig.fmap.MapContext;
199
import com.iver.cit.gvsig.fmap.core.FPoint2D;
200
import com.iver.cit.gvsig.fmap.core.FShape;
201
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
202
import com.iver.cit.gvsig.fmap.core.styles.IMask;
203
import com.iver.utiles.StringUtilities;
204
import com.iver.utiles.XMLEntity;
205

    
206

    
207
/**
208
 * Symbol that manages symbols from a TrueType font source
209
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
210
 */
211
public class CharacterMarkerSymbol extends AbstractMarkerSymbol {
212
        private Font font = new Font("Arial", Font.PLAIN, 20);
213
        private int unicode;
214
        private ISymbol selectionSymbol;
215
        private VisualCorrection visualCorrection;
216

    
217
        /**
218
         * Creates a new instance of CharacterMarker with default values
219
         *
220
         */
221
        public CharacterMarkerSymbol() {
222
                super();
223
        }
224

    
225
        /**
226
         * Creates a new instance of CharacterMarker specifying the marker source
227
         * font, the character code corresponding to the symbol, and the color that
228
         * will be used in rendering time.
229
         *
230
         * @param font -
231
         *            src Font
232
         * @param charCode -
233
         *            character code of the symbol for this font
234
         * @param color -
235
         *            color to be used in when rendering.
236
         */
237
        public CharacterMarkerSymbol(Font font, int charCode, Color color) {
238
                super();
239
                this.font = font;
240
                unicode = charCode;
241
                setColor(color);
242
        }
243

    
244
        public Font getFont() {
245
                return font;
246
        }
247

    
248
        public void setFont(Font font) {
249
                this.font = font;
250
        }
251

    
252
        public ISymbol getSymbolForSelection() {
253
                if (selectionSymbol == null) {
254
                        XMLEntity xml = getXMLEntity();
255
                        xml.putProperty("color", StringUtilities.color2String(MapContext.getSelectionColor()));
256
                        selectionSymbol = SymbologyFactory.createSymbolFromXML(xml, getDescription() + " version for selection.");
257
                }
258
                return selectionSymbol;
259
        }
260

    
261
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
262
                g.setColor(getColor());
263
                RenderingHints old = g.getRenderingHints();
264
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
265
                double theta = getRotation();
266

    
267
                int xOffset = (int) getOffset().getX(); // * multiplicador d'unitats;
268
                int yOffset = (int) getOffset().getY(); // * multiplicador d'unitats
269

    
270
                double size = getSize();
271
                Point2D p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
272
                                .getY());
273

    
274
                if (isVisuallyCorrected()) {
275
                        size *= visualCorrection.sizeScale;
276
                        p.setLocation(p.getX() - xOffset*size*visualCorrection.xOffsetScale, p.getY() - yOffset*size*visualCorrection.yOffsetScale);
277

    
278
                }
279
                g.setFont(getFont().deriveFont((float)size));
280

    
281

    
282

    
283

    
284

    
285
                g.translate((int) (p.getX() + xOffset), (int) (p.getY()  + yOffset));
286
                if (theta != 0)        g.rotate(theta);
287

    
288
                char[] text = new char[] { (char) unicode };
289

    
290
                IMask mask = getMask();
291
                if (mask != null) {
292
                        FontRenderContext frc = g.getFontRenderContext();
293

    
294
                        GlyphVector gv = font.createGlyphVector(frc, text );
295

    
296
                        Shape markerShape = gv.getOutline(0, 0);
297
                        mask.
298
                                 getFillSymbol().
299
                                         draw(g, null, mask.getHaloShape(markerShape));
300

    
301
                }
302
                g.drawChars(text, 0, text.length, - (int) (size*0.4), (int) (size*0.4));
303

    
304

    
305
                if (theta!=0) g.rotate(-theta);
306
                g.translate(-(int) (p.getX() + xOffset), - (int) (p.getY() + yOffset));
307
                g.setRenderingHints(old);
308
        }
309

    
310
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
311
                        Shape shp) {
312
                return 0;
313
        }
314

    
315
        public XMLEntity getXMLEntity() {
316
                XMLEntity xml = new XMLEntity();
317

    
318
                // the class name
319
                xml.putProperty("className", getClassName());
320

    
321
                // color
322
                xml.putProperty("color", StringUtilities.color2String(getColor()));
323

    
324
                // font
325
                xml.putProperty("font", font.getFontName());
326

    
327
                // font style
328
                xml.putProperty("fontStyle", font.getStyle());
329

    
330
                // marker size
331
                xml.putProperty("size", font.getSize());
332

    
333
                // symbol code
334
                xml.putProperty("symbolCode", unicode);
335

    
336
                // description
337
                xml.putProperty("desc", getDescription());
338

    
339
                // is shape visible
340
                xml.putProperty("isShapeVisible", isShapeVisible());
341

    
342
                // x offset
343
                xml.putProperty("xOffset", getOffset().getX());
344

    
345
                // y offset
346
                xml.putProperty("yOffset", getOffset().getY());
347

    
348
                // rotation
349
                xml.putProperty("rotation", getRotation());
350

    
351
                // measure unit
352
                xml.putProperty("unit", getUnit());
353

    
354
                // reference system
355
                xml.putProperty("referenceSystem", getReferenceSystem());
356

    
357
                return xml;
358
        }
359

    
360
        public int getSymbolType() {
361
                return FShape.POINT;
362
        }
363

    
364

    
365
        public void setUnicode(int symbol) {
366
                this.unicode = symbol;
367
        }
368

    
369
        public int getUnicode() {
370
                return unicode;
371
        }
372

    
373
        public String getClassName() {
374
                return this.getClass().getName();
375
        }
376

    
377
        public void setXMLEntity(XMLEntity xml) {
378
                setColor(StringUtilities.string2Color(xml.getStringProperty("color")));
379
                Point p = new Point();
380
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
381

    
382
                setDescription(xml.getStringProperty("desc"));
383
                font = new Font(xml.getStringProperty("font"),
384
                                xml.getIntProperty("fontStyle"),
385
                                (int) xml.getFloatProperty("size"));
386
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
387
                unicode = xml.getIntProperty("symbolCode");
388
                setOffset(p);
389
                setRotation(xml.getDoubleProperty("rotation"));
390
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
391
                setUnit(xml.getIntProperty("unit"));
392
        }
393

    
394
        public void print(Graphics2D g, AffineTransform at, FShape shape) throws ReadDriverException {
395
                // TODO Implement it
396
                throw new Error("Not yet implemented!");
397

    
398
        }
399

    
400
        public double getSize() {
401
                return font.getSize();
402
        }
403

    
404
        public void setSize(double size) {
405
                if (size == 0) {
406
                        try {
407
                                Double.parseDouble("setSize com size="+size);
408
                        }catch(Exception e) {
409
                                e.printStackTrace();
410
                        }
411
                }
412
                font = new Font(font.getName(), font.getStyle(), (int) size);
413
        }
414

    
415
        public boolean isVisuallyCorrected() {
416
                return visualCorrection != null;
417
        }
418

    
419
        public void setVisuallyCorrected(boolean visuallyCorrected) {
420
                if (visuallyCorrected && visualCorrection == null) {
421
                        CharacterMarkerSymbol clone = (CharacterMarkerSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), getDescription());
422
                        int frameSize = 200;
423
                        double symbolSize = 100;
424
                        BufferedImage bi = new BufferedImage(frameSize, frameSize, BufferedImage.TYPE_INT_ARGB);
425
                        Graphics2D aGraphics = bi.createGraphics();
426
                        clone.setOffset(new Point2D.Double(0,0));
427
                        clone.setSize(symbolSize);
428
                        clone.setColor(Color.PINK);
429

    
430

    
431
                        // draw it at center of the image
432
                        FPoint2D pCenter =  new FPoint2D(frameSize/2,frameSize/2);
433
                        clone.draw(aGraphics, new AffineTransform(), pCenter);
434
                        int realTop    = -1;
435
                        int realBottom = -1;
436
                        int realLeft   = -1;
437
                        int realRight  = -1;
438

    
439
                        // let's see where the highest pixel is in Y-axis
440
                        boolean done = false;
441
                        for (int j = 0; !done && j < bi.getHeight(); j++) {
442
                                for (int i = 0; !done && i < bi.getWidth(); i++) {
443
                                        if (bi.getRGB(i, j) != 0) {
444
                                                realTop = j;
445
                                                done = true;
446
                                        }
447
                                }
448
                        }
449

    
450
                        // let's see where the lowest pixel is in Y-axis
451
                        done = false;
452
                        for (int j =  bi.getHeight()-1; !done &&  j >= 0; j--) {
453
                                for (int i = 0; !done &&  i < bi.getWidth(); i++) {
454
                                        if (bi.getRGB(i, j) != 0) {
455
                                                realBottom = i;
456
                                                done = true;
457
                                        }
458
                                }
459
                        }
460

    
461
                        // let's see where the first pixel at left is in X-axis
462
                        done = false;
463
                        for (int i = 0; !done &&  i < bi.getWidth(); i++) {
464
                                for (int j = 0; !done &&  j < bi.getHeight(); j++) {
465
                                        if (bi.getRGB(i, j) != 0) {
466
                                                realLeft = i;
467
                                                done = true;
468
                                        }
469
                                }
470
                        }
471

    
472
                        // let's see where the first pixel at right is in X-axis
473
                        done = false;
474
                        for (int i = bi.getWidth()-1; !done &&  i >=0 ; i--) {
475
                                for (int j = 0; !done &&  j < bi.getHeight(); j++) {
476
                                        if (bi.getRGB(i, j) != 0) {
477
                                                realRight = i;
478
                                                done = true;
479
                                        }
480
                                }
481
                        }
482
                        int realWidth = realRight-realLeft;
483
                        int realHeight = realTop-realBottom;
484

    
485
                        visualCorrection = new VisualCorrection();
486
                        if (realBottom!=-1 && realTop!=-1 && realLeft!=-1 && realRight != -1) {
487
                                double correctingSize = Math.max(realHeight, realWidth);
488
                                visualCorrection.sizeScale = clone.getSize() / correctingSize;
489
                        }
490

    
491
                        if (realLeft!=-1 && realRight!=-1) {
492
                                double correctingCenterX = (((realWidth)*0.5)+realLeft);
493
                                double correctingCenterY = (((realHeight)*0.5)+realBottom);
494
                                visualCorrection.xOffsetScale = (pCenter.getX() - correctingCenterX) / frameSize;
495
                                visualCorrection.yOffsetScale = (pCenter.getY() - correctingCenterY) / frameSize;
496
                        }
497

    
498
//                        clone.setSize(clone.getSize()*visualCorrection.sizeScale);
499
//                        clone.setOffset(new Point2D.Double(clone.getSize()*.5*visualCorrection.xOffsetScale, clone.getSize()*.5*visualCorrection.yOffsetScale));
500
//                        clone.setColor(Color.ORANGE);
501
//
502
//                        clone.draw(aGraphics, new AffineTransform(), pCenter);
503
//                        try {
504
//                                ImageIO.write(bi, "png",
505
//                                                new File("E:/prova-imatges/"+
506
//                                                                System.currentTimeMillis()
507
//                                                                +".png"));
508
//                        } catch (IOException e) {e.printStackTrace();};
509

    
510

    
511
                } else {
512
                        visualCorrection = null;
513
                }
514
        }
515

    
516
        private class VisualCorrection {
517
                double xOffsetScale = 1;
518
                double yOffsetScale = 1;
519
                double sizeScale = 1;
520
        }
521
}