Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / SimpleTextSymbol.java @ 23303

History | View | Annotate | Download (9.3 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 org.gvsig.fmap.mapcontext.rendering.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 org.apache.log4j.Logger;
59
import org.gvsig.fmap.geom.Geometry;
60
import org.gvsig.fmap.geom.primitive.GeneralPathX;
61
import org.gvsig.fmap.geom.primitive.Surface2D;
62
import org.gvsig.fmap.mapcontext.ViewPort;
63

    
64
import com.iver.utiles.StringUtilities;
65
import com.iver.utiles.XMLEntity;
66
import com.iver.utiles.swing.threads.Cancellable;
67

    
68
/**
69
 * SimpleTextSymbol is a class used to create symbols composed using a text defined by
70
 * the user.This text can be edited (changing the color, the font of the characters, and
71
 * the rotation of the text).
72
 * @author jaume dominguez faus - jaume.dominguez@iver.es
73
 *
74
 */
75
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol {
76
        private String text = "";
77
        private Font font = new Font("Arial", Font.PLAIN, 12);
78
        private Color textColor = Color.BLACK;
79
        private double rotation;
80
        private FontRenderContext frc = new FontRenderContext(
81
                        new AffineTransform(), false, true);
82
        private boolean autoresize;
83
        public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Cancellable cancel) {
84
                if (!isShapeVisible()) {
85
                        return;
86
                }
87
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
88
                g.setColor(textColor);
89
                g.setFont(font);
90
                g.translate(((org.gvsig.fmap.geom.primitive.Point2D) geom).getX(), ((org.gvsig.fmap.geom.primitive.Point2D) geom).getY());
91

    
92
                g.rotate(rotation);
93
                Rectangle2D bounds = getBounds();
94

    
95
                g.drawString(getText(), 0, (int) bounds.getHeight());
96
                g.rotate(-rotation);
97
                g.translate(-((org.gvsig.fmap.geom.primitive.Point2D) geom).getX(), -((org.gvsig.fmap.geom.primitive.Point2D) geom).getY());
98
        }
99

    
100
        public void drawInsideRectangle(Graphics2D g,
101
                        AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
102
                int s = getFont().getSize();
103
                if (autoresize) {
104
                        if (s==0) {
105
                                s =1;
106
                                setFontSize(s);
107
                        }
108
                        g.setFont(getFont());
109
                    FontMetrics fm = g.getFontMetrics();
110
                    Rectangle2D rect = fm.getStringBounds(text, g);
111
                    double width = rect.getWidth();
112
                    double height = rect.getHeight();
113
                    double rWidth = r.getWidth();
114
                    double rHeight = r.getHeight();
115
                    double ratioText = width/height;
116
                    double ratioRect = rWidth/rHeight;
117

    
118
                    if (ratioText>ratioRect) {
119
                            s = (int) (s*(rWidth/width));
120
                    } else {
121
                            s = (int) (s*(rHeight/height));
122
                    }
123
//                    r.setLocation(r.x, (int) Math.round(r.getY()+r.getHeight()));
124
                }
125
                setFontSize(s);
126
//                g.setColor(Color.red);
127
//                g.draw(r);
128
                draw(g, null, new org.gvsig.fmap.geom.primitive.Point2D(r.getX(), r.getY()), null);
129

    
130

    
131
        }
132

    
133
        public int getOnePointRgb() {
134
                return textColor.getRGB();
135
        }
136

    
137
        public void getPixExtentPlus(org.gvsig.fmap.geom.Geometry geom, float[] distances,
138
                        ViewPort viewPort, int dpi) {
139
                throw new Error("Not yet implemented!");
140

    
141
        }
142

    
143
        public ISymbol getSymbolForSelection() {
144
                return this; // a text is not selectable
145
        }
146

    
147
        public int getSymbolType() {
148
                return Geometry.TYPES.TEXT;
149
        }
150

    
151
        public XMLEntity getXMLEntity() {
152
                XMLEntity xml = new XMLEntity();
153
                xml.putProperty("className", getClassName());
154
                xml.putProperty("desc", getDescription());
155
                xml.putProperty("isShapeVisible", isShapeVisible());
156
                xml.putProperty("font", font.getName());
157
                xml.putProperty("fontStyle", font.getStyle());
158
                xml.putProperty("size", font.getSize());
159
                xml.putProperty("text", text);
160
                xml.putProperty("textColor", StringUtilities.color2String(textColor));
161
                xml.putProperty("unit", getUnit());
162
                xml.putProperty("referenceSystem", getReferenceSystem());
163
                xml.putProperty("autoresizeFlag", isAutoresizeEnabled());
164
                return xml;
165
        }
166

    
167
        public boolean isSuitableFor(Geometry geom) {
168
                return true;
169
        }
170

    
171
        public void setXMLEntity(XMLEntity xml) {
172
                font = new Font(xml.getStringProperty("font"),
173
                                xml.getIntProperty("fontStyle"),
174
                                xml.getIntProperty("size"));
175
                setDescription(xml.getStringProperty("desc"));
176
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
177
                text = xml.getStringProperty("text");
178
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
179
                setUnit(xml.getIntProperty("unit"));
180
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
181
                setAutoresizeEnabled(xml.getBooleanProperty("autoresizeFlag"));
182
        }
183

    
184
        public String getClassName() {
185
                return getClass().getName();
186
        }
187

    
188
        public void print(Graphics2D g, AffineTransform at, org.gvsig.fmap.geom.Geometry geom, PrintRequestAttributeSet properties){
189
                float originalSize = getFont().getSize2D();
190
                float size=originalSize;
191
                // scale it to size
192
                PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class);
193
                if (pq.equals(PrintQuality.NORMAL)){
194
                        size *= (double) 300/72;
195
                }else if (pq.equals(PrintQuality.HIGH)){
196
                        size *= (double) 600/72;
197
                }else if (pq.equals(PrintQuality.DRAFT)){
198
                        //        unitFactor *= 72; (which is the same than doing nothing)
199
                }
200
                setFont(getFont().deriveFont(size));
201
                draw(g,at,geom,null);
202
                setFont(getFont().deriveFont(originalSize));
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
                if (font == null) {
223
                        Logger.getLogger(this.getClass()).warn("font <-- null");
224

    
225
                        return;
226
                }
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
                Font newFont = new Font(font.getName(), font.getStyle(), (int) size);
236
                if (newFont == null) {
237
                        Logger.getLogger(this.getClass()).warn(
238
                                        "Font(" + font.getName() + ", "
239
                                        + font.getStyle() + ", " + (int) size + " --> null");
240
                } else {
241
                        this.font = newFont;
242
                }
243
        }
