root/dwt/graphics/Font.d

Revision 246:fd9c62a2998e, 8.7 kB (checked in by Frank Benoit <benoit@tionex.de>, 6 months ago)

Updater SWT 3.4M7 to 3.4

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module dwt.graphics.Font;
14
15
16 import dwt.DWT;
17 import dwt.DWTError;
18 import dwt.DWTException;
19 import dwt.internal.win32.OS;
20
21 import dwt.graphics.Resource;
22 import dwt.graphics.FontData;
23 import dwt.graphics.Device;
24
25 import tango.text.convert.Format;
26 import dwt.dwthelper.utils;
27 //import tango.stdc.stringz;
28
29 /**
30  * Instances of this class manage operating system resources that
31  * define how text looks when it is displayed. Fonts may be constructed
32  * by providing a device and either name, size and style information
33  * or a <code>FontData</code> object which encapsulates this data.
34  * <p>
35  * Application code must explicitly invoke the <code>Font.dispose()</code>
36  * method to release the operating system resources managed by each instance
37  * when those instances are no longer required.
38  * </p>
39  *
40  * @see FontData
41  * @see <a href="http://www.eclipse.org/swt/snippets/#font">Font snippets</a>
42  * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Examples: GraphicsExample, PaintExample</a>
43  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
44  */
45
46 public final class Font : Resource {
47
48     alias Resource.init_ init_;
49     /**
50      * the handle to the OS font resource
51      * (Warning: This field is platform dependent)
52      * <p>
53      * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT
54      * public API. It is marked public only so that it can be shared
55      * within the packages provided by DWT. It is not available on all
56      * platforms and should never be accessed from application code.
57      * </p>
58      */
59     public HFONT handle;
60
61 /**
62  * Prevents uninitialized instances from being created outside the package.
63  */
64 this(Device device) {
65     super(device);
66 }
67
68 /**
69  * Constructs a new font given a device and font data
70  * which describes the desired font's appearance.
71  * <p>
72  * You must dispose the font when it is no longer required.
73  * </p>
74  *
75  * @param device the device to create the font on
76  * @param fd the FontData that describes the desired font (must not be null)
77  *
78  * @exception IllegalArgumentException <ul>
79  *    <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
80  *    <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li>
81  * </ul>
82  * @exception DWTError <ul>
83  *    <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li>
84  * </ul>
85  */
86 public this(Device device, FontData fd) {
87     super(device);
88     init_(fd);
89     init_();
90 }
91
92 /**
93  * Constructs a new font given a device and an array
94  * of font data which describes the desired font's
95  * appearance.
96  * <p>
97  * You must dispose the font when it is no longer required.
98  * </p>
99  *
100  * @param device the device to create the font on
101  * @param fds the array of FontData that describes the desired font (must not be null)
102  *
103  * @exception IllegalArgumentException <ul>
104  *    <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
105  *    <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li>
106  *    <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li>
107  *    <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li>
108  * </ul>
109  * @exception DWTError <ul>
110  *    <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li>
111  * </ul>
112  *
113  * @since 2.1
114  */
115 public this(Device device, FontData[] fds) {
116     super(device);
117     if (fds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
118     if (fds.length is 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
119     for (int i=0; i<fds.length; i++) {
120         if (fds[i] is null) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
121     }
122     init_(fds[0]);
123     init_();
124 }
125
126 /**
127  * Constructs a new font given a device, a font name,
128  * the height of the desired font in points, and a font
129  * style.
130  * <p>
131  * You must dispose the font when it is no longer required.
132  * </p>
133  *
134  * @param device the device to create the font on
135  * @param name the name of the font (must not be null)
136  * @param height the font height in points
137  * @param style a bit or combination of NORMAL, BOLD, ITALIC
138  *
139  * @exception IllegalArgumentException <ul>
140  *    <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
141  *    <li>ERROR_NULL_ARGUMENT - if the name argument is null</li>
142  *    <li>ERROR_INVALID_ARGUMENT - if the height is negative</li>
143  * </ul>
144  * @exception DWTError <ul>
145  *    <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li>
146  * </ul>
147  */
148 public this(Device device, String name, int height, int style) {
149     super(device);
150     if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
151     init_(new FontData (name, height, style));
152     init_();
153 }
154
155 /*public*/ this(Device device, String name, float height, int style) {
156     super(device);
157     if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
158     init_(new FontData (name, height, style));
159     init_();
160 }
161 void destroy() {
162     OS.DeleteObject(handle);
163     handle = null;
164 }
165
166 /**
167  * Compares the argument to the receiver, and returns true
168  * if they represent the <em>same</em> object using a class
169  * specific comparison.
170  *
171  * @param object the object to compare with this object
172  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
173  *
174  * @see #hashCode
175  */
176 public int opEquals(Object object) {
177     if (object is this) return true;
178     if ( auto font = cast(Font)object ){
179         return device is font.device && handle is font.handle;
180     }
181     return false;
182 }
183
184 /**
185  * Returns an array of <code>FontData</code>s representing the receiver.
186  * On Windows, only one FontData will be returned per font. On X however,
187  * a <code>Font</code> object <em>may</em> be composed of multiple X
188  * fonts. To support this case, we return an array of font data objects.
189  *
190  * @return an array of font data objects describing the receiver
191  *
192  * @exception DWTException <ul>
193  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
194  * </ul>
195  */
196 public FontData[] getFontData() {
197     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
198     LOGFONT* logFont = new LOGFONT();
199     OS.GetObject(handle, LOGFONT.sizeof, logFont);
200     return [ cast(FontData) FontData.win32_new(logFont, device.computePoints(logFont, handle))];
201 }
202
203 /**
204  * Returns an integer hash code for the receiver. Any two
205  * objects that return <code>true</code> when passed to
206  * <code>equals</code> must return the same value for this
207  * method.
208  *
209  * @return the receiver's hash
210  *
211  * @see #equals
212  */
213 override public hash_t toHash () {
214     return cast(hash_t)handle;
215 }
216
217 void init_ (FontData fd) {
218     if (fd is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
219     LOGFONT* logFont = &fd.data;
220     int lfHeight = logFont.lfHeight;
221     logFont.lfHeight = device.computePixels(fd.height);
222     handle = OS.CreateFontIndirect(logFont);
223     logFont.lfHeight = lfHeight;
224     if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES);
225 }
226
227 /**
228  * Returns <code>true</code> if the font has been disposed,
229  * and <code>false</code> otherwise.
230  * <p>
231  * This method gets the dispose state for the font.
232  * When a font has been disposed, it is an error to
233  * invoke any other method using the font.
234  *
235  * @return <code>true</code> when the font is disposed and <code>false</code> otherwise
236  */
237 override public bool isDisposed() {
238     return handle is null;
239 }
240
241 /**
242  * Returns a string containing a concise, human-readable
243  * description of the receiver.
244  *
245  * @return a string representation of the receiver
246  */
247 override public String toString () {
248     if (isDisposed()) return "Font {*DISPOSED*}";
249     return Format( "Font {{{}}", handle );
250 }
251
252 /**
253  * Invokes platform specific functionality to allocate a new font.
254  * <p>
255  * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
256  * API for <code>Font</code>. It is marked public only so that it
257  * can be shared within the packages provided by DWT. It is not
258  * available on all platforms, and should never be called from
259  * application code.
260  * </p>
261  *
262  * @param device the device on which to allocate the color
263  * @param handle the handle for the font
264  * @return a new font object containing the specified device and handle
265  */
266 public static Font win32_new(Device device, HFONT handle) {
267     Font font = new Font(device);
268     font.handle = handle;
269     return font;
270 }
271
272 }
Note: See TracBrowser for help on using the browser.