Statistics
| Revision:

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

History | View | Annotate | Download (11.1 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
import java.awt.geom.Rectangle2D;
53

    
54
import javax.print.attribute.PrintRequestAttributeSet;
55

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

    
58
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
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.v02.FConverter;
67
import com.iver.utiles.StringUtilities;
68
import com.iver.utiles.XMLEntity;
69
import com.iver.utiles.swing.threads.Cancellable;
70
import com.vividsolutions.jts.geom.Geometry;
71
import com.vividsolutions.jts.geom.Point;
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 = new Font("Arial", Font.PLAIN, 12);
83
        private Color textColor = Color.BLACK;
84
        private double rotation;
85
        private FontRenderContext frc = new FontRenderContext(
86
                        new AffineTransform(), false, true);
87

    
88
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
89
                if (!isShapeVisible()) return;
90

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

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

    
112
                        if (geo == null) {
113
                                return;
114
                        }
115

    
116
                        Point pJTS = geo.getCentroid();
117

    
118
                        if (pJTS != null) {
119
                                FShape pLabel = FConverter.jts_to_java2d(pJTS);
120
                                p= new Point2D.Double(((FPoint2D) pLabel).getX(),
121
                                                ((FPoint2D) pLabel).getY());
122
                        }
123
                        break;
124
                }
125

    
126
                g.setColor(textColor);
127
                g.setFont(font);
128
                g.translate(p.getX(), p.getY());
129

    
130
                // TODO comprovar que els radians no estiga rotant-los al rev?s
131
                // en AttrInTableLabels est? posat a -rot per a que els veja b?
132
                // i ?s prou sospit?s. Mira on m?s es fa ?s d'este s?mbol i comprova
133
                // que all? no estiga fent-se tamb? -rot, si ?s aix?, ?s perqu? ac?
134
                // est? al reves (i en tots els llocs on es gasta)
135
                g.rotate(rot);
136
                g.drawString(getText(), 0, getBounds().height);
137
                Rectangle2D bounds = getBounds();
138
                bounds.setFrame(0, 0, bounds.getWidth(), bounds.getHeight());
139
                g.rotate(-rot);
140
                g.translate(-p.getX(), -p.getY());
141
        }
142

    
143
        public void drawInsideRectangle(Graphics2D g,
144
                        AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
145
                float fontSize = getFont().getSize();
146
                float fontScaleFactor = (float) ((getText().length()*fontSize) * 0.8) // text length with a 20% margin
147
                                                                / r.width;
148
                g.setColor(textColor);
149
                g.translate(r.x, r.y);
150
                fontSize = fontSize /fontScaleFactor;
151
                if (fontSize > r.height)
152
                        fontSize = r.height;
153
                g.setFont(new Font(getFont().getName(), getFont().getStyle(), (int)fontSize));
154
                g.drawString(getText(), 0, fontSize);
155
                
156
                g.translate(-r.x, -r.y);
157
                
158
        }
159

    
160
        public int getOnePointRgb() {
161
                return textColor.getRGB();
162
        }
163

    
164
        public void getPixExtentPlus(FShape shp, float[] distances,
165
                        ViewPort viewPort, int dpi) {
166
                // TODO Implement it
167
                throw new Error("Not yet implemented!");
168

    
169
        }
170

    
171
        public ISymbol getSymbolForSelection() {
172
                return this; // a text is not selectable
173
        }
174

    
175
        public int getSymbolType() {
176
                return FShape.TEXT;
177
        }
178

    
179
        public XMLEntity getXMLEntity() {
180
                XMLEntity xml = new XMLEntity();
181
                xml.putProperty("className", getClassName());
182
                xml.putProperty("desc", getDescription());
183
                xml.putProperty("isShapeVisible", isShapeVisible());
184
                xml.putProperty("font", font.getName());
185
                xml.putProperty("fontStyle", font.getStyle());
186
                xml.putProperty("size", font.getSize());
187
                xml.putProperty("text", text);
188
                xml.putProperty("textColor", StringUtilities.color2String(textColor));
189
                xml.putProperty("unit", getUnit());
190
                xml.putProperty("referenceSystem", getReferenceSystem());
191
                return xml;
192
        }
193

    
194
        public boolean isSuitableFor(IGeometry geom) {
195
                return true;
196
        }
197

    
198
        public void setXMLEntity(XMLEntity xml) {
199
                font = new Font(xml.getStringProperty("font"),
200
                                xml.getIntProperty("fontStyle"),
201
                                xml.getIntProperty("size"));
202
                setDescription(xml.getStringProperty("desc"));
203
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
204
                text = xml.getStringProperty("text");
205
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
206
                setUnit(xml.getIntProperty("unit"));
207
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
208
        }
209

    
210
        public String getClassName() {
211
                return getClass().getName();
212
        }
213

    
214
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
215
                        throws ReadDriverException {
216
                // TODO Implement it
217
                throw new Error("Not yet implemented!");
218

    
219
        }
