Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / px / dxf / DxfText.java @ 8026

History | View | Annotate | Download (7.27 KB)

1
/*
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

    
31
import org.cresques.px.Extent;
32

    
33
import java.awt.Font;
34
import java.awt.Graphics2D;
35
import java.awt.geom.Point2D;
36

    
37

    
38
/**
39
 * Entidad TEXT de un fichero DXF.
40
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
41
 * @author jmorell
42
 * jmorell, 050406: El segundo punto es opcional.
43
 */
44
public class DxfText extends DxfEntity {
45
    public final static int ALIGN_LEFT = 0;
46
    public final static int ALIGN_CENTER = 1;
47
    public final static int ALIGN_RIGHT = 2;
48
    public final static int ALIGN_ALIGNED = 3;
49
    public final static int ALIGN_MIDDLE = 4;
50
    public final static int ALIGN_FIT = 5;
51
    private String text = null;
52
    Point2D[] pts;
53
    private Point2D pt;
54
    private double rot = 0.0;
55
    private double h = 1.0;
56
    int align = ALIGN_LEFT;
57
    private boolean twoPointsFlag;
58

    
59
    /**
60
     * Constructor de DxfText.
61
     * @param proj, proyecci?n cartogr?fica en la que se encuentra el DxfText.
62
     * @param layer, capa del DXF en la que se encuentra el DxfText.
63
     * @param txt, texto.
64
     */
65
    public DxfText(IProjection proj, DxfLayer layer, String txt) {
66
        super(proj, layer);
67

    
68
        //System.out.println("Dxf: TEXT '"+txt+"'.");
69
        extent = new Extent();
70
        text = txt;
71
        pts = new Point2D[2];
72
        pt = new Point2D.Double();
73
        twoPointsFlag = false;
74
    }
75

    
76
    /**
77
     * Establece el punto de inserci?n del DxfText.
78
     * @param pt, punto de inserci?n.
79
     */
80
    public void setPt(Point2D pt) {
81
        this.pt = pt;
82
    }
83

    
84
    /**
85
     * @return Returns the pt.
86
     */
87
    public Point2D getPt() {
88
        return pt;
89
    }
90

    
91
    /**
92
     * Establece si el texto se situa a trav?s de dos puntos de inserci?n. Estos dos
93
     * puntos definir?n su orientaci?n. 
94
     * @param f
95
     */
96
    public void setTwoPointsFlag(boolean f) {
97
        twoPointsFlag = f;
98
    }
99

    
100
    /**
101
     * Informa sobre si el texto se situa a trav?s de dos puntos de inserci?n. Estos dos
102
     * puntos definir?n su orientaci?n.
103
     * @return boolean
104
     */
105
    public boolean getTwoPointsFlag() {
106
        return twoPointsFlag;
107
    }
108

    
109
    /**
110
     * Establece el primer punto de inserci?n del texto cuando este se inserta a trav?s
111
     * de dos puntos de inserci?n.
112
     * @param pt
113
     */
114
    public void setPt1(Point2D pt) {
115
        pts[0] = pt;
116
        extent.add(pt);
117
    }
118

    
119
    /**
120
     * Obtiene el primer punto de inserci?n del texto cuando este se inserta a trav?s
121
     * de dos puntos de inserci?n.
122
     * @return Point2D
123
     */
124
    public Point2D getPt1() {
125
        return pts[0];
126
    }
127

    
128
    /**
129
     * Establece el segundo punto de inserci?n del texto cuando este se inserta a trav?s
130
     * de dos puntos de inserci?n.
131
     * @param pt
132
     */
133
    public void setPt2(Point2D pt) {
134
        pts[1] = pt;
135
        extent.add(pt);
136
    }
137

    
138
    /**
139
     * Obtiene el segundo punto de inserci?n del texto cuando este se inserta a trav?s
140
     * de dos puntos de inserci?n.
141
     * @return Point2D
142
     */
143
    public Point2D getPt2() {
144
        return pts[1];
145
    }
146

    
147
    /**
148
     * Establece la altura del texto.
149
     * @param h, altura del texto.
150
     */
151
    public void setHeight(double h) {
152
        this.h = h;
153
    }
154

    
155
    /**
156
     * Establece la inclinaci?n del texto.
157
     * @param r, ?ngulo de inclinaci?n del texto.
158
     */
159
    public void setRotation(double r) {
160
        rot = r;
161
    }
162

    
163
    /**
164
     * Devuelve la inclinaci?n del texto.
165
     * @return double
166
     */
167
    public double getRotation() {
168
        return rot;
169
    }
170

    
171
    /**
172
     * Devuelve el texto.
173
     * @return String
174
     */
175
    public String getText() {
176
        return text;
177
    }
178

    
179
    /**
180
     * Permite reproyectar un DxfText dado un conjunto de coordenadas de transformaci?n.
181
     * @param rp, coordenadas de transformaci?n.
182
     */
183
    public void reProject(ICoordTrans rp) {
184
        Point2D[] savePts = pts;
185

    
186
        pts = new Point2D[2];
187
        extent = new Extent();
188

    
189
        Point2D ptDest = null;
190

    
191
        for (int i = 0; i < savePts.length; i++) {
192
            ptDest = rp.getPDest().createPoint(0.0, 0.0);
193

    
194
            if (savePts[i] == null) {
195
                ptDest = null;
196
            } else {
197
                ptDest = rp.convert((Point2D) savePts[i], ptDest);
198
                extent.add(ptDest);
199
            }
200

    
201
            pts[i] = ptDest;
202
        }
203

    
204
        // Reproyecto la altura del texto
205
        Point2D ptOrig = rp.getPOrig().createPoint(savePts[0].getX(),
206
                                                   savePts[0].getY() + h);
207
        ptDest = rp.getPDest().createPoint(0D, 0D);
208
        ptDest = rp.convert(ptOrig, ptDest);
209
        h = ptDest.getY() - pts[0].getY();
210
        setProjection(rp.getPDest());
211
    }
212

    
213
    /**
214
     * Permite dibujar un DxfText.
215
     */
216
    public void draw(Graphics2D g, ViewPortData vp) {
217
        if (dxfColor == AcadColor.BYLAYER) {
218
            g.setColor(layer.getColor());
219
        } else {
220
            g.setColor(AcadColor.getColor(dxfColor));
221
        }
222

    
223
        Font fntSave = g.getFont();
224
        Font fnt;
225
        Point2D ptT0 = new Point2D.Double(pts[0].getX(), pts[0].getY());
226
        Point2D ptT1 = new Point2D.Double(pts[0].getX() + h, pts[0].getY() + h);
227
        vp.mat.transform(ptT0, ptT0);
228
        vp.mat.transform(ptT1, ptT1);
229
        fnt = new Font(fntSave.getName(), fntSave.getStyle(),
230
                       (int) (ptT1.getX() - ptT0.getX()));
231
        g.setFont(fnt);
232
        ptT0.setLocation(pts[0].getX(), pts[0].getY());
233
        vp.mat.transform(ptT0, ptT0);
234

    
235
        // Codigo para implementar rotacion de textos.
236
        // Falta depurar.
237
        /*System.out.println("040906: rot = " + rot);
238
        //angle = pathLen.angleAtLength(midDistance);
239
        rot = Math.toRadians(rot);
240
        if (rot < 0) rot = rot + 2.0*Math.PI;
241
        if ((rot > (Math.PI/2)) && (rot < (3 * Math.PI /2))) rot = rot - (Math.PI/2.0);
242
        rot = Math.toDegrees(rot);
243
        //theLabel.setRotation(Math.toDegrees(angle));
244
        AffineTransform transfRot = AffineTransform.getRotateInstance(rot, ptT0.getX(), ptT0.getY());
245
        AffineTransform ant = g.getTransform();
246
        g.setTransform(transfRot);*/
247
        g.drawString(text, (int) ptT0.getX(), (int) ptT0.getY());
248
        g.setFont(fntSave);
249

    
250
        //g.setTransform(ant);
251
    }
252

    
253
    /* (non-Javadoc)
254
     * @see org.cresques.px.dxf.DxfEntity#toDxfFileString()
255
     */
256
    public String toDxfString() {
257
        // TODO Auto-generated method stub
258
        return "";
259
    }
260
}