root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / TextPath.java @ 11009
History | View | Annotate | Download (3.9 KB)
1 | 10679 | jaume | /* 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$
|
||
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 com.iver.cit.gvsig.fmap.core; |
||
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 | /**
|
||
70 | * <p>Class that represents baseline of a string and allows the baseline to
|
||
71 | * be composed as contiguous segments with distinct slope each.<br></p>
|
||
72 | *
|
||
73 | * <p>Once a TextPath is created for a string it is possible to know where
|
||
74 | * the character at a determined position in the string is placed and
|
||
75 | * rotated.<br></p>
|
||
76 | *
|
||
77 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
78 | *
|
||
79 | */
|
||
80 | public class TextPath { |
||
81 | |||
82 | private char[] text; |
||
83 | private double[][] pos; |
||
84 | |||
85 | /**
|
||
86 | * <p>Creates a new instance of TextPath with the current graphics
|
||
87 | * context.<br></p>
|
||
88 | *
|
||
89 | * <p>Given a <b>Graphics2D</b>, TextPath can know which Font and FontRenderContext
|
||
90 | * is in use. So, it can calculate the position and rotation of each
|
||
91 | * character in <b>char[] text</b> based in the path defined by the
|
||
92 | * <b>FShape path</b> argument.</p>
|
||
93 | * @param g, Graphics2D
|
||
94 | * @param path, FShape
|
||
95 | * @param text, char[]
|
||
96 | */
|
||
97 | public TextPath(Graphics2D g, FShape path, char[] text) { |
||
98 | this.text = text;
|
||
99 | compute(g, path); |
||
100 | } |
||
101 | |||
102 | /**
|
||
103 | * Initializes the position vector.
|
||
104 | * @param g
|
||
105 | * @param path
|
||
106 | */
|
||
107 | private void compute(Graphics2D g, FShape path) { |
||
108 | PathLength pl = new PathLength(path);
|
||
109 | |||
110 | Font font = g.getFont();
|
||
111 | FontRenderContext frc = g.getFontRenderContext();
|
||
112 | |||
113 | GlyphVector gv = font.createGlyphVector(frc, text);
|
||
114 | |||
115 | int numGlyphs = gv.getNumGlyphs();
|
||
116 | pos = new double[numGlyphs][3]; |
||
117 | double lineLength = pl.lengthOfPath() / numGlyphs;// - ( margin + margin ) |
||
118 | |||
119 | |||
120 | for (int i = 0; i < numGlyphs; i++) { |
||
121 | float charDistance = (float) lineLength*i;//(gv.getGlyphPosition(i).getX() * lineLength); // + margin |
||
122 | Point2D p = pl.pointAtLength(charDistance);
|
||
123 | pos[i][0] = p.getX();
|
||
124 | pos[i][1] = p.getY();
|
||
125 | pos[i][2] = (double) pl.angleAtLength(charDistance); |
||
126 | |||
127 | } |
||
128 | |||
129 | } |
||
130 | |||
131 | /**
|
||
132 | * <p>Returns the placement of the next character to draw and the corresponding
|
||
133 | * rotation in a double array of three elements with this order:</p><br>
|
||
134 | *
|
||
135 | * <p><b>double[0]</b> Position in X in the screen</p>
|
||
136 | * <p><b>double[1]</b> Position in Y in the screen</p>
|
||
137 | * <p><b>double[2]</b> Angle of the character.</p>
|
||
138 | * @return
|
||
139 | */
|
||
140 | public double[] nextPosForGlyph(int glyphIndex) { |
||
141 | return pos[glyphIndex];
|
||
142 | } |
||
143 | |||
144 | } |