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 		if(name.length){
32 			xfont = XftFontOpenName(dpy, screen, name.toStringz);
33 			pattern = FcNameParse(cast(FcChar8*)name.toStringz);
34 			if(!xfont || !pattern){
35 				if(xfont){
36 					XftFontClose(dpy, xfont);
37 					xfont = null;
38 				}
39 				throw new Exception("Cannot load font " ~ name);
40 			}
41 		}else if(pattern){
42 			xfont = XftFontOpenPattern(dpy, pattern);
43 			if(!xfont)
44 				throw new Exception("Error, cannot load font pattern");
45 			else
46 				pattern = null;
47 		}
48 		ascent = xfont.ascent;
49 		descent = xfont.descent;
50 		h = ascent + descent;
51 	}
52 
53 	int[2] size(string text){
54 		XGlyphInfo ext;
55 		if(!text.length)
56 			return [0,0];
57 		XftTextExtentsUtf8(dpy, xfont, cast(XftChar8*)text.toStringz, cast(int)text.length, &ext);
58 		return[ext.xOff, h];
59 	}
60 
61 	int width(string text){
62 		return size(text)[0];
63 	}
64 
65 	void destroy(){
66 		if(pattern)
67 			FcPatternDestroy(pattern);
68 		XftFontClose(dpy, xfont);
69 	}
70 
71 }