Statistics
| Revision:

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

History | View | Annotate | Download (9.79 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.Graphics2D;
46
import java.awt.Rectangle;
47
import java.awt.Shape;
48
import java.awt.font.FontRenderContext;
49
import java.awt.font.GlyphVector;
50
import java.awt.geom.AffineTransform;
51
import java.awt.geom.Point2D;
52

    
53
import javax.print.attribute.PrintRequestAttributeSet;
54

    
55
import org.apache.batik.ext.awt.geom.PathLength;
56

    
57
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
58
import com.iver.cit.gvsig.fmap.core.FPoint2D;
59
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
60
import com.iver.cit.gvsig.fmap.core.FShape;
61
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
62
import com.iver.cit.gvsig.fmap.core.IGeometry;
63
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
64
import com.iver.utiles.StringUtilities;
65
import com.iver.utiles.XMLEntity;
66
import com.vividsolutions.jts.geom.Geometry;
67
import com.vividsolutions.jts.geom.Point;
68

    
69
/**
70
 * SimpleTextSymbol is a class used to create symbols composed using a text defined by
71
 * the user.This text can be edited (changing the color, the font of the characters, and
72
 * the rotation of the text).
73
 * @author jaume dominguez faus - jaume.dominguez@iver.es
74
 *
75
 */
76
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol {
77
        private String text = "";
78
        private Font font = new Font("Arial", Font.PLAIN, 10);
79
        private Color textColor = Color.BLACK;
80
        private double rotation;
81
        private FontRenderContext frc = new FontRenderContext(
82
                        new AffineTransform(), false, true);
83

    
84
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
85
                if (!isShapeVisible()) return;
86

    
87
                // TODO remove this stuff and allow only points
88
                Point2D p = null;
89
                double rot=0;
90
//                shp.transform(affineTransform);
91
                switch (shp.getShapeType()) {
92
                case FShape.POINT:
93
                        FPoint2D fp=(FPoint2D) shp;
94
                        p = new Point2D.Double(fp.getX(),fp.getY());
95
                        rot = rotation;
96
                        break;
97
                case FShape.LINE:
98
                        PathLength pathLen = new PathLength(shp);
99

    
100
                        // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
101
                        float midDistance = pathLen.lengthOfPath() / 2;
102
                        p = pathLen.pointAtLength(midDistance);
103
                        rot = pathLen.angleAtLength(midDistance);
104
                        break;
105
                case FShape.POLYGON:
106
                        Geometry geo = FConverter.java2d_to_jts(shp);
107

    
108
                        if (geo == null) {
109
                                return;
110
                        }
111

    
112
                        Point pJTS = geo.getCentroid();
113

    
114
                        if (pJTS != null) {
115
                                FShape pLabel = FConverter.jts_to_java2d(pJTS);
116
                                p= new Point2D.Double(((FPoint2D) pLabel).getX(),
117
                                                ((FPoint2D) pLabel).getY());
118
                        }
119
                        break;
120
                }
121

    
122
                g.setColor(textColor);
123
                g.setFont(font);
124
                g.translate(p.getX(), p.getY());
125

    
126
                // TODO comprovar que els radians no estiga rotant-los al rev?s
127
                // en AttrInTableLabels est? posat a -rot per a que els veja b?
128
                // i ?s prou sospit?s. Mira on m?s es fa ?s d'este s?mbol i comprova
129
                // que all? no estiga fent-se tamb? -rot, si ?s aix?, ?s perqu? ac?
130
                // est? al reves (i en tots els llocs on es gasta)
131
                g.rotate(-rot);
132
                g.drawString(getText(), 0, 0);
133
                g.rotate(rot);
134
                g.translate(-p.getX(), -p.getY());
135
        }
136

    
137
        public void drawInsideRectangle(Graphics2D g,
138
                        AffineTransform scaleInstance, Rectangle r) {
139
                float fontSize = getFont().getSize();
140
                float fontScaleFactor = (float) ((getText().length()*fontSize) * 0.8) // text length with a 20% margin
141
                                                                / r.width;
142
                g.setColor(textColor);
143
                g.translate(r.x, r.y);
144
                fontSize = fontSize / fontScaleFactor;
145
                if (fontSize > r.height)
146
                        fontSize = r.height;
147
                g.setFont(new Font(getFont().getName(), getFont().getStyle(), (int)fontSize));
148
                g.drawString(getText(), 0, fontSize);
149
                g.translate(-r.x, -r.y);
150
        }
151

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

    
156
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
157
                        Shape shp) {
158
                // TODO Implement it
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
                return xml;
182
        }
