root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / SimpleTextSymbol.java @ 11104
History | View | Annotate | Download (8.21 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.Graphics2D; |
46 |
import java.awt.Rectangle; |
47 |
import java.awt.Shape; |
48 |
import java.awt.font.FontRenderContext; |
49 |
import java.awt.font.GlyphVector; |
50 |
import java.awt.geom.AffineTransform; |
51 |
import java.awt.geom.Point2D; |
52 |
|
53 |
import javax.print.attribute.PrintRequestAttributeSet; |
54 |
|
55 |
import org.apache.batik.ext.awt.geom.PathLength; |
56 |
|
57 |
import sun.reflect.ReflectionFactory.GetReflectionFactoryAction; |
58 |
|
59 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
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.FPolyline2D; |
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.TextPath; |
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.vividsolutions.jts.geom.Geometry; |
71 |
import com.vividsolutions.jts.geom.Point; |
72 |
|
73 |
/**
|
74 |
*
|
75 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
76 |
*
|
77 |
*/
|
78 |
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol { |
79 |
private String text = ""; |
80 |
private Font font = new Font("Arial", Font.PLAIN, 10); |
81 |
private Color textColor = Color.BLACK; |
82 |
private double rotation; |
83 |
|
84 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) { |
85 |
if (!isShapeVisible()) return; |
86 |
|
87 |
Point2D p = null; |
88 |
double rot=0; |
89 |
// shp.transform(affineTransform);
|
90 |
switch (shp.getShapeType()) {
|
91 |
case FShape.POINT:
|
92 |
FPoint2D fp=(FPoint2D) shp; |
93 |
p = new Point2D.Double(fp.getX(),fp.getY()); |
94 |
rot = rotation; |
95 |
break;
|
96 |
case FShape.LINE:
|
97 |
PathLength pathLen = new PathLength(shp);
|
98 |
|
99 |
// if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
|
100 |
float midDistance = pathLen.lengthOfPath() / 2; |
101 |
p = pathLen.pointAtLength(midDistance); |
102 |
rot = pathLen.angleAtLength(midDistance); |
103 |
break;
|
104 |
case FShape.POLYGON:
|
105 |
Geometry geo = FConverter.java2d_to_jts(shp); |
106 |
|
107 |
if (geo == null) { |
108 |
return;
|
109 |
} |
110 |
|
111 |
Point pJTS = geo.getCentroid();
|
112 |
|
113 |
if (pJTS != null) { |
114 |
FShape pLabel = FConverter.jts_to_java2d(pJTS); |
115 |
p= new Point2D.Double(((FPoint2D) pLabel).getX(), |
116 |
((FPoint2D) pLabel).getY()); |
117 |
} |
118 |
break;
|
119 |
} |
120 |
|
121 |
g.setColor(textColor); |
122 |
g.setFont(font); |
123 |
g.translate(p.getX(), p.getY()); |
124 |
|
125 |
// TODO comprovar que els radians no estiga rotant-los al rev?s
|
126 |
// en AttrInTableLabels est? posat a -rot per a que els veja b?
|
127 |
// i ?s prou sospit?s. Mira on m?s es fa ?s d'este s?mbol i comprova
|
128 |
// que all? no estiga fent-se tamb? -rot, si ?s aix?, ?s perqu? ac?
|
129 |
// est? al reves (i en tots els llocs on es gasta)
|
130 |
g.rotate(rot); |
131 |
g.drawString(getText(), 0, 0); |
132 |
g.rotate(-rot); |
133 |
g.translate(-p.getX(), -p.getY()); |
134 |
} |
135 |
|
136 |
public void drawInsideRectangle(Graphics2D g, |
137 |
AffineTransform scaleInstance, Rectangle r) { |
138 |
FontRenderContext frc = g.getFontRenderContext();
|
139 |
GlyphVector gv = font.createGlyphVector(frc, text);
|
140 |
float fontSize = getFont().getSize();
|
141 |
float fontScaleFactor = (float) ((getText().length()*fontSize) * 0.8) // text length with a 20% margin |
142 |
/ r.width; |
143 |
|
144 |
fontSize = fontSize / fontScaleFactor; |
145 |
// if (fontSize > r.height)
|
146 |
// fontSize = r.height;
|
147 |
g.setFont(getFont().deriveFont(fontSize)); |
148 |
g.drawString(getText(), r.x*0.9F, ( r.height*0.5F) + (fontSize)); |
149 |
// g.drawString(getText(), r.x, r.height);//( r.height*0.5F) + (fontSize*0.5F));
|
150 |
|
151 |
|
152 |
} |
153 |
|
154 |
public int getOnePointRgb() { |
155 |
return textColor.getRGB();
|
156 |
} |
157 |
|
158 |
public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform, |
159 |
Shape shp) {
|
160 |
// TODO Implement it
|
161 |
throw new Error("Not yet implemented!"); |
162 |
|
163 |
} |
164 |
|
165 |
public ISymbol getSymbolForSelection() {
|
166 |
return this; // a text is not selectable |
167 |
} |
168 |
|
169 |
public int getSymbolType() { |
170 |
return FShape.TEXT;
|
171 |
} |
172 |
|
173 |
public XMLEntity getXMLEntity() {
|
174 |
XMLEntity xml = new XMLEntity();
|
175 |
xml.putProperty("className", getClassName());
|
176 |
xml.putProperty("desc", getDescription());
|
177 |
xml.putProperty("isShapeVisible", isShapeVisible());
|
178 |
xml.putProperty("font", font.getName());
|
179 |
xml.putProperty("fontStyle", font.getStyle());
|
180 |
xml.putProperty("size", font.getSize());
|
181 |
xml.putProperty("text", text);
|
182 |
xml.putProperty("textColor", StringUtilities.color2String(textColor));
|
183 |
return xml;
|
184 |
} |
185 |
|
186 |
public boolean isSuitableFor(IGeometry geom) { |
187 |
return true; |
188 |
} |
189 |
|
190 |
public void setXMLEntity(XMLEntity xml) { |
191 |
font = new Font(xml.getStringProperty("font"), |
192 |
xml.getIntProperty("fontStyle"),
|
193 |
xml.getIntProperty("size"));
|
194 |
setDescription(xml.getStringProperty("desc"));
|
195 |
setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
196 |
text = xml.getStringProperty("text");
|
197 |
textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
|
198 |
} |
199 |
|
200 |
public String getClassName() { |
201 |
return getClass().getName();
|
202 |
} |
203 |
|
204 |
public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) |
205 |
throws ReadDriverException {
|
206 |
// TODO Implement it
|
207 |
throw new Error("Not yet implemented!"); |
208 |
|
209 |
} |
210 |
|
211 |
public String getText() { |
212 |
return text;
|
213 |
} |
214 |
|
215 |
public Font getFont() { |
216 |
return font;
|
217 |
} |
218 |
|
219 |
public Color getTextColor() { |
220 |
return textColor;
|
221 |
} |
222 |
|
223 |
public void setText(String text) { |
224 |
this.text = text;
|
225 |
} |
226 |
|
227 |
public void setFont(Font font) { |
228 |
this.font = font;
|
229 |
} |
230 |
|
231 |
public void setTextColor(Color color) { |
232 |
this.textColor = color;
|
233 |
} |
234 |
|
235 |
public void setFontSize(double size) { |
236 |
this.font = new Font(font.getName(), font.getStyle(), (int) size); |
237 |
} |
238 |
|
239 |
public void setRotation(double rotation) { |
240 |
this.rotation = rotation;
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* Returns an FShape which represents a rectangle containing the text in
|
245 |
* <b>screen</b> units.
|
246 |
*/
|
247 |
public FShape getTextWrappingShape(Graphics2D g, /*AffineTransform affineTransform,*/ FShape targetSymbolShape ) { |
248 |
// assuming FShape is a point with the starting position of the text
|
249 |
Font font = getFont();
|
250 |
|
251 |
FontRenderContext frc = g.getFontRenderContext();
|
252 |
|
253 |
GlyphVector gv = font.createGlyphVector(frc, text);
|
254 |
Point2D p = null; |
255 |
|
256 |
switch (targetSymbolShape.getShapeType()) {
|
257 |
case FShape.POINT:
|
258 |
FPoint2D fp=(FPoint2D) targetSymbolShape; |
259 |
p = new Point2D.Double(fp.getX(),fp.getY()); |
260 |
break;
|
261 |
case FShape.LINE:
|
262 |
PathLength pathLen = new PathLength(targetSymbolShape);
|
263 |
|
264 |
// if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
|
265 |
float midDistance = pathLen.lengthOfPath() / 2; |
266 |
p = pathLen.pointAtLength(midDistance); |
267 |
break;
|
268 |
case FShape.POLYGON:
|
269 |
Geometry geo = FConverter.java2d_to_jts(targetSymbolShape); |
270 |
|
271 |
if (geo == null) { |
272 |
return null; |
273 |
} |
274 |
|
275 |
Point pJTS = geo.getCentroid();
|
276 |
|
277 |
if (pJTS != null) { |
278 |
FShape pLabel = FConverter.jts_to_java2d(pJTS); |
279 |
p= new Point2D.Double(((FPoint2D) pLabel).getX(), |
280 |
((FPoint2D) pLabel).getY()); |
281 |
} |
282 |
break;
|
283 |
} |
284 |
|
285 |
|
286 |
/*p.transform(affineTransform);*/
|
287 |
Shape shape = gv.getOutline((float) p.getX(), (float) p.getY()); |
288 |
|
289 |
FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D())); |
290 |
if (rotation != 0) { |
291 |
myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY()));
|
292 |
} |
293 |
return myFShape;
|
294 |
} |
295 |
|
296 |
} |