KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > Font


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: Font.java,v $
11    Revision 1.11 2004/05/06 12:35:21 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.10 2004/05/05 12:43:19 bobintetley
15    Patches/new files from Laurent Martell
16
17    Revision 1.9 2004/04/16 10:19:06 dannaab
18    Misc bug fixes, InputMap implementation, preliminary undo support
19
20    Revision 1.8 2004/01/27 12:10:15 bobintetley
21    Win32 compatibility fixes
22
23    Revision 1.7 2004/01/26 08:10:59 bobintetley
24    Many bugfixes and addition of SwingSet
25
26    Revision 1.6 2003/12/14 09:13:38 bobintetley
27    Added CVS log to source headers
28
29 */

30
31 package swingwt.awt;
32
33 import java.util.Hashtable JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import swingwtx.swing.SwingUtilities;
37
38 public class Font {
39     
40     private org.eclipse.swt.graphics.Font peer = null;
41     
42     /** Destroy the font when finalize is called. Not required for
43      * the constructor passing the font - only do this when this
44      * instance created the SWT font */

45     protected boolean disposeOnFinalize = true;
46
47     public static final int PLAIN = 0;
48     public static final int BOLD = 1;
49     public static final int ITALIC = 2;
50     public static final int ROMAN_BASELINE = 0;
51     public static final int CENTER_BASELINE = 1;
52     public static final int HANGING_BASELINE = 2;
53     public static final int TRUETYPE_FONT = 0;
54
55     
56     protected String JavaDoc name = "Dialog";
57     protected int style = PLAIN;
58     protected int size = 12;
59     
60     public Font(final String JavaDoc name, final int style, final int size) {
61         this.name = name;
62         this.style = style;
63         this.size = size;
64         createPeer();
65     }
66     
67     public Font(final String JavaDoc name, final int style, final int size, boolean f) {
68         this.name = name;
69         this.style = style;
70         this.size = size;
71         createPeer();
72     }
73     
74     public Font(Map JavaDoc attributes) {
75         setAttributes(attributes);
76         createPeer();
77     }
78     
79     public Font(org.eclipse.swt.graphics.Font swtfont) {
80         populateFromSWTFont(swtfont);
81     }
82     
83     public static Font getFont(Map JavaDoc attributes) {
84         return new Font(attributes);
85     }
86     
87     /** Creates the SWT peer from the Font info we have */
88     protected void createPeer() {
89         SwingUtilities.invokeSync( new Runnable JavaDoc() {
90             public void run() {
91                 // Map AWT constants to SWT
92
int swtStyle = org.eclipse.swt.SWT.NONE;
93                 if ((style & BOLD) > 0)
94                     swtStyle = swtStyle | org.eclipse.swt.SWT.BOLD;
95                 if ((style & ITALIC) > 0)
96                     swtStyle = swtStyle | org.eclipse.swt.SWT.ITALIC;
97                 peer = new org.eclipse.swt.graphics.Font(swingwtx.swing.SwingWTUtils.getDisplay(), name, size, swtStyle);
98                 // If this class created it, it's up to it to
99
// destroy the peer.
100
disposeOnFinalize = true;
101             }
102         });
103     }
104     
105     /** Sets font properties based on a SWT font */
106     protected void populateFromSWTFont(org.eclipse.swt.graphics.Font swtfont) {
107         // Must be on dispatch thread if we have a real SWT font to throw about
108
peer = swtfont;
109         disposeOnFinalize = false;
110         
111         // Obtain style info from SWT font for use by derive
112
org.eclipse.swt.graphics.FontData[] fd = peer.getFontData();
113         if (fd != null)
114             if (fd.length > 0) {
115                 name = fd[0].getName();
116                 style = PLAIN;
117                 if ((fd[0].getStyle() & org.eclipse.swt.SWT.BOLD) > 0)
118                     style = style | BOLD;
119                 if ((fd[0].getStyle() & org.eclipse.swt.SWT.ITALIC) > 0)
120                     style = style | ITALIC;
121                 size = fd[0].getHeight();
122             }
123     }
124     
125     public void setAttributes(Map JavaDoc attributes) {
126         if (attributes != null) {
127             String JavaDoc af = attributes.get("family").toString();
128             if (af != null) this.name = attributes.get("family").toString();
129             String JavaDoc as = attributes.get("size").toString();
130             if (as != null) this.size = Integer.parseInt(attributes.get("size").toString());
131             String JavaDoc ast = attributes.get("style").toString();
132             if (ast != null) this.style = Integer.parseInt(attributes.get("style").toString());
133         }
134     }
135     
136     public Map JavaDoc getAttributes() {
137         Hashtable JavaDoc ht = new Hashtable JavaDoc();
138         ht.put("family", this.name);
139         ht.put("size", new Integer JavaDoc(this.size));
140         ht.put("style", new Integer JavaDoc(this.style));
141         return ht;
142     }
143     
144     public String JavaDoc getName() { return name; }
145     public int getStyle() { return style; }
146     public int getSize() { return size; }
147     public float getSize2D() { return (float) size; }
148     
149     public boolean isBold() { return (style & BOLD) > 0; }
150     public boolean isPlain() { return style == PLAIN; }
151     public boolean isItalic() { return (style & ITALIC) > 0; }
152     
153     public Font deriveFont(int style) {
154         return new Font(this.name, style, this.size);
155     }
156     
157     public org.eclipse.swt.graphics.Font getSWTFont() {
158         return peer;
159     }
160     
161     public void dispose() {
162         peer.dispose();
163     }
164     
165     protected void finalize() throws Throwable JavaDoc {
166         if (disposeOnFinalize) dispose();
167     }
168 }
169
Popular Tags