wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

loadfont.c (1593B)


      1 /* Copyright ©2007-2010 Kris Maglione <maglione.k at Gmail>
      2  * See LICENSE file for license details.
      3  */
      4 #include <string.h>
      5 #include "../x11.h"
      6 
      7 Font*
      8 loadfont(const char *name) {
      9 	XFontStruct **xfonts;
     10 	char **missing, **font_names;
     11 	Biobuf *b;
     12 	Font *f;
     13 	int n, i;
     14 
     15 	missing = nil;
     16 	f = emallocz(sizeof *f);
     17 	f->name = estrdup(name);
     18 	if(!strncmp(f->name, "xft:", 4)) {
     19 		f->type = FXft;
     20 
     21 		if(!havexft())
     22 			goto error;
     23 
     24 		f->font.xft = xft->fontopen(display, scr.screen, f->name + 4);
     25 		if(!f->font.xft)
     26 			f->font.xft = xft->fontopenname(display, scr.screen, f->name + 4);
     27 		if(!f->font.xft)
     28 			goto error;
     29 
     30 		f->ascent = f->font.xft->ascent;
     31 		f->descent = f->font.xft->descent;
     32 	}else {
     33 		f->font.set = XCreateFontSet(display, name, &missing, &n, nil);
     34 		if(missing) {
     35 			if(false) {
     36 				b = Bfdopen(dup(2), O_WRONLY);
     37 				Bprint(b, "%s: note: missing fontset%s for '%s':", argv0,
     38 						(n > 1 ? "s" : ""), name);
     39 				for(i = 0; i < n; i++)
     40 					Bprint(b, "%s %s", (i ? "," : ""), missing[i]);
     41 				Bprint(b, "\n");
     42 				Bterm(b);
     43 			}
     44 			freestringlist(missing);
     45 		}
     46 
     47 		if(f->font.set) {
     48 			f->type = FFontSet;
     49 			XFontsOfFontSet(f->font.set, &xfonts, &font_names);
     50 			f->ascent = xfonts[0]->ascent;
     51 			f->descent = xfonts[0]->descent;
     52 		}else {
     53 			f->type = FX11;
     54 			f->font.x11 = XLoadQueryFont(display, name);
     55 			if(!f->font.x11)
     56 				goto error;
     57 
     58 			f->ascent = f->font.x11->ascent;
     59 			f->descent = f->font.x11->descent;
     60 		}
     61 	}
     62 	f->height = f->ascent + f->descent;
     63 	return f;
     64 
     65 error:
     66 	fprint(2, "%s: cannot load font: %s\n", argv0, name);
     67 	f->type = 0;
     68 	freefont(f);
     69 	return nil;
     70 }