Statistics
| Revision:

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

History | View | Annotate | Download (10.5 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
package com.iver.cit.gvsig.fmap.core.symbols;
42

    
43
import java.awt.Color;
44
import java.awt.Font;
45
import java.awt.FontMetrics;
46
import java.awt.Graphics2D;
47
import java.awt.Rectangle;
48
import java.awt.RenderingHints;
49
import java.awt.Shape;
50
import java.awt.font.FontRenderContext;
51
import java.awt.font.GlyphVector;
52
import java.awt.geom.AffineTransform;
53
import java.awt.geom.Rectangle2D;
54

    
55
import javax.print.attribute.PrintRequestAttributeSet;
56
import javax.print.attribute.standard.PrintQuality;
57

    
58
import com.iver.cit.gvsig.fmap.MapContext;
59
import com.iver.cit.gvsig.fmap.ViewPort;
60
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
61
import com.iver.cit.gvsig.fmap.core.FPoint2D;
62
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
63
import com.iver.cit.gvsig.fmap.core.FShape;
64
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
65
import com.iver.cit.gvsig.fmap.core.IGeometry;
66
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
67
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
68
import com.iver.utiles.StringUtilities;
69
import com.iver.utiles.XMLEntity;
70
import com.iver.utiles.swing.threads.Cancellable;
71

    
72
/**
73
 * SimpleTextSymbol is a class used to create symbols composed using a text defined by
74
 * the user.This text can be edited (changing the color, the font of the characters, and
75
 * the rotation of the text).
76
 * @author jaume dominguez faus - jaume.dominguez@iver.es
77
 *
78
 */
79
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol {
80
        private String text = "";
81
        private Font font = SymbologyFactory.DefaultTextFont;
82
        private Color textColor = Color.BLACK;
83
        private double rotation;
84
        private FontRenderContext frc = new FontRenderContext(
85
                        new AffineTransform(), false, true);
86
        private boolean autoresize;
87
        private Rectangle bounds = null;
88
        private FShape horizontalTextWrappingShape = null;
89
        private PrintRequestAttributeSet properties;
90

    
91
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
92
                if (!isShapeVisible()) return;
93
                double shpX = ((FPoint2D) shp).getX();
94
                double shpY = ((FPoint2D) shp).getY();
95
                //Parche porque a veces llegan puntos cuyas coordenadas no han podido ser calculadas y vienen como NaN
96
                if( Double.isNaN(shpX) || Double.isNaN(shpY)){
97
                        return;
98
                }
99
                //Fin del parche
100
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
101
                g.setColor(textColor);
102
                g.setFont(font);
103
                g.translate(shpX, shpY);
104

    
105
                g.rotate(rotation);
106
                Rectangle2D bounds = getHorizontalTextWrappingShape(new FPoint2D(0,0)).getBounds();
107
                // Antes tomabamos el resultado de getBounds pero ya se le hab?a aplicado
108
                // la rotaci?n, con lo que no obten?amos el la altura correcta de la fuente.
109

    
110
                // Alineamos el texto de manera que la parte superior
111
                // izquierda de la primera letra est? en (0,0).
112
                g.drawString(getText(), 0, (int)-bounds.getY());
113
                g.rotate(-rotation);
114
                g.translate(-shpX, -shpY);
115
        }
116

    
117
        public void drawInsideRectangle(Graphics2D g,
118
                        AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException {
119
                int s = getFont().getSize();
120

    
121
                if (autoresize) {
122
                        if (s==0) {
123
                                s =1;
124
                                setFontSize(s);
125
                        }
126
                        g.setFont(getFont());
127
                    FontMetrics fm = g.getFontMetrics();
128
                    Rectangle2D rect = fm.getStringBounds(text, g);
129
                    double width = rect.getWidth();
130
                    double height = rect.getHeight();
131
                    double rWidth = r.getWidth();
132
                    double rHeight = r.getHeight();
133
                    double ratioText = width/height;
134
                    double ratioRect = rWidth/rHeight;
135

    
136
                    if (ratioText>ratioRect) {
137
                            s = (int) (s*(rWidth/width));
138
                    } else {
139
                            s = (int) (s*(rHeight/height));
140
                    }
141
                    setFontSize(s);
142
                }
143

    
144
                //Only for debugging purpose
145
//                g.drawRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight());
146
                if (properties==null)
147
                        draw(g, null, new FPoint2D(r.getX(), r.getY()), null);
148
                else
149
                        print(g, new AffineTransform(), new FPoint2D(r.getX(), r.getY()), properties);
150

    
151
        }
152

    
153
        public int getOnePointRgb() {
154
                return textColor.getRGB();
155
        }
156

    
157
        public void getPixExtentPlus(FShape shp, float[] distances,
158
                        ViewPort viewPort, int dpi) {
159
                throw new Error("Not yet implemented!");
160

    
161
        }
162

    
163
        public ISymbol getSymbolForSelection() {
164
                return this; // a text is not selectable
165
        }
166

    
167
        public int getSymbolType() {
168
                return FShape.TEXT;
169
        }
170

    
171
        public XMLEntity getXMLEntity() {
172
                XMLEntity xml = new XMLEntity();
173
                xml.putProperty("className", getClassName());
174
                xml.putProperty("desc", getDescription());
175
                xml.putProperty("isShapeVisible", isShapeVisible());
176
                xml.putProperty("font", font.getName());
177
                xml.putProperty("fontStyle", font.getStyle());
178
                xml.putProperty("size", font.getSize());
179
                xml.putProperty("text", text);
180
                xml.putProperty("textColor", StringUtilities.color2String(textColor));
181
                xml.putProperty("unit", getUnit());
182
                xml.putProperty("referenceSystem", getReferenceSystem());
183
                xml.putProperty("autoresizeFlag", isAutoresizeEnabled());
184
                return xml;
185
        }
186

    
187
        public boolean isSuitableFor(IGeometry geom) {
188
                return true;
189
        }
190

    
191
        public void setXMLEntity(XMLEntity xml) {
192
                font = new Font(xml.getStringProperty("font"),
193
                                xml.getIntProperty("fontStyle"),
194
                                xml.getIntProperty("size"));
195
                setDescription(xml.getStringProperty("desc"));
196
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
197
                text = xml.getStringProperty("text");
198
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
199
                setUnit(xml.getIntProperty("unit"));
200
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
201
                setAutoresizeEnabled(xml.getBooleanProperty("autoresizeFlag"));
202
                this.bounds = null;
203
                this.horizontalTextWrappingShape = null;
204
        }
205

    
206
        public String getClassName() {
207
                return getClass().getName();
208
        }
209

    
210
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) {
211
                this.properties=properties;
212
        draw(g, at, shape, null);
213
        this.properties=null;
214

    
215
        }
216

    
217
        public String getText() {
218
                return text;
219
        }
220

    
221
        public Font getFont() {
222
                return font;
223
        }
224

    
225
        public Color getTextColor() {
226
                return textColor;
227
        }
228

    
229
        public void setText(String text) {
230
                if(text != null && !text.equals(this.text)){
231
                        this.text = text;
232
                        this.bounds = null;
233
                        this.horizontalTextWrappingShape = null;
234
                }
235
        }
236

    
237
        public void setFont(Font font) {
238
                if (font != null && !font.equals(this.font)){
239
                        this.font = font;
240
                        this.bounds = null;
241
                        this.horizontalTextWrappingShape = null;
242
                }
243
        }
244

    
245
        public void setTextColor(Color color) {
246
                this.textColor = color;
247
        }
248

    
249
        public void setFontSize(double size) {
250
                if (size != this.font.getSize2D()){
251
                        this.font = this.font.deriveFont((float) size);
252
                        this.bounds = null;
253
                        this.horizontalTextWrappingShape = null;
254
                }
255
//                this.font = new Font(this.font.getName(),this.font.getStyle(),(int)size);
256
        }
257

    
258
        /**
259
         * Defines the angle of rotation for the text that composes the symbol
260
         *
261
         * @param rotation
262
         */
263
        public void setRotation(double rotation) {
264
                if(rotation != this.rotation){
265
                        this.rotation = rotation;
266
                        this.bounds = null;
267
                }
268
        }
269

    
270
        public double getRotation() {
271
                return rotation;
272
        }
273

    
274
        /**
275
         * Returns an FShape which represents a rectangle containing the text in
276
         * <b>screen</b> units.
277
         */
278
        private FShape getHorizontalTextWrappingShape(FPoint2D p) {
279
                if (this.horizontalTextWrappingShape  == null){
280
                        Font font = getFont();
281
                        /* Para tama?os de fuente de letras excesivamente grandes obtenemos
282
                         * shapes con todas las coordenadas a 0, por eso limitamos el tama?o
283
                         * a 1000 y despu?s reescalamos el bounds.
284
                         */
285
                        double scale = 1;
286
                        float fontSize = font.getSize2D();
287
                        if (fontSize > 1000){
288
                                scale = fontSize/1000;
289
                                fontSize = 1000;
290
                        }
291
                        font = font.deriveFont(fontSize);
292
                        GlyphVector gv = font.createGlyphVector(frc, text);
293
                        Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
294
                        FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
295

    
296
                        if(scale != 1){
297
                                myFShape.transform(AffineTransform.getScaleInstance(scale, scale));
298
                        }
299

    
300
                        this.horizontalTextWrappingShape = myFShape;
301
                }
302
                return this.horizontalTextWrappingShape;
303
        }
304

    
305
        /**
306
         * Returns an FShape which represents a rectangle containing the text in
307
         * <b>screen</b> units.
308
         */
309
        public FShape getTextWrappingShape(FPoint2D p) {
310

    
311
                FShape myFShape = getHorizontalTextWrappingShape(p);
312
                myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
313

    
314
                if (rotation != 0) {
315
                        myFShape.transform(AffineTransform.getRotateInstance(rotation));
316
                }
317
                return myFShape;
318
        }
319

    
320
        public Rectangle getBounds() {
321
//                FontMetrics fm = g.getFontMetrics();
322
//                Rectangle2D rect = fm.getStringBounds("graphics", g);
323

    
324
                if(this.bounds == null){
325
                        this.bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds();
326
                }
327
                return this.bounds;
328
        }
329

    
330
        public void setCartographicSize(double cartographicSize, FShape shp) {
331
                setFontSize(cartographicSize);
332
        }
333

    
334
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
335
                double oldSize = getFont().getSize();
336
                setCartographicSize(getCartographicSize(
337
                                                                viewPort,
338
                                                                dpi,
339
                                                                shp),
340
                                                        shp);
341
                return oldSize;
342
        }
343

    
344
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
345
                return CartographicSupportToolkit.
346
                                        getCartographicLength(this,
347
                                                                                  getFont().getSize(),
348
                                                                                  viewPort,
349
                                                                                  dpi);
350
        }
351

    
352
        public boolean isAutoresizeEnabled() {
353
                return autoresize;
354
        }
355

    
356
        public void setAutoresizeEnabled(boolean autoresizeFlag) {
357
                this.autoresize = autoresizeFlag;
358
        }
359
}