summarylogtreecommitdiffstats
path: root/xft.patch
blob: 28d98e6cdf1963c3415ee9794802df1d9138994d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
diff --git a/Makefile b/Makefile
index 2edbe43..bf26d3b 100644
--- a/Makefile
+++ b/Makefile
@@ -16,6 +16,9 @@ LIBS      := -lX11 -lImlib2
 # libexif: jpeg auto-orientation, exif thumbnails
 	CPPFLAGS += -DHAVE_LIBEXIF
 	LIBS     += -lexif
+# xft support
+	CPPFLAGS += -I$(PREFIX)/include/freetype2
+	LIBS     += -lutil -lXext -lXft -lfontconfig
 
 
 .PHONY: clean install uninstall
diff --git a/image.c b/image.c
index ee9c30d..222e280 100644
--- a/image.c
+++ b/image.c
@@ -451,7 +451,7 @@ void img_render(img_t *img)
 	int sx, sy, sw, sh;
 	int dx, dy, dw, dh;
 	Imlib_Image bg;
-	unsigned long c;
+	XftColor c;
 
 	if (img == NULL || img->im == NULL || img->win == NULL)
 		return;
@@ -523,7 +523,7 @@ void img_render(img_t *img)
 			imlib_image_put_back_data(data);
 		} else {
 			c = win->fullscreen ? win->fscol : win->bgcol;
-			imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF);
+			imlib_context_set_color(c.color.red & 0xFF, c.color.blue & 0xFF, c.color.green & 0xFF, 0xFF);
 			imlib_image_fill_rectangle(0, 0, dw, dh);
 		}
 		imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
