Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / SimpleTextSymbol.java @ 25764

History | View | Annotate | Download (8.81 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

    
89
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
90
                if (!isShapeVisible()) return;
91
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
92
                g.setColor(textColor);
93
                g.setFont(font);
94
                g.translate(((FPoint2D) shp).getX(), ((FPoint2D) shp).getY());
95

    
96
                g.rotate(rotation);
97
                Rectangle2D bounds = getBounds();
98
                // getBounds devuelve el bounds de un texto dibujado de manera que
99
                // la linea base de la primera letra est? en el punto (0,0).
100
                // Por eso, si queremos alinear el texto de manera que la parte superior
101
                // izquierda de la primera letra est? en (0,0) debemos moverlo seg?n
102
                // el valor de la ordenada, en lugar de seg?n su altura.
103
                g.drawString(getText(), 0, (int)-bounds.getY());//(int) bounds.getHeight());
104
                g.rotate(-rotation);
105
                g.translate(-((FPoint2D) shp).getX(), -((FPoint2D) shp).getY());
106
        }
107

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

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

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

    
135
                //Only for debugging purpose
136
//                g.drawRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight());
137

    
138
                draw(g, null, new FPoint2D(r.getX(), r.getY()), null);
139

    
140

    
141
        }
142

    
143
        public int getOnePointRgb() {
144
                return textColor.getRGB();
145
        }
146

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

    
151
        }
152

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

    
157
        public int getSymbolType() {
158
                return FShape.TEXT;
159
        }
160

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

    
177
        public boolean isSuitableFor(IGeometry geom) {
178
                return true;
179
        }
180

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

    
194
        public String getClassName() {
195
                return getClass().getName();
196
        }
197

    
198
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
199
                        throws ReadDriverException {
200
                // TODO Implement it
201
                throw new Error("Not yet implemented!");
202

    
203
        }
204

    
205
        public String getText() {
206
                return text;
207
        }
208

    
209
        public Font getFont() {
210
                return font;
211
        }
212

    
213
        public Color getTextColor() {
214
                return textColor;
215
        }
216

    
217
        public void setText(String text) {
218
                this.text = text;
219
        }
220

    
221
        public void setFont(Font font) {
222
                this.font = font;
223
        }
224

    
225
        public void setTextColor(Color color) {
226
                this.textColor = color;
227
        }
228

    
229
        public void setFontSize(double size) {
230
                this.font = this.font.deriveFont((float) size);
231
//                this.font = new Font(this.font.getName(),this.font.getStyle(),(int)size);
232
        }
233

    
234
        /**
235
         * Defines the angle of rotation for the text that composes the symbol
236
         *
237
         * @param rotation
238
         */
239
        public void setRotation(double rotation) {
240
                this.rotation = rotation;
241
        }
242

    
243
        public double getRotation() {
244
                return rotation;
245
        }
246

    
247
        /**
248
         * Returns an FShape which represents a rectangle containing the text in
249
         * <b>screen</b> units.
250
         */
251
        public FShape getTextWrappingShape(FPoint2D p) {
252
                Font font = getFont();
253
                GlyphVector gv = font.createGlyphVector(frc, text);
254

    
255
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
256
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
257

    
258
                myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
259

    
260
                if (rotation != 0) {
261
                        myFShape.transform(AffineTransform.getRotateInstance(rotation));
262
                }
263
                return myFShape;
264
        }
265

    
266
        public Rectangle getBounds() {
267
//                FontMetrics fm = g.getFontMetrics();
268
//                Rectangle2D rect = fm.getStringBounds("graphics", g);
269

    
270
                Rectangle bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds();
271
                return bounds;
272
        }
273

    
274
        public void setCartographicSize(double cartographicSize, FShape shp) {
275
                setFontSize(cartographicSize);
276
        }
277

    
278
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
279
                double oldSize = getFont().getSize();
280
                setCartographicSize(getCartographicSize(
281
                                                                viewPort,
282
                                                                dpi,
283
                                                                shp),
284
                                                        shp);
285
                return oldSize;
286
        }
287

    
288
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
289
                return CartographicSupportToolkit.
290
                                        getCartographicLength(this,
291
                                                                                  getFont().getSize(),
292
                                                                                  viewPort,
293
                                                                                  dpi);
294
        }
295

    
296
        public boolean isAutoresizeEnabled() {
297
                return autoresize;
298
        }
299

    
300
        public void setAutoresizeEnabled(boolean autoresizeFlag) {
301
                this.autoresize = autoresizeFlag;
302
        }
303
}