Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FTextLayout.java @ 4835

History | View | Annotate | Download (7.39 KB)

1
package com.iver.cit.gvsig.fmap.core.v02;
2

    
3
import java.awt.Font;
4
import java.awt.FontMetrics;
5
import java.awt.Graphics2D;
6
import java.awt.Point;
7
import java.awt.Shape;
8
import java.awt.font.FontRenderContext;
9
import java.awt.font.TextHitInfo;
10
import java.awt.geom.Point2D;
11
import java.awt.geom.Rectangle2D;
12

    
13
import com.iver.cit.gvsig.fmap.ViewPort;
14

    
15

    
16
/**
17
 * DOCUMENT ME!
18
 *
19
 * @author Vicente Caballero Navarro
20
 */
21
public class FTextLayout {
22
    private java.awt.font.TextLayout tl;
23
    private String s;
24
        private boolean isSelected=false;
25

    
26
    /**
27
     * Crea un nuevo TextLayout.
28
     *
29
     * @param s DOCUMENT ME!
30
     * @param f DOCUMENT ME!
31
     * @param frc DOCUMENT ME!
32
     */
33
    public FTextLayout(String s, Font f, FontRenderContext frc) {
34
        this.s = s;
35
        tl = new java.awt.font.TextLayout(s, f, frc);
36

    
37
    }
38

    
39
    /**
40
     * Construcci?n del rect?ngulo
41
     *
42
     * @param p
43
     * @param g DOCUMENT ME!
44
     * @param justification DOCUMENT ME!
45
     * @param vp DOCUMENT ME!
46
     *
47
     * @return
48
     */
49
    public Rectangle2D getBoundBox(Point2D p, Graphics2D g,
50
        int justification, ViewPort vp) {
51
        Rectangle2D bound = tl.getBounds();
52
        Rectangle2D bounding = vp.toMapRectangle(bound);
53

    
54
        FontMetrics metrics = g.getFontMetrics();
55

    
56
        int w = metrics.stringWidth(s);
57
        double width = vp.toMapDistance(w);
58
        int h = metrics.getMaxAscent();
59
        double height = vp.toMapDistance(h);
60
        double dist = vp.toMapDistance(3);
61

    
62
        switch (justification) {
63
            case FLabel.LEFT_BOTTOM:
64
                bounding=justification(p, bounding, 0, -dist);
65

    
66
                break;
67

    
68
            case FLabel.LEFT_CENTER:
69
                     bounding=justification(p, bounding, 0, -(height / 2));
70

    
71
                break;
72

    
73
            case FLabel.LEFT_TOP:
74
                     bounding=justification(p, bounding, 0, -height);
75

    
76
                break;
77

    
78
            case FLabel.CENTER_BOTTOM:
79
                     bounding=justification(p, bounding, -(width / 2), -dist);
80

    
81
                break;
82

    
83
            case FLabel.CENTER_CENTER:
84
                     bounding=justification(p, bounding, -(width / 2), -(height / 2));
85

    
86
                break;
87

    
88
            case FLabel.CENTER_TOP:
89
                     bounding=justification(p, bounding, -(width / 2), -height);
90

    
91
                break;
92

    
93
            case FLabel.RIGHT_BOTTOM:
94
                     bounding=justification(p, bounding, -width, -dist);
95

    
96
                break;
97

    
98
            case FLabel.RIGHT_CENTER:
99
                     bounding=justification(p, bounding, -width, -(height / 2));
100

    
101
                break;
102

    
103
            case FLabel.RIGHT_TOP:
104
                     bounding=justification(p, bounding, -width, -height);
105

    
106
                break;
107
        }
108

    
109
        return bounding;
110
        /*
111
            Point2D pOrigen=vp.fromMapPoint(p);
112
            return vp.toMapRectangle(getBoundingBox(pOrigen,justification,g));
113
    */
114
    }
115

    
116
    /**
117
     * DOCUMENT ME!
118
     *
119
     * @param p DOCUMENT ME!
120
     * @param r DOCUMENT ME!
121
     * @param x DOCUMENT ME!
122
     * @param y DOCUMENT ME!
123
     */
124
    private Rectangle2D justification(Point2D p, Rectangle2D r, double x, double y) {
125
        r.setRect(p.getX() + x, p.getY() - y, r.getWidth(), r.getHeight());
126
        return r;
127
    }
128

    
129
    /**
130
     * DOCUMENT ME!
131
     *
132
     * @param g2 DOCUMENT ME!
133
     * @param justification DOCUMENT ME!
134
     */
135
    public void draw(Graphics2D g2, int justification) {
136
        FontMetrics metrics = g2.getFontMetrics();
137
        float width = metrics.stringWidth(s);
138
        float height = metrics.getMaxAscent();
139

    
140
        switch (justification) {
141
        /*
142
            case FLabel.LEFT_BOTTOM:
143
                tl.draw(g2, 0, -3);
144

145
                break;
146

147
            case FLabel.LEFT_CENTER:
148
                tl.draw(g2, 0, -(height / 2));
149

150
                break;
151

152
            case FLabel.LEFT_TOP:
153
                tl.draw(g2, 0, -height);
154

155
                break;
156

157
            case FLabel.CENTER_BOTTOM:
158
                tl.draw(g2, -(width / 2), -3);
159

160
                break;
161

162
            case FLabel.CENTER_CENTER:
163
                tl.draw(g2, -(width / 2), -(height / 2));
164

165
                break;
166

167
            case FLabel.CENTER_TOP:
168
                tl.draw(g2, -(width / 2), -height);
169

170
                break;
171

172
            case FLabel.RIGHT_BOTTOM:
173
                tl.draw(g2, -width, -3);
174

175
                break;
176

177
            case FLabel.RIGHT_CENTER:
178
                tl.draw(g2, -width, -(height / 2));
179

180
                break;
181

182
            case FLabel.RIGHT_TOP:
183
                tl.draw(g2, -width, -height);
184

185
                break;
186
                */
187
            case FLabel.LEFT_BOTTOM:
188
                                g2.drawString(s, 0, 0 - 3);
189

    
190
                                break;
191

    
192
                        case FLabel.LEFT_CENTER:
193
                                g2.drawString(s, 0, 0 - (height / 2));
194

    
195
                                break;
196

    
197
                        case FLabel.LEFT_TOP:
198
                                g2.drawString(s, 0, 0 - height);
199

    
200
                                break;
201

    
202
                        case FLabel.CENTER_BOTTOM:
203
                                g2.drawString(s, 0 - (width / 2), 0 - 3);
204

    
205
                                break;
206

    
207
                        case FLabel.CENTER_CENTER:
208
                                g2.drawString(s, 0 - (width / 2),
209
                                        0 - (height / 2));
210

    
211
                                break;
212

    
213
                        case FLabel.CENTER_TOP:
214
                                g2.drawString(s, 0 - (width / 2), 0 -
215
                                        height);
216

    
217
                                break;
218

    
219
                        case FLabel.RIGHT_BOTTOM:
220
                                g2.drawString(s, 0 - width, 0 - 3);
221

    
222
                                break;
223

    
224
                        case FLabel.RIGHT_CENTER:
225
                                g2.drawString(s, 0 - width, 0 -
226
                                        (height / 2));
227

    
228
                                break;
229

    
230
                        case FLabel.RIGHT_TOP:
231
                                g2.drawString(s, 0 - width, 0 - height);
232

    
233
                                break;
234

    
235
        }
236
    }
237

    
238
        public TextHitInfo getNextRightHit(int insertionIndex) {
239
                return tl.getNextRightHit(insertionIndex);
240
        }
241

    
242
        public TextHitInfo getNextLeftHit(int insertionIndex) {
243
                return tl.getNextLeftHit(insertionIndex);
244
        }
245

    
246
        public void setSelected(boolean b) {
247
                isSelected=b;
248
        }
249

    
250
        public Rectangle2D getBoundingBox(Point2D p1,int justification,Graphics2D g,ViewPort vp){
251
                Rectangle2D bounding=tl.getBounds();
252

    
253
            FontMetrics metrics = g.getFontMetrics();
254
            double dist = -12;
255
            int width = metrics.stringWidth(s);
256
            int height = metrics.getMaxAscent();
257
            //Point2D p=vp.fromMapPoint(p1);
258
            Point p=new Point(0,0);
259
            switch (justification) {
260
        case FLabel.LEFT_BOTTOM:
261
                bounding =justification(p, bounding, 0, -dist);
262
                break;
263

    
264
        case FLabel.LEFT_CENTER:
265
                bounding=justification(p, bounding, 0, -(height / 2));
266

    
267
            break;
268

    
269
        case FLabel.LEFT_TOP:
270
            bounding=justification(p, bounding, 0, -height);
271

    
272
            break;
273

    
274
        case FLabel.CENTER_BOTTOM:
275
            bounding=justification(p, bounding, -(width / 2), -dist);
276

    
277
            break;
278

    
279
        case FLabel.CENTER_CENTER:
280
            bounding=justification(p, bounding, -(width / 2), -(height / 2));
281

    
282
            break;
283

    
284
        case FLabel.CENTER_TOP:
285
            bounding=justification(p, bounding, -(width / 2), -height);
286

    
287
            break;
288

    
289
        case FLabel.RIGHT_BOTTOM:
290
            bounding=justification(p, bounding, -width, -dist);
291

    
292
            break;
293

    
294
        case FLabel.RIGHT_CENTER:
295
            bounding=justification(p, bounding, -width, -(height / 2));
296

    
297
            break;
298

    
299
        case FLabel.RIGHT_TOP:
300
            bounding=justification(p, bounding, -width, -height);
301

    
302
            break;
303
    }
304
           /* Rectangle2D rtl=tl.getBounds();
305
            Rectangle2D r=new Rectangle2D.Double(rtl.getX(),rtl.getY()-rtl.getHeight()/2,rtl.getWidth(),rtl.getHeight());
306
                */
307
                return bounding;
308
        }
309

    
310
        public boolean isSelected() {
311
                return isSelected;
312
        }
313

    
314
        public void addChar(char keyChar) {
315
                s.concat(String.valueOf(keyChar));
316

    
317
        }
318

    
319
        public Shape[] getCaretShapes(int insertionIndex) {
320
                return tl.getCaretShapes(insertionIndex);
321
        }
322

    
323
}