diff --git a/main.c b/main.c
index 904a567..a0e7238 100644
--- a/main.c
+++ b/main.c
@@ -406,8 +406,8 @@ void update_info(void)
 	if (ow_info) {
 		fn = strlen(files[fileidx].name);
 		if (fn < l->size &&
-		    win_textwidth(files[fileidx].name, fn, true) +
-		    win_textwidth(r->buf, r->p - r->buf, true) < win.w)
+		    win_textwidth(&win, files[fileidx].name, fn, true) +
+		    win_textwidth(&win, r->buf, r->p - r->buf, true) < win.w)
 		{
 			strncpy(l->buf, files[fileidx].name, l->size);
 		} else {
diff --git a/thumbs.c b/thumbs.c
index 8051dd8..ff971bd 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -485,7 +485,7 @@ void tns_mark(tns_t *tns, int n, bool mark)
 	if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
 		win_t *win = tns->win;
 		thumb_t *t = &tns->thumbs[n];
-		unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
+		XftColor col = win->fullscreen ? win->fscol : win->bgcol;
 		int x = t->x + t->w, y = t->y + t->h;
 
 		win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
@@ -509,7 +509,7 @@ void tns_highlight(tns_t *tns, int n, bool hl)
 	if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
 		win_t *win = tns->win;
 		thumb_t *t = &tns->thumbs[n];
-		unsigned long col;
+		XftColor col;
 		int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
 
 		if (hl)
diff --git a/window.c b/window.c
index ff7f116..a7a34c1 100644
--- a/window.c
+++ b/window.c
@@ -24,6 +24,8 @@
 #include <locale.h>
 #include <X11/cursorfont.h>
 #include <X11/Xatom.h>
+#include <fontconfig/fontconfig.h>
+#include <X11/Xft/Xft.h>
 
 #include "options.h"
 #include "util.h"
@@ -45,8 +47,7 @@ static GC gc;
 static struct {
 	int ascent;
 	int descent;
-	XFontStruct *xfont;
-	XFontSet set;
+	XftFont *xfont;
 } font;
 
 static int fontheight;
@@ -57,52 +58,31 @@ Atom atoms[ATOM_COUNT];
 static Bool fs_support;
 static Bool fs_warned;
 
-void win_init_font(Display *dpy, const char *fontstr)
+void win_init_font(Display *dpy, int screen, const char *fontstr)
 {
-	int n;
-	char *def, **missing;
-
-	font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
-	if (missing)
-		XFreeStringList(missing);
-	if (font.set) {
-		XFontStruct **xfonts;
-		char **font_names;
-
-		font.ascent = font.descent = 0;
-		XExtentsOfFontSet(font.set);
-		n = XFontsOfFontSet(font.set, &xfonts, &font_names);
-		while (n--) {
-			font.ascent  = MAX(font.ascent, (*xfonts)->ascent);
-			font.descent = MAX(font.descent,(*xfonts)->descent);
-			xfonts++;
-		}
-	} else {
-		if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
-		    (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
-		{
-			die("could not load font: %s", fontstr);
-		}
-		font.ascent  = font.xfont->ascent;
-		font.descent = font.xfont->descent;
-	}
+	if (!(font.xfont = XftFontOpenName(dpy, screen, fontstr))
+	&&  !(font.xfont = XftFontOpenName(dpy, screen, "fixed")))
+		die("error, cannot load font: '%s'\n", fontstr);
+
+	font.ascent = font.xfont->ascent;
+	font.descent = font.xfont->descent;
+
 	fontheight = font.ascent + font.descent;
 	barheight = fontheight + 2 * V_TEXT_PAD;
 }
 
-unsigned long win_alloc_color(win_t *win, const char *name)
+XftColor win_alloc_color(win_t *win, const char *name)
 {
-	XColor col;
+	XftColor col;
 
-	if (win == NULL)
-		return 0UL;
-	if (XAllocNamedColor(win->env.dpy,
-	                     DefaultColormap(win->env.dpy, win->env.scr),
-	                     name, &col, &col) == 0)
-	{
-		die("could not allocate color: %s", name);
+	if (win) {
+		if (!XftColorAllocName(win->env.dpy, DefaultVisual(win->env.dpy, win->env.scr),
+																				DefaultColormap(win->env.dpy, win->env.scr),
+																				name, &col)) {
+			die("could not allocate color: %s\n", name);
+		}
 	}
-	return col.pixel;
+	return col;
 }
 
 void win_check_wm_support(Display *dpy, Window root)
@@ -161,7 +141,7 @@ void win_init(win_t *win)
 	if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
 		warn("no locale support");
 
-	win_init_font(e->dpy, BAR_FONT);
+	win_init_font(e->dpy, e->scr, BAR_FONT);
 
 	win->bgcol     = win_alloc_color(win, WIN_BG_COLOR);
 	win->fscol     = win_alloc_color(win, WIN_FS_COLOR);
@@ -304,7 +284,7 @@ void win_open(win_t *win)
 	win->buf.h = e->scrh;
 	win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
 	                            win->buf.w, win->buf.h, e->depth);
-	XSetForeground(e->dpy, gc, fullscreen ? win->fscol : win->bgcol);
+	XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
 	XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
 	XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
 
@@ -411,7 +391,7 @@ void win_clear(win_t *win)
 		win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
 		                            win->buf.w, win->buf.h, e->depth);
 	}
-	XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
+	XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
 	XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
 }
 
@@ -421,6 +401,7 @@ void win_draw_bar(win_t *win)
 	char rest[3];
 	const char *dots = "...";
 	win_env_t *e;
+	XftDraw *d;
 	win_bar_t *l, *r;
 
 	if (win == NULL || win->xwin == None)
@@ -432,25 +413,24 @@ void win_draw_bar(win_t *win)
 	y = win->h + font.ascent + V_TEXT_PAD;
 	w = win->w;
 
-	XSetForeground(e->dpy, gc, win->bar.bgcol);
+	XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
 	XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
 
