diff mac/dw.m @ 1146:9d97610b2140

Adding dw_pixmap_set_font() which is equivalent to dw_window_set_font() except for pixmaps. When drawing to pixmaps, normally the font is obtained from the associated window handle. This will allow overriding of the font for this pixmap, and will allow setting fonts on pixmaps that don't have associated window handles. This commit has the Mac support. Other platforms coming in the next hour.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 11 Sep 2011 21:36:07 +0000
parents 5878dbfafbe2
children 091ed7c20b3f
line wrap: on
line diff
--- a/mac/dw.m	Sun Sep 11 20:52:44 2011 +0000
+++ b/mac/dw.m	Sun Sep 11 21:36:07 2011 +0000
@@ -4803,9 +4803,9 @@
     }
     if(pixmap)
     {
-        NSFont *font = nil;
+        NSFont *font = pixmap->font;
         DWRender *render = pixmap->handle;
-        if([render isMemberOfClass:[DWRender class]])
+        if(!font && [render isMemberOfClass:[DWRender class]])
         {
             font = [render font];
         }
@@ -6074,6 +6074,24 @@
     return retval;
 }
 
+/* Internal function to convert fontname to NSFont */
+NSFont *_dw_font_by_name(char *fontname)
+{
+    char *fontcopy = strdup(fontname);
+    char *name = strchr(fontcopy, '.');
+    NSFont *font = nil;
+    
+    if(name)
+    {
+        int size = atoi(fontcopy);
+        *name = 0;
+        name++;
+        font = [NSFont fontWithName:[ NSString stringWithUTF8String:name ] size:(float)size];
+    }
+    free(fontcopy);
+    return font;
+}
+
 /*
  * Create a bitmap object to be packed.
  * Parameters:
@@ -6276,6 +6294,35 @@
     return pixmap;
 }
 
+
+/*
+ * Sets the font used by a specified pixmap.
+ * Normally the pixmap font is obtained from the associated window handle.
+ * However this can be used to override that, or for pixmaps with no window.
+ * Parameters:
+ *          pixmap: Handle to a pixmap returned by dw_pixmap_new() or
+ *                  passed to the application via a callback.
+ *          fontname: Name and size of the font in the form "size.fontname"
+ */
+int API dw_pixmap_set_font(HPIXMAP pixmap, char *fontname)
+{
+    if(pixmap)
+    {
+        NSFont *font = _dw_font_by_name(fontname);
+        
+        if(font)
+        {
+            NSFont *oldfont = pixmap->font;
+            [font retain];
+            pixmap->font = font;
+            if(oldfont)
+                [oldfont release];
+            return DW_ERROR_NONE;
+        }
+    }
+    return DW_ERROR_GENERAL;
+}
+
 /*
  * Destroys an allocated pixmap.
  * Parameters:
@@ -6284,11 +6331,13 @@
  */
 void API dw_pixmap_destroy(HPIXMAP pixmap)
 {
-    if ( pixmap )
-    {
-       NSBitmapImageRep *image = (NSBitmapImageRep *)pixmap->image;
-       [image release];
-       free(pixmap);
+    if(pixmap)
+    {
+        NSBitmapImageRep *image = (NSBitmapImageRep *)pixmap->image;
+        NSFont *font = pixmap->font;
+        [image release];
+        [font release];
+        free(pixmap);
     }
 }
 
@@ -7266,23 +7315,6 @@
     }
 }
 
-NSFont *_dw_font_by_name(char *fontname)
-{
-    char *fontcopy = strdup(fontname);
-    char *name = strchr(fontcopy, '.');
-    NSFont *font = nil;
-
-    if(name)
-    {
-        int size = atoi(fontcopy);
-        *name = 0;
-        name++;
-        font = [NSFont fontWithName:[ NSString stringWithUTF8String:name ] size:(float)size];
-    }
-    free(fontcopy);
-    return font;
-}
-
 /* Allows the user to choose a font using the system's font chooser dialog.
  * Parameters:
  *       currfont: current font