comparison dwt/graphics/Font.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents 642f460a0908
children cfa563df4fdd
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
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 * 10 *
11 * Port to the D programming language: 11 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com> 12 * Jacob Carlborg <doob@me.com>
13 *******************************************************************************/ 13 *******************************************************************************/
14 module dwt.graphics.Font; 14 module dwt.graphics.Font;
15 15
16 16
17 import dwt.DWT; 17 import dwt.DWT;
18 import dwt.DWTError; 18 import dwt.DWTError;
19 import dwt.DWTException; 19 import dwt.DWTException;
20 import dwt.internal.cocoa.NSAutoreleasePool;
20 import dwt.internal.cocoa.NSFont; 21 import dwt.internal.cocoa.NSFont;
21 import dwt.internal.cocoa.NSString; 22 import dwt.internal.cocoa.NSString;
23 import dwt.internal.cocoa.NSThread;
22 24
23 import tango.stdc.stringz; 25 import tango.stdc.stringz;
24 import tango.text.convert.Format; 26 import tango.text.convert.Format;
25 static import tango.text.convert.Utf; 27 static import tango.text.convert.Utf;
26 28
40 * method to release the operating system resources managed by each instance 42 * method to release the operating system resources managed by each instance
41 * when those instances are no longer required. 43 * when those instances are no longer required.
42 * </p> 44 * </p>
43 * 45 *
44 * @see FontData 46 * @see FontData
47 * @see <a href="http://www.eclipse.org/swt/snippets/#font">Font snippets</a>
48 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Examples: GraphicsExample, PaintExample</a>
49 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
45 */ 50 */
46 public final class Font : Resource { 51 public final class Font : Resource {
47 52
48 alias Resource.init_ init_; 53 alias Resource.init_ init_;
49 54
82 * </ul> 87 * </ul>
83 */ 88 */
84 public this(Device device, FontData fd) { 89 public this(Device device, FontData fd) {
85 super(device); 90 super(device);
86 if (fd is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 91 if (fd is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
87 init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName); 92 NSAutoreleasePool pool = null;
88 init_(); 93 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
94 try {
95 init(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName);
96 init();
97 } finally {
98 if (pool !is null) pool.release();
99 }
89 } 100 }
90 101
91 /** 102 /**
92 * Constructs a new font given a device and an array 103 * Constructs a new font given a device and an array
93 * of font data which describes the desired font's 104 * of font data which describes the desired font's
116 if (fds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 127 if (fds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
117 if (fds.length is 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 128 if (fds.length is 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
118 for (int i=0; i<fds.length; i++) { 129 for (int i=0; i<fds.length; i++) {
119 if (fds[i] is null) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 130 if (fds[i] is null) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
120 } 131 }
121 FontData fd = fds[0]; 132 NSAutoreleasePool pool = null;
133 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
134 try {
135 FontData fd = fds[0];
122 init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName); 136 init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName);
123 init_(); 137 init_();
138 } finally {
139 if (pool !is null) pool.release();
140 }
124 } 141 }
125 142
126 /** 143 /**
127 * Constructs a new font given a device, a font name, 144 * Constructs a new font given a device, a font name,
128 * the height of the desired font in points, and a font 145 * the height of the desired font in points, and a font
145 * <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li> 162 * <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li>
146 * </ul> 163 * </ul>
147 */ 164 */
148 public this(Device device, String name, int height, int style) { 165 public this(Device device, String name, int height, int style) {
149 super(device); 166 super(device);
150 init_(name, height, style, null); 167 NSAutoreleasePool pool = null;
151 init_(); 168 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
169 try {
170 init_(name, height, style, null);
171 init_();
172 } finally {
173 if (pool !is null) pool.release();
174 }
152 } 175 }
153 176
154 /+/*public*/ this(Device device, String name, float height, int style) { 177 /+/*public*/ this(Device device, String name, float height, int style) {
155 super(device); 178 super(device);
156 init_(name, height, style, null); 179 init_(name, height, style, null);
193 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> 216 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
194 * </ul> 217 * </ul>
195 */ 218 */
196 public FontData[] getFontData() { 219 public FontData[] getFontData() {
197 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 220 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
198 NSString family = handle.familyName(); 221 NSAutoreleasePool pool = null;
199 wchar[] buffer1 = new wchar[family.length()]; 222 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
200 family.getCharacters_(buffer1.ptr); 223 try {
201 String name = tango.text.convert.Utf.toString(buffer1).dup; 224 NSString family = handle.familyName();
202 NSString str = handle.fontName(); 225 String name = family.getString();
203 wchar[] buffer = new wchar[str.length()]; 226 NSString str = handle.fontName();
204 str.getCharacters_(buffer.ptr); 227 String nsName = str.getString();
205 String nsName = tango.text.convert.Utf.toString(buffer).dup; 228 int style = DWT.NORMAL;
206 int style = DWT.NORMAL; 229 if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC;
207 if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC; 230 if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD;
208 if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD; 231 Point dpi = device.dpi, screenDPI = device.getScreenDPI();
209 FontData data = new FontData(name, handle.pointSize(), style); 232 FontData data = new FontData(name, (float)/*64*/handle.pointSize() * screenDPI.y / dpi.y, style);
210 data.nsName = nsName; 233 data.nsName = nsName;
211 return [data]; 234 return [data];
235 } finally {
236 if (pool !is null) pool.release();
237 }
212 } 238 }
213 239
214 /** 240 /**
215 * Invokes platform specific functionality to allocate a new font. 241 * Invokes platform specific functionality to allocate a new font.
216 * <p> 242 * <p>
229 * @private 255 * @private
230 */ 256 */
231 public static Font cocoa_new(Device device, NSFont handle) { 257 public static Font cocoa_new(Device device, NSFont handle) {
232 Font font = new Font(device); 258 Font font = new Font(device);
233 font.handle = handle; 259 font.handle = handle;
260 font.handle.retain();
234 return font; 261 return font;
235 } 262 }
236 263
237 /** 264 /**
238 * Returns an integer hash code for the receiver. Any two 265 * Returns an integer hash code for the receiver. Any two
251 alias toHash hashCode; 278 alias toHash hashCode;
252 279
253 void init_(String name, float height, int style, String nsName) { 280 void init_(String name, float height, int style, String nsName) {
254 //if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 281 //if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
255 if (height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 282 if (height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
283 Point dpi = device.dpi, screenDPI = device.getScreenDPI();
284 float size = height * dpi.y / screenDPI.y;
256 if (nsName !is null) { 285 if (nsName !is null) {
257 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height); 286 handle = NSFont.fontWithName(NSString.stringWith(nsName), size);
258 } else { 287 } else {
259 nsName = name; 288 nsName = name;
260 if ((style & DWT.BOLD) !is 0) nsName ~= " Bold"; 289 if ((style & DWT.BOLD) !is 0) nsName ~= " Bold";
261 if ((style & DWT.ITALIC) !is 0) nsName ~= " Italic"; 290 if ((style & DWT.ITALIC) !is 0) nsName ~= " Italic";
262 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height); 291 handle = NSFont.fontWithName(NSString.stringWith(nsName), size);
263 if (handle is null && (style & DWT.ITALIC) !is 0) { 292 if (handle is null && (style & DWT.ITALIC) !is 0) {
264 nsName = name; 293 nsName = name;
265 if ((style & DWT.BOLD) !is 0) nsName ~= " Bold"; 294 if ((style & DWT.BOLD) !is 0) nsName ~= " Bold";
266 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height); 295 handle = NSFont.fontWithName(NSString.stringWith(nsName), size);
267 } 296 }
268 if (handle is null && (style & DWT.BOLD) !is 0) { 297 if (handle is null && (style & DWT.BOLD) !is 0) {
269 nsName = name; 298 nsName = name;
270 handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height); 299 handle = NSFont.fontWithName(NSString.stringWith(nsName), size);
271 } 300 }
272 } 301 }
273 if (handle is null) { 302 if (handle is null) {
274 handle = device.systemFont.handle; 303 handle = device.systemFont.handle;
275 } 304 }