Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / px / dxf / DxfText.java @ 2312

History | View | Annotate | Download (4.7 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 java.awt.Font;
27
import java.awt.Graphics2D;
28
import java.awt.geom.Point2D;
29

    
30
import org.cresques.cts.ICoordTrans;
31
import org.cresques.cts.IProjection;
32
import org.cresques.geo.ViewPortData;
33
import org.cresques.px.Extent;
34

    
35
/**
36
 * Entidad TEXT de AutoCAD
37
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>* @author administrador
38
 * 
39
 * jmorell, 050406: El segundo punto es opcional.
40
 */
41
public class DxfText extends DxfEntity {
42
        public final static int ALIGN_LEFT                = 0;
43
        public final static int ALIGN_CENTER        = 1;
44
        public final static int ALIGN_RIGHT                = 2;
45
        public final static int ALIGN_ALIGNED        = 3;
46
        public final static int ALIGN_MIDDLE        = 4;
47
        public final static int ALIGN_FIT                = 5;
48
        private String text = null;
49
        Point2D [] pts;
50
        private Point2D pt;
51
        private double rot = 0.0;
52
        private double h = 1.0;
53
        int align = ALIGN_LEFT;
54
        private boolean twoPointsFlag;
55
        
56
        public DxfText(IProjection proj, DxfLayer layer, String txt) {
57
                super(proj, layer);
58
                //System.out.println("Dxf: TEXT '"+txt+"'.");
59
                extent = new Extent();
60
                text = txt;
61
                pts = new Point2D[2];
62
                pt = new Point2D.Double();
63
                twoPointsFlag = false;
64
        }
65
        
66
        public void setPt(Point2D pt) {
67
                this.pt = pt;
68
        }
69
        public Point2D getPt() {
70
                return pt;
71
        }
72
        public void setTwoPointsFlag(boolean f) {
73
                twoPointsFlag = f;
74
        }
75
        public boolean getTwoPointsFlag() {
76
                return twoPointsFlag;
77
        }
78

    
79
        public void setPt1(Point2D pt) { 
80
                pts[0] = pt;
81
                extent.add(pt);
82
        }
83
        public Point2D getPt1() { return pts[0]; }
84
        public void setPt2(Point2D pt) {
85
                pts[1] = pt;
86
                extent.add(pt);
87
        }
88
        public Point2D getPt2() { return pts[1]; }
89
        public void setHeight(double h) { this.h = h; }
90
        public void setRotation(double r) { rot = r; }
91
        public double getRotation() { return rot; }
92
        public String getText() { return text; }
93

    
94
        public void reProject(ICoordTrans rp) {
95
                Point2D [] savePts = pts;
96

    
97
                pts = new Point2D[2];
98
                extent = new Extent();
99
                Point2D ptDest = null;
100
                for (int i=0; i<savePts.length; i++) {
101
                        ptDest = rp.getPDest().createPoint(0.0,0.0);
102
                        if (savePts[i] == null)
103
                                ptDest = null;
104
                        else {
105
                                ptDest = rp.convert((Point2D) savePts[i], ptDest);
106
                                extent.add(ptDest);
107
                        }
108
                        pts[i] = ptDest;
109
                }
110
                // Reproyecto la altura del texto
111
                Point2D ptOrig = rp.getPOrig().createPoint(savePts[0].getX(),savePts[0].getY()+h);
112
                ptDest = rp.getPDest().createPoint(0D, 0D);
113
                ptDest = rp.convert(ptOrig, ptDest);
114
                h = ptDest.getY()-pts[0].getY();
115
                setProjection(rp.getPDest());
116
        }
117

    
118
        public void draw(Graphics2D g, ViewPortData vp) {
119
                if (dxfColor == AcadColor.BYLAYER)
120
                        g.setColor(layer.getColor());
121
                else
122
                        g.setColor(AcadColor.getColor(dxfColor));
123
                Font fntSave = g.getFont(), fnt;
124
                Point2D ptT0 = new Point2D.Double(pts[0].getX(), pts[0].getY());
125
                Point2D ptT1 = new Point2D.Double(pts[0].getX()+h, pts[0].getY()+h);
126
                vp.mat.transform(ptT0, ptT0);
127
                vp.mat.transform(ptT1, ptT1);
128
                fnt = new Font(fntSave.getName(), fntSave.getStyle(), (int) (ptT1.getX()-ptT0.getX()));
129
                g.setFont(fnt);
130
                ptT0.setLocation(pts[0].getX(), pts[0].getY());
131
                vp.mat.transform(ptT0, ptT0);
132
                
133
                // Codigo para implementar rotacion de textos.
134
                // Falta depurar.
135
                /*System.out.println("040906: rot = " + rot);
136
                //angle = pathLen.angleAtLength(midDistance);
137
                rot = Math.toRadians(rot);
138
                if (rot < 0) rot = rot + 2.0*Math.PI;
139
                if ((rot > (Math.PI/2)) && (rot < (3 * Math.PI /2))) rot = rot - (Math.PI/2.0);
140
                rot = Math.toDegrees(rot);
141
                //theLabel.setRotation(Math.toDegrees(angle));
142
                AffineTransform transfRot = AffineTransform.getRotateInstance(rot, ptT0.getX(), ptT0.getY());
143
                AffineTransform ant = g.getTransform();
144
                g.setTransform(transfRot);*/
145
                
146
                g.drawString(text, (int) ptT0.getX(), (int)ptT0.getY());
147
                g.setFont(fntSave);
148
                
149
                //g.setTransform(ant);
150
        }
151

    
152
        /* (non-Javadoc)
153
         * @see org.cresques.px.dxf.DxfEntity#toDxfFileString()
154
         */
155
        public String toDxfString() {
156
                // TODO Auto-generated method stub
157
                return "";
158
        }
159
}