root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / SimpleTextSymbol.java @ 32111
History | View | Annotate | Download (10.6 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 |
import javax.print.attribute.standard.PrintQuality; |
57 |
|
58 |
import com.iver.cit.gvsig.fmap.MapContext; |
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.SymbologyFactory; |
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.iver.utiles.swing.threads.Cancellable; |
71 |
|
72 |
/**
|
73 |
* SimpleTextSymbol is a class used to create symbols composed using a text defined by
|
74 |
* the user.This text can be edited (changing the color, the font of the characters, and
|
75 |
* the rotation of the text).
|
76 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
77 |
*
|
78 |
*/
|
79 |
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol { |
80 |
private String text = ""; |
81 |
private Font font = SymbologyFactory.DefaultTextFont; |
82 |
private Color textColor = Color.BLACK; |
83 |
private double rotation; |
84 |
private FontRenderContext frc = new FontRenderContext( |
85 |
new AffineTransform(), false, true); |
86 |
private boolean autoresize; |
87 |
private Rectangle bounds = null; |
88 |
private FShape horizontalTextWrappingShape = null; |
89 |
private PrintRequestAttributeSet properties; |
90 |
|
91 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { |
92 |
if (!isShapeVisible()) return; |
93 |
double shpX = ((FPoint2D) shp).getX();
|
94 |
double shpY = ((FPoint2D) shp).getY();
|
95 |
//Parche porque a veces llegan puntos cuyas coordenadas no han podido ser calculadas y vienen como NaN
|
96 |
if( Double.isNaN(shpX) || Double.isNaN(shpY)){ |
97 |
return;
|
98 |
} |
99 |
//Fin del parche
|
100 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
101 |
g.setColor(textColor); |
102 |
g.setFont(font); |
103 |
g.translate(shpX, shpY); |
104 |
|
105 |
g.rotate(rotation); |
106 |
Rectangle2D bounds = getHorizontalTextWrappingShape(new FPoint2D(0,0)).getBounds(); |
107 |
// Antes tomabamos el resultado de getBounds pero ya se le hab?a aplicado
|
108 |
// la rotaci?n, con lo que no obten?amos el la altura correcta de la fuente.
|
109 |
|
110 |
// Alineamos el texto de manera que la parte superior
|
111 |
// izquierda de la primera letra est? en (0,0).
|
112 |
// Para chino hay que escoger una fuente como esta (SimSun)
|
113 |
// g.setFont(new Font("SimSun",Font.PLAIN, 12));
|
114 |
|
115 |
g.drawString(getText(), 0, (int)-bounds.getY()); |
116 |
g.rotate(-rotation); |
117 |
g.translate(-shpX, -shpY); |
118 |
} |
119 |
|
120 |
public void drawInsideRectangle(Graphics2D g, |
121 |
AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException { |
122 |
int s = getFont().getSize();
|
123 |
|
124 |
if (autoresize) {
|
125 |
if (s==0) { |
126 |
s =1;
|
127 |
setFontSize(s); |
128 |
} |
129 |
g.setFont(getFont()); |
130 |
FontMetrics fm = g.getFontMetrics();
|
131 |
Rectangle2D rect = fm.getStringBounds(text, g);
|
132 |
double width = rect.getWidth();
|
133 |
double height = rect.getHeight();
|
134 |
double rWidth = r.getWidth();
|
135 |
double rHeight = r.getHeight();
|
136 |
double ratioText = width/height;
|
137 |
double ratioRect = rWidth/rHeight;
|
138 |
|
139 |
if (ratioText>ratioRect) {
|
140 |
s = (int) (s*(rWidth/width));
|
141 |
} else {
|
142 |
s = (int) (s*(rHeight/height));
|
143 |
} |
144 |
setFontSize(s); |
145 |
} |
146 |
|
147 |
//Only for debugging purpose
|
148 |
// g.drawRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight());
|
149 |
if (properties==null) |
150 |
draw(g, null, new FPoint2D(r.getX(), r.getY()), null); |
151 |
else
|
152 |
print(g, new AffineTransform(), new FPoint2D(r.getX(), r.getY()), properties); |
153 |
|
154 |
} |
155 |
|
156 |
public int getOnePointRgb() { |
157 |
return textColor.getRGB();
|
158 |
} |
159 |
|
160 |
public void getPixExtentPlus(FShape shp, float[] distances, |
161 |
ViewPort viewPort, int dpi) {
|
162 |
throw new Error("Not yet implemented!"); |
163 |
|
164 |
} |
165 |
|
166 |
public ISymbol getSymbolForSelection() {
|
167 |
return this; // a text is not selectable |
168 |
} |
169 |
|
170 |
public int getSymbolType() { |
171 |
return FShape.TEXT;
|
172 |
} |
173 |
|
174 |
public XMLEntity getXMLEntity() {
|
175 |
XMLEntity xml = new XMLEntity();
|
176 |
xml.putProperty("className", getClassName());
|
177 |
xml.putProperty("desc", getDescription());
|
178 |
xml.putProperty("isShapeVisible", isShapeVisible());
|
179 |
xml.putProperty("font", font.getName());
|
180 |
xml.putProperty("fontStyle", font.getStyle());
|
181 |
xml.putProperty("size", font.getSize());
|
182 |
xml.putProperty("text", text);
|
183 |
xml.putProperty("textColor", StringUtilities.color2String(textColor));
|
184 |
xml.putProperty("unit", getUnit());
|
185 |
xml.putProperty("referenceSystem", getReferenceSystem());
|
186 |
xml.putProperty("autoresizeFlag", isAutoresizeEnabled());
|
187 |
return xml;
|
188 |
} |
189 |
|
190 |
public boolean isSuitableFor(IGeometry geom) { |
191 |
return true; |
192 |
} |
193 |
|
194 |
public void setXMLEntity(XMLEntity xml) { |
195 |
font = new Font(xml.getStringProperty("font"), |
196 |
xml.getIntProperty("fontStyle"),
|
197 |
xml.getIntProperty("size"));
|
198 |
setDescription(xml.getStringProperty("desc"));
|
199 |
setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
200 |
text = xml.getStringProperty("text");
|
201 |
textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
|
202 |
setUnit(xml.getIntProperty("unit"));
|
203 |
setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
204 |
setAutoresizeEnabled(xml.getBooleanProperty("autoresizeFlag"));
|
205 |
this.bounds = null; |
206 |
this.horizontalTextWrappingShape = null; |
207 |
} |
208 |
|
209 |
public String getClassName() { |
210 |
return getClass().getName();
|
211 |
} |
212 |
|
213 |
public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) { |
214 |
this.properties=properties;
|
215 |
draw(g, at, shape, null);
|
216 |
this.properties=null; |
217 |
|
218 |
} |
219 |
|
220 |
public String getText() { |
221 |
return text;
|
222 |
} |
223 |
|
224 |
public Font getFont() { |
225 |
return font;
|
226 |
} |
227 |
|
228 |
public Color getTextColor() { |
229 |
return textColor;
|
230 |
} |
231 |
|
232 |
public void setText(String text) { |
233 |
if(text != null && !text.equals(this.text)){ |
234 |
this.text = text;
|
235 |
this.bounds = null; |
236 |
this.horizontalTextWrappingShape = null; |
237 |
} |
238 |
} |
239 |
|
240 |
public void setFont(Font font) { |
241 |
if (font != null && !font.equals(this.font)){ |
242 |
this.font = font;
|
243 |
this.bounds = null; |
244 |
this.horizontalTextWrappingShape = null; |
245 |
} |
246 |
} |
247 |
|
248 |
public void setTextColor(Color color) { |
249 |
this.textColor = color;
|
250 |
} |
251 |
|
252 |
public void setFontSize(double size) { |
253 |
if (size != this.font.getSize2D()){ |
254 |
this.font = this.font.deriveFont((float) size); |
255 |
this.bounds = null; |
256 |
this.horizontalTextWrappingShape = null; |
257 |
} |
258 |
// this.font = new Font(this.font.getName(),this.font.getStyle(),(int)size);
|
259 |
} |
260 |
|
261 |
/**
|
262 |
* Defines the angle of rotation for the text that composes the symbol
|
263 |
*
|
264 |
* @param rotation
|
265 |
*/
|
266 |
public void setRotation(double rotation) { |
267 |
if(rotation != this.rotation){ |
268 |
this.rotation = rotation;
|
269 |
this.bounds = null; |
270 |
} |
271 |
} |
272 |
|
273 |
public double getRotation() { |
274 |
return rotation;
|
275 |
} |
276 |
|
277 |
/**
|
278 |
* Returns an FShape which represents a rectangle containing the text in
|
279 |
* <b>screen</b> units.
|
280 |
*/
|
281 |
private FShape getHorizontalTextWrappingShape(FPoint2D p) {
|
282 |
if (this.horizontalTextWrappingShape == null){ |
283 |
Font font = getFont();
|
284 |
/* Para tama?os de fuente de letras excesivamente grandes obtenemos
|
285 |
* shapes con todas las coordenadas a 0, por eso limitamos el tama?o
|
286 |
* a 1000 y despu?s reescalamos el bounds.
|
287 |
*/
|
288 |
double scale = 1; |
289 |
float fontSize = font.getSize2D();
|
290 |
if (fontSize > 1000){ |
291 |
scale = fontSize/1000;
|
292 |
fontSize = 1000;
|
293 |
} |
294 |
font = font.deriveFont(fontSize); |
295 |
GlyphVector gv = font.createGlyphVector(frc, text);
|
296 |
Shape shape = gv.getOutline((float) p.getX(), (float) p.getY()); |
297 |
FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D())); |
298 |
|
299 |
if(scale != 1){ |
300 |
myFShape.transform(AffineTransform.getScaleInstance(scale, scale));
|
301 |
} |
302 |
|
303 |
this.horizontalTextWrappingShape = myFShape;
|
304 |
} |
305 |
return this.horizontalTextWrappingShape; |
306 |
} |
307 |
|
308 |
/**
|
309 |
* Returns an FShape which represents a rectangle containing the text in
|
310 |
* <b>screen</b> units.
|
311 |
*/
|
312 |
public FShape getTextWrappingShape(FPoint2D p) {
|
313 |
|
314 |
FShape myFShape = getHorizontalTextWrappingShape(p); |
315 |
myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
|
316 |
|
317 |
if (rotation != 0) { |
318 |
myFShape.transform(AffineTransform.getRotateInstance(rotation));
|
319 |
} |
320 |
return myFShape;
|
321 |
} |
322 |
|
323 |
public Rectangle getBounds() { |
324 |
// FontMetrics fm = g.getFontMetrics();
|
325 |
// Rectangle2D rect = fm.getStringBounds("graphics", g);
|
326 |
|
327 |
if(this.bounds == null){ |
328 |
this.bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds(); |
329 |
} |
330 |
return this.bounds; |
331 |
} |
332 |
|
333 |
public void setCartographicSize(double cartographicSize, FShape shp) { |
334 |
setFontSize(cartographicSize); |
335 |
} |
336 |
|
337 |
public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
338 |
double oldSize = getFont().getSize();
|
339 |
setCartographicSize(getCartographicSize( |
340 |
viewPort, |
341 |
dpi, |
342 |
shp), |
343 |
shp); |
344 |
return oldSize;
|
345 |
} |
346 |
|
347 |
public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
348 |
return CartographicSupportToolkit.
|
349 |
getCartographicLength(this,
|
350 |
getFont().getSize(), |
351 |
viewPort, |
352 |
dpi); |
353 |
} |
354 |
|
355 |
public boolean isAutoresizeEnabled() { |
356 |
return autoresize;
|
357 |
} |
358 |
|
359 |
public void setAutoresizeEnabled(boolean autoresizeFlag) { |
360 |
this.autoresize = autoresizeFlag;
|
361 |
} |
362 |
} |