Mercurial > projects > dwt-mac
comparison dwt/graphics/Font.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 649b8e223d5a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 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 *******************************************************************************/ | |
11 module dwt.graphics.Font; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTError; | |
18 import dwt.DWTException; | |
19 import dwt.internal.cocoa.NSFont; | |
20 import dwt.internal.cocoa.NSString; | |
21 | |
22 /** | |
23 * Instances of this class manage operating system resources that | |
24 * define how text looks when it is displayed. Fonts may be constructed | |
25 * by providing a device and either name, size and style information | |
26 * or a <code>FontData</code> object which encapsulates this data. | |
27 * <p> | |
28 * Application code must explicitly invoke the <code>Font.dispose()</code> | |
29 * method to release the operating system resources managed by each instance | |
30 * when those instances are no longer required. | |
31 * </p> | |
32 * | |
33 * @see FontData | |
34 */ | |
35 public final class Font extends Resource { | |
36 | |
37 /** | |
38 * the handle to the OS font resource | |
39 * (Warning: This field is platform dependent) | |
40 * <p> | |
41 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT | |
42 * public API. It is marked public only so that it can be shared | |
43 * within the packages provided by DWT. It is not available on all | |
44 * platforms and should never be accessed from application code. | |
45 * </p> | |
46 */ | |
47 public NSFont handle; | |
48 | |
49 Font(Device device) { | |
50 super(device); | |
51 } | |
52 | |
53 /** | |
54 * Constructs a new font given a device and font data | |
55 * which describes the desired font's appearance. | |
56 * <p> | |
57 * You must dispose the font when it is no longer required. | |
58 * </p> | |
59 * | |
60 * @param device the device to create the font on | |
61 * @param fd the FontData that describes the desired font (must not be null) | |
62 * | |
63 * @exception IllegalArgumentException <ul> | |
64 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
65 * <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li> | |
66 * </ul> | |
67 * @exception DWTError <ul> | |
68 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> | |
69 * </ul> | |
70 */ | |
71 public Font(Device device, FontData fd) { | |
72 super(device); | |
73 if (fd is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
74 init(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName); | |
75 init(); | |
76 } | |
77 | |
78 /** | |
79 * Constructs a new font given a device and an array | |
80 * of font data which describes the desired font's | |
81 * appearance. | |
82 * <p> | |
83 * You must dispose the font when it is no longer required. | |
84 * </p> | |
85 * | |
86 * @param device the device to create the font on | |
87 * @param fds the array of FontData that describes the desired font (must not be null) | |
88 * | |
89 * @exception IllegalArgumentException <ul> | |
90 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
91 * <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li> | |
92 * <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li> | |
93 * <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li> | |
94 * </ul> | |
95 * @exception DWTError <ul> | |
96 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> | |
97 * </ul> | |
98 * | |
99 * @since 2.1 | |
100 */ | |
101 public Font(Device device, FontData[] fds) { | |
102 super(device); | |
103 if (fds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
104 if (fds.length is 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
105 for (int i=0; i<fds.length; i++) { | |
106 if (fds[i] is null) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
107 } | |
108 FontData fd = fds[0]; | |
109 init(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName); | |
110 init(); | |
111 } | |
112 | |
113 /** | |
114 * Constructs a new font given a device, a font name, | |
115 * the height of the desired font in points, and a font | |
116 * style. | |
117 * <p> | |
118 * You must dispose the font when it is no longer required. | |
119 * </p> | |
120 * | |
121 * @param device the device to create the font on | |
122 * @param name the name of the font (must not be null) | |
123 * @param height the font height in points | |
124 * @param style a bit or combination of NORMAL, BOLD, ITALIC | |
125 * | |
126 * @exception IllegalArgumentException <ul> | |
127 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
128 * <li>ERROR_NULL_ARGUMENT - if the name argument is null</li> | |
129 * <li>ERROR_INVALID_ARGUMENT - if the height is negative</li> | |
130 * </ul> | |
131 * @exception DWTError <ul> | |
132 * <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li> | |
133 * </ul> | |
134 */ | |
135 public Font(Device device, String name, int height, int style) { | |
136 super(device); | |
137 init(name, height, style, null); | |
138 init(); | |
139 } | |
140 | |
141 /*public*/ Font(Device device, String name, float height, int style) { | |
142 super(device); | |
143 init(name, height, style, null); | |
144 init(); | |
145 } | |
146 | |
147 void destroy() { | |
148 handle.release(); | |
149 handle = null; | |
150 } | |
151 | |
152 /** | |
153 * Compares the argument to the receiver, and returns true | |
154 * if they represent the <em>same</em> object using a class | |
155 * specific comparison. | |
156 * | |
157 * @param object the object to compare with this object | |
158 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | |
159 * | |
160 * @see #hashCode | |
161 */ | |
162 public bool equals(Object object) { | |
163 if (object is this) return true; | |
164 if (!(object instanceof Font)) return false; | |
165 Font font = (Font)object; | |
166 return handle is font.handle; | |
167 } | |
168 | |
169 /** | |
170 * Returns an array of <code>FontData</code>s representing the receiver. | |
171 * On Windows, only one FontData will be returned per font. On X however, | |
172 * a <code>Font</code> object <em>may</em> be composed of multiple X | |
173 * fonts. To support this case, we return an array of font data objects. | |
174 * | |
175 * @return an array of font data objects describing the receiver | |
176 * | |
177 * @exception DWTException <ul> | |
178 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
179 * </ul> | |
180 */ | |
181 public FontData[] getFontData() { | |
182 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
183 NSString family = handle.familyName(); | |
184 char[] buffer1 = new char[family.length()]; | |
185 family.getCharacters_(buffer1); | |
186 String name = new String(buffer1); | |
187 NSString str = handle.fontName(); | |
188 char[] buffer = new char[str.length()]; | |
189 str.getCharacters_(buffer); | |
190 String nsName = new String(buffer); | |
191 int style = DWT.NORMAL; | |
192 if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC; | |
193 if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD; | |
194 FontData data = new FontData(name, handle.pointSize(), style); | |
195 data.nsName = nsName; | |
196 return new FontData[]{data}; | |
197 } | |
198 | |
199 /** | |
200 * Invokes platform specific functionality to allocate a new font. | |
201 * <p> | |
202 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public | |
203 * API for <code>Font</code>. It is marked public only so that it | |
204 * can be shared within the packages provided by DWT. It is not | |
205 * available on all platforms, and should never be called from | |
206 * application code. | |
207 * </p> | |
208 * | |
209 * @param device the device on which to allocate the color | |
210 * @param handle the handle for the font | |
211 * @param style the style for the font | |
212 * @param size the size for the font | |
213 * | |
214 * @private | |
215 */ | |
216 public static Font cocoa_new(Device device, NSFont handle) { | |
217 Font font = new Font(device); | |
218 font.handle = handle; | |
219 return font; | |
220 } | |
221 | |
222 /** | |
223 * Returns an integer hash code for the receiver. Any two | |
224 * objects that return <code>true</code> when passed to | |
225 * <code>equals</code> must return the same value for this | |
226 * method. | |
227 * | |
228 * @return the receiver's hash | |
229 * | |
230 * @see #equals | |
231 */ | |
232 public int hashCode() { | |
233 return handle !is null ? handle.id : 0; | |
234 } | |
235 | |
236 void init(String name, float height, int style, String nsName) { | |
237 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
238 if (height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
239 if (nsName !is null) { | |
240 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), height); | |
241 } else { | |
242 nsName = name; | |
243 if ((style & DWT.BOLD) !is 0) nsName += " Bold"; | |
244 if ((style & DWT.ITALIC) !is 0) nsName += " Italic"; | |
245 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), height); | |
246 if (handle is null && (style & DWT.ITALIC) !is 0) { | |
247 nsName = name; | |
248 if ((style & DWT.BOLD) !is 0) nsName += " Bold"; | |
249 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), height); | |
250 } | |
251 if (handle is null && (style & DWT.BOLD) !is 0) { | |
252 nsName = name; | |
253 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), height); | |
254 } | |
255 } | |
256 if (handle is null) { | |
257 handle = device.systemFont.handle; | |
258 } | |
259 handle.retain(); | |
260 } | |
261 | |
262 /** | |
263 * Returns <code>true</code> if the font has been disposed, | |
264 * and <code>false</code> otherwise. | |
265 * <p> | |
266 * This method gets the dispose state for the font. | |
267 * When a font has been disposed, it is an error to | |
268 * invoke any other method using the font. | |
269 * | |
270 * @return <code>true</code> when the font is disposed and <code>false</code> otherwise | |
271 */ | |
272 public bool isDisposed() { | |
273 return handle is null; | |
274 } | |
275 | |
276 /** | |
277 * Returns a string containing a concise, human-readable | |
278 * description of the receiver. | |
279 * | |
280 * @return a string representation of the receiver | |
281 */ | |
282 public String toString () { | |
283 if (isDisposed()) return "Font {*DISPOSED*}"; | |
284 return "Font {" + handle + "}"; | |
285 } | |
286 | |
287 } |