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 @ 40560
History | View | Annotate | Download (16.5 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 |
|
169 |
GeneralPathX correctedPath = new GeneralPathX();
|
170 |
int controlPoints = 16; |
171 |
double[][] points = new double[controlPoints][2]; |
172 |
double prevX, prevY;
|
173 |
double xsum=0, ysum=0; |
174 |
int nextPos = 0; |
175 |
boolean bufferComplete = false; |
176 |
boolean movedTo = false; |
177 |
for (float curPos = 0; curPos<pl.lengthOfPath(); curPos = curPos+interval) { |
178 |
prevX = points[nextPos][0];
|
179 |
prevY = points[nextPos][1];
|
180 |
Point2D point =pl.pointAtLength(curPos);
|
181 |
if (!movedTo) {
|
182 |
correctedPath.moveTo(point.getX(), point.getY()); |
183 |
movedTo = true;
|
184 |
} |
185 |
|
186 |
points[nextPos][0] = point.getX();
|
187 |
points[nextPos][1] = point.getY();
|
188 |
|
189 |
if (!bufferComplete) {
|
190 |
xsum += points[nextPos][0];
|
191 |
ysum += points[nextPos][1];
|
192 |
nextPos++; |
193 |
if (nextPos==controlPoints) {
|
194 |
nextPos = 0;
|
195 |
bufferComplete = true;
|
196 |
|
197 |
|
198 |
/**
|
199 |
* calculate the beginning of the line
|
200 |
*/
|
201 |
// this will be the first interpolated point
|
202 |
double auxX2 = xsum/controlPoints;
|
203 |
double auxY2 = ysum/controlPoints;
|
204 |
|
205 |
for (int i=1; i<controlPoints/2-1; i++) { |
206 |
// calculate the points from the origin of the geometry to the first interpolated point
|
207 |
double auxX = (points[0][0]+points[i][0]+auxX2)/3; |
208 |
double auxY = (points[0][1]+points[i][1]+auxY2)/3; |
209 |
correctedPath.lineTo(auxX, auxY); |
210 |
} |
211 |
correctedPath.lineTo(auxX2, auxY2); |
212 |
} |
213 |
} |
214 |
else {
|
215 |
|
216 |
xsum = xsum - prevX + points[nextPos][0];
|
217 |
ysum = ysum - prevY + points[nextPos][1];
|
218 |
if (!movedTo) {
|
219 |
correctedPath.moveTo(xsum/controlPoints, ysum/controlPoints); |
220 |
movedTo = true;
|
221 |
} |
222 |
else {
|
223 |
correctedPath.lineTo(xsum/controlPoints, ysum/controlPoints); |
224 |
} |
225 |
|
226 |
nextPos = (nextPos+1)%controlPoints;
|
227 |
} |
228 |
} |
229 |
Point2D endPoint = pl.pointAtLength(pl.lengthOfPath());
|
230 |
// last point in the geom
|
231 |
double endPointX = endPoint.getX();
|
232 |
double endPointY = endPoint.getY();
|
233 |
|
234 |
if (bufferComplete) {
|
235 |
/**
|
236 |
* calculate the points from the last interpolated point to the end of the geometry
|
237 |
*/
|
238 |
|
239 |
// last interpolated point
|
240 |
double auxX2 = xsum/controlPoints;
|
241 |
double auxY2 = ysum/controlPoints;
|
242 |
nextPos = (nextPos+(controlPoints/2))%controlPoints;
|
243 |
for (int i=0; i<controlPoints/2-1; i++) { |
244 |
// calculate the points from the last interpolated point to the end of the geometry
|
245 |
double auxX = (auxX2+points[nextPos][0]+endPointX)/3; |
246 |
double auxY = (auxY2+points[nextPos][1]+endPointY)/3; |
247 |
correctedPath.lineTo(auxX, auxY); |
248 |
nextPos = (nextPos+1)%controlPoints;
|
249 |
} |
250 |
} |
251 |
correctedPath.lineTo(endPointX, endPointY); |
252 |
|
253 |
return geomManager.createCurve(new GeneralPathX( |
254 |
correctedPath.getPathIterator(null)), SUBTYPES.GEOM2D);
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* Initializes the position vector.
|
259 |
* @param g
|
260 |
* @param path
|
261 |
*/
|
262 |
private double[][] computeAtRight(GlyphVector gv, PathLength pl, char[] text) { |
263 |
numGlyphs = gv.getNumGlyphs(); |
264 |
double[][] pos = new double[numGlyphs][3]; |
265 |
float[] charAnchors = new float[numGlyphs]; |
266 |
|
267 |
/**
|
268 |
* Compute glyph positions using linear distances
|
269 |
*/
|
270 |
float lengthOfPath = pl.lengthOfPath();
|
271 |
// char distance from the right side
|
272 |
float charDistance = lengthOfPath-margin;
|
273 |
int glyphsConsumed = numGlyphs-1; |
274 |
float previousAngle = 0.0f; |
275 |
float angle = 0.0f; |
276 |
boolean correction = true; |
277 |
float charWidth = characterWidth;
|
278 |
for (int i = numGlyphs-1; i>=0; i--) { |
279 |
if (correction && charDistance>=0) { |
280 |
previousAngle = angle; |
281 |
angle = pl.angleAtLength(charDistance); |
282 |
if (i<numGlyphs-1) { |
283 |
// correct distance according to angle between current and previous glyph
|
284 |
int turn = Angle.getTurn(previousAngle, angle);
|
285 |
if (turn==1) { // if turn is positive => increase distance |
286 |
float auxDistance = charDistance - (float)(charWidth*2.5f*Angle.diff(previousAngle, angle)/Math.PI); |
287 |
float auxAngle = pl.angleAtLength(auxDistance);
|
288 |
if (Angle.getTurn(previousAngle, auxAngle)==1) { // ensure new position also has positive turn |
289 |
charDistance = auxDistance; |
290 |
angle = auxAngle; |
291 |
} |
292 |
} |
293 |
else if (turn==-1) { // if turn is negative => decrease distance |
294 |
float auxDistance = charDistance + (float)(charWidth*0.9f*Angle.diff(previousAngle, angle)/Math.PI); |
295 |
float auxAngle = pl.angleAtLength(auxDistance);
|
296 |
if (Angle.getTurn(previousAngle, auxAngle)==-1) { // ensure new position also has negative turn |
297 |
charDistance = auxDistance; |
298 |
angle = auxAngle; |
299 |
} |
300 |
} |
301 |
} |
302 |
} |
303 |
|
304 |
if (wordSpacing!=DEFAULT_WORD_SPACING
|
305 |
&& isWordSeparator(text[gv.getGlyphCharIndex(glyphsConsumed)], wordSeparators)) { |
306 |
charWidth = wordSpacing; |
307 |
} |
308 |
else {
|
309 |
charWidth = Math.max(gv.getGlyphMetrics(glyphsConsumed).getAdvance(), characterWidth);
|
310 |
|
311 |
} |
312 |
charDistance -= charWidth; |
313 |
charAnchors[glyphsConsumed] = charDistance; |
314 |
charDistance -= characterSpacing; |
315 |
glyphsConsumed--; |
316 |
} |
317 |
|
318 |
/**
|
319 |
* Calculate 2D positions for the glyphs from the calculated linear distances
|
320 |
*/
|
321 |
for (int i = numGlyphs-1; i>=0; i--) { |
322 |
float anchor = (rightToLeft) ? charAnchors[charAnchors.length-1-i] : charAnchors[i]; |
323 |
Point2D p = pl.pointAtLength( anchor );
|
324 |
if (p == null) { |
325 |
if (i<numGlyphs-1) { // place in a straight line the glyphs that don't fit in the shape |
326 |
pos[i][0] = pos[i+1][0] + (charAnchors[i]-charAnchors[i+1])*Math.cos(pos[i+1][2]); |
327 |
pos[i][1] = pos[i+1][1] + (charAnchors[i]-charAnchors[i+1])*Math.sin(pos[i+1][2]); |
328 |
pos[i][2] = pos[i+1][2]; |
329 |
} else {
|
330 |
pos[i][0] = NO_POS;
|
331 |
pos[i][1] = NO_POS;
|
332 |
} |
333 |
continue;
|
334 |
} |
335 |
pos[i][0] = p.getX();
|
336 |
pos[i][1] = p.getY();
|
337 |
pos[i][2] = pl.angleAtLength( anchor );
|
338 |
} |
339 |
return pos;
|
340 |
} |
341 |
|
342 |
/**
|
343 |
* Initializes the position vector.
|
344 |
* @param g
|
345 |
* @param path
|
346 |
*/
|
347 |
private double[][] computeAtLeft(GlyphVector gv, PathLength pl, char[] text) { |
348 |
numGlyphs = gv.getNumGlyphs(); |
349 |
double[][] pos = new double[numGlyphs][3]; |
350 |
float[] charAnchors = new float[numGlyphs]; |
351 |
float[] charWidths = new float[numGlyphs]; |
352 |
|
353 |
/**
|
354 |
* Compute glyph positions using linear distances
|
355 |
*/
|
356 |
float lengthOfPath = pl.lengthOfPath();
|
357 |
float charDistance = margin;
|
358 |
int glyphsConsumed = 0; |
359 |
float previousAngle = 0.0f; |
360 |
float angle = 0.0f; |
361 |
boolean correction = true; |
362 |
float charWidth = characterWidth;
|
363 |
for (int i = 0; i < gv.getNumGlyphs(); i++) { |
364 |
|
365 |
if (correction && charDistance<=lengthOfPath) {
|
366 |
previousAngle = angle; |
367 |
angle = pl.angleAtLength(charDistance); |
368 |
if (i>0) { |
369 |
// correct distance according to angle between current and previous glyph
|
370 |
int turn = Angle.getTurn(previousAngle, angle);
|
371 |
if (turn==1) { // if turn is positive => decrease distance |
372 |
float auxDistance = charDistance - (float)(charWidth*0.9*Angle.diff(previousAngle, angle)/Math.PI); |
373 |
float auxAngle = pl.angleAtLength(auxDistance);
|
374 |
if (Angle.getTurn(previousAngle, auxAngle)==1) { // ensure new position also has positive turn |
375 |
charDistance = auxDistance; |
376 |
angle = auxAngle; |
377 |
} |
378 |
} |
379 |
else if (turn == -1){ // if turn is negative => increase distance |
380 |
|
381 |
float auxDistance = charDistance + (float)(charWidth*2.5*Angle.diff(previousAngle, angle)/Math.PI); |
382 |
float auxAngle = pl.angleAtLength(auxDistance);
|
383 |
if (Angle.getTurn(previousAngle, auxAngle)==-1) { // ensure new position also has negative turn |
384 |
charDistance = auxDistance; |
385 |
angle = auxAngle; |
386 |
} |
387 |
} |
388 |
} |
389 |
} |
390 |
if (wordSpacing!=DEFAULT_WORD_SPACING
|
391 |
&& isWordSeparator(text[gv.getGlyphCharIndex(glyphsConsumed)], wordSeparators)) { |
392 |
// use defined wordspacing
|
393 |
charWidth = wordSpacing; |
394 |
} |
395 |
else {
|
396 |
charWidth = Math.max(gv.getGlyphMetrics(glyphsConsumed).getAdvance(), characterWidth);
|
397 |
|
398 |
} |
399 |
charWidths[glyphsConsumed] = charWidth; |
400 |
charAnchors[glyphsConsumed] = charDistance; |
401 |
charDistance += charWidth; |
402 |
charDistance += characterSpacing; |
403 |
glyphsConsumed++; |
404 |
} |
405 |
|
406 |
/**
|
407 |
* Calculate 2D positions for the glyphs from the calculated linear distances
|
408 |
*/
|
409 |
for (int i = 0; i < charAnchors.length; i++) { |
410 |
float anchor = (rightToLeft) ? charAnchors[charAnchors.length-1-i] : charAnchors[i]; |
411 |
Point2D p = pl.pointAtLength( anchor );
|
412 |
if (p == null) { |
413 |
if (i>0) { // place in a straight line the glyphs that don't fit in the shape |
414 |
pos[i][0] = pos[i-1][0] + (charAnchors[i]-charAnchors[i-1])*Math.cos(pos[i-1][2]); |
415 |
pos[i][1] = pos[i-1][1] + (charAnchors[i]-charAnchors[i-1])*Math.sin(pos[i-1][2]); |
416 |
pos[i][2] = pos[i-1][2]; |
417 |
} else {
|
418 |
pos[i][0] = NO_POS;
|
419 |
pos[i][1] = NO_POS;
|
420 |
} |
421 |
continue;
|
422 |
} |
423 |
pos[i][2] = pl.angleAtLength( anchor );
|
424 |
// pos[i][0] = p.getX() - charWidths[i]*Math.cos(pos[i][2]);
|
425 |
// pos[i][1] = p.getY() - charWidths[i]*Math.sin(pos[i][2]);
|
426 |
pos[i][0] = p.getX();
|
427 |
pos[i][1] = p.getY();
|
428 |
} |
429 |
return pos;
|
430 |
} |
431 |
|
432 |
|
433 |
/**
|
434 |
* Initializes the position vector.
|
435 |
* @param g
|
436 |
* @param path
|
437 |
*/
|
438 |
private void computeAtMiddle(FontRenderContext frc, char[] text, Font font, PathLength pl) { |
439 |
if (text.length==0) { |
440 |
return; // nothing to compute if text length is 0 |
441 |
} |
442 |
int middleChar = (text.length-1)/2; |
443 |
char[] text1 = new char[middleChar+1]; |
444 |
char[] text2 = new char[text.length-text1.length]; |
445 |
System.arraycopy(text, 0, text1, 0, text1.length); |
446 |
System.arraycopy(text, text1.length, text2, 0, text2.length); |
447 |
|
448 |
float halfLength = pl.lengthOfPath()/2.0f; |
449 |
margin = halfLength; |
450 |
GlyphVector gv = font.createGlyphVector(frc, text1);
|
451 |
double[][] pos1 = computeAtRight(gv, pl, text1); |
452 |
int glyphCount = numGlyphs;
|
453 |
gv = font.createGlyphVector(frc, text2); |
454 |
margin = halfLength + characterSpacing; |
455 |
double[][] pos2 = computeAtLeft(gv, pl, text2); |
456 |
numGlyphs += glyphCount; |
457 |
posList = new double[pos1.length+pos2.length][3]; |
458 |
System.arraycopy(pos1, 0, posList, 0, pos1.length); |
459 |
System.arraycopy(pos2, 0, posList, pos1.length, pos2.length); |
460 |
} |
461 |
|
462 |
|
463 |
/**
|
464 |
* <p>Returns the placement of the next character to draw and the corresponding
|
465 |
* rotation in a double array of three elements with this order:</p><br>
|
466 |
*
|
467 |
* <p><b>double[0]</b> Position in X in the screen</p>
|
468 |
* <p><b>double[1]</b> Position in Y in the screen</p>
|
469 |
* <p><b>double[2]</b> Angle of the character.</p>
|
470 |
* @return
|
471 |
*/
|
472 |
public double[] nextPosForGlyph(int glyphIndex) { |
473 |
return posList[glyphIndex];
|
474 |
} |
475 |
|
476 |
public int getGlyphCount() { |
477 |
return numGlyphs;
|
478 |
} |
479 |
|
480 |
protected static boolean isWordSeparator(char c, char[] wordSeparators) { |
481 |
char separator;
|
482 |
for (int i = 0; i < wordSeparators.length; i++) { |
483 |
separator = wordSeparators[i]; |
484 |
if (c==separator) {
|
485 |
return true; |
486 |
} |
487 |
} |
488 |
return false; |
489 |
} |
490 |
|
491 |
} |