diff 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
line wrap: on
line diff
--- a/dwt/graphics/Font.d	Tue Oct 21 15:20:04 2008 +0200
+++ b/dwt/graphics/Font.d	Mon Dec 01 17:07:00 2008 +0100
@@ -1,5 +1,5 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,7 +9,7 @@
  *     IBM Corporation - initial API and implementation
  *     
  * Port to the D programming language:
- *     Jacob Carlborg <jacob.carlborg@gmail.com>
+ *     Jacob Carlborg <doob@me.com>
  *******************************************************************************/
 module dwt.graphics.Font;
 
@@ -17,8 +17,10 @@
 import dwt.DWT;
 import dwt.DWTError;
 import dwt.DWTException;
+import dwt.internal.cocoa.NSAutoreleasePool;
 import dwt.internal.cocoa.NSFont;
 import dwt.internal.cocoa.NSString;
+import dwt.internal.cocoa.NSThread;
 
 import tango.stdc.stringz;
 import tango.text.convert.Format;
@@ -42,6 +44,9 @@
  * </p>
  *
  * @see FontData
+ * @see <a href="http://www.eclipse.org/swt/snippets/#font">Font snippets</a>
+ * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Examples: GraphicsExample, PaintExample</a>
+ * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  */
 public final class Font : Resource {
 
@@ -84,8 +89,14 @@
 public this(Device device, FontData fd) {
     super(device);
     if (fd is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
-    init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName);
-    init_();
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        init(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName);
+        init();
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**  
@@ -118,9 +129,15 @@
     for (int i=0; i<fds.length; i++) {
         if (fds[i] is null) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
     }
-    FontData fd = fds[0];
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        FontData fd = fds[0];
     init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.nsName);
     init_();
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**  
@@ -147,8 +164,14 @@
  */
 public this(Device device, String name, int height, int style) {
     super(device);
-    init_(name, height, style, null);
-    init_();
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        init_(name, height, style, null);
+        init_();
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /+/*public*/ this(Device device, String name, float height, int style) {
@@ -195,20 +218,23 @@
  */
 public FontData[] getFontData() {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSString family = handle.familyName();
-    wchar[] buffer1 = new wchar[family.length()];
-    family.getCharacters_(buffer1.ptr);
-    String name = tango.text.convert.Utf.toString(buffer1).dup;
-    NSString str = handle.fontName();
-    wchar[] buffer = new wchar[str.length()];
-    str.getCharacters_(buffer.ptr);
-    String nsName = tango.text.convert.Utf.toString(buffer).dup;
-    int style = DWT.NORMAL;
-    if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC;
-    if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD;
-    FontData data = new FontData(name, handle.pointSize(), style);
-    data.nsName = nsName;
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        NSString family = handle.familyName();
+        String name = family.getString();
+        NSString str = handle.fontName();
+        String nsName = str.getString();
+        int style = DWT.NORMAL;
+        if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC;
+        if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD;
+        Point dpi = device.dpi, screenDPI = device.getScreenDPI();
+        FontData data = new FontData(name, (float)/*64*/handle.pointSize() * screenDPI.y / dpi.y, style);
+        data.nsName = nsName;
     return [data];
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**  
@@ -231,6 +257,7 @@
 public static Font cocoa_new(Device device, NSFont handle) {
     Font font = new Font(device);
     font.handle = handle;
+    font.handle.retain();
     return font;
 }
 
@@ -253,21 +280,23 @@
 void init_(String name, float height, int style, String nsName) {
     //if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
     if (height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
+    Point dpi = device.dpi, screenDPI = device.getScreenDPI();
+    float size = height * dpi.y / screenDPI.y;
     if (nsName !is null) {
-        handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height);
+        handle = NSFont.fontWithName(NSString.stringWith(nsName), size);
     } else {
         nsName = name;
         if ((style & DWT.BOLD) !is 0) nsName ~= " Bold";
         if ((style & DWT.ITALIC) !is 0) nsName ~= " Italic";
-        handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height);
+        handle = NSFont.fontWithName(NSString.stringWith(nsName), size);
         if (handle is null && (style & DWT.ITALIC) !is 0) {
             nsName = name;
             if ((style & DWT.BOLD) !is 0) nsName ~= " Bold";
-            handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height);
+            handle = NSFont.fontWithName(NSString.stringWith(nsName), size);
         }
         if (handle is null && (style & DWT.BOLD) !is 0) {
             nsName = name;
-            handle = NSFont.static_fontWithName_size_(NSString.stringWith(nsName), cast(CGFloat) height);
+            handle = NSFont.fontWithName(NSString.stringWith(nsName), size);
         }
     }
     if (handle is null) {