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