Mercurial > projects > dwt-win
annotate dwt/graphics/Font.d @ 213:36f5cb12e1a2
Update to SWT 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 17 May 2008 17:34:28 +0200 |
parents | ab60f3309436 |
children | fd9c62a2998e |
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 |
213 | 45 alias Resource.init_ init_; |
15 | 46 /** |
47 * the handle to the OS font resource | |
48 * (Warning: This field is platform dependent) | |
49 * <p> | |
50 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT | |
51 * public API. It is marked public only so that it can be shared | |
52 * within the packages provided by DWT. It is not available on all | |
53 * platforms and should never be accessed from application code. | |
54 * </p> | |
55 */ | |
56 public HFONT handle; | |
23 | 57 |
15 | 58 /** |
59 * Prevents uninitialized instances from being created outside the package. | |
60 */ | |
213 | 61 this(Device device) { |
62 super(device); | |
15 | 63 } |
64 | |
23 | 65 /** |
15 | 66 * Constructs a new font given a device and font data |
67 * which describes the desired font's appearance. | |
68 * <p> | |
23 | 69 * You must dispose the font when it is no longer required. |
15 | 70 * </p> |
71 * | |
72 * @param device the device to create the font on | |
73 * @param fd the FontData that describes the desired font (must not be null) | |
23 | 74 * |
15 | 75 * @exception IllegalArgumentException <ul> |
76 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
77 * <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li> | |
78 * </ul> | |
79 * @exception DWTError <ul> | |
80 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> | |
81 * </ul> | |
82 */ | |
83 public this(Device device, FontData fd) { | |
213 | 84 super(device); |
85 init_(fd); | |
86 init_(); | |
15 | 87 } |
88 | |
23 | 89 /** |
15 | 90 * Constructs a new font given a device and an array |
91 * of font data which describes the desired font's | |
92 * appearance. | |
93 * <p> | |
23 | 94 * You must dispose the font when it is no longer required. |
15 | 95 * </p> |
96 * | |
97 * @param device the device to create the font on | |
98 * @param fds the array of FontData that describes the desired font (must not be null) | |
23 | 99 * |
15 | 100 * @exception IllegalArgumentException <ul> |
101 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
102 * <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li> | |
103 * <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li> | |
104 * <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li> | |
105 * </ul> | |
106 * @exception DWTError <ul> | |
107 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> | |
108 * </ul> | |
23 | 109 * |
15 | 110 * @since 2.1 |
111 */ | |
112 public this(Device device, FontData[] fds) { | |
213 | 113 super(device); |
15 | 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 } | |
213 | 119 init_(fds[0]); |
120 init_(); | |
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) { |
213 | 146 super(device); |
15 | 147 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
213 | 148 init_(new FontData (name, height, style)); |
149 init_(); | |
15 | 150 } |
151 | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
152 /*public*/ this(Device device, String name, float height, int style) { |
213 | 153 super(device); |
15 | 154 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
213 | 155 init_(new FontData (name, height, style)); |
156 init_(); | |
15 | 157 } |
213 | 158 void destroy() { |
15 | 159 OS.DeleteObject(handle); |
160 handle = 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 int opEquals(Object object) { | |
174 if (object is this) return true; | |
175 if ( auto font = cast(Font)object ){ | |
176 return device is font.device && handle is font.handle; | |
177 } | |
178 return false; | |
179 } | |
180 | |
181 /** | |
182 * Returns an array of <code>FontData</code>s representing the receiver. | |
23 | 183 * On Windows, only one FontData will be returned per font. On X however, |
184 * a <code>Font</code> object <em>may</em> be composed of multiple X | |
15 | 185 * fonts. To support this case, we return an array of font data objects. |
186 * | |
187 * @return an array of font data objects describing the receiver | |
188 * | |
189 * @exception DWTException <ul> | |
190 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
191 * </ul> | |
192 */ | |
193 public FontData[] getFontData() { | |
194 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
|
195 LOGFONT* logFont = new LOGFONT(); |
15 | 196 OS.GetObject(handle, LOGFONT.sizeof, logFont); |
197 return [ cast(FontData) FontData.win32_new(logFont, device.computePoints(logFont, handle))]; | |
198 } | |
199 | |
200 /** | |
23 | 201 * Returns an integer hash code for the receiver. Any two |
202 * objects that return <code>true</code> when passed to | |
15 | 203 * <code>equals</code> must return the same value for this |
204 * method. | |
205 * | |
206 * @return the receiver's hash | |
207 * | |
208 * @see #equals | |
209 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
210 override public hash_t toHash () { |
15 | 211 return cast(hash_t)handle; |
212 } | |
213 | |
213 | 214 void init_ (FontData fd) { |
15 | 215 if (fd is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
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 is null) DWT.error(DWT.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 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
234 override public bool isDisposed() { |
15 | 235 return handle is null; |
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 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
203
diff
changeset
|
244 override public String toString () { |
15 | 245 if (isDisposed()) return "Font {*DISPOSED*}"; |
246 return Format( "Font {{{}}", handle ); | |
247 } | |
248 | |
23 | 249 /** |
15 | 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 DWT. 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, HFONT handle) { | |
213 | 264 Font font = new Font(device); |
15 | 265 font.handle = handle; |
266 return font; | |
267 } | |
268 | |
269 } |