aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-10-20 22:55:39 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-10-20 22:55:39 +0200
commite2e7fcb2198d40e2a50591932ee2b2a8f9969a5f (patch)
treecf366754a79debd240c231ce108575d9bb9d1313
parent1f2226df1380f178240bb81dddcad6c5ff2e9d62 (diff)
drw: simplify drw_font_xcreate and prevent a potential unneeded allocation
-rw-r--r--drw.c41
1 files changed, 19 insertions, 22 deletions
diff --git a/drw.c b/drw.c
index 4815e3a..4364117 100644
--- a/drw.c
+++ b/drw.c
@@ -108,12 +108,8 @@ static Fnt *
108drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) 108drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
109{ 109{
110 Fnt *font; 110 Fnt *font;
111 111 XftFont *xfont = NULL;
112 if (!(fontname || fontpattern)) 112 FcPattern *pattern = NULL;
113 die("No font specified.\n");
114
115 if (!(font = calloc(1, sizeof(Fnt))))
116 return NULL;
117 113
118 if (fontname) { 114 if (fontname) {
119 /* Using the pattern found at font->xfont->pattern does not yield same 115 /* 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)
122 * behaviour whereas the former just results in 118 * behaviour whereas the former just results in
123 * missing-character-rectangles being drawn, at least with some fonts. 119 * missing-character-rectangles being drawn, at least with some fonts.
124 */ 120 */
125 if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) || 121 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
126 !(font->pattern = FcNameParse((FcChar8 *) fontname))) { 122 fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
127 if (font->xfont) { 123 return NULL;
128 XftFontClose(drw->dpy, font->xfont); 124 }
129 font->xfont = NULL; 125 if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
130 }
131 fprintf(stderr, "error, cannot load font: '%s'\n", fontname); 126 fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
127 XftFontClose(drw->dpy, xfont);
128 return NULL;
132 } 129 }
133 } else if (fontpattern) { 130 } else if (fontpattern) {
134 if (!(font->xfont = XftFontOpenPattern(drw->dpy, fontpattern))) 131 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
135 fprintf(stderr, "error, cannot load font pattern.\n"); 132 fprintf(stderr, "error, cannot load font pattern.\n");
136 else 133 return NULL;
137 font->pattern = NULL; 134 }
138 } 135 } else {
139 136 die("no font specified.\n");
140 if (!font->xfont) {
141 free(font);
142 return NULL;
143 } 137 }
144 138
145 font->ascent = font->xfont->ascent; 139 font = ecalloc(1, sizeof(Fnt));
146 font->descent = font->xfont->descent; 140 font->xfont = xfont;
141 font->pattern = pattern;
142 font->ascent = xfont->ascent;
143 font->descent = xfont->descent;
147 font->h = font->ascent + font->descent; 144 font->h = font->ascent + font->descent;
148 font->dpy = drw->dpy; 145 font->dpy = drw->dpy;
149 146