1 module ws.x.font;
2 
3 version(Posix):
4 
5 
6 import
7 	std.string,
8 	x11.Xlib,
9 	x11.extensions.Xrender,
10 	ws.bindings.xft,
11 	ws.bindings.fontconfig;
12 
13 
14 class Font {
15 
16 	Display* dpy;
17 	int ascent;
18 	int descent;
19 	uint h;
20 	XftFont* xfont;
21 	FcPattern* pattern;
22 
23 	this(Display* dpy, int screen, string name){
24 		this(dpy, screen, name, null);
25 	}
26 
27 	this(Display* dpy, int screen, string name, FcPattern* pattern){
28 		if(!name.length && !pattern)
29 			throw new Exception("No font specified.");
30 		this.dpy = dpy;
31 		XSync(dpy, false);
32 		if(name.length){
33 			xfont = XftFontOpenName(dpy, screen, name.toStringz);
34 			pattern = FcNameParse(cast(FcChar8*)name.toStringz);
35 			if(!xfont || !pattern){
36 				if(xfont){
37 					XftFontClose(dpy, xfont);
38 					xfont = null;
39 				}
40 				throw new Exception("Cannot load font " ~ name);
41 			}
42 		}else if(pattern){
43 			xfont = XftFontOpenPattern(dpy, pattern);
44 			if(!xfont)
45 				throw new Exception("Error, cannot load font pattern");
46 			else
47 				pattern = null;
48 		}
49 		ascent = xfont.ascent;
50 		descent = xfont.descent;
51 		h = ascent + descent;
52 	}
53 
54 	int[2] size(string text){
55 		XGlyphInfo ext;
56 		if(!text.length)
57 			return [0,0];
58 		XftTextExtentsUtf8(dpy, xfont, cast(XftChar8*)text.toStringz, cast(int)text.length, &ext);
59 		return[ext.xOff, h];
60 	}
61 
62 	int width(string text){
63 		return size(text)[0];
64 	}
65 
66 	void destroy(){
67 		if(pattern)
68 			FcPatternDestroy(pattern);
69 		XftFontClose(dpy, xfont);
70 	}
71 
72 }