183

    
184
        public boolean isSuitableFor(IGeometry geom) {
185
                return true;
186
        }
187

    
188
        public void setXMLEntity(XMLEntity xml) {
189
                font = new Font(xml.getStringProperty("font"),
190
                                xml.getIntProperty("fontStyle"),
191
                                xml.getIntProperty("size"));
192
                setDescription(xml.getStringProperty("desc"));
193
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
194
                text = xml.getStringProperty("text");
195
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
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
                this.text = text;
223
        }
224

    
225
        public void setFont(Font font) {
226
                this.font = font;
227
        }
228

    
229
        public void setTextColor(Color color) {
230
                this.textColor = color;
231
        }
232

    
233
        public void setFontSize(double size) {
234
                this.font = new Font(font.getName(), font.getStyle(), (int) size);
235
        }
236

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

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

    
253
//                FontRenderContext frc = g.getFontRenderContext();
254

    
255

    
256
                GlyphVector gv = font.createGlyphVector(frc, text);
257

    
258
                /*p.transform(affineTransform);*/
259
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
260
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
261
                if (rotation != 0) {
262
                        myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY()));
263
                }
264
                return myFShape;
265
        }
266

    
267
        /**
268
         * Returns an FShape which represents a rectangle containing the text in
269
         * <b>screen</b> units.
270
         */
271
        private FShape _getTextWrappingShape(Graphics2D g, /*AffineTransform affineTransform,*/ FPoint2D p) {
272
                // assuming FShape is a point with the starting position of the text
273
                Font font = getFont();
274

    
275
//                FontRenderContext frc = g.getFontRenderContext();
276
                FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, true);
277

    
278

    
279
                GlyphVector gv = font.createGlyphVector(frc, text);
280

    
281
                /*p.transform(affineTransform);*/
282
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
283
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
284
                if (rotation != 0) {
285
                        myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY()));
286
                }
287
                return myFShape;
288
        }
289
        /**
290
         * Returns an FShape which represents a rectangle containing the text in
291
         * <b>screen</b> units.
292
         */
293
        private FShape _getTextWrappingShape(Graphics2D g, /*AffineTransform affineTransform,*/ FShape targetSymbolShape ) {
294
                // assuming FShape is a point with the starting position of the text
295
                Font font = getFont();
296

    
297
                FontRenderContext frc = g.getFontRenderContext();
298
                GlyphVector gv = font.createGlyphVector(frc, text);
299
                Point2D p = null;
300

    
301
                switch (targetSymbolShape.getShapeType()) {
302
                case FShape.POINT:
303
                        FPoint2D fp=(FPoint2D) targetSymbolShape;
304
                        p = new Point2D.Double(fp.getX(),fp.getY());
305
                        break;
306
                case FShape.LINE:
307
                        PathLength pathLen = new PathLength(targetSymbolShape);
308

    
309
                        // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
310
                        float midDistance = pathLen.lengthOfPath() / 2;
311
                        p = pathLen.pointAtLength(midDistance);
312
                        break;
313
                case FShape.POLYGON:
314
                        Geometry geo = FConverter.java2d_to_jts(targetSymbolShape);
315

    
316
                        if (geo == null) {
317
                                return null;
318
                        }
319

    
320
                        Point pJTS = geo.getCentroid();
321

    
322
                        if (pJTS != null) {
323
                                FShape pLabel = FConverter.jts_to_java2d(pJTS);
324
                                p= new Point2D.Double(((FPoint2D) pLabel).getX(),
325
                                                ((FPoint2D) pLabel).getY());
326
                        }
327
                        break;
328
                }
329

    
330

    
331
                /*p.transform(affineTransform);*/
332
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
333

    
334
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
335
                if (rotation != 0) {
336
                        myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY()));
337
                }
338
                return myFShape;
339
        }
340

    
341
}