diff --git a/darwin/drawtext.m b/darwin/drawtext.m index c29f71be..3274f4bf 100644 --- a/darwin/drawtext.m +++ b/darwin/drawtext.m @@ -46,6 +46,23 @@ struct uiDrawTextFont { CTFontRef f; }; +uiDrawTextFont *mkTextFont(CTFontRef f, BOOL retain) +{ + uiDrawTextFont *font; + + font = uiNew(uiDrawTextFont); + font->f = f; + if (retain) + CFRetain(font->f); + return font; +} + +uiDrawTextFont *mkTextFontFromNSFont(NSFont *f) +{ + // toll-free bridging; we do retain, though + return mkTextFont((CTFontRef) f, YES); +} + static CFMutableDictionaryRef newAttrList(void) { CFMutableDictionaryRef attr; @@ -365,12 +382,10 @@ CFMutableDictionaryRef extractAttributes(CTFontDescriptorRef desc) uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc) { - uiDrawTextFont *font; + CTFontRef f; CFMutableDictionaryRef attr; CTFontDescriptorRef cfdesc; - font = uiNew(uiDrawTextFont); - attr = newAttrList(); addFontFamilyAttr(attr, desc->Family); addFontSizeAttr(attr, desc->Size); @@ -388,10 +403,10 @@ uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc) // TODO release attr? */ // specify the initial size again just to be safe - font->f = CTFontCreateWithFontDescriptor(cfdesc, desc->Size, NULL); + f = CTFontCreateWithFontDescriptor(cfdesc, desc->Size, NULL); // TODO release cfdesc? - return font; + return mkTextFont(f, NO); // we hold the initial reference; no need to retain again } void uiDrawFreeTextFont(uiDrawTextFont *font) @@ -585,6 +600,7 @@ static void prepareContextForText(CGContextRef c, CGFloat cheight, double *y) *y = cheight - *y; } +// TODO placement is incorrect for Helvetica void doDrawText(CGContextRef c, CGFloat cheight, double x, double y, uiDrawTextLayout *layout) { struct framesetter fs; diff --git a/darwin/uipriv_darwin.h b/darwin/uipriv_darwin.h index 1c912188..6774fd33 100644 --- a/darwin/uipriv_darwin.h +++ b/darwin/uipriv_darwin.h @@ -82,6 +82,8 @@ extern uiDrawContext *newContext(CGContextRef, CGFloat); extern void freeContext(uiDrawContext *); // drawtext.m +extern uiDrawTextFont *mkTextFont(CTFontRef f, BOOL retain); +extern uiDrawTextFont *mkTextFontFromNSFont(NSFont *f); extern void doDrawText(CGContextRef c, CGFloat cheight, double x, double y, uiDrawTextLayout *layout); // fontbutton.m diff --git a/unix/draw.c b/unix/draw.c index 9a56c14e..67571032 100644 --- a/unix/draw.c +++ b/unix/draw.c @@ -487,6 +487,17 @@ struct uiDrawTextFont { PangoFont *f; }; +uiDrawTextFont *mkTextFont(PangoFont *f, gboolean ref) +{ + uiDrawTextFont *font; + + font = uiNew(uiDrawTextFont); + font->f = f; + if (ref) + g_object_ref(font->f); + return font; +} + static const PangoWeight pangoWeights[] = { [uiDrawTextWeightThin] = PANGO_WEIGHT_THIN, [uiDrawTextWeightUltraLight] = PANGO_WEIGHT_ULTRALIGHT, @@ -527,13 +538,11 @@ static const PangoStretch pangoStretches[] = { uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc) { - uiDrawTextFont *font; + PangoFont *f; PangoFontDescription *pdesc; //TODO PangoVariant variant; PangoContext *context; - font = uiNew(uiDrawTextFont); - pdesc = pango_font_description_new(); pango_font_description_set_family(pdesc, desc->Family); @@ -555,14 +564,14 @@ TODO // in this case, the context is necessary for the metrics to be correct context = mkGenericPangoCairoContext(); - font->f = pango_font_map_load_font(pango_cairo_font_map_get_default(), context, pdesc); - if (font->f == NULL) { + f = pango_font_map_load_font(pango_cairo_font_map_get_default(), context, pdesc); + if (f == NULL) { // TODO g_error("[libui] no match in uiDrawLoadClosestFont(); report to andlabs"); } g_object_unref(context); - return font; + return mkTextFont(f, FALSE); // we hold the initial reference; no need to retain } void uiDrawFreeTextFont(uiDrawTextFont *font) diff --git a/unix/uipriv_unix.h b/unix/uipriv_unix.h index 5355787b..d779f9be 100644 --- a/unix/uipriv_unix.h +++ b/unix/uipriv_unix.h @@ -43,6 +43,7 @@ extern void childSetMargined(struct child *c, int margined); // draw.c extern uiDrawContext *newContext(cairo_t *); extern void freeContext(uiDrawContext *); +extern uiDrawTextFont *mkTextFont(PangoFont *f, gboolean add); // TODO #define uiControlQueueResize(...)