Statistics
| Revision:

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

History | View | Annotate | Download (10.3 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
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
94
                g.setColor(textColor);
95
                g.setFont(font);
96
                g.translate(((FPoint2D) shp).getX(), ((FPoint2D) shp).getY());
97

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

    
103
                // Alineamos el texto de manera que la parte superior
104
                // izquierda de la primera letra est? en (0,0).
105
                g.drawString(getText(), 0, (int)-bounds.getY());
106
                g.rotate(-rotation);
107
                g.translate(-((FPoint2D) shp).getX(), -((FPoint2D) shp).getY());
108
        }
109

    
110
        public void drawInsideRectangle(Graphics2D g,
111
                        AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException {
112
                int s = getFont().getSize();
113

    
114
                if (autoresize) {
115
                        if (s==0) {
116
                                s =1;
117
                                setFontSize(s);
118
                        }
119
                        g.setFont(getFont());
120
                    FontMetrics fm = g.getFontMetrics();
121
                    Rectangle2D rect = fm.getStringBounds(text, g);
122
                    double width = rect.getWidth();
123
                    double height = rect.getHeight();
124
                    double rWidth = r.getWidth();
125
                    double rHeight = r.getHeight();
126
                    double ratioText = width/height;
127
                    double ratioRect = rWidth/rHeight;
128

    
129
                    if (ratioText>ratioRect) {
130
                            s = (int) (s*(rWidth/width));
131
                    } else {
132
                            s = (int) (s*(rHeight/height));
133
                    }
134
                    setFontSize(s);
135
                }
136

    
137
                //Only for debugging purpose
138
//                g.drawRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight());
139
                if (properties==null)
140
                        draw(g, null, new FPoint2D(r.getX(), r.getY()), null);
141
                else
142
                        print(g, new AffineTransform(), new FPoint2D(r.getX(), r.getY()), properties);
143

    
144
        }
145

    
146
        public int getOnePointRgb() {
147
                return textColor.getRGB();
148
        }
149

    
150
        public void getPixExtentPlus(FShape shp, float[] distances,
151
                        ViewPort viewPort, int dpi) {
152
                throw new Error("Not yet implemented!");
153

    
154
        }
155

    
156
        public ISymbol getSymbolForSelection() {
157
                return this; // a text is not selectable
158
        }
159

    
160
        public int getSymbolType() {
161
                return FShape.TEXT;
162
        }
163

    
164
        public XMLEntity getXMLEntity() {
165
                XMLEntity xml = new XMLEntity();
166
                xml.putProperty("className", getClassName());
167
                xml.putProperty("desc", getDescription());
168
                xml.putProperty("isShapeVisible", isShapeVisible());
169
                xml.putProperty("font", font.getName());
170
                xml.putProperty("fontStyle", font.getStyle());
171
                xml.putProperty("size", font.getSize());
172
                xml.putProperty("text", text);
173
                xml.putProperty("textColor", StringUtilities.color2String(textColor));
174
                xml.putProperty("unit", getUnit());
175
                xml.putProperty("referenceSystem", getReferenceSystem());
176
                xml.putProperty("autoresizeFlag", isAutoresizeEnabled());
177
                return xml;
178
        }
179

    
180
        public boolean isSuitableFor(IGeometry geom) {
181
                return true;
182
        }
183

    
184
        public void setXMLEntity(XMLEntity xml) {
185
                font = new Font(xml.getStringProperty("font"),
186
                                xml.getIntProperty("fontStyle"),
187
                                xml.getIntProperty("size"));
188
                setDescription(xml.getStringProperty("desc"));
189
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
190
                text = xml.getStringProperty("text");
191
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
192
                setUnit(xml.getIntProperty("unit"));
193
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
194
                setAutoresizeEnabled(xml.getBooleanProperty("autoresizeFlag"));
195
                this.bounds = null;
196
                this.horizontalTextWrappingShape = null;
197
        }
198

    
199
        public String getClassName() {
200
                return getClass().getName();
201
        }
202

    
203
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) {
204
                this.properties=properties;
205
        draw(g, at, shape, null);
206
        this.properties=null;
207

    
208
        }
209

    
210
        public String getText() {
211
                return text;
212
        }
213

    
214
        public Font getFont() {
215
                return font;
216
        }
217

    
218
        public Color getTextColor() {
219
                return textColor;
220
        }
221

    
222
        public void setText(String text) {
223
                if(text != null && !text.equals(this.text)){
224
                        this.text = text;
225
                        this.bounds = null;
226
                        this.horizontalTextWrappingShape = null;
227
                }
228
        }
229

    
230
        public void setFont(Font font) {
231
                if (font != null && !font.equals(this.font)){
232
                        this.font = font;
233
                        this.bounds = null;
234
                        this.horizontalTextWrappingShape = null;
235
                }
236
        }
237

    
238
        public void setTextColor(Color color) {
239
                this.textColor = color;
240
        }
241

    
242
        public void setFontSize(double size) {
243
                if (size != this.font.getSize2D()){
244
                        this.font = this.font.deriveFont((float) size);
245
                        this.bounds = null;
246
                        this.horizontalTextWrappingShape = null;
247
                }
248
//                this.font = new Font(this.font.getName(),this.font.getStyle(),(int)size);
249
        }
250

    
251
        /**
252
         * Defines the angle of rotation for the text that composes the symbol
253
         *
254
         * @param rotation
255
         */
256
        public void setRotation(double rotation) {
257
                if(rotation != this.rotation){
258
                        this.rotation = rotation;
259
                        this.bounds = null;
260
                }
261
        }
262

    
263
        public double getRotation() {
264
                return rotation;
265
        }
266

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

    
289
                        if(scale != 1){
290
                                myFShape.transform(AffineTransform.getScaleInstance(scale, scale));
291
                        }
292

    
293
                        this.horizontalTextWrappingShape = myFShape;
294
                }
295
                return this.horizontalTextWrappingShape;
296
        }
297

    
298
        /**
299
         * Returns an FShape which represents a rectangle containing the text in
300
         * <b>screen</b> units.
301
         */
302
        public FShape getTextWrappingShape(FPoint2D p) {
303

    
304
                FShape myFShape = getHorizontalTextWrappingShape(p);
305
                myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
306

    
307
                if (rotation != 0) {
308
                        myFShape.transform(AffineTransform.getRotateInstance(rotation));
309
                }
310
                return myFShape;
311
        }
312

    
313
        public Rectangle getBounds() {
314
//                FontMetrics fm = g.getFontMetrics();
315
//                Rectangle2D rect = fm.getStringBounds("graphics", g);
316

    
317
                if(this.bounds == null){
318
                        this.bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds();
319
                }
320
                return this.bounds;
321
        }
322

    
323
        public void setCartographicSize(double cartographicSize, FShape shp) {
324
                setFontSize(cartographicSize);
325
        }
326

    
327
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
328
                double oldSize = getFont().getSize();
329
                setCartographicSize(getCartographicSize(
330
                                                                viewPort,
331
                                                                dpi,
332
                                                                shp),
333
                                                        shp);
334
                return oldSize;
335
        }
336

    
337
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
338
                return CartographicSupportToolkit.
339
                                        getCartographicLength(this,
340
                                                                                  getFont().getSize(),
341
                                                                                  viewPort,
342
                                                                                  dpi);
343
        }
344

    
345
        public boolean isAutoresizeEnabled() {
346
                return autoresize;
347
        }
348

    
349
        public void setAutoresizeEnabled(boolean autoresizeFlag) {
350
                this.autoresize = autoresizeFlag;
351
        }
352
}