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