Mercurial > projects > dwt-win
annotate dwt/graphics/Font.d @ 246:fd9c62a2998e
Updater SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 01 Jul 2008 10:15:59 +0200 |
parents | 36f5cb12e1a2 |
children | c49e17d48b76 |
rev | line source |
---|---|
23 | 1 /******************************************************************************* |
246 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
15 | 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 | |
246 | 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> | |
15 | 44 */ |
45 | |
46 public final class Font : Resource { | |
23 | 47 |
213 | 48 alias Resource.init_ init_; |
15 | 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; | |
23 | 60 |
15 | 61 /** |
62 * Prevents uninitialized instances from being created outside the package. | |
63 */ | |
213 | 64 this(Device device) { |
65 super(device); | |
15 | 66 } |
67 | |
23 | 68 /** |
15 | 69 * Constructs a new font given a device and font data |
70 * which describes the desired font's appearance. | |
71 * <p> | |
23 | 72 * You must dispose the font when it is no longer required. |
15 | 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) | |
23 | 77 * |
15 | 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) { | |
213 | 87 super(device); |
88 init_(fd); | |
89 init_(); | |
15 | 90 } |
91 | |
23 | 92 /** |
15 | 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> | |
23 | 97 * You must dispose the font when it is no longer required. |
15 | 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) | |
23 | 102 * |
15 | 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> | |
23 | 112 * |
15 | 113 * @since 2.1 |
114 */ | |
115 public this(Device device, FontData[] fds) { | |
213 | 116 super(device); |
15 | 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 } | |
213 | 122 init_(fds[0]); |
123 init_(); | |
15 | 124 } |
125 | |
23 | 126 /** |
15 | 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> | |
23 | 131 * You must dispose the font when it is no longer required. |
15 | 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 | |
23 | 138 * |
15 | 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 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
148 public this(Device device, String name, int height, int style) { |
213 | 149 super(device); |
15 | 150 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
213 | 151 init_(new FontData (name, height, style)); |
152 init_(); | |
15 | 153 } |
154 | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
155 /*public*/ this(Device device, String name, float height, int style) { |
213 | 156 super(device); |
15 | 157 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
213 | 158 init_(new FontData (name, height, style)); |
159 init_(); | |
15 | 160 } |
213 | 161 void destroy() { |
15 | 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. | |
23 | 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 | |
15 | 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); | |
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
|
198 LOGFONT* logFont = new LOGFONT(); |
15 | 199 OS.GetObject(handle, LOGFONT.sizeof, logFont); |
200 return [ cast(FontData) FontData.win32_new(logFont, device.computePoints(logFont, handle))]; | |
201 } | |
202 | |
203 /** | |
23 | 204 * Returns an integer hash code for the receiver. Any two |
205 * objects that return <code>true</code> when passed to | |
15 | 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 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
213 override public hash_t toHash () { |
15 | 214 return cast(hash_t)handle; |
215 } | |
216 | |
213 | 217 void init_ (FontData fd) { |
15 | 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 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
237 override public bool isDisposed() { |
15 | 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 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
247 override public String toString () { |
15 | 248 if (isDisposed()) return "Font {*DISPOSED*}"; |
249 return Format( "Font {{{}}", handle ); | |
250 } | |
251 | |
23 | 252 /** |
15 | 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) { | |
213 | 267 Font font = new Font(device); |
15 | 268 font.handle = handle; |
269 return font; | |
270 } | |
271 | |
272 } |