Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / CharacterMarkerSymbol.java @ 19509

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

    
191
import java.awt.Color;
192
import java.awt.Font;
193
import java.awt.Graphics2D;
194
import java.awt.Point;
195
import java.awt.RenderingHints;
196
import java.awt.Shape;
197
import java.awt.font.FontRenderContext;
198
import java.awt.font.GlyphVector;
199
import java.awt.geom.AffineTransform;
200
import java.awt.geom.Point2D;
201
import java.awt.image.BufferedImage;
202

    
203
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
204
import com.iver.cit.gvsig.fmap.MapContext;
205
import com.iver.cit.gvsig.fmap.core.FPoint2D;
206
import com.iver.cit.gvsig.fmap.core.FShape;
207
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
208
import com.iver.cit.gvsig.fmap.core.styles.IMask;
209
import com.iver.utiles.StringUtilities;
210
import com.iver.utiles.XMLEntity;
211
import com.iver.utiles.swing.threads.Cancellable;
212

    
213

    
214
/**
215
 * Allows to use a source of TrueType characters  to define the marker that will
216
 * substitute the symbol.If the picture is defined in a source, the performance is
217
 * more agile.
218
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
219
 */
220
public class CharacterMarkerSymbol extends AbstractMarkerSymbol {
221
        private Font font = new Font("Arial", Font.PLAIN, 20);
222
        private int unicode;
223
        private ISymbol selectionSymbol;
224
        private VisualCorrection visualCorrection;
225

    
226
        /**
227
         * Creates a new instance of CharacterMarker with default values
228
         *
229
         */
230
        public CharacterMarkerSymbol() {
231
                super();
232
        }
233

    
234
        /**
235
         * Creates a new instance of CharacterMarker specifying the marker source
236
         * font, the character code corresponding to the symbol, and the color that
237
         * will be used in rendering time.
238
         *
239
         * @param font -
240
         *            src Font
241
         * @param charCode -
242
         *            character code of the symbol for this font
243
         * @param color -
244
         *            color to be used in when rendering.
245
         */
246
        public CharacterMarkerSymbol(Font font, int charCode, Color color) {
247
                super();
248
                this.font = font;
249
                unicode = charCode;
250
                setColor(color);
251
        }
252
        /**
253
         * Returns the font that will be used to define the symbol
254
         * @return font
255
         */
256
        public Font getFont() {
257
                return font;
258
        }
259
        /**
260
         * Sets the font that will be used to define the symbol
261
         * @return font
262
         */
263
        public void setFont(Font font) {
264
                this.font = font;
265
        }
266

    
267
        public ISymbol getSymbolForSelection() {
268
                if (selectionSymbol == null) {
269
                        XMLEntity xml = getXMLEntity();
270
                        xml.putProperty("color", StringUtilities.color2String(MapContext.getSelectionColor()));
271
                        selectionSymbol = SymbologyFactory.createSymbolFromXML(xml, getDescription() + " version for selection.");
272
                }
273
                return selectionSymbol;
274
        }
275

    
276
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
277
                g.setColor(getColor());
278
                double theta = getRotation();
279

    
280
                int xOffset = (int) getOffset().getX(); // * multiplicador d'unitats;
281
                int yOffset = (int) getOffset().getY(); // * multiplicador d'unitats
282

    
283
                double size = getSize();
284
                if (size < 0.0001) {
285
                        return;
286
                }
287
                Point2D p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
288
                                .getY());
289

    
290
                if (isVisuallyCorrected()) {
291
                        size *= visualCorrection.sizeScale;
292
                        p.setLocation(p.getX() - xOffset*size*visualCorrection.xOffsetScale, p.getY() - yOffset*size*visualCorrection.yOffsetScale);
293

    
294
                }
295
                g.setFont(getFont().deriveFont((float)size));
296

    
297
                g.translate((int) (p.getX() + xOffset), (int) (p.getY()  + yOffset));
298
                if (theta != 0)        g.rotate(theta);
299

    
300
                char[] text = new char[] { (char) unicode };
301

    
302
                IMask mask = getMask();
303
                if (mask != null) {
304
                        FontRenderContext frc = g.getFontRenderContext();
305

    
306
                        GlyphVector gv = font.createGlyphVector(frc, text );
307

    
308
                        Shape markerShape = gv.getOutline(0, 0);
309
                        mask.
310
                                 getFillSymbol().
311
                                         draw(g, null, mask.getHaloShape(markerShape), cancel);
312

    
313
                }
