KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > FontMetrics


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: FontMetrics.java,v $
11    Revision 1.3 2004/04/23 10:02:47 bobintetley
12    MouseEvent BUTTON_MASK constants, JSlider.createStandardLabels(), FontMetrics
13    thread safety, Component/Toolkit.getFontMetrics() and MouseMotionAdapter
14
15    Revision 1.2 2004/01/15 16:02:15 bobintetley
16    Using deprecated String methods fixedd
17
18    Revision 1.1 2004/01/15 15:20:29 bobintetley
19    Java2D work
20
21
22 */

23
24 package swingwt.awt;
25
26 import swingwt.awt.font.*;
27 import swingwt.awt.geom.*;
28
29 import swingwtx.swing.SwingUtilities;
30
31 public class FontMetrics {
32     
33     protected Font font;
34     
35     /** An SWT graphics context used to get info about the font */
36     protected org.eclipse.swt.graphics.GC gc = null;
37     /** A drawable surface for the GC to be generated from */
38     protected org.eclipse.swt.graphics.Image im = null;
39
40     protected FontMetrics(Font font) { this.font = font; }
41     
42     private void checkGC() {
43         SwingUtilities.invokeSync(new Runnable JavaDoc() {
44             public void run() {
45                 if (gc != null) return;
46                 im = new org.eclipse.swt.graphics.Image(swingwtx.swing.SwingWTUtils.getDisplay(), 100, 100);
47                 gc = new org.eclipse.swt.graphics.GC(im);
48             }
49         });
50     }
51     
52     public Font getFont() { return font; }
53     public int getLeading() { return 0; }
54     public int getAscent() {
55         final int[] ret = new int[1];
56         SwingUtilities.invokeSync(new Runnable JavaDoc() {
57             public void run() {
58                 checkGC();
59                 ret[0] = gc.stringExtent("I").y;
60             }
61         });
62         return ret[0];
63     }
64     public int getDescent() { return 0; }
65     public int getHeight() { return getLeading() + getAscent() + getDescent(); }
66     public int getMaxAscent() { return getAscent(); }
67     public int getMaxDescent() { return getDescent(); }
68     public int getMaxDecent() { return getMaxDescent(); }
69     public int getMaxAdvance() { return -1; }
70     public int charWidth(int ch) { return charWidth((char)ch); }
71     public int charWidth(char ch) {
72     if (ch < 256) {
73         return getWidths()[ch];
74     }
75     char data[] = {ch};
76     return charsWidth(data, 0, 1);
77     }
78     public int stringWidth(final String JavaDoc str) {
79         final int[] ret = new int[1];
80         SwingUtilities.invokeSync(new Runnable JavaDoc() {
81             public void run() {
82                 checkGC();
83                 ret[0] = gc.stringExtent(str).x;
84             }
85         });
86         return ret[0];
87     }
88     public int charsWidth(char data[], int off, int len) {
89     return stringWidth(new String JavaDoc(data, off, len));
90     }
91     public int bytesWidth(byte data[], int off, int len) {
92     return stringWidth(new String JavaDoc(data).substring(off, off+len));
93     }
94     public int[] getWidths() {
95     int widths[] = new int[256];
96     for (char ch = 0 ; ch < 256 ; ch++) {
97         widths[ch] = charWidth(ch);
98     }
99     return widths;
100     }
101     public boolean hasUniformLineMetrics() { return false; }
102
103     public LineMetrics getLineMetrics( String JavaDoc str, Graphics context) {
104         return new LineMetrics(str.length(), getHeight());
105     }
106     
107     public LineMetrics getLineMetrics( String JavaDoc str,
108                                             int beginIndex, int limit,
109                                             Graphics context) {
110         return new LineMetrics(limit - beginIndex, getHeight());
111     }
112
113     public LineMetrics getLineMetrics(char [] chars,
114                                             int beginIndex, int limit,
115                                             Graphics context) {
116         return getLineMetrics(
117                                 new String JavaDoc(chars), beginIndex, limit, context);
118     }
119     public Rectangle2D getStringBounds(final String JavaDoc str, Graphics context) {
120         final Rectangle[] ret = new Rectangle[1];
121         SwingUtilities.invokeSync(new Runnable JavaDoc() {
122             public void run() {
123                 checkGC();
124                 org.eclipse.swt.graphics.Point p = gc.stringExtent(str);
125                 ret[0] = new Rectangle(p.x, p.y);
126             }
127         });
128         return ret[0];
129     }
130     public Rectangle2D getStringBounds( String JavaDoc str,
131                                         int beginIndex, int limit,
132                                         Graphics context) {
133         return getStringBounds(str, context);
134     }
135     public Rectangle2D getStringBounds( char [] chars,
136                                         int beginIndex, int limit,
137                                         Graphics context) {
138         return getStringBounds(new String JavaDoc(chars), context);
139     }
140     public Rectangle2D getMaxCharBounds(Graphics context) {
141         return new Rectangle(0, 0);
142     }
143     public String JavaDoc toString() {
144     return getClass().getName() +
145         "[font=" + getFont() +
146         "ascent=" + getAscent() +
147         ", descent=" + getDescent() +
148         ", height=" + getHeight() + "]";
149     }
150     
151 }
152
Popular Tags