220

    
221
        public String getText() {
222
                return text;
223
        }
224

    
225
        public Font getFont() {
226
                return font;
227
        }
228

    
229
        public Color getTextColor() {
230
                return textColor;
231
        }
232

    
233
        public void setText(String text) {
234
                this.text = text;
235
        }
236

    
237
        public void setFont(Font font) {
238
                this.font = font;
239
        }
240

    
241
        public void setTextColor(Color color) {
242
                this.textColor = color;
243
        }
244

    
245
        public void setFontSize(double size) {
246
                this.font = new Font(font.getName(), font.getStyle(), (int) size);
247
        }
248

    
249
        /**
250
         * Defines the angle of rotation for the text that composes the symbol
251
         *
252
         * @param rotation
253
         */
254
        public void setRotation(double rotation) {
255
                this.rotation = rotation;
256
        }
257

    
258
        /**
259
         * Returns an FShape which represents a rectangle containing the text in
260
         * <b>screen</b> units.
261
         */
262
        public FShape getTextWrappingShape(FPoint2D p) {
263
                Font font = getFont();
264
                GlyphVector gv = font.createGlyphVector(frc, text);
265

    
266
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
267
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds()));
268
                
269
                myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
270
                
271
                if (rotation != 0) {
272
                        myFShape.transform(AffineTransform.getRotateInstance(rotation));
273
                }
274
                return myFShape;
275
        }
276
        
277
        public Rectangle getBounds() {
278
                Rectangle bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds();
279
//                bounds.setLocation(
280
//                                (int) Math.round(bounds.getX()), 
281
//                                (int) Math.round(bounds.getY()-bounds.getHeight()));
282
//        
283
                return bounds;
284
        }
285

    
286
        /**
287
         * Returns an FShape which represents a rectangle containing the text in
288
         * <b>screen</b> units.
289
         */
290
        private FShape _getTextWrappingShape(Graphics2D g, /*AffineTransform affineTransform,*/ FPoint2D p) {
291
                // assuming FShape is a point with the starting position of the text
292
                Font font = getFont();
293

    
294
//                FontRenderContext frc = g.getFontRenderContext();
295
                FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, true);
296

    
297

    
298
                GlyphVector gv = font.createGlyphVector(frc, text);
299

    
300
                /*p.transform(affineTransform);*/
301
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
302
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
303
                if (rotation != 0) {
304
                        myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY()));
305
                }
306
                return myFShape;
307
        }
308
        /**
309
         * Returns an FShape which represents a rectangle containing the text in
310
         * <b>screen</b> units.
311
         */
312
        private FShape _getTextWrappingShape(Graphics2D g, /*AffineTransform affineTransform,*/ FShape targetSymbolShape ) {
313
                // assuming FShape is a point with the starting position of the text
314
                Font font = getFont();
315

    
316
                FontRenderContext frc = g.getFontRenderContext();
317
                GlyphVector gv = font.createGlyphVector(frc, text);
318
                Point2D p = null;
319

    
320
                switch (targetSymbolShape.getShapeType()) {
321
                case FShape.POINT:
322
                        FPoint2D fp=(FPoint2D) targetSymbolShape;
323
                        p = new Point2D.Double(fp.getX(),fp.getY());
324
                        break;
325
                case FShape.LINE:
326
                        PathLength pathLen = new PathLength(targetSymbolShape);
327

    
328
                        // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
329
                        float midDistance = pathLen.lengthOfPath() / 2;
330
                        p = pathLen.pointAtLength(midDistance);
331
                        break;
332
                case FShape.POLYGON:
333
                        Geometry geo = FConverter.java2d_to_jts(targetSymbolShape);
334

    
335
                        if (geo == null) {
336
                                return null;
337
                        }
338

    
339
                        Point pJTS = geo.getCentroid();
340

    
341
                        if (pJTS != null) {
342
                                FShape pLabel = FConverter.jts_to_java2d(pJTS);
343
                                p= new Point2D.Double(((FPoint2D) pLabel).getX(),
344
                                                ((FPoint2D) pLabel).getY());
345
                        }
346
                        break;
347
                }
348

    
349

    
350
                /*p.transform(affineTransform);*/
351
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
352

    
353
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
354
                if (rotation != 0) {
355
                        myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY()));
356
                }
357
                return myFShape;
358
        }
359

    
360

    
361
        public void setCartographicSize(double cartographicSize, FShape shp) {
362
                setFontSize(cartographicSize);
363
        }
364

    
365
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
366
                double oldSize = getFont().getSize();
367
                setCartographicSize(getCartographicSize(
368
                                                                viewPort,
369
                                                                dpi,
370
                                                                shp),
371
                                                        shp);
372
                return oldSize;
373
        }
374

    
375
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
376
                return CartographicSupportToolkit.
377
                                        getCartographicLength(this,
378
                                                                                  getFont().getSize(),
379
                                                                                  viewPort,
380
                                                                                  dpi);
381
        }
382
}