svn-gvsig-desktop / tags / v1_1_Build_1012 / libraries / libCq_CMS_praster / src / org / cresques / px / dxf / DxfText.java @ 12987
History | View | Annotate | Download (8.11 KB)
1 | 8026 | nacho | /*
|
---|---|---|---|
2 | * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
||
3 | *
|
||
4 | * Copyright (C) 2004-5.
|
||
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 2
|
||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * cresques@gmail.com
|
||
23 | */
|
||
24 | package org.cresques.px.dxf; |
||
25 | |||
26 | import org.cresques.cts.ICoordTrans; |
||
27 | import org.cresques.cts.IProjection; |
||
28 | |||
29 | import org.cresques.geo.ViewPortData; |
||
30 | 12744 | caballero | import org.cresques.io.DxfGroup; |
31 | 8026 | nacho | |
32 | import org.cresques.px.Extent; |
||
33 | |||
34 | import java.awt.Font; |
||
35 | import java.awt.Graphics2D; |
||
36 | import java.awt.geom.Point2D; |
||
37 | |||
38 | |||
39 | /**
|
||
40 | * Entidad TEXT de un fichero DXF.
|
||
41 | * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
||
42 | * @author jmorell
|
||
43 | * jmorell, 050406: El segundo punto es opcional.
|
||
44 | */
|
||
45 | public class DxfText extends DxfEntity { |
||
46 | public final static int ALIGN_LEFT = 0; |
||
47 | public final static int ALIGN_CENTER = 1; |
||
48 | public final static int ALIGN_RIGHT = 2; |
||
49 | public final static int ALIGN_ALIGNED = 3; |
||
50 | public final static int ALIGN_MIDDLE = 4; |
||
51 | public final static int ALIGN_FIT = 5; |
||
52 | private String text = null; |
||
53 | Point2D[] pts; |
||
54 | private Point2D pt; |
||
55 | private double rot = 0.0; |
||
56 | private double h = 1.0; |
||
57 | int align = ALIGN_LEFT;
|
||
58 | private boolean twoPointsFlag; |
||
59 | |||
60 | /**
|
||
61 | * Constructor de DxfText.
|
||
62 | * @param proj, proyecci?n cartogr?fica en la que se encuentra el DxfText.
|
||
63 | * @param layer, capa del DXF en la que se encuentra el DxfText.
|
||
64 | * @param txt, texto.
|
||
65 | */
|
||
66 | public DxfText(IProjection proj, DxfLayer layer, String txt) { |
||
67 | super(proj, layer);
|
||
68 | |||
69 | //System.out.println("Dxf: TEXT '"+txt+"'.");
|
||
70 | extent = new Extent();
|
||
71 | text = txt; |
||
72 | pts = new Point2D[2]; |
||
73 | pt = new Point2D.Double(); |
||
74 | twoPointsFlag = false;
|
||
75 | } |
||
76 | |||
77 | /**
|
||
78 | * Establece el punto de inserci?n del DxfText.
|
||
79 | * @param pt, punto de inserci?n.
|
||
80 | */
|
||
81 | public void setPt(Point2D pt) { |
||
82 | this.pt = pt;
|
||
83 | } |
||
84 | |||
85 | /**
|
||
86 | * @return Returns the pt.
|
||
87 | */
|
||
88 | public Point2D getPt() { |
||
89 | return pt;
|
||
90 | } |
||
91 | |||
92 | /**
|
||
93 | * Establece si el texto se situa a trav?s de dos puntos de inserci?n. Estos dos
|
||
94 | * puntos definir?n su orientaci?n.
|
||
95 | * @param f
|
||
96 | */
|
||
97 | public void setTwoPointsFlag(boolean f) { |
||
98 | twoPointsFlag = f; |
||
99 | } |
||
100 | |||
101 | /**
|
||
102 | * Informa sobre si el texto se situa a trav?s de dos puntos de inserci?n. Estos dos
|
||
103 | * puntos definir?n su orientaci?n.
|
||
104 | * @return boolean
|
||
105 | */
|
||
106 | public boolean getTwoPointsFlag() { |
||
107 | return twoPointsFlag;
|
||
108 | } |
||
109 | |||
110 | /**
|
||
111 | * Establece el primer punto de inserci?n del texto cuando este se inserta a trav?s
|
||
112 | * de dos puntos de inserci?n.
|
||
113 | * @param pt
|
||
114 | */
|
||
115 | public void setPt1(Point2D pt) { |
||
116 | pts[0] = pt;
|
||
117 | extent.add(pt); |
||
118 | } |
||
119 | |||
120 | /**
|
||
121 | * Obtiene el primer punto de inserci?n del texto cuando este se inserta a trav?s
|
||
122 | * de dos puntos de inserci?n.
|
||
123 | * @return Point2D
|
||
124 | */
|
||
125 | public Point2D getPt1() { |
||
126 | return pts[0]; |
||
127 | } |
||
128 | |||
129 | /**
|
||
130 | * Establece el segundo punto de inserci?n del texto cuando este se inserta a trav?s
|
||
131 | * de dos puntos de inserci?n.
|
||
132 | * @param pt
|
||
133 | */
|
||
134 | public void setPt2(Point2D pt) { |
||
135 | pts[1] = pt;
|
||
136 | extent.add(pt); |
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * Obtiene el segundo punto de inserci?n del texto cuando este se inserta a trav?s
|
||
141 | * de dos puntos de inserci?n.
|
||
142 | * @return Point2D
|
||
143 | */
|
||
144 | public Point2D getPt2() { |
||
145 | return pts[1]; |
||
146 | } |
||
147 | |||
148 | /**
|
||
149 | * Establece la altura del texto.
|
||
150 | * @param h, altura del texto.
|
||
151 | */
|
||
152 | public void setHeight(double h) { |
||
153 | this.h = h;
|
||
154 | } |
||
155 | 12744 | caballero | public double getHeight(){ |
156 | return h;
|
||
157 | } |
||
158 | 8026 | nacho | /**
|
159 | * Establece la inclinaci?n del texto.
|
||
160 | * @param r, ?ngulo de inclinaci?n del texto.
|
||
161 | */
|
||
162 | public void setRotation(double r) { |
||
163 | rot = r; |
||
164 | } |
||
165 | |||
166 | /**
|
||
167 | * Devuelve la inclinaci?n del texto.
|
||
168 | * @return double
|
||
169 | */
|
||
170 | public double getRotation() { |
||
171 | return rot;
|
||
172 | } |
||
173 | |||
174 | /**
|
||
175 | * Devuelve el texto.
|
||
176 | * @return String
|
||
177 | */
|
||
178 | public String getText() { |
||
179 | return text;
|
||
180 | } |
||
181 | |||
182 | /**
|
||
183 | * Permite reproyectar un DxfText dado un conjunto de coordenadas de transformaci?n.
|
||
184 | * @param rp, coordenadas de transformaci?n.
|
||
185 | */
|
||
186 | public void reProject(ICoordTrans rp) { |
||
187 | Point2D[] savePts = pts; |
||
188 | |||
189 | pts = new Point2D[2]; |
||
190 | extent = new Extent();
|
||
191 | |||
192 | Point2D ptDest = null; |
||
193 | |||
194 | for (int i = 0; i < savePts.length; i++) { |
||
195 | ptDest = rp.getPDest().createPoint(0.0, 0.0); |
||
196 | |||
197 | if (savePts[i] == null) { |
||
198 | ptDest = null;
|
||
199 | } else {
|
||
200 | ptDest = rp.convert((Point2D) savePts[i], ptDest);
|
||
201 | extent.add(ptDest); |
||
202 | } |
||
203 | |||
204 | pts[i] = ptDest; |
||
205 | } |
||
206 | |||
207 | // Reproyecto la altura del texto
|
||
208 | Point2D ptOrig = rp.getPOrig().createPoint(savePts[0].getX(), |
||
209 | savePts[0].getY() + h);
|
||
210 | ptDest = rp.getPDest().createPoint(0D, 0D); |
||
211 | ptDest = rp.convert(ptOrig, ptDest); |
||
212 | h = ptDest.getY() - pts[0].getY();
|
||
213 | setProjection(rp.getPDest()); |
||
214 | } |
||
215 | |||
216 | /**
|
||
217 | * Permite dibujar un DxfText.
|
||
218 | */
|
||
219 | public void draw(Graphics2D g, ViewPortData vp) { |
||
220 | if (dxfColor == AcadColor.BYLAYER) {
|
||
221 | g.setColor(layer.getColor()); |
||
222 | } else {
|
||
223 | g.setColor(AcadColor.getColor(dxfColor)); |
||
224 | } |
||
225 | |||
226 | Font fntSave = g.getFont();
|
||
227 | Font fnt;
|
||
228 | Point2D ptT0 = new Point2D.Double(pts[0].getX(), pts[0].getY()); |
||
229 | Point2D ptT1 = new Point2D.Double(pts[0].getX() + h, pts[0].getY() + h); |
||
230 | vp.mat.transform(ptT0, ptT0); |
||
231 | vp.mat.transform(ptT1, ptT1); |
||
232 | fnt = new Font(fntSave.getName(), fntSave.getStyle(), |
||
233 | (int) (ptT1.getX() - ptT0.getX()));
|
||
234 | g.setFont(fnt); |
||
235 | ptT0.setLocation(pts[0].getX(), pts[0].getY()); |
||
236 | vp.mat.transform(ptT0, ptT0); |
||
237 | |||
238 | // Codigo para implementar rotacion de textos.
|
||
239 | // Falta depurar.
|
||
240 | /*System.out.println("040906: rot = " + rot);
|
||
241 | //angle = pathLen.angleAtLength(midDistance);
|
||
242 | rot = Math.toRadians(rot);
|
||
243 | if (rot < 0) rot = rot + 2.0*Math.PI;
|
||
244 | if ((rot > (Math.PI/2)) && (rot < (3 * Math.PI /2))) rot = rot - (Math.PI/2.0);
|
||
245 | rot = Math.toDegrees(rot);
|
||
246 | //theLabel.setRotation(Math.toDegrees(angle));
|
||
247 | AffineTransform transfRot = AffineTransform.getRotateInstance(rot, ptT0.getX(), ptT0.getY());
|
||
248 | AffineTransform ant = g.getTransform();
|
||
249 | g.setTransform(transfRot);*/
|
||
250 | g.drawString(text, (int) ptT0.getX(), (int) ptT0.getY()); |
||
251 | g.setFont(fntSave); |
||
252 | |||
253 | //g.setTransform(ant);
|
||
254 | } |
||
255 | |||
256 | 12744 | caballero | /**
|
257 | * Permite la escritura de entidades DxfText en un fichero DXF2000.
|
||
258 | * @return String, la cadena que se escribir? en el fichero con la informaci?n
|
||
259 | * del DxfText.
|
||
260 | 8026 | nacho | */
|
261 | public String toDxfString() { |
||
262 | 12744 | caballero | StringBuffer sb = null; |
263 | sb = new StringBuffer(DxfGroup.toString(0, "TEXT")); |
||
264 | sb.append(DxfGroup.toString(1, getText()));
|
||
265 | sb.append(DxfGroup.toString(5, getHandle()));
|
||
266 | |||
267 | // sb.append(DxfGroup.toString(100, "AcDbText"));
|
||
268 | sb.append(DxfGroup.toString(8, layer.getName()));
|
||
269 | sb.append(DxfGroup.toString(62, dxfColor));
|
||
270 | // sb.append(DxfGroup.toString(100, "AcDbText"));
|
||
271 | sb.append(DxfGroup.toString(10, pt.getX(), 6)); |
||
272 | sb.append(DxfGroup.toString(20, pt.getY(), 6)); |
||
273 | sb.append(DxfGroup.toString(30, 0.0, 6)); |
||
274 | sb.append(DxfGroup.toString(40, getHeight(), 6)); |
||
275 | sb.append(DxfGroup.toString(50, getRotation(), 6)); |
||
276 | |||
277 | |||
278 | return sb.toString();
|
||
279 | 8026 | nacho | } |
280 | } |