244

    
245
        /**
246
         * Defines the angle of rotation for the text that composes the symbol
247
         *
248
         * @param rotation
249
         */
250
        public void setRotation(double rotation) {
251
                this.rotation = rotation;
252
        }
253

    
254
        /**
255
         * Returns an Geometry which represents a rectangle containing the text in
256
         * <b>screen</b> units.
257
         */
258
        public Geometry getTextWrappingShape(org.gvsig.fmap.geom.primitive.Point2D p) {
259
                Font font = getFont();
260
                GlyphVector gv = font.createGlyphVector(frc, text);
261

    
262
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
263
                Geometry myFShape = new Surface2D(new GeneralPathX(shape.getBounds2D()));
264

    
265
                myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
266

    
267
                if (rotation != 0) {
268
                        myFShape.transform(AffineTransform.getRotateInstance(rotation));
269
                }
270
                return myFShape;
271
        }
272

    
273
        public Rectangle getBounds() {
274
//                FontMetrics fm = g.getFontMetrics();
275
//                Rectangle2D rect = fm.getStringBounds("graphics", g);
276

    
277
                Rectangle bounds = getTextWrappingShape(new org.gvsig.fmap.geom.primitive.Point2D(0,0)).getBounds();
278
                return bounds;
279
        }
280

    
281
        public void setCartographicSize(double cartographicSize, Geometry geom) {
282
                setFontSize(cartographicSize);
283
        }
284

    
285
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
286
                double oldSize = getFont().getSize();
287
                setCartographicSize(getCartographicSize(
288
                                                                viewPort,
289
                                                                dpi,
290
                                                                geom),
291
                                                        geom);
292
                return oldSize;
293
        }
294

    
295
        public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
296
                return CartographicSupportToolkit.
297
                                        getCartographicLength(this,
298
                                                                                  getFont().getSize(),
299
                                                                                  viewPort,
300
                                                                                  dpi);
301
        }
302
        public boolean isAutoresizeEnabled() {
303
                return autoresize;
304
        }
305

    
306
        public void setAutoresizeEnabled(boolean autoresizeFlag) {
307
                this.autoresize = autoresizeFlag;
308
        }
309
}