svn-gvsig-desktop / branches / simbologia / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / TextPath.java @ 10436
History | View | Annotate | Download (3.82 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 10436 2007-02-21 07:34:09Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.1.2.3 2007-02-21 07:34:09 jaume
|
47 |
* labeling starts working
|
48 |
*
|
49 |
* Revision 1.1.2.2 2007/02/09 07:47:05 jaume
|
50 |
* Isymbol moved
|
51 |
*
|
52 |
* Revision 1.1.2.1 2007/02/06 17:01:04 jaume
|
53 |
* first version (only lines)
|
54 |
*
|
55 |
*
|
56 |
*/
|
57 |
package com.iver.cit.gvsig.fmap.core; |
58 |
|
59 |
import java.awt.Font; |
60 |
import java.awt.Graphics2D; |
61 |
import java.awt.font.FontRenderContext; |
62 |
import java.awt.font.GlyphVector; |
63 |
import java.awt.geom.Point2D; |
64 |
|
65 |
import org.apache.batik.ext.awt.geom.PathLength; |
66 |
/**
|
67 |
* <p>Class that represents baseline of a string and allows the baseline to
|
68 |
* be composed as contiguous segments with distinct slope each.<br></p>
|
69 |
*
|
70 |
* <p>Once a TextPath is created for a string it is possible to know where
|
71 |
* the character at a determined position in the string is placed and
|
72 |
* rotated.<br></p>
|
73 |
*
|
74 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
75 |
*
|
76 |
*/
|
77 |
public class TextPath { |
78 |
|
79 |
private char[] text; |
80 |
private double[][] pos; |
81 |
|
82 |
/**
|
83 |
* <p>Creates a new instance of TextPath with the current graphics
|
84 |
* context.<br></p>
|
85 |
*
|
86 |
* <p>Given a <b>Graphics2D</b>, TextPath can know which Font and FontRenderContext
|
87 |
* is in use. So, it can calculate the position and rotation of each
|
88 |
* character in <b>char[] text</b> based in the path defined by the
|
89 |
* <b>FShape path</b> argument.</p>
|
90 |
* @param g, Graphics2D
|
91 |
* @param path, FShape
|
92 |
* @param text, char[]
|
93 |
*/
|
94 |
public TextPath(Graphics2D g, FShape path, char[] text) { |
95 |
this.text = text;
|
96 |
compute(g, path); |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Initializes the position vector.
|
101 |
* @param g
|
102 |
* @param path
|
103 |
*/
|
104 |
private void compute(Graphics2D g, FShape path) { |
105 |
PathLength pl = new PathLength(path);
|
106 |
|
107 |
Font font = g.getFont();
|
108 |
FontRenderContext frc = g.getFontRenderContext();
|
109 |
|
110 |
GlyphVector gv = font.createGlyphVector(frc, text);
|
111 |
|
112 |
int numGlyphs = gv.getNumGlyphs();
|
113 |
pos = new double[numGlyphs][3]; |
114 |
double lineLength = pl.lengthOfPath() / numGlyphs;// - ( margin + margin ) |
115 |
|
116 |
|
117 |
for (int i = 0; i < numGlyphs; i++) { |
118 |
float charDistance = (float) lineLength*i;//(gv.getGlyphPosition(i).getX() * lineLength); // + margin |
119 |
Point2D p = pl.pointAtLength(charDistance);
|
120 |
pos[i][0] = p.getX();
|
121 |
pos[i][1] = p.getY();
|
122 |
pos[i][2] = (double) pl.angleAtLength(charDistance); |
123 |
|
124 |
} |
125 |
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* <p>Returns the placement of the next character to draw and the corresponding
|
130 |
* rotation in a double array of three elements with this order:</p><br>
|
131 |
*
|
132 |
* <p><b>double[0]</b> Position in X in the screen</p>
|
133 |
* <p><b>double[1]</b> Position in Y in the screen</p>
|
134 |
* <p><b>double[2]</b> Angle of the character.</p>
|
135 |
* @return
|
136 |
*/
|
137 |
public double[] nextPosForGlyph(int glyphIndex) { |
138 |
return pos[glyphIndex];
|
139 |
} |
140 |
|
141 |
} |