commit e2e7fcb2198d40e2a50591932ee2b2a8f9969a5f from: Hiltjo Posthuma date: Tue Oct 20 20:55:39 2015 UTC drw: simplify drw_font_xcreate and prevent a potential unneeded allocation commit - 1f2226df1380f178240bb81dddcad6c5ff2e9d62 commit + e2e7fcb2198d40e2a50591932ee2b2a8f9969a5f blob - 4815e3ae2a58a543aefb4a49101ae964b19c4e24 blob + 4364117a811fc83ea21ba475d8dcd4b550297fd5 --- drw.c +++ drw.c @@ -108,13 +108,9 @@ static Fnt * drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) { Fnt *font; + XftFont *xfont = NULL; + FcPattern *pattern = NULL; - if (!(fontname || fontpattern)) - die("No font specified.\n"); - - if (!(font = calloc(1, sizeof(Fnt)))) - return NULL; - if (fontname) { /* Using the pattern found at font->xfont->pattern does not yield same * the same substitution results as using the pattern returned by @@ -122,28 +118,29 @@ drw_font_xcreate(Drw *drw, const char *fontname, FcPat * behaviour whereas the former just results in * missing-character-rectangles being drawn, at least with some fonts. */ - if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) || - !(font->pattern = FcNameParse((FcChar8 *) fontname))) { - if (font->xfont) { - XftFontClose(drw->dpy, font->xfont); - font->xfont = NULL; - } + if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, 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) { - if (!(font->xfont = XftFontOpenPattern(drw->dpy, fontpattern))) + if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) { fprintf(stderr, "error, cannot load font pattern.\n"); - else - font->pattern = NULL; + return NULL; + } + } else { + die("no font specified.\n"); } - if (!font->xfont) { - free(font); - return NULL; - } - - font->ascent = font->xfont->ascent; - font->descent = font->xfont->descent; + font = ecalloc(1, sizeof(Fnt)); + font->xfont = xfont; + font->pattern = pattern; + font->ascent = xfont->ascent; + font->descent = xfont->descent; font->h = font->ascent + font->descent; font->dpy = drw->dpy;