Mercurial > projects > dwt-mac
comparison dwt/graphics/FontData.d @ 30:93b13b15f0b1
Ported a couple of modules in dwt.graphics
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Thu, 11 Sep 2008 23:16:53 +0200 |
parents | a9ab4c738ed8 |
children | 7d135fe0caf2 |
comparison
equal
deleted
inserted
replaced
29:d408fc12b991 | 30:93b13b15f0b1 |
---|---|
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * | |
11 * Port to the D programming language: | |
12 * Jacob Carlborg <jacob.carlborg@gmail.com> | |
10 *******************************************************************************/ | 13 *******************************************************************************/ |
11 module dwt.graphics.FontData; | 14 module dwt.graphics.FontData; |
12 | 15 |
16 | |
17 import dwt.DWT; | |
18 | |
13 import dwt.dwthelper.utils; | 19 import dwt.dwthelper.utils; |
14 | |
15 | |
16 import dwt.DWT; | |
17 | 20 |
18 /** | 21 /** |
19 * Instances of this class describe operating system fonts. | 22 * Instances of this class describe operating system fonts. |
20 * <p> | 23 * <p> |
21 * For platform-independent behaviour, use the get and set methods | 24 * For platform-independent behaviour, use the get and set methods |
108 * Note that the representation varies between platforms, | 111 * Note that the representation varies between platforms, |
109 * and a FontData can only be created from a string that was | 112 * and a FontData can only be created from a string that was |
110 * generated on the same platform. | 113 * generated on the same platform. |
111 * </p> | 114 * </p> |
112 * | 115 * |
113 * @param string the string representation of a <code>FontData</code> (must not be null) | 116 * @param str the string representation of a <code>FontData</code> (must not be null) |
114 * | 117 * |
115 * @exception IllegalArgumentException <ul> | 118 * @exception IllegalArgumentException <ul> |
116 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> | 119 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> |
117 * <li>ERROR_INVALID_ARGUMENT - if the argument does not represent a valid description</li> | 120 * <li>ERROR_INVALID_ARGUMENT - if the argument does not represent a valid description</li> |
118 * </ul> | 121 * </ul> |
119 * | 122 * |
120 * @see #toString | 123 * @see #toString |
121 */ | 124 */ |
122 public this(String string) { | 125 public this(String str) { |
123 if (string is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | 126 //if (str is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
124 int start = 0; | 127 int start = 0; |
125 int end = string.indexOf('|'); | 128 int end = str.indexOf('|'); |
126 if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 129 if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
127 String version1 = string.substring(start, end); | 130 String version1 = str.substring(start, end); |
128 try { | 131 try { |
129 if (Integer.parseInt(version1) !is 1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 132 if (Integer.parseInt(version1) !is 1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
130 } catch (NumberFormatException e) { | 133 } catch (NumberFormatException e) { |
131 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 134 DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
132 } | 135 } |
133 | 136 |
134 start = end + 1; | 137 start = end + 1; |
135 end = string.indexOf('|', start); | 138 end = str.indexOf('|', start); |
136 if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 139 if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
137 String name = string.substring(start, end); | 140 String name = str.substring(start, end); |
138 | 141 |
139 start = end + 1; | 142 start = end + 1; |
140 end = string.indexOf('|', start); | 143 end = str.indexOf('|', start); |
141 if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 144 if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
142 float height = 0; | 145 float height = 0; |
143 try { | 146 try { |
144 height = Float.parseFloat(string.substring(start, end)); | 147 height = Float.parseFloat(str.substring(start, end)); |
145 } catch (NumberFormatException e) { | 148 } catch (NumberFormatException e) { |
146 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 149 DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
147 } | 150 } |
148 | 151 |
149 start = end + 1; | 152 start = end + 1; |
150 end = string.indexOf('|', start); | 153 end = str.indexOf('|', start); |
151 if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 154 if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
152 int style = 0; | 155 int style = 0; |
153 try { | 156 try { |
154 style = Integer.parseInt(string.substring(start, end)); | 157 style = Integer.parseInt(str.substring(start, end)); |
155 } catch (NumberFormatException e) { | 158 } catch (NumberFormatException e) { |
156 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 159 DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
157 } | 160 } |
158 | 161 |
159 start = end + 1; | 162 start = end + 1; |
160 end = string.indexOf('|', start); | 163 end = str.indexOf('|', start); |
161 setName(name); | 164 setName(name); |
162 setHeight(height); | 165 setHeight(height); |
163 setStyle(style); | 166 setStyle(style); |
164 if (end is -1) return; | 167 if (end is -1) return; |
165 String platform = string.substring(start, end); | 168 String platform = str.substring(start, end); |
166 | 169 |
167 start = end + 1; | 170 start = end + 1; |
168 end = string.indexOf('|', start); | 171 end = str.indexOf('|', start); |
169 if (end is -1) return; | 172 if (end is -1) return; |
170 String version2 = string.substring(start, end); | 173 String version2 = str.substring(start, end); |
171 | 174 |
172 if (platform.equals("COCOA") && version2.equals("1")) { | 175 if (platform.equals("COCOA") && version2.equals("1")) { |
173 start = end + 1; | 176 start = end + 1; |
174 end = string.length(); | 177 end = str.length(); |
175 if (start < end) nsName = string.substring(start, end); | 178 if (start < end) nsName = str.substring(start, end); |
176 } | 179 } |
177 } | 180 } |
178 | 181 |
179 /** | 182 /** |
180 * Constructs a new font data given a font name, | 183 * Constructs a new font data given a font name, |
194 setName(name); | 197 setName(name); |
195 setHeight(height); | 198 setHeight(height); |
196 setStyle(style); | 199 setStyle(style); |
197 } | 200 } |
198 | 201 |
199 /*public*/ FontData(String name, float height, int style) { | 202 /*public*/ this(String name, float height, int style) { |
200 setName(name); | 203 setName(name); |
201 setHeight(height); | 204 setHeight(height); |
202 setStyle(style); | 205 setStyle(style); |
203 } | 206 } |
204 | 207 |
210 * @param object the object to compare with this object | 213 * @param object the object to compare with this object |
211 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | 214 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise |
212 * | 215 * |
213 * @see #hashCode | 216 * @see #hashCode |
214 */ | 217 */ |
215 public bool equals (Object object) { | 218 public bool opEquals (Object object) { |
216 if (object is this) return true; | 219 if (object is this) return true; |
217 if (!( null !is cast(FontData)object )) return false; | 220 if (!( null !is cast(FontData)object )) return false; |
218 FontData data = cast(FontData)object; | 221 FontData data = cast(FontData)object; |
219 return name.equals(data.name) && height is data.height && style is data.style; | 222 return name.equals(data.name) && height is data.height && style is data.style; |
220 } | 223 } |
224 | |
225 alias opEquals equals; | |
221 | 226 |
222 /** | 227 /** |
223 * Returns the height of the receiver in points. | 228 * Returns the height of the receiver in points. |
224 * | 229 * |
225 * @return the height of this FontData | 230 * @return the height of this FontData |
310 * | 315 * |
311 * @return the receiver's hash | 316 * @return the receiver's hash |
312 * | 317 * |
313 * @see #equals | 318 * @see #equals |
314 */ | 319 */ |
315 public int hashCode () { | 320 public hast_t toHash () { |
316 return name.hashCode() ^ getHeight() ^ style; | 321 return name.hashCode() ^ getHeight() ^ style; |
317 } | 322 } |
323 | |
324 alias toHash hashCode; | |
318 | 325 |
319 /** | 326 /** |
320 * Sets the height of the receiver. The parameter is | 327 * Sets the height of the receiver. The parameter is |
321 * specified in terms of points, where a point is one | 328 * specified in terms of points, where a point is one |
322 * seventy-second of an inch. | 329 * seventy-second of an inch. |
400 * </ul> | 407 * </ul> |
401 * | 408 * |
402 * @see #getName | 409 * @see #getName |
403 */ | 410 */ |
404 public void setName(String name) { | 411 public void setName(String name) { |
405 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | 412 //if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
406 this.name = name; | 413 this.name = name; |
407 nsName = null; | 414 nsName = null; |
408 } | 415 } |
409 | 416 |
410 /** | 417 /** |