svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / legend / styling / TextPath.java @ 27028
History | View | Annotate | Download (16.2 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 |
|
42 |
/* CVS MESSAGES:
|
43 |
*
|
44 |
* $Id: TextPath.java 25636 2008-12-01 08:42:11Z vcaballero $
|
45 |
* $Log$
|
46 |
* Revision 1.2 2007-03-09 11:20:57 jaume
|
47 |
* Advanced symbology (start committing)
|
48 |
*
|
49 |
* Revision 1.1.2.3 2007/02/21 07:34:09 jaume
|
50 |
* labeling starts working
|
51 |
*
|
52 |
* Revision 1.1.2.2 2007/02/09 07:47:05 jaume
|
53 |
* Isymbol moved
|
54 |
*
|
55 |
* Revision 1.1.2.1 2007/02/06 17:01:04 jaume
|
56 |
* first version (only lines)
|
57 |
*
|
58 |
*
|
59 |
*/
|
60 |
package org.gvsig.fmap.mapcontext.rendering.legend.styling; |
61 |
|
62 |
import java.awt.Font; |
63 |
import java.awt.Graphics2D; |
64 |
import java.awt.font.FontRenderContext; |
65 |
import java.awt.font.GlyphVector; |
66 |
import java.awt.geom.Point2D; |
67 |
|
68 |
import org.apache.batik.ext.awt.geom.PathLength; |
69 |
import org.gvsig.fmap.geom.Geometry; |
70 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
71 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
72 |
import org.gvsig.fmap.mapcontext.Messages; |
73 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol; |
74 |
|
75 |
import com.vividsolutions.jts.algorithm.Angle; |
76 |
/**
|
77 |
* <p>Class that represents baseline of a string and allows the baseline to
|
78 |
* be composed as contiguous segments with distinct slope each.<br></p>
|
79 |
*
|
80 |
* <p>Once a TextPath is created for a string it is possible to know where
|
81 |
* the character at a determined position in the string is placed and
|
82 |
* rotated.<br></p>
|
83 |
*
|
84 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
85 |
*
|
86 |
*/
|
87 |
public class TextPath { |
88 |
|
89 |
public static final int NO_POS = Integer.MIN_VALUE; |
90 |
/**
|
91 |
* Don't set a concrete word spacing. The word is separated using the normal
|
92 |
* width of the separator glyph.
|
93 |
*/
|
94 |
public static final int DEFAULT_WORD_SPACING = Integer.MIN_VALUE; |
95 |
|
96 |
private char[] text; |
97 |
/**
|
98 |
* An array which contains the calculated positions for the glyphs
|
99 |
* Each row represents a glyph, and it contains the X coord, the Y coord, and the rotation angle
|
100 |
*/
|
101 |
private double[][] posList; |
102 |
private int alignment; |
103 |
private float characterSpacing; |
104 |
private boolean kerning; |
105 |
private float wordSpacing; |
106 |
private float margin; |
107 |
private boolean rightToLeft; |
108 |
private int numGlyphs = 0; |
109 |
private float characterWidth; |
110 |
private char[] wordSeparators = {' '}; // in the future, separators might be provided as parameter |
111 |
|
112 |
/**
|
113 |
* <p>Creates a new instance of TextPath with the current graphics
|
114 |
* context.<br></p>
|
115 |
*
|
116 |
* <p>Given a <b>Graphics2D</b>, TextPath can know which Font and FontRenderContext
|
117 |
* is in use. So, it can calculate the position and rotation of each
|
118 |
* character in <b>char[] text</b> based in the path defined by the
|
119 |
* <b>FShape path</b> argument.</p>
|
120 |
* @param g, Graphics2D
|
121 |
* @param path, FShape
|
122 |
* @param text, char[]
|
123 |
*/
|
124 |
public TextPath(Graphics2D g, Geometry path, char[] text, Font font, |
125 |
float characterSpacing, float characterWidth, boolean kerning, |
126 |
float leading, int alignment, float wordSpacing, float margin, |
127 |
boolean rightToLeft) {
|
128 |
this.text = text;
|
129 |
if (alignment == ITextSymbol.SYMBOL_STYLE_ALIGNMENT_LEFT ||
|
130 |
alignment == ITextSymbol.SYMBOL_STYLE_ALIGNMENT_RIGHT |
131 |
|| |
132 |
alignment == ITextSymbol.SYMBOL_STYLE_ALIGNMENT_CENTERED || |
133 |
alignment == ITextSymbol.SYMBOL_STYLE_ALIGNMENT_JUSTIFY) { |
134 |
this.alignment = alignment;
|
135 |
} else {
|
136 |
throw new IllegalArgumentException( |
137 |
Messages.getString("invalid_value_for") + ": " + |
138 |
Messages.getString("alignment")+" ( "+alignment+")"); |
139 |
} |
140 |
this.characterWidth = characterWidth;
|
141 |
this.characterSpacing = characterSpacing;
|
142 |
this.kerning = kerning;
|
143 |
this.wordSpacing = wordSpacing;
|
144 |
this.margin = margin;
|
145 |
this.rightToLeft = rightToLeft;
|
146 |
|
147 |
FontRenderContext frc = g.getFontRenderContext();
|
148 |
/* java 6 code
|
149 |
* TODO keep this!!
|
150 |
if (kerning) {
|
151 |
HashMap<TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
|
152 |
attrs.put(TextAttribute.KERNING , TextAttribute.KERNING_ON);
|
153 |
}
|
154 |
*/
|
155 |
GlyphVector gv = font.createGlyphVector(frc, text);
|
156 |
|
157 |
PathLength pl = new PathLength(softenShape(path, gv));
|
158 |
|
159 |
if (alignment==ITextSymbol.SYMBOL_STYLE_ALIGNMENT_RIGHT) {
|
160 |
posList = computeAtRight(gv, pl, text); |
161 |
} |
162 |
else if (alignment==ITextSymbol.SYMBOL_STYLE_ALIGNMENT_CENTERED) { |
163 |
computeAtMiddle(frc, text, font, pl); |
164 |
} |
165 |
else {
|
166 |
posList = computeAtLeft(gv, pl, text); |
167 |
} |
168 |
} |
169 |
|
170 |
protected Geometry softenShape(Geometry shape, GlyphVector gv) { |
171 |
|
172 |
float interval = (float) gv.getVisualBounds().getWidth()/(gv.getNumGlyphs()*3); |
173 |
|
174 |
PathLength pl = new PathLength(shape);
|
175 |
|
176 |
GeneralPathX correctedPath = new GeneralPathX();
|
177 |
int controlPoints = 16; |
178 |
double[][] points = new double[controlPoints][2]; |
179 |
double prevX, prevY;
|
180 |
double xsum=0, ysum=0; |
181 |
int nextPos = 0; |
182 |
boolean bufferComplete = false; |
183 |
boolean movedTo = false; |
184 |
for (float curPos = 0; curPos<pl.lengthOfPath(); curPos = curPos+interval) { |
185 |
prevX = points[nextPos][0];
|
186 |
prevY = points[nextPos][1];
|
187 |
Point2D point =pl.pointAtLength(curPos);
|
188 |
if (!movedTo) {
|
189 |
correctedPath.moveTo(point.getX(), point.getY()); |
190 |
movedTo = true;
|
191 |
} |
192 |
|
193 |
points[nextPos][0] = point.getX();
|
194 |
points[nextPos][1] = point.getY();
|
195 |
|
196 |
if (!bufferComplete) {
|
197 |
xsum += points[nextPos][0];
|
198 |
ysum += points[nextPos][1];
|
199 |
nextPos++; |
200 |
if (nextPos==controlPoints) {
|
201 |
nextPos = 0;
|
202 |
bufferComplete = true;
|
203 |
|
204 |
|
205 |
/**
|
206 |
* calculate the beginning of the line
|
207 |
*/
|
208 |
// this will be the first interpolated point
|
209 |
double auxX2 = xsum/controlPoints;
|
210 |
double auxY2 = ysum/controlPoints;
|
211 |
|
212 |
for (int i=1; i<controlPoints/2-1; i++) { |
213 |
// calculate the points from the origin of the geometry to the first interpolated point
|
214 |
double auxX = (points[0][0]+points[i][0]+auxX2)/3; |
215 |
double auxY = (points[0][1]+points[i][1]+auxY2)/3; |
216 |
correctedPath.lineTo(auxX, auxY); |
217 |
} |
218 |
correctedPath.lineTo(auxX2, auxY2); |
219 |
} |
220 |
} |
221 |
else {
|
222 |
|
223 |
xsum = xsum - prevX + points[nextPos][0];
|
224 |
ysum = ysum - prevY + points[nextPos][1];
|
225 |
if (!movedTo) {
|
226 |
correctedPath.moveTo(xsum/controlPoints, ysum/controlPoints); |
227 |
movedTo = true;
|
228 |
} |
229 |
else {
|
230 |
correctedPath.lineTo(xsum/controlPoints, ysum/controlPoints); |
231 |
} |
232 |
|
233 |
nextPos = (nextPos+1)%controlPoints;
|
234 |
} |
235 |
} |
236 |
Point2D endPoint = pl.pointAtLength(pl.lengthOfPath());
|
237 |
// last point in the geom
|
238 |
double endPointX = endPoint.getX();
|
239 |
double endPointY = endPoint.getY();
|
240 |
|
241 |
if (bufferComplete) {
|
242 |
/**
|
243 |
* calculate the points from the last interpolated point to the end of the geometry
|
244 |
*/
|
245 |
|
246 |
// last interpolated point
|
247 |
double auxX2 = xsum/controlPoints;
|
248 |
double auxY2 = ysum/controlPoints;
|
249 |
nextPos = (nextPos+(controlPoints/2))%controlPoints;
|
250 |
for (int i=0; i<controlPoints/2-1; i++) { |
251 |
// calculate the points from the last interpolated point to the end of the geometry
|
252 |
double auxX = (auxX2+points[nextPos][0]+endPointX)/3; |
253 |
double auxY = (auxY2+points[nextPos][1]+endPointY)/3; |
254 |
correctedPath.lineTo(auxX, auxY); |
255 |
nextPos = (nextPos+1)%controlPoints;
|
256 |
} |
257 |
} |
258 |
correctedPath.lineTo(endPointX, endPointY); |
259 |
|
260 |
return UtilFunctions.createCurve(new GeneralPathX( |
261 |
correctedPath)); |
262 |
} |
263 |
|
264 |
/**
|
265 |
* Initializes the position vector.
|
266 |
* @param g
|
267 |
* @param path
|
268 |
*/
|
269 |
private double[][] computeAtRight(GlyphVector gv, PathLength pl, char[] text) { |
270 |
numGlyphs = gv.getNumGlyphs(); |
271 |
double[][] pos = new double[numGlyphs][3]; |
272 |
float[] charAnchors = new float[numGlyphs]; |
273 |
|
274 |
/**
|
275 |
* Compute glyph positions using linear distances
|
276 |
*/
|
277 |
float lengthOfPath = pl.lengthOfPath();
|
278 |
// char distance from the right side
|
279 |
float charDistance = lengthOfPath-margin;
|
280 |
int glyphsConsumed = numGlyphs-1; |
281 |
float previousAngle = 0.0f; |
282 |
float angle = 0.0f; |
283 |
boolean correction = true; |
284 |
float charWidth = characterWidth;
|
285 |
for (int i = numGlyphs-1; i>=0; i--) { |
286 |
if (correction && charDistance>=0) { |
287 |
previousAngle = angle; |
288 |
angle = pl.angleAtLength(charDistance); |
289 |
if (i<numGlyphs-1) { |
290 |
// correct distance according to angle between current and previous glyph
|
291 |
int turn = Angle.getTurn(previousAngle, angle);
|
292 |
if (turn==1) { // if turn is positive => increase distance |
293 |
float auxDistance = charDistance - (float)(charWidth*2.5f*Angle.diff(previousAngle, angle)/Math.PI); |
294 |
float auxAngle = pl.angleAtLength(auxDistance);
|
295 |
if (Angle.getTurn(previousAngle, auxAngle)==1) { // ensure new position also has positive turn |
296 |
charDistance = auxDistance; |
297 |
angle = auxAngle; |
298 |
} |
299 |
} |
300 |
else if (turn==-1) { // if turn is negative => decrease distance |
301 |
float auxDistance = charDistance + (float)(charWidth*0.9f*Angle.diff(previousAngle, angle)/Math.PI); |
302 |
float auxAngle = pl.angleAtLength(auxDistance);
|
303 |
if (Angle.getTurn(previousAngle, auxAngle)==-1) { // ensure new position also has negative turn |
304 |
charDistance = auxDistance; |
305 |
angle = auxAngle; |
306 |
} |
307 |
} |
308 |
} |
309 |
} |
310 |
|
311 |
if (wordSpacing!=DEFAULT_WORD_SPACING
|
312 |
&& isWordSeparator(text[gv.getGlyphCharIndex(glyphsConsumed)], wordSeparators)) { |
313 |
charWidth = wordSpacing; |
314 |
} |
315 |
else {
|
316 |
charWidth = Math.max(gv.getGlyphMetrics(glyphsConsumed).getAdvance(), characterWidth);
|
317 |
|
318 |
} |
319 |
charDistance -= charWidth; |
320 |
charAnchors[glyphsConsumed] = charDistance; |
321 |
charDistance -= characterSpacing; |
322 |
glyphsConsumed--; |
323 |
} |
324 |
|
325 |
/**
|
326 |
* Calculate 2D positions for the glyphs from the calculated linear distances
|
327 |
*/
|
328 |
for (int i = numGlyphs-1; i>=0; i--) { |
329 |
float anchor = (rightToLeft) ? charAnchors[charAnchors.length-1-i] : charAnchors[i]; |
330 |
Point2D p = pl.pointAtLength( anchor );
|
331 |
if (p == null) { |
332 |
if (i<numGlyphs-1) { // place in a straight line the glyphs that don't fit in the shape |
333 |
pos[i][0] = pos[i+1][0] + (charAnchors[i]-charAnchors[i+1])*Math.cos(pos[i+1][2]); |
334 |
pos[i][1] = pos[i+1][1] + (charAnchors[i]-charAnchors[i+1])*Math.sin(pos[i+1][2]); |
335 |
pos[i][2] = pos[i+1][2]; |
336 |
} else {
|
337 |
pos[i][0] = NO_POS;
|
338 |
pos[i][1] = NO_POS;
|
339 |
} |
340 |
continue;
|
341 |
} |
342 |
pos[i][0] = p.getX();
|
343 |
pos[i][1] = p.getY();
|
344 |
pos[i][2] = pl.angleAtLength( anchor );
|
345 |
} |
346 |
return pos;
|
347 |
} |
348 |
|
349 |
/**
|
350 |
* Initializes the position vector.
|
351 |
* @param g
|
352 |
* @param path
|
353 |
*/
|
354 |
private double[][] computeAtLeft(GlyphVector gv, PathLength pl, char[] text) { |
355 |
numGlyphs = gv.getNumGlyphs(); |
356 |
double[][] pos = new double[numGlyphs][3]; |
357 |
float[] charAnchors = new float[numGlyphs]; |
358 |
float[] charWidths = new float[numGlyphs]; |
359 |
|
360 |
/**
|
361 |
* Compute glyph positions using linear distances
|
362 |
*/
|
363 |
float lengthOfPath = pl.lengthOfPath();
|
364 |
float charDistance = margin;
|
365 |
int glyphsConsumed = 0; |
366 |
float previousAngle = 0.0f; |
367 |
float angle = 0.0f; |
368 |
boolean correction = true; |
369 |
float charWidth = characterWidth;
|
370 |
for (int i = 0; i < gv.getNumGlyphs(); i++) { |
371 |
|
372 |
if (correction && charDistance<=lengthOfPath) {
|
373 |
previousAngle = angle; |
374 |
angle = pl.angleAtLength(charDistance); |
375 |
if (i>0) { |
376 |
// correct distance according to angle between current and previous glyph
|
377 |
int turn = Angle.getTurn(previousAngle, angle);
|
378 |
if (turn==1) { // if turn is positive => decrease distance |
379 |
float auxDistance = charDistance - (float)(charWidth*0.9*Angle.diff(previousAngle, angle)/Math.PI); |
380 |
float auxAngle = pl.angleAtLength(auxDistance);
|
381 |
if (Angle.getTurn(previousAngle, auxAngle)==1) { // ensure new position also has positive turn |
382 |
charDistance = auxDistance; |
383 |
angle = auxAngle; |
384 |
} |
385 |
} |
386 |
else if (turn == -1){ // if turn is negative => increase distance |
387 |
|
388 |
float auxDistance = charDistance + (float)(charWidth*2.5*Angle.diff(previousAngle, angle)/Math.PI); |
389 |
float auxAngle = pl.angleAtLength(auxDistance);
|
390 |
if (Angle.getTurn(previousAngle, auxAngle)==-1) { // ensure new position also has negative turn |
391 |
charDistance = auxDistance; |
392 |
angle = auxAngle; |
393 |
} |
394 |
} |
395 |
} |
396 |
} |
397 |
if (wordSpacing!=DEFAULT_WORD_SPACING
|
398 |
&& isWordSeparator(text[gv.getGlyphCharIndex(glyphsConsumed)], wordSeparators)) { |
399 |
// use defined wordspacing
|
400 |
charWidth = wordSpacing; |
401 |
} |
402 |
else {
|
403 |
charWidth = Math.max(gv.getGlyphMetrics(glyphsConsumed).getAdvance(), characterWidth);
|
404 |
|
405 |
} |
406 |
charWidths[glyphsConsumed] = charWidth; |
407 |
charAnchors[glyphsConsumed] = charDistance; |
408 |
charDistance += charWidth; |
409 |
charDistance += characterSpacing; |
410 |
glyphsConsumed++; |
411 |
} |
412 |
|
413 |
/**
|
414 |
* Calculate 2D positions for the glyphs from the calculated linear distances
|
415 |
*/
|
416 |
for (int i = 0; i < charAnchors.length; i++) { |
417 |
float anchor = (rightToLeft) ? charAnchors[charAnchors.length-1-i] : charAnchors[i]; |
418 |
Point2D p = pl.pointAtLength( anchor );
|
419 |
if (p == null) { |
420 |
if (i>0) { // place in a straight line the glyphs that don't fit in the shape |
421 |
pos[i][0] = pos[i-1][0] + (charAnchors[i]-charAnchors[i-1])*Math.cos(pos[i-1][2]); |
422 |
pos[i][1] = pos[i-1][1] + (charAnchors[i]-charAnchors[i-1])*Math.sin(pos[i-1][2]); |
423 |
pos[i][2] = pos[i-1][2]; |
424 |
} else {
|
425 |
pos[i][0] = NO_POS;
|
426 |
pos[i][1] = NO_POS;
|
427 |
} |
428 |
continue;
|
429 |
} |
430 |
pos[i][2] = pl.angleAtLength( anchor );
|
431 |
// pos[i][0] = p.getX() - charWidths[i]*Math.cos(pos[i][2]);
|
432 |
// pos[i][1] = p.getY() - charWidths[i]*Math.sin(pos[i][2]);
|
433 |
pos[i][0] = p.getX();
|
434 |
pos[i][1] = p.getY();
|
435 |
} |
436 |
return pos;
|
437 |
} |
438 |
|
439 |
|
440 |
/**
|
441 |
* Initializes the position vector.
|
442 |
* @param g
|
443 |
* @param path
|
444 |
*/
|
445 |
private void computeAtMiddle(FontRenderContext frc, char[] text, Font font, PathLength pl) { |
446 |
if (text.length==0) { |
447 |
return; // nothing to compute if text length is 0 |
448 |
} |
449 |
int middleChar = (text.length-1)/2; |
450 |
char[] text1 = new char[middleChar+1]; |
451 |
char[] text2 = new char[text.length-text1.length]; |
452 |
System.arraycopy(text, 0, text1, 0, text1.length); |
453 |
System.arraycopy(text, text1.length, text2, 0, text2.length); |
454 |
|
455 |
float halfLength = pl.lengthOfPath()/2.0f; |
456 |
margin = halfLength; |
457 |
GlyphVector gv = font.createGlyphVector(frc, text1);
|
458 |
double[][] pos1 = computeAtRight(gv, pl, text1); |
459 |
int glyphCount = numGlyphs;
|
460 |
gv = font.createGlyphVector(frc, text2); |
461 |
margin = halfLength + characterSpacing; |
462 |
double[][] pos2 = computeAtLeft(gv, pl, text2); |
463 |
numGlyphs += glyphCount; |
464 |
posList = new double[pos1.length+pos2.length][3]; |
465 |
System.arraycopy(pos1, 0, posList, 0, pos1.length); |
466 |
System.arraycopy(pos2, 0, posList, pos1.length, pos2.length); |
467 |
} |
468 |
|
469 |
|
470 |
/**
|
471 |
* <p>Returns the placement of the next character to draw and the corresponding
|
472 |
* rotation in a double array of three elements with this order:</p><br>
|
473 |
*
|
474 |
* <p><b>double[0]</b> Position in X in the screen</p>
|
475 |
* <p><b>double[1]</b> Position in Y in the screen</p>
|
476 |
* <p><b>double[2]</b> Angle of the character.</p>
|
477 |
* @return
|
478 |
*/
|
479 |
public double[] nextPosForGlyph(int glyphIndex) { |
480 |
return posList[glyphIndex];
|
481 |
} |
482 |
|
483 |
public int getGlyphCount() { |
484 |
return numGlyphs;
|
485 |
} |
486 |
|
487 |
protected static boolean isWordSeparator(char c, char[] wordSeparators) { |
488 |
char separator;
|
489 |
for (int i = 0; i < wordSeparators.length; i++) { |
490 |
separator = wordSeparators[i]; |
491 |
if (c==separator) {
|
492 |
return true; |
493 |
} |
494 |
} |
495 |
return false; |
496 |
} |
497 |
|
498 |
} |