Implemented the uiDrawTextFont direct from system font-making functions on other platforms.

This commit is contained in:
Pietro Gagliardi 2016-04-19 11:23:05 -04:00
parent a1d0e669c8
commit c2d165af94
4 changed files with 39 additions and 11 deletions

View File

@ -46,6 +46,23 @@ struct uiDrawTextFont {
CTFontRef f; 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) static CFMutableDictionaryRef newAttrList(void)
{ {
CFMutableDictionaryRef attr; CFMutableDictionaryRef attr;
@ -365,12 +382,10 @@ CFMutableDictionaryRef extractAttributes(CTFontDescriptorRef desc)
uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc) uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc)
{ {
uiDrawTextFont *font; CTFontRef f;
CFMutableDictionaryRef attr; CFMutableDictionaryRef attr;
CTFontDescriptorRef cfdesc; CTFontDescriptorRef cfdesc;
font = uiNew(uiDrawTextFont);
attr = newAttrList(); attr = newAttrList();
addFontFamilyAttr(attr, desc->Family); addFontFamilyAttr(attr, desc->Family);
addFontSizeAttr(attr, desc->Size); addFontSizeAttr(attr, desc->Size);
@ -388,10 +403,10 @@ uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc)
// TODO release attr? // TODO release attr?
*/ */
// specify the initial size again just to be safe // 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? // TODO release cfdesc?
return font; return mkTextFont(f, NO); // we hold the initial reference; no need to retain again
} }
void uiDrawFreeTextFont(uiDrawTextFont *font) void uiDrawFreeTextFont(uiDrawTextFont *font)
@ -585,6 +600,7 @@ static void prepareContextForText(CGContextRef c, CGFloat cheight, double *y)
*y = cheight - *y; *y = cheight - *y;
} }
// TODO placement is incorrect for Helvetica
void doDrawText(CGContextRef c, CGFloat cheight, double x, double y, uiDrawTextLayout *layout) void doDrawText(CGContextRef c, CGFloat cheight, double x, double y, uiDrawTextLayout *layout)
{ {
struct framesetter fs; struct framesetter fs;

View File

@ -82,6 +82,8 @@ extern uiDrawContext *newContext(CGContextRef, CGFloat);
extern void freeContext(uiDrawContext *); extern void freeContext(uiDrawContext *);
// drawtext.m // 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); extern void doDrawText(CGContextRef c, CGFloat cheight, double x, double y, uiDrawTextLayout *layout);
// fontbutton.m // fontbutton.m

View File

@ -487,6 +487,17 @@ struct uiDrawTextFont {
PangoFont *f; 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[] = { static const PangoWeight pangoWeights[] = {
[uiDrawTextWeightThin] = PANGO_WEIGHT_THIN, [uiDrawTextWeightThin] = PANGO_WEIGHT_THIN,
[uiDrawTextWeightUltraLight] = PANGO_WEIGHT_ULTRALIGHT, [uiDrawTextWeightUltraLight] = PANGO_WEIGHT_ULTRALIGHT,
@ -527,13 +538,11 @@ static const PangoStretch pangoStretches[] = {
uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc) uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc)
{ {
uiDrawTextFont *font; PangoFont *f;
PangoFontDescription *pdesc; PangoFontDescription *pdesc;
//TODO PangoVariant variant; //TODO PangoVariant variant;
PangoContext *context; PangoContext *context;
font = uiNew(uiDrawTextFont);
pdesc = pango_font_description_new(); pdesc = pango_font_description_new();
pango_font_description_set_family(pdesc, pango_font_description_set_family(pdesc,
desc->Family); desc->Family);
@ -555,14 +564,14 @@ TODO
// in this case, the context is necessary for the metrics to be correct // in this case, the context is necessary for the metrics to be correct
context = mkGenericPangoCairoContext(); context = mkGenericPangoCairoContext();
font->f = pango_font_map_load_font(pango_cairo_font_map_get_default(), context, pdesc); f = pango_font_map_load_font(pango_cairo_font_map_get_default(), context, pdesc);
if (font->f == NULL) { if (f == NULL) {
// TODO // TODO
g_error("[libui] no match in uiDrawLoadClosestFont(); report to andlabs"); g_error("[libui] no match in uiDrawLoadClosestFont(); report to andlabs");
} }
g_object_unref(context); g_object_unref(context);
return font; return mkTextFont(f, FALSE); // we hold the initial reference; no need to retain
} }
void uiDrawFreeTextFont(uiDrawTextFont *font) void uiDrawFreeTextFont(uiDrawTextFont *font)

View File

@ -43,6 +43,7 @@ extern void childSetMargined(struct child *c, int margined);
// draw.c // draw.c
extern uiDrawContext *newContext(cairo_t *); extern uiDrawContext *newContext(cairo_t *);
extern void freeContext(uiDrawContext *); extern void freeContext(uiDrawContext *);
extern uiDrawTextFont *mkTextFont(PangoFont *f, gboolean add);
// TODO // TODO
#define uiControlQueueResize(...) #define uiControlQueueResize(...)