1
0

drw: simplify drw_font_xcreate and prevent a potential unneeded allocation

This commit is contained in:
Hiltjo Posthuma 2015-10-20 22:55:39 +02:00
parent 1f2226df13
commit e2e7fcb219

41
drw.c
View File

@ -108,12 +108,8 @@ static Fnt *
drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
{ {
Fnt *font; Fnt *font;
XftFont *xfont = NULL;
if (!(fontname || fontpattern)) FcPattern *pattern = NULL;
die("No font specified.\n");
if (!(font = calloc(1, sizeof(Fnt))))
return NULL;
if (fontname) { if (fontname) {
/* Using the pattern found at font->xfont->pattern does not yield same /* Using the pattern found at font->xfont->pattern does not yield same
@ -122,28 +118,29 @@ drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
* behaviour whereas the former just results in * behaviour whereas the former just results in
* missing-character-rectangles being drawn, at least with some fonts. * missing-character-rectangles being drawn, at least with some fonts.
*/ */
if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) || if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
!(font->pattern = FcNameParse((FcChar8 *) fontname))) {
if (font->xfont) {
XftFontClose(drw->dpy, font->xfont);
font->xfont = NULL;
}
fprintf(stderr, "error, cannot load font: '%s'\n", fontname); fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
return NULL;
}
if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
XftFontClose(drw->dpy, xfont);
return NULL;
} }
} else if (fontpattern) { } else if (fontpattern) {
if (!(font->xfont = XftFontOpenPattern(drw->dpy, fontpattern))) if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
fprintf(stderr, "error, cannot load font pattern.\n"); fprintf(stderr, "error, cannot load font pattern.\n");
else return NULL;
font->pattern = NULL; }
} else {
die("no font specified.\n");
} }
if (!font->xfont) { font = ecalloc(1, sizeof(Fnt));
free(font); font->xfont = xfont;
return NULL; font->pattern = pattern;
} font->ascent = xfont->ascent;
font->descent = xfont->descent;
font->ascent = font->xfont->ascent;
font->descent = font->xfont->descent;
font->h = font->ascent + font->descent; font->h = font->ascent + font->descent;
font->dpy = drw->dpy; font->dpy = drw->dpy;