-	XSetForeground(e->dpy, gc, win->bar.fgcol);
-	XSetBackground(e->dpy, gc, win->bar.bgcol);
+	XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
+	XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
 
 	if ((len = strlen(r->buf)) > 0) {
-		if ((tw = win_textwidth(r->buf, len, true)) > w)
+		if ((tw = win_textwidth(win, r->buf, len, true)) > w)
 			return;
 		x = win->w - tw + H_TEXT_PAD;
 		w -= tw;
-		if (font.set)
-			XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, r->buf, len);
-		else
-			XDrawString(e->dpy, win->buf.pm, gc, x, y, r->buf, len);
+		d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr), DefaultColormap(e->dpy, e->scr));
+		XftDrawStringUtf8(d, &win->bar.fgcol, font.xfont, x, y, (XftChar8 *)r->buf, len);
+		XftDrawDestroy(d);
 	}
 	if ((len = strlen(l->buf)) > 0) {
 		olen = len;
-		while (len > 0 && (tw = win_textwidth(l->buf, len, true)) > w)
+		while (len > 0 && (tw = win_textwidth(win, l->buf, len, true)) > w)
 			len--;
 		if (len > 0) {
 			if (len != olen) {
@@ -461,10 +441,9 @@ void win_draw_bar(win_t *win)
 				memcpy(l->buf + len - w, dots, w);
 			}
 			x = H_TEXT_PAD;
-			if (font.set)
-				XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, l->buf, len);
-			else
-				XDrawString(e->dpy, win->buf.pm, gc, x, y, l->buf, len);
+			d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr), DefaultColormap(e->dpy, e->scr));
+			XftDrawStringUtf8(d, &win->bar.fgcol, font.xfont, x, y, (XftChar8 *)l->buf, len);
+			XftDrawDestroy(d);
 			if (len != olen)
 			  memcpy(l->buf + len - w, rest, w);
 		}
@@ -485,7 +464,7 @@ void win_draw(win_t *win)
 }
 
 void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
-                   unsigned long col)
+                   XftColor col)
 {
 	XGCValues gcval;
 
@@ -493,7 +472,7 @@ void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
 		return;
 
 	gcval.line_width = lw;
-	gcval.foreground = col;
+	gcval.foreground = col.pixel;
 	XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
 
 	if (fill)
@@ -502,17 +481,13 @@ void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
 		XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
 }
 
-int win_textwidth(const char *text, unsigned int len, bool with_padding)
+int win_textwidth(win_t *win, const char *text, unsigned int len, bool with_padding)
 {
-	XRectangle r;
+	XGlyphInfo ext;
 	int padding = with_padding ? 2 * H_TEXT_PAD : 0;
 
-	if (font.set) {
-		XmbTextExtents(font.set, text, len, NULL, &r);
-		return r.width + padding;
-	} else {
-		return XTextWidth(font.xfont, text, len) + padding;
-	}
+	XftTextExtentsUtf8(win->env.dpy, font.xfont, (XftChar8 *)text, len, &ext);
+	return ext.xOff + padding;
 }
 
 void win_set_title(win_t *win, const char *title)
diff --git a/window.h b/window.h
index 8761120..85ebb42 100644
--- a/window.h
+++ b/window.h
@@ -21,6 +21,7 @@
 
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
+#include <X11/Xft/Xft.h>
 
 #include "types.h"
 
@@ -59,9 +60,9 @@ typedef struct {
 	Window xwin;
 	win_env_t env;
 
-	unsigned long bgcol;
-	unsigned long fscol;
-	unsigned long selcol;
+	XftColor bgcol;
+	XftColor fscol;
+	XftColor selcol;
 
 	int x;
 	int y;
@@ -81,8 +82,8 @@ typedef struct {
 		unsigned int h;
 		win_bar_t l;
 		win_bar_t r;
-		unsigned long bgcol;
-		unsigned long fgcol;
+		XftColor bgcol;
+		XftColor fgcol;
 	} bar;
 } win_t;
 
@@ -99,9 +100,9 @@ void win_toggle_bar(win_t*);
 
 void win_clear(win_t*);
 void win_draw(win_t*);
-void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
+void win_draw_rect(win_t*, int, int, int, int, bool, int, XftColor);
 
-int win_textwidth(const char*, unsigned int, bool);
+int win_textwidth(win_t*, const char*, unsigned int, bool);
 
 void win_set_title(win_t*, const char*);
 void win_set_cursor(win_t*, cursor_t);