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