Revision 20700
trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/core/symbols/SimpleTextSymbol.java | ||
---|---|---|
44 | 44 |
import java.awt.Font; |
45 | 45 |
import java.awt.Graphics2D; |
46 | 46 |
import java.awt.Rectangle; |
47 |
import java.awt.RenderingHints; |
|
47 | 48 |
import java.awt.Shape; |
48 | 49 |
import java.awt.font.FontRenderContext; |
49 | 50 |
import java.awt.font.GlyphVector; |
... | ... | |
87 | 88 |
|
88 | 89 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { |
89 | 90 |
if (!isShapeVisible()) return; |
90 |
|
|
91 |
// TODO remove this stuff and allow only points |
|
92 |
Point2D p = null; |
|
93 |
double rot=0; |
|
94 |
// shp.transform(affineTransform); |
|
95 |
switch (shp.getShapeType()) { |
|
96 |
case FShape.POINT: |
|
97 |
FPoint2D fp=(FPoint2D) shp; |
|
98 |
p = new Point2D.Double(fp.getX(),fp.getY()); |
|
99 |
rot = rotation; |
|
100 |
break; |
|
101 |
case FShape.LINE: |
|
102 |
PathLength pathLen = new PathLength(shp); |
|
103 |
|
|
104 |
// if (pathLen.lengthOfPath() < width / mT.getScaleX()) return; |
|
105 |
float midDistance = pathLen.lengthOfPath() / 2; |
|
106 |
p = pathLen.pointAtLength(midDistance); |
|
107 |
rot = pathLen.angleAtLength(midDistance); |
|
108 |
break; |
|
109 |
case FShape.POLYGON: |
|
110 |
Geometry geo = FConverter.java2d_to_jts(shp); |
|
111 |
|
|
112 |
if (geo == null) { |
|
113 |
return; |
|
114 |
} |
|
115 |
|
|
116 |
Point pJTS = geo.getCentroid(); |
|
117 |
|
|
118 |
if (pJTS != null) { |
|
119 |
FShape pLabel = FConverter.jts_to_java2d(pJTS); |
|
120 |
p= new Point2D.Double(((FPoint2D) pLabel).getX(), |
|
121 |
((FPoint2D) pLabel).getY()); |
|
122 |
} |
|
123 |
break; |
|
124 |
} |
|
125 |
|
|
91 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|
126 | 92 |
g.setColor(textColor); |
127 | 93 |
g.setFont(font); |
128 |
g.translate(p.getX(), p.getY());
|
|
94 |
g.translate(((FPoint2D) shp).getX(), ((FPoint2D) shp).getY());
|
|
129 | 95 |
|
130 |
// TODO comprovar que els radians no estiga rotant-los al rev?s |
|
131 |
// en AttrInTableLabels est? posat a -rot per a que els veja b? |
|
132 |
// i ?s prou sospit?s. Mira on m?s es fa ?s d'este s?mbol i comprova |
|
133 |
// que all? no estiga fent-se tamb? -rot, si ?s aix?, ?s perqu? ac? |
|
134 |
// est? al reves (i en tots els llocs on es gasta) |
|
135 |
g.rotate(rot); |
|
96 |
g.rotate(rotation); |
|
136 | 97 |
g.drawString(getText(), 0, getBounds().height); |
137 | 98 |
Rectangle2D bounds = getBounds(); |
138 | 99 |
bounds.setFrame(0, 0, bounds.getWidth(), bounds.getHeight()); |
139 |
g.rotate(-rot); |
|
140 |
g.translate(-p.getX(), -p.getY());
|
|
100 |
g.rotate(-rotation);
|
|
101 |
g.translate(-((FPoint2D) shp).getX(), -((FPoint2D) shp).getY());
|
|
141 | 102 |
} |
142 | 103 |
|
143 | 104 |
public void drawInsideRectangle(Graphics2D g, |
144 | 105 |
AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException { |
145 |
float fontSize = getFont().getSize(); |
|
146 |
float fontScaleFactor = (float) ((getText().length()*fontSize) * 0.8) // text length with a 20% margin |
|
147 |
/ r.width; |
|
148 |
g.setColor(textColor); |
|
149 |
g.translate(r.x, r.y); |
|
150 |
fontSize = fontSize /fontScaleFactor; |
|
151 |
if (fontSize > r.height) |
|
152 |
fontSize = r.height; |
|
153 |
g.setFont(new Font(getFont().getName(), getFont().getStyle(), (int)fontSize)); |
|
154 |
g.drawString(getText(), 0, fontSize); |
|
155 |
|
|
156 |
g.translate(-r.x, -r.y); |
|
157 |
|
|
106 |
int s = getFont().getSize(); |
|
107 |
setFontSize(r.getHeight()); |
|
108 |
draw(g, null, new FPoint2D(r.getX(), r.getY()), null); |
|
109 |
setFontSize(s); |
|
158 | 110 |
} |
159 | 111 |
|
160 | 112 |
public int getOnePointRgb() { |
... | ... | |
163 | 115 |
|
164 | 116 |
public void getPixExtentPlus(FShape shp, float[] distances, |
165 | 117 |
ViewPort viewPort, int dpi) { |
166 |
// TODO Implement it |
|
167 | 118 |
throw new Error("Not yet implemented!"); |
168 | 119 |
|
169 | 120 |
} |
... | ... | |
264 | 215 |
GlyphVector gv = font.createGlyphVector(frc, text); |
265 | 216 |
|
266 | 217 |
Shape shape = gv.getOutline((float) p.getX(), (float) p.getY()); |
267 |
FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds())); |
|
218 |
FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
|
|
268 | 219 |
|
269 | 220 |
myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY())); |
270 | 221 |
|
... | ... | |
275 | 226 |
} |
276 | 227 |
|
277 | 228 |
public Rectangle getBounds() { |
229 |
// FontMetrics fm = g.getFontMetrics(); |
|
230 |
// Rectangle2D rect = fm.getStringBounds("graphics", g); |
|
231 |
|
|
278 | 232 |
Rectangle bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds(); |
279 |
// bounds.setLocation( |
|
280 |
// (int) Math.round(bounds.getX()), |
|
281 |
// (int) Math.round(bounds.getY()-bounds.getHeight())); |
|
282 |
// |
|
283 | 233 |
return bounds; |
284 | 234 |
} |
285 | 235 |
|
286 |
/** |
|
287 |
* Returns an FShape which represents a rectangle containing the text in |
|
288 |
* <b>screen</b> units. |
|
289 |
*/ |
|
290 |
private FShape _getTextWrappingShape(Graphics2D g, /*AffineTransform affineTransform,*/ FPoint2D p) { |
|
291 |
// assuming FShape is a point with the starting position of the text |
|
292 |
Font font = getFont(); |
|
293 |
|
|
294 |
// FontRenderContext frc = g.getFontRenderContext(); |
|
295 |
FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, true); |
|
296 |
|
|
297 |
|
|
298 |
GlyphVector gv = font.createGlyphVector(frc, text); |
|
299 |
|
|
300 |
/*p.transform(affineTransform);*/ |
|
301 |
Shape shape = gv.getOutline((float) p.getX(), (float) p.getY()); |
|
302 |
FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D())); |
|
303 |
if (rotation != 0) { |
|
304 |
myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY())); |
|
305 |
} |
|
306 |
return myFShape; |
|
307 |
} |
|
308 |
/** |
|
309 |
* Returns an FShape which represents a rectangle containing the text in |
|
310 |
* <b>screen</b> units. |
|
311 |
*/ |
|
312 |
private FShape _getTextWrappingShape(Graphics2D g, /*AffineTransform affineTransform,*/ FShape targetSymbolShape ) { |
|
313 |
// assuming FShape is a point with the starting position of the text |
|
314 |
Font font = getFont(); |
|
315 |
|
|
316 |
FontRenderContext frc = g.getFontRenderContext(); |
|
317 |
GlyphVector gv = font.createGlyphVector(frc, text); |
|
318 |
Point2D p = null; |
|
319 |
|
|
320 |
switch (targetSymbolShape.getShapeType()) { |
|
321 |
case FShape.POINT: |
|
322 |
FPoint2D fp=(FPoint2D) targetSymbolShape; |
|
323 |
p = new Point2D.Double(fp.getX(),fp.getY()); |
|
324 |
break; |
|
325 |
case FShape.LINE: |
|
326 |
PathLength pathLen = new PathLength(targetSymbolShape); |
|
327 |
|
|
328 |
// if (pathLen.lengthOfPath() < width / mT.getScaleX()) return; |
|
329 |
float midDistance = pathLen.lengthOfPath() / 2; |
|
330 |
p = pathLen.pointAtLength(midDistance); |
|
331 |
break; |
|
332 |
case FShape.POLYGON: |
|
333 |
Geometry geo = FConverter.java2d_to_jts(targetSymbolShape); |
|
334 |
|
|
335 |
if (geo == null) { |
|
336 |
return null; |
|
337 |
} |
|
338 |
|
|
339 |
Point pJTS = geo.getCentroid(); |
|
340 |
|
|
341 |
if (pJTS != null) { |
|
342 |
FShape pLabel = FConverter.jts_to_java2d(pJTS); |
|
343 |
p= new Point2D.Double(((FPoint2D) pLabel).getX(), |
|
344 |
((FPoint2D) pLabel).getY()); |
|
345 |
} |
|
346 |
break; |
|
347 |
} |
|
348 |
|
|
349 |
|
|
350 |
/*p.transform(affineTransform);*/ |
|
351 |
Shape shape = gv.getOutline((float) p.getX(), (float) p.getY()); |
|
352 |
|
|
353 |
FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D())); |
|
354 |
if (rotation != 0) { |
|
355 |
myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY())); |
|
356 |
} |
|
357 |
return myFShape; |
|
358 |
} |
|
359 |
|
|
360 |
|
|
361 | 236 |
public void setCartographicSize(double cartographicSize, FShape shp) { |
362 | 237 |
setFontSize(cartographicSize); |
363 | 238 |
} |
Also available in: Unified diff