Statistics
| Revision:

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

History | View | Annotate | Download (10.2 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.hardcode.gdbms.driver.exceptions.ReadDriverException;
59
import com.iver.cit.gvsig.fmap.MapContext;
60
import com.iver.cit.gvsig.fmap.ViewPort;
61
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
62
import com.iver.cit.gvsig.fmap.core.FPoint2D;
63
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
64
import com.iver.cit.gvsig.fmap.core.FShape;
65
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
66
import com.iver.cit.gvsig.fmap.core.IGeometry;
67
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
68
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
69
import com.iver.utiles.StringUtilities;
70
import com.iver.utiles.XMLEntity;
71
import com.iver.utiles.swing.threads.Cancellable;
72

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

    
140
                draw(g, null, new FPoint2D(r.getX(), r.getY()), null);
141

    
142

    
143
        }
144

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

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

    
153
        }
154

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

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

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

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

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

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

    
202
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
203
                        throws ReadDriverException {
204
                // TODO Implement it
205
                throw new Error("Not yet implemented!");
206

    
207
        }
208

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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