comparison dwt/graphics/Font.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 db5a898b2119
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.Font; 14 module dwt.graphics.Font;
12
13 import dwt.dwthelper.utils;
14 15
15 16
16 import dwt.DWT; 17 import dwt.DWT;
17 import dwt.DWTError; 18 import dwt.DWTError;
18 import dwt.DWTException; 19 import dwt.DWTException;
19 import dwt.internal.cocoa.NSFont; 20 import dwt.internal.cocoa.NSFont;
20 import dwt.internal.cocoa.NSString; 21 import dwt.internal.cocoa.NSString;
22
23 import tango.stdc.stringz;
24 import tango.text.convert.Utf;
25
26 import dwt.dwthelper.utils;
27 import dwt.graphics.Device;
28 import dwt.graphics.FontData;
29 import dwt.graphics.Resource;
30 import dwt.internal.cocoa.CGFloat;
21 31
22 /** 32 /**
23 * Instances of this class manage operating system resources that 33 * Instances of this class manage operating system resources that
24 * define how text looks when it is displayed. Fonts may be constructed 34 * define how text looks when it is displayed. Fonts may be constructed
25 * by providing a device and either name, size and style information 35 * by providing a device and either name, size and style information
136 super(device); 146 super(device);
137 init(name, height, style, null); 147 init(name, height, style, null);
138 init(); 148 init();
139 } 149 }
140 150
141 /*public*/ Font(Device device, String name, float height, int style) { 151 /*public*/ this(Device device, String name, float height, int style) {
142 super(device); 152 super(device);
143 init(name, height, style, null); 153 init(name, height, style, null);
144 init(); 154 init();
145 } 155 }
146 156
157 * @param object the object to compare with this object 167 * @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 168 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
159 * 169 *
160 * @see #hashCode 170 * @see #hashCode
161 */ 171 */
162 public bool equals(Object object) { 172 public bool opEquals(Object object) {
163 if (object is this) return true; 173 if (object is this) return true;
164 if (!( null !is cast(Font)object )) return false; 174 if (!( null !is cast(Font)object )) return false;
165 Font font = cast(Font)object; 175 Font font = cast(Font)object;
166 return handle is font.handle; 176 return handle is font.handle;
167 } 177 }
168 178
179 alias opEquals equals;
180
169 /** 181 /**
170 * Returns an array of <code>FontData</code>s representing the receiver. 182 * 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, 183 * 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 184 * 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. 185 * fonts. To support this case, we return an array of font data objects.
179 * </ul> 191 * </ul>
180 */ 192 */
181 public FontData[] getFontData() { 193 public FontData[] getFontData() {
182 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 194 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
183 NSString family = handle.familyName(); 195 NSString family = handle.familyName();
184 char[] buffer1 = new char[family.length()]; 196 wchar[] buffer1 = new wchar[family.length()];
185 family.getCharacters_(buffer1); 197 family.getCharacters_(buffer1);
186 String name = new String(buffer1); 198 String name = buffer1.toString().dup;
187 NSString str = handle.fontName(); 199 NSString str = handle.fontName();
188 char[] buffer = new char[str.length()]; 200 wchar[] buffer = new wchar[str.length()];
189 str.getCharacters_(buffer); 201 str.getCharacters_(buffer);
190 String nsName = new String(buffer); 202 String nsName = buffer.toString().dup;
191 int style = DWT.NORMAL; 203 int style = DWT.NORMAL;
192 if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC; 204 if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC;
193 if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD; 205 if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD;
194 FontData data = new FontData(name, handle.pointSize(), style); 206 FontData data = new FontData(name, handle.pointSize(), style);
195 data.nsName = nsName; 207 data.nsName = nsName;
196 return new FontData[]{data}; 208 return new FontData[][data];
197 } 209 }
198 210
199 /** 211 /**
200 * Invokes platform specific functionality to allocate a new font. 212 * Invokes platform specific functionality to allocate a new font.
201 * <p> 213 * <p>
227 * 239 *
228 * @return the receiver's hash 240 * @return the receiver's hash
229 * 241 *
230 * @see #equals 242 * @see #equals
231 */ 243 */
232 public int hashCode() { 244 public hash_t toHash() {
233 return handle !is null ? handle.id : 0; 245 return handle !is null ? handle.id : 0;
234 } 246 }
235 247
248 alias toHash hashCode;
249
236 void init(String name, float height, int style, String nsName) { 250 void init(String name, float height, int style, String nsName) {
237 if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 251 //if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
238 if (height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 252 if (height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
239 if (nsName !is null) { 253 if (nsName !is null) {
240 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), height); 254 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height);
241 } else { 255 } else {
242 nsName = name; 256 nsName = name;
243 if ((style & DWT.BOLD) !is 0) nsName += " Bold"; 257 if ((style & DWT.BOLD) !is 0) nsName += " Bold";
244 if ((style & DWT.ITALIC) !is 0) nsName += " Italic"; 258 if ((style & DWT.ITALIC) !is 0) nsName += " Italic";
245 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), height); 259 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height);
246 if (handle is null && (style & DWT.ITALIC) !is 0) { 260 if (handle is null && (style & DWT.ITALIC) !is 0) {
247 nsName = name; 261 nsName = name;
248 if ((style & DWT.BOLD) !is 0) nsName += " Bold"; 262 if ((style & DWT.BOLD) !is 0) nsName += " Bold";
249 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), height); 263 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height);
250 } 264 }
251 if (handle is null && (style & DWT.BOLD) !is 0) { 265 if (handle is null && (style & DWT.BOLD) !is 0) {
252 nsName = name; 266 nsName = name;
253 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), height); 267 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height);
254 } 268 }
255 } 269 }
256 if (handle is null) { 270 if (handle is null) {
257 handle = device.systemFont.handle; 271 handle = device.systemFont.handle;
258 } 272 }
279 * 293 *
280 * @return a string representation of the receiver 294 * @return a string representation of the receiver
281 */ 295 */
282 public String toString () { 296 public String toString () {
283 if (isDisposed()) return "Font {*DISPOSED*}"; 297 if (isDisposed()) return "Font {*DISPOSED*}";
284 return "Font {" + handle + "}"; 298 return "Font {" ~ handle ~ "}";
285 } 299 }
286 300
287 } 301 }