Statistics
| Revision:

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

History | View | Annotate | Download (8.11 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 sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
58

    
59
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
60
import com.iver.cit.gvsig.fmap.core.FPoint2D;
61
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
62
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
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.TextPath;
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.vividsolutions.jts.geom.Geometry;
71
import com.vividsolutions.jts.geom.Point;
72

    
73
/**
74
 *
75
 * @author jaume dominguez faus - jaume.dominguez@iver.es
76
 *
77
 */
78
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol {
79
        private String text = "";
80
        private Font font = new Font("Arial", Font.PLAIN, 10);
81
        private Color textColor = Color.BLACK;
82
        private double rotation;
83

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

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

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

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

    
111
                        Point pJTS = geo.getCentroid();
112

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

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

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

    
136
        public void drawInsideRectangle(Graphics2D g,
137
                        AffineTransform scaleInstance, Rectangle r) {
138
                FontRenderContext frc = g.getFontRenderContext();
139
                GlyphVector gv = font.createGlyphVector(frc, text);
140
                gv.
141
                float fontSize = getFont().getSize();
142
                float fontScaleFactor = (float) ((getText().length()*fontSize) * 0.8) // text length with a 20% margin
143
                                                                / r.width;
144

    
145
                fontSize = fontSize * fontScaleFactor;
146
                g.setFont(getFont().deriveFont(fontSize));
147
                System.out.println(r);
148
                g.drawString(getText(), r.x*0.9F, ( r.height*0.5F) + (fontSize*0.5F));
149

    
150

    
151
        }
152

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

    
157
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
158
                        Shape shp) {
159
                // TODO Implement it
160
                throw new Error("Not yet implemented!");
161

    
162
        }
163

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

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

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

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

    
189
        public void setXMLEntity(XMLEntity xml) {
190
                font = new Font(xml.getStringProperty("font"),
191
                                xml.getIntProperty("fontStyle"),
192
                                xml.getIntProperty("size"));
193
                setDescription(xml.getStringProperty("desc"));
194
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
195
                text = xml.getStringProperty("text");
196
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
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
                        throws ReadDriverException {
205
                // TODO Implement it
206
                throw new Error("Not yet implemented!");
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
                this.text = text;
224
        }
225

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

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

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

    
238
        public void setRotation(double rotation) {
239
                this.rotation = rotation;
240
        }
241

    
242
        /**
243
         * Returns an FShape which represents a rectangle containing the text in
244
         * <b>screen</b> units.
245
         */
246
        public FShape getTextWrappingShape(Graphics2D g, /*AffineTransform affineTransform,*/ FShape targetSymbolShape ) {
247
                // assuming FShape is a point with the starting position of the text
248
                Font font = getFont();
249

    
250
                FontRenderContext frc = g.getFontRenderContext();
251

    
252
                GlyphVector gv = font.createGlyphVector(frc, text);
253
                Point2D p = null;
254

    
255
                switch (targetSymbolShape.getShapeType()) {
256
                case FShape.POINT:
257
                        FPoint2D fp=(FPoint2D) targetSymbolShape;
258
                        p = new Point2D.Double(fp.getX(),fp.getY());
259
                        break;
260
                case FShape.LINE:
261
                        PathLength pathLen = new PathLength(targetSymbolShape);
262

    
263
                        // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
264
                        float midDistance = pathLen.lengthOfPath() / 2;
265
                        p = pathLen.pointAtLength(midDistance);
266
                        break;
267
                case FShape.POLYGON:
268
                        Geometry geo = FConverter.java2d_to_jts(targetSymbolShape);
269

    
270
                        if (geo == null) {
271
                                return null;
272
                        }
273

    
274
                        Point pJTS = geo.getCentroid();
275

    
276
                        if (pJTS != null) {
277
                                FShape pLabel = FConverter.jts_to_java2d(pJTS);
278
                                p= new Point2D.Double(((FPoint2D) pLabel).getX(),
279
                                                ((FPoint2D) pLabel).getY());
280
                        }
281
                        break;
282
                }
283

    
284

    
285
                /*p.transform(affineTransform);*/
286
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
287

    
288
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
289
                if (rotation != 0) {
290
                        myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY()));
291
                }
292
                return myFShape;
293
        }
294

    
295
}