svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / SimpleTextSymbol.java @ 21535
History | View | Annotate | Download (8.28 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.cit.gvsig.fmap.core.v02.FConstant; |
66 |
import com.iver.utiles.StringUtilities; |
67 |
import com.iver.utiles.XMLEntity; |
68 |
import com.iver.utiles.swing.threads.Cancellable; |
69 |
|
70 |
/**
|
71 |
* SimpleTextSymbol is a class used to create symbols composed using a text defined by
|
72 |
* the user.This text can be edited (changing the color, the font of the characters, and
|
73 |
* the rotation of the text).
|
74 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
75 |
*
|
76 |
*/
|
77 |
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol { |
78 |
private String text = ""; |
79 |
private Font font = new Font("Arial", Font.PLAIN, 12); |
80 |
private Color textColor = Color.BLACK; |
81 |
private double rotation; |
82 |
private FontRenderContext frc = new FontRenderContext( |
83 |
new AffineTransform(), false, true); |
84 |
private boolean autoresize; |
85 |
|
86 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { |
87 |
if (!isShapeVisible()) return; |
88 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
89 |
g.setColor(textColor); |
90 |
g.setFont(font); |
91 |
g.translate(((FPoint2D) shp).getX(), ((FPoint2D) shp).getY()); |
92 |
|
93 |
g.rotate(rotation); |
94 |
Rectangle2D bounds = getBounds();
|
95 |
|
96 |
g.drawString(getText(), 0, (int) bounds.getHeight()); |
97 |
// bounds.setFrame(0, 0, bounds.getWidth(), bounds.getHeight());
|
98 |
g.rotate(-rotation); |
99 |
g.translate(-((FPoint2D) shp).getX(), -((FPoint2D) shp).getY()); |
100 |
} |
101 |
|
102 |
public void drawInsideRectangle(Graphics2D g, |
103 |
AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException { |
104 |
int s = getFont().getSize();
|
105 |
if (autoresize) {
|
106 |
if (s==0) { |
107 |
s =1;
|
108 |
setFontSize(s); |
109 |
} |
110 |
g.setFont(getFont()); |
111 |
FontMetrics fm = g.getFontMetrics();
|
112 |
Rectangle2D rect = fm.getStringBounds(text, g);
|
113 |
double width = rect.getWidth();
|
114 |
double height = rect.getHeight();
|
115 |
double rWidth = r.getWidth();
|
116 |
double rHeight = r.getHeight();
|
117 |
double ratioText = width/height;
|
118 |
double ratioRect = rWidth/rHeight;
|
119 |
|
120 |
if (ratioText>ratioRect) {
|
121 |
s = (int) (s*(rWidth/width));
|
122 |
} else {
|
123 |
s = (int) (s*(rHeight/height));
|
124 |
} |
125 |
// r.setLocation(r.x, (int) Math.round(r.getY()+r.getHeight()));
|
126 |
} |
127 |
setFontSize(s); |
128 |
// g.setColor(Color.red);
|
129 |
// g.draw(r);
|
130 |
draw(g, null, new FPoint2D(r.getX(), r.getY()), null); |
131 |
|
132 |
|
133 |
} |
134 |
|
135 |
public int getOnePointRgb() { |
136 |
return textColor.getRGB();
|
137 |
} |
138 |
|
139 |
public void getPixExtentPlus(FShape shp, float[] distances, |
140 |
ViewPort viewPort, int dpi) {
|
141 |
throw new Error("Not yet implemented!"); |
142 |
|
143 |
} |
144 |
|
145 |
public ISymbol getSymbolForSelection() {
|
146 |
return this; // a text is not selectable |
147 |
} |
148 |
|
149 |
public int getSymbolType() { |
150 |
return FShape.TEXT;
|
151 |
} |
152 |
|
153 |
public XMLEntity getXMLEntity() {
|
154 |
XMLEntity xml = new XMLEntity();
|
155 |
xml.putProperty("className", getClassName());
|
156 |
xml.putProperty("desc", getDescription());
|
157 |
xml.putProperty("isShapeVisible", isShapeVisible());
|
158 |
xml.putProperty("font", font.getName());
|
159 |
xml.putProperty("fontStyle", font.getStyle());
|
160 |
xml.putProperty("size", font.getSize());
|
161 |
xml.putProperty("text", text);
|
162 |
xml.putProperty("textColor", StringUtilities.color2String(textColor));
|
163 |
xml.putProperty("unit", getUnit());
|
164 |
xml.putProperty("referenceSystem", getReferenceSystem());
|
165 |
xml.putProperty("autoresizeFlag", isAutoresizeEnabled());
|
166 |
return xml;
|
167 |
} |
168 |
|
169 |
public boolean isSuitableFor(IGeometry geom) { |
170 |
return true; |
171 |
} |
172 |
|
173 |
public void setXMLEntity(XMLEntity xml) { |
174 |
font = new Font(xml.getStringProperty("font"), |
175 |
xml.getIntProperty("fontStyle"),
|
176 |
xml.getIntProperty("size"));
|
177 |
setDescription(xml.getStringProperty("desc"));
|
178 |
setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
179 |
text = xml.getStringProperty("text");
|
180 |
textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
|
181 |
setUnit(xml.getIntProperty("unit"));
|
182 |
setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
183 |
setAutoresizeEnabled(xml.getBooleanProperty("autoresizeFlag"));
|
184 |
} |
185 |
|
186 |
public String getClassName() { |
187 |
return getClass().getName();
|
188 |
} |
189 |
|
190 |
public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) |
191 |
throws ReadDriverException {
|
192 |
// TODO Implement it
|
193 |
throw new Error("Not yet implemented!"); |
194 |
|
195 |
} |
196 |
|
197 |
public String getText() { |
198 |
return text;
|
199 |
} |
200 |
|
201 |
public Font getFont() { |
202 |
return font;
|
203 |
} |
204 |
|
205 |
public Color getTextColor() { |
206 |
return textColor;
|
207 |
} |
208 |
|
209 |
public void setText(String text) { |
210 |
this.text = text;
|
211 |
} |
212 |
|
213 |
public void setFont(Font font) { |
214 |
this.font = font;
|
215 |
} |
216 |
|
217 |
public void setTextColor(Color color) { |
218 |
this.textColor = color;
|
219 |
} |
220 |
|
221 |
public void setFontSize(double size) { |
222 |
this.font = new Font(font.getName(), font.getStyle(), (int) size); |
223 |
} |
224 |
|
225 |
/**
|
226 |
* Defines the angle of rotation for the text that composes the symbol
|
227 |
*
|
228 |
* @param rotation
|
229 |
*/
|
230 |
public void setRotation(double rotation) { |
231 |
this.rotation = rotation;
|
232 |
} |
233 |
|
234 |
/**
|
235 |
* Returns an FShape which represents a rectangle containing the text in
|
236 |
* <b>screen</b> units.
|
237 |
*/
|
238 |
public FShape getTextWrappingShape(FPoint2D p) {
|
239 |
Font font = getFont();
|
240 |
GlyphVector gv = font.createGlyphVector(frc, text);
|
241 |
|
242 |
Shape shape = gv.getOutline((float) p.getX(), (float) p.getY()); |
243 |
FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D())); |
244 |
|
245 |
myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
|
246 |
|
247 |
if (rotation != 0) { |
248 |
myFShape.transform(AffineTransform.getRotateInstance(rotation));
|
249 |
} |
250 |
return myFShape;
|
251 |
} |
252 |
|
253 |
public Rectangle getBounds() { |
254 |
// FontMetrics fm = g.getFontMetrics();
|
255 |
// Rectangle2D rect = fm.getStringBounds("graphics", g);
|
256 |
|
257 |
Rectangle bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds(); |
258 |
return bounds;
|
259 |
} |
260 |
|
261 |
public void setCartographicSize(double cartographicSize, FShape shp) { |
262 |
setFontSize(cartographicSize); |
263 |
} |
264 |
|
265 |
public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
266 |
double oldSize = getFont().getSize();
|
267 |
setCartographicSize(getCartographicSize( |
268 |
viewPort, |
269 |
dpi, |
270 |
shp), |
271 |
shp); |
272 |
return oldSize;
|
273 |
} |
274 |
|
275 |
public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
276 |
return CartographicSupportToolkit.
|
277 |
getCartographicLength(this,
|
278 |
getFont().getSize(), |
279 |
viewPort, |
280 |
dpi); |
281 |
} |
282 |
|
283 |
public boolean isAutoresizeEnabled() { |
284 |
return autoresize;
|
285 |
} |
286 |
|
287 |
public void setAutoresizeEnabled(boolean autoresizeFlag) { |
288 |
this.autoresize = autoresizeFlag;
|
289 |
} |
290 |
} |