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