314
                g.drawChars(text, 0, text.length, - (int) (size*0.4), (int) (size*0.4));
315

    
316

    
317
                if (theta!=0) g.rotate(-theta);
318
                g.translate(-(int) (p.getX() + xOffset), - (int) (p.getY() + yOffset));
319
        }
320

    
321
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
322
                        Shape shp) {
323
                return 0;
324
        }
325

    
326
        public XMLEntity getXMLEntity() {
327
                XMLEntity xml = new XMLEntity();
328

    
329
                // the class name
330
                xml.putProperty("className", getClassName());
331

    
332
                // color
333
                xml.putProperty("color", StringUtilities.color2String(getColor()));
334

    
335
                // font
336
                xml.putProperty("font", font.getFontName());
337

    
338
                // font style
339
                xml.putProperty("fontStyle", font.getStyle());
340

    
341
                // marker size
342
                xml.putProperty("size", font.getSize());
343

    
344
                // symbol code
345
                xml.putProperty("symbolCode", unicode);
346

    
347
                // description
348
                xml.putProperty("desc", getDescription());
349

    
350
                // is shape visible
351
                xml.putProperty("isShapeVisible", isShapeVisible());
352

    
353
                // x offset
354
                xml.putProperty("xOffset", getOffset().getX());
355

    
356
                // y offset
357
                xml.putProperty("yOffset", getOffset().getY());
358

    
359
                // rotation
360
                xml.putProperty("rotation", getRotation());
361

    
362
                // measure unit
363
                xml.putProperty("unit", getUnit());
364

    
365
                // reference system
366
                xml.putProperty("referenceSystem", getReferenceSystem());
367

    
368
                return xml;
369
        }
370

    
371
        public int getSymbolType() {
372
                return FShape.POINT;
373
        }
374

    
375
        /**
376
         * Sets the unicode for a symbol represented by a character
377
         * @param symbol, int
378
         */
379
        public void setUnicode(int symbol) {
380
                this.unicode = symbol;
381
        }
382

    
383
        /**
384
         * Obtains the unicode for a symbol
385
         * @return unicode, int
386
         */
387
        public int getUnicode() {
388
                return unicode;
389
        }
390

    
391
        public String getClassName() {
392
                return this.getClass().getName();
393
        }
394

    
395
        public void setXMLEntity(XMLEntity xml) {
396
                setColor(StringUtilities.string2Color(xml.getStringProperty("color")));
397
                Point p = new Point();
398
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
399

    
400
                setDescription(xml.getStringProperty("desc"));
401
                font = new Font(xml.getStringProperty("font"),
402
                                xml.getIntProperty("fontStyle"),
403
                                (int) xml.getFloatProperty("size"));
404
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
405
                unicode = xml.getIntProperty("symbolCode");
406
                setOffset(p);
407
                setRotation(xml.getDoubleProperty("rotation"));
408
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
409
                setUnit(xml.getIntProperty("unit"));
410
        }
411

    
412
        public void print(Graphics2D g, AffineTransform at, FShape shape) throws ReadDriverException {
413
                // TODO Implement it
414
                throw new Error("Not yet implemented!");
415

    
416
        }
417

    
418
        public double getSize() {
419
                return font.getSize();
420
        }
421

    
422
        public void setSize(double size) {
423
                if (size == 0) {
424
                        try {
425
                                Double.parseDouble("setSize com size="+size);
426
                        }catch(Exception e) {
427
                                e.printStackTrace();
428
                        }
429
                }
430
                font = new Font(font.getName(), font.getStyle(), (int) size);
431
        }
432
        /**
433
         * Returns true of false depending if the character marker symbol selected is visually
434
         * corrected.That is, if the character has been properly scaled to use a specific number
435
         * of pixels to be represented.
436
         *
437
         * @return boolean
438
         */
439
        public boolean isVisuallyCorrected() {
440
                return visualCorrection != null;
441
        }
442
        /**
443
         * Sets the visual correction for a character in order to scale it if it is necessary.
444
         *
445
         * @return boolean
446
         */
