KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > graphics > Font


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.graphics;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16
17 /**
18  * Instances of this class manage operating system resources that
19  * define how text looks when it is displayed. Fonts may be constructed
20  * by providing a device and either name, size and style information
21  * or a <code>FontData</code> object which encapsulates this data.
22  * <p>
23  * Application code must explicitly invoke the <code>Font.dispose()</code>
24  * method to release the operating system resources managed by each instance
25  * when those instances are no longer required.
26  * </p>
27  *
28  * @see FontData
29  */

30
31 public final class Font extends Resource {
32     
33     /**
34      * the handle to the OS font resource
35      * (Warning: This field is platform dependent)
36      * <p>
37      * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
38      * public API. It is marked public only so that it can be shared
39      * within the packages provided by SWT. It is not available on all
40      * platforms and should never be accessed from application code.
41      * </p>
42      */

43     public int handle;
44     
45 /**
46  * Prevents uninitialized instances from being created outside the package.
47  */

48 Font() {
49 }
50
51 /**
52  * Constructs a new font given a device and font data
53  * which describes the desired font's appearance.
54  * <p>
55  * You must dispose the font when it is no longer required.
56  * </p>
57  *
58  * @param device the device to create the font on
59  * @param fd the FontData that describes the desired font (must not be null)
60  *
61  * @exception IllegalArgumentException <ul>
62  * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
63  * <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li>
64  * </ul>
65  * @exception SWTError <ul>
66  * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li>
67  * </ul>
68  */

69 public Font(Device device, FontData fd) {
70     if (device == null) device = Device.getDevice();
71     if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
72     init(device, fd);
73     if (device.tracking) device.new_Object(this);
74 }
75
76 /**
77  * Constructs a new font given a device and an array
78  * of font data which describes the desired font's
79  * appearance.
80  * <p>
81  * You must dispose the font when it is no longer required.
82  * </p>
83  *
84  * @param device the device to create the font on
85  * @param fds the array of FontData that describes the desired font (must not be null)
86  *
87  * @exception IllegalArgumentException <ul>
88  * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
89  * <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li>
90  * <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li>
91  * <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li>
92  * </ul>
93  * @exception SWTError <ul>
94  * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li>
95  * </ul>
96  *
97  * @since 2.1
98  */

99 public Font(Device device, FontData[] fds) {
100     if (device == null) device = Device.getDevice();
101     if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
102     if (fds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
103     if (fds.length == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
104     for (int i=0; i<fds.length; i++) {
105         if (fds[i] == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
106     }
107     init(device, fds[0]);
108     if (device.tracking) device.new_Object(this);
109 }
110
111 /**
112  * Constructs a new font given a device, a font name,
113  * the height of the desired font in points, and a font
114  * style.
115  * <p>
116  * You must dispose the font when it is no longer required.
117  * </p>
118  *
119  * @param device the device to create the font on
120  * @param name the name of the font (must not be null)
121  * @param height the font height in points
122  * @param style a bit or combination of NORMAL, BOLD, ITALIC
123  *
124  * @exception IllegalArgumentException <ul>
125  * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
126  * <li>ERROR_NULL_ARGUMENT - if the name argument is null</li>
127  * <li>ERROR_INVALID_ARGUMENT - if the height is negative</li>
128  * </ul>
129  * @exception SWTError <ul>
130  * <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li>
131  * </ul>
132  */

133 public Font(Device device, String JavaDoc name, int height, int style) {
134     if (device == null) device = Device.getDevice();
135     if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
136     if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
137     init(device, new FontData (name, height, style));
138     if (device.tracking) device.new_Object(this);
139 }
140
141 /*public*/ Font(Device device, String JavaDoc name, float height, int style) {
142     if (device == null) device = Device.getDevice();
143     if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
144     if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
145     init(device, new FontData (name, height, style));
146     if (device.tracking) device.new_Object(this);
147 }
148
149 /**
150  * Disposes of the operating system resources associated with
151  * the font. Applications must dispose of all fonts which
152  * they allocate.
153  */

154 public void dispose() {
155     if (handle == 0) return;
156     if (device.isDisposed()) return;
157     OS.DeleteObject(handle);
158     handle = 0;
159     if (device.tracking) device.dispose_Object(this);
160     device = null;
161 }
162
163 /**
164  * Compares the argument to the receiver, and returns true
165  * if they represent the <em>same</em> object using a class
166  * specific comparison.
167  *
168  * @param object the object to compare with this object
169  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
170  *
171  * @see #hashCode
172  */

173 public boolean equals(Object JavaDoc object) {
174     if (object == this) return true;
175     if (!(object instanceof Font)) return false;
176     Font font = (Font) object;
177     return device == font.device && handle == font.handle;
178 }
179
180 /**
181  * Returns an array of <code>FontData</code>s representing the receiver.
182  * On Windows, only one FontData will be returned per font. On X however,
183  * a <code>Font</code> object <em>may</em> be composed of multiple X
184  * fonts. To support this case, we return an array of font data objects.
185  *
186  * @return an array of font data objects describing the receiver
187  *
188  * @exception SWTException <ul>
189  * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
190  * </ul>
191  */

192 public FontData[] getFontData() {
193     if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
194     LOGFONT logFont = OS.IsUnicode ? (LOGFONT)new LOGFONTW() : new LOGFONTA();
195     OS.GetObject(handle, LOGFONT.sizeof, logFont);
196     return new FontData[] {FontData.win32_new(logFont, device.computePoints(logFont, handle))};
197 }
198
199 /**
200  * Returns an integer hash code for the receiver. Any two
201  * objects that return <code>true</code> when passed to
202  * <code>equals</code> must return the same value for this
203  * method.
204  *
205  * @return the receiver's hash
206  *
207  * @see #equals
208  */

209 public int hashCode () {
210     return handle;
211 }
212
213 void init (Device device, FontData fd) {
214     if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
215     this.device = device;
216     LOGFONT logFont = fd.data;
217     int lfHeight = logFont.lfHeight;
218     logFont.lfHeight = device.computePixels(fd.height);
219     handle = OS.CreateFontIndirect(logFont);
220     logFont.lfHeight = lfHeight;
221     if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
222 }
223
224 /**
225  * Returns <code>true</code> if the font has been disposed,
226  * and <code>false</code> otherwise.
227  * <p>
228  * This method gets the dispose state for the font.
229  * When a font has been disposed, it is an error to
230  * invoke any other method using the font.
231  *
232  * @return <code>true</code> when the font is disposed and <code>false</code> otherwise
233  */

234 public boolean isDisposed() {
235     return handle == 0;
236 }
237
238 /**
239  * Returns a string containing a concise, human-readable
240  * description of the receiver.
241  *
242  * @return a string representation of the receiver
243  */

244 public String JavaDoc toString () {
245     if (isDisposed()) return "Font {*DISPOSED*}";
246     return "Font {" + handle + "}";
247 }
248
249 /**
250  * Invokes platform specific functionality to allocate a new font.
251  * <p>
252  * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
253  * API for <code>Font</code>. It is marked public only so that it
254  * can be shared within the packages provided by SWT. It is not
255  * available on all platforms, and should never be called from
256  * application code.
257  * </p>
258  *
259  * @param device the device on which to allocate the color
260  * @param handle the handle for the font
261  * @return a new font object containing the specified device and handle
262  */

263 public static Font win32_new(Device device, int handle) {
264     if (device == null) device = Device.getDevice();
265     Font font = new Font();
266     font.handle = handle;
267     font.device = device;
268     return font;
269 }
270
271 }
272
Popular Tags