svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / SimpleTextSymbol.java @ 23499
History | View | Annotate | Download (8.19 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 |
|
57 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
58 |
import com.iver.cit.gvsig.fmap.ViewPort; |
59 |
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit; |
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.FShape; |
63 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
64 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
65 |
import com.iver.utiles.StringUtilities; |
66 |
import com.iver.utiles.XMLEntity; |
67 |
import com.iver.utiles.swing.threads.Cancellable; |
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, 12); |
79 |
private Color textColor = Color.BLACK; |
80 |
private double rotation; |
81 |
private FontRenderContext frc = new FontRenderContext( |
82 |
new AffineTransform(), false, true); |
83 |
private boolean autoresize; |
84 |
|
85 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { |
86 |
if (!isShapeVisible()) return; |
87 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
88 |
g.setColor(textColor); |
89 |
g.setFont(font); |
90 |
g.translate(((FPoint2D) shp).getX(), ((FPoint2D) shp).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(-((FPoint2D) shp).getX(), -((FPoint2D) shp).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 FPoint2D(r.getX(), r.getY()), null); |
129 |
|
130 |
|
131 |
} |
132 |
|
133 |
public int getOnePointRgb() { |
134 |
return textColor.getRGB();
|
135 |
} |
136 |
|
137 |
public void getPixExtentPlus(FShape shp, 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 FShape.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(IGeometry 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, FShape shape, PrintRequestAttributeSet properties) |
189 |
throws ReadDriverException {
|
190 |
// TODO Implement it
|
191 |
throw new Error("Not yet implemented!"); |
192 |
|
193 |
} |
194 |
|
195 |
public String getText() { |
196 |
return text;
|
197 |
} |
198 |
|
199 |
public Font getFont() { |
200 |
return font;
|
201 |
} |
202 |
|
203 |
public Color getTextColor() { |
204 |
return textColor;
|
205 |
} |
206 |
|
207 |
public void setText(String text) { |
208 |
this.text = text;
|
209 |
} |
210 |
|
211 |
public void setFont(Font font) { |
212 |
this.font = font;
|
213 |
} |
214 |
|
215 |
public void setTextColor(Color color) { |
216 |
this.textColor = color;
|
217 |
} |
218 |
|
219 |
public void setFontSize(double size) { |
220 |
this.font = new Font(font.getName(), font.getStyle(), (int) size); |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Defines the angle of rotation for the text that composes the symbol
|
225 |
*
|
226 |
* @param rotation
|
227 |
*/
|
228 |
public void setRotation(double rotation) { |
229 |
this.rotation = rotation;
|
230 |
} |
231 |
|
232 |
public double getRotation() { |
233 |
return rotation;
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* Returns an FShape which represents a rectangle containing the text in
|
238 |
* <b>screen</b> units.
|
239 |
*/
|
240 |
public FShape getTextWrappingShape(FPoint2D p) {
|
241 |
Font font = getFont();
|
242 |
GlyphVector gv = font.createGlyphVector(frc, text);
|
243 |
|
244 |
Shape shape = gv.getOutline((float) p.getX(), (float) p.getY()); |
245 |
FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D())); |
246 |
|
247 |
myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
|
248 |
|
249 |
if (rotation != 0) { |
250 |
myFShape.transform(AffineTransform.getRotateInstance(rotation));
|
251 |
} |
252 |
return myFShape;
|
253 |
} |
254 |
|
255 |
public Rectangle getBounds() { |
256 |
// FontMetrics fm = g.getFontMetrics();
|
257 |
// Rectangle2D rect = fm.getStringBounds("graphics", g);
|
258 |
|
259 |
Rectangle bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds(); |
260 |
return bounds;
|
261 |
} |
262 |
|
263 |
public void setCartographicSize(double cartographicSize, FShape shp) { |
264 |
setFontSize(cartographicSize); |
265 |
} |
266 |
|
267 |
public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
268 |
double oldSize = getFont().getSize();
|
269 |
setCartographicSize(getCartographicSize( |
270 |
viewPort, |
271 |
dpi, |
272 |
shp), |
273 |
shp); |
274 |
return oldSize;
|
275 |
} |
276 |
|
277 |
public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
278 |
return CartographicSupportToolkit.
|
279 |
getCartographicLength(this,
|
280 |
getFont().getSize(), |
281 |
viewPort, |
282 |
dpi); |
283 |
} |
284 |
|
285 |
public boolean isAutoresizeEnabled() { |
286 |
return autoresize;
|
287 |
} |
288 |
|
289 |
public void setAutoresizeEnabled(boolean autoresizeFlag) { |
290 |
this.autoresize = autoresizeFlag;
|
291 |
} |
292 |
} |