447
        public void setVisuallyCorrected(boolean visuallyCorrected) {
448
                if (visuallyCorrected && visualCorrection == null) {
449
                        CharacterMarkerSymbol clone = (CharacterMarkerSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), getDescription());
450
                        int frameSize = 200;
451
                        double symbolSize = 100;
452
                        BufferedImage bi = new BufferedImage(frameSize, frameSize, BufferedImage.TYPE_INT_ARGB);
453
                        Graphics2D aGraphics = bi.createGraphics();
454
                        clone.setOffset(new Point2D.Double(0,0));
455
                        clone.setSize(symbolSize);
456
                        clone.setColor(Color.PINK);
457

    
458

    
459
                        // draw it at center of the image
460
                        FPoint2D pCenter =  new FPoint2D(frameSize/2,frameSize/2);
461
                        clone.draw(aGraphics, new AffineTransform(), pCenter, null);
462
                        int realTop    = -1;
463
                        int realBottom = -1;
464
                        int realLeft   = -1;
465
                        int realRight  = -1;
466

    
467
                        // let's see where the highest pixel is in Y-axis
468
                        boolean done = false;
469
                        for (int j = 0; !done && j < bi.getHeight(); j++) {
470
                                for (int i = 0; !done && i < bi.getWidth(); i++) {
471
                                        if (bi.getRGB(i, j) != 0) {
472
                                                realTop = j;
473
                                                done = true;
474
                                        }
475
                                }
476
                        }
477

    
478
                        // let's see where the lowest pixel is in Y-axis
479
                        done = false;
480
                        for (int j =  bi.getHeight()-1; !done &&  j >= 0; j--) {
481
                                for (int i = 0; !done &&  i < bi.getWidth(); i++) {
482
                                        if (bi.getRGB(i, j) != 0) {
483
                                                realBottom = i;
484
                                                done = true;
485
                                        }
486
                                }
487
                        }
488

    
489
                        // let's see where the first pixel at left is in X-axis
490
                        done = false;
491
                        for (int i = 0; !done &&  i < bi.getWidth(); i++) {
492
                                for (int j = 0; !done &&  j < bi.getHeight(); j++) {
493
                                        if (bi.getRGB(i, j) != 0) {
494
                                                realLeft = i;
495
                                                done = true;
496
                                        }
497
                                }
498
                        }
499

    
500
                        // let's see where the first pixel at right is in X-axis
501
                        done = false;
502
                        for (int i = bi.getWidth()-1; !done &&  i >=0 ; i--) {
503
                                for (int j = 0; !done &&  j < bi.getHeight(); j++) {
504
                                        if (bi.getRGB(i, j) != 0) {
505
                                                realRight = i;
506
                                                done = true;
507
                                        }
508
                                }
509
                        }
510
                        int realWidth = realRight-realLeft;
511
                        int realHeight = realTop-realBottom;
512

    
513
                        visualCorrection = new VisualCorrection();
514
                        if (realBottom!=-1 && realTop!=-1 && realLeft!=-1 && realRight != -1) {
515
                                double correctingSize = Math.max(realHeight, realWidth);
516
                                visualCorrection.sizeScale = clone.getSize() / correctingSize;
517
                        }
518

    
519
                        if (realLeft!=-1 && realRight!=-1) {
520
                                double correctingCenterX = (((realWidth)*0.5)+realLeft);
521
                                double correctingCenterY = (((realHeight)*0.5)+realBottom);
522
                                visualCorrection.xOffsetScale = (pCenter.getX() - correctingCenterX) / frameSize;
523
                                visualCorrection.yOffsetScale = (pCenter.getY() - correctingCenterY) / frameSize;
524
                        }
525

    
526
//                        clone.setSize(clone.getSize()*visualCorrection.sizeScale);
527
//                        clone.setOffset(new Point2D.Double(clone.getSize()*.5*visualCorrection.xOffsetScale, clone.getSize()*.5*visualCorrection.yOffsetScale));
528
//                        clone.setColor(Color.ORANGE);
529
//
530
//                        clone.draw(aGraphics, new AffineTransform(), pCenter);
531
//                        try {
532
//                                ImageIO.write(bi, "png",
533
//                                                new File("E:/prova-imatges/"+
534
//                                                                System.currentTimeMillis()
535
//                                                                +".png"));
536
//                        } catch (IOException e) {e.printStackTrace();};
537

    
538

    
539
                } else {
540
                        visualCorrection = null;
541
                }
542
        }
543
        /**
544
         * Class to be used for the methods that control the visual correction of a character.
545
         * This visual correction has the responsibility of modify the dimensions of the
546
         * character to be used as a symbol(in case that the user wants to use more or less
547
         * pixels to represent it )
548
         *
549
         */
550
        private class VisualCorrection {
551
                double xOffsetScale = 1;
552
                double yOffsetScale = 1;
553
                double sizeScale = 1;
554
        }
555
}