drw: simplify drw_font_xcreate and prevent a potential unneeded allocation e2e7fcb2
Hiltjo Posthuma · 2015-10-20 22:55 1 file(s) · +19 −22
drw.c +19 −22
108 108
drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
109 109
{
110 110
	Fnt *font;
111 -
112 -
	if (!(fontname || fontpattern))
113 -
		die("No font specified.\n");
114 -
115 -
	if (!(font = calloc(1, sizeof(Fnt))))
116 -
		return NULL;
111 +
	XftFont *xfont = NULL;
112 +
	FcPattern *pattern = NULL;
117 113
118 114
	if (fontname) {
119 115
		/* Using the pattern found at font->xfont->pattern does not yield same
122 118
		 * behaviour whereas the former just results in
123 119
		 * missing-character-rectangles being drawn, at least with some fonts.
124 120
		 */
125 -
		if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) ||
126 -
		    !(font->pattern = FcNameParse((FcChar8 *) fontname))) {
127 -
			if (font->xfont) {
128 -
				XftFontClose(drw->dpy, font->xfont);
129 -
				font->xfont = NULL;
130 -
			}
121 +
		if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
122 +
			fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
123 +
			return NULL;
124 +
		}
125 +
		if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
131 126
			fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
127 +
			XftFontClose(drw->dpy, xfont);
128 +
			return NULL;
132 129
		}
133 130
	} else if (fontpattern) {
134 -
		if (!(font->xfont = XftFontOpenPattern(drw->dpy, fontpattern)))
131 +
		if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
135 132
			fprintf(stderr, "error, cannot load font pattern.\n");
136 -
		else
137 -
			font->pattern = NULL;
138 -
	}
139 -
140 -
	if (!font->xfont) {
141 -
		free(font);
142 -
		return NULL;
133 +
			return NULL;
134 +
		}
135 +
	} else {
136 +
		die("no font specified.\n");
143 137
	}
144 138
145 -
	font->ascent = font->xfont->ascent;
146 -
	font->descent = font->xfont->descent;
139 +
	font = ecalloc(1, sizeof(Fnt));
140 +
	font->xfont = xfont;
141 +
	font->pattern = pattern;
142 +
	font->ascent = xfont->ascent;
143 +
	font->descent = xfont->descent;
147 144
	font->h = font->ascent + font->descent;
148 145
	font->dpy = drw->dpy;
149 146