From 5d208730cd85806634025eefa3e7f146391a5e0c Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sun, 12 Jun 2011 17:10:10 +0200 Subject: [PATCH] Wmfs: Make Xft font optional #1 --- src/config.c | 7 +++++- src/draw.c | 59 ++++++++++++++++++++++++++++++++++++--------------- src/frame.c | 2 +- src/init.c | 44 +++++++++++++++++++++++++++++++++----- src/structs.h | 7 ++++++ src/wmfs.c | 8 ++++++- src/wmfs.h | 6 +++--- wmfsrc | 1 + 8 files changed, 106 insertions(+), 28 deletions(-) diff --git a/src/config.c b/src/config.c index b947534..8c55c2b 100644 --- a/src/config.c +++ b/src/config.c @@ -166,13 +166,19 @@ mouse_section(MouseBinding mb[], struct conf_sec **sec) static void conf_misc_section(void) { + bool xft = False; int pad = 12; uint opacity = 255; struct conf_sec *sec; sec = fetch_section_first(NULL, "misc"); +#ifdef HAVE_XFT + xft = True; +#endif /* HAVE_XFT */ + conf.font = fetch_opt_first(sec, "sans-9", "font").str; + conf.use_xft = fetch_opt_first(sec, (xft ? "true" : "false"), "use_xft").boolean; conf.raisefocus = fetch_opt_first(sec, "false", "raisefocus").boolean; conf.focus_fmouse = fetch_opt_first(sec, "true", "focus_follow_mouse").boolean; conf.focus_fmov = fetch_opt_first(sec, "false", "focus_follow_movement").boolean; @@ -192,7 +198,6 @@ conf_misc_section(void) if(pad > 24 || pad < 1) { warnx("configuration : pad value (%d) incorrect.", pad); - pad = 12; } diff --git a/src/draw.c b/src/draw.c index fd77f20..936c8f9 100644 --- a/src/draw.c +++ b/src/draw.c @@ -90,8 +90,6 @@ draw_text(Drawable d, int x, int y, char* fg, char *str) void draw_image_ofset_text(Drawable d, int x, int y, char* fg, char *str, int x_image_ofset, int y_image_ofset) { - XftColor xftcolor; - XftDraw *xftd; #ifdef HAVE_IMLIB char *ostr = NULL; int i, ni, sw = 0; @@ -107,7 +105,6 @@ draw_image_ofset_text(Drawable d, int x, int y, char* fg, char *str, int x_image /* To draw image everywhere we can draw text */ #ifdef HAVE_IMLIB - ostr = xstrdup(str); textlen = strlen(ostr); @@ -123,19 +120,34 @@ draw_image_ofset_text(Drawable d, int x, int y, char* fg, char *str, int x_image } #endif /* HAVE_IMLIB */ - /* Transform X Drawable -> Xft Drawable */ - xftd = XftDrawCreate(dpy, d, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN)); +#ifdef HAVE_XFT + if(conf.use_xft) + { + XftColor xftcolor; + XftDraw *xftd; - /* Alloc text color */ - XftColorAllocName(dpy, DefaultVisual(dpy, SCREEN), - DefaultColormap(dpy, SCREEN), fg, &xftcolor); + /* Transform X Drawable -> Xft Drawable */ + xftd = XftDrawCreate(dpy, d, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN)); - XftDrawStringUtf8(xftd, &xftcolor, font, x, y, (FcChar8 *)str, strlen(str)); + /* Alloc text color */ + XftColorAllocName(dpy, DefaultVisual(dpy, SCREEN), + DefaultColormap(dpy, SCREEN), fg, &xftcolor); - /* Free the text color and XftDraw */ - XftColorFree(dpy, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN), &xftcolor); + XftDrawStringUtf8(xftd, &xftcolor, font.font, x, y, (FcChar8 *)str, strlen(str)); + + /* Free the text color and XftDraw */ + XftColorFree(dpy, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN), &xftcolor); + + XftDrawDestroy(xftd); + } + else +#endif /* HAVE_XFT */ + { + /* Use font set */ + XSetForeground(dpy, gc, getcolor(fg)); + XmbDrawString(dpy, d, font.fontset, gc, x, y, str, strlen(str)); + } - XftDrawDestroy(xftd); #ifdef HAVE_IMLIB if(strstr(ostr, "i[")) @@ -199,7 +211,7 @@ draw_graph(Drawable dr, int x, int y, int w, int h, uint color, char *data) ushort textw(char *text) { - XGlyphInfo gl; + ushort ret = 0; #ifdef HAVE_IMLIB char *ostr = NULL; ImageAttr im[128]; @@ -209,9 +221,7 @@ textw(char *text) if(!text) return 0; - #ifdef HAVE_IMLIB - ostr = xstrdup(text); textlen = strlen(ostr); @@ -219,7 +229,22 @@ textw(char *text) parse_image_block(im, text); #endif /* HAVE_IMLIB */ - XftTextExtentsUtf8(dpy, font, (FcChar8 *)text, strlen(text), &gl); +#ifdef HAVE_XFT + if(conf.use_xft) + { + XGlyphInfo gl; + + XftTextExtentsUtf8(dpy, font.font, (FcChar8 *)text, strlen(text), &gl); + ret = gl.width + font.de; + } + else +#endif /* HAVE_XFT */ + { + XRectangle r; + + XmbTextExtents(font.fontset, text, strlen(text), NULL, &r); + ret = r.width; + } #ifdef HAVE_IMLIB if(strstr(ostr, "i[")) @@ -228,5 +253,5 @@ textw(char *text) free(ostr); #endif /* HAVE_IMLIB */ - return gl.width + font->descent; + return ret; } diff --git a/src/frame.c b/src/frame.c index 7c83c4b..3121cc6 100644 --- a/src/frame.c +++ b/src/frame.c @@ -313,7 +313,7 @@ frame_update(Client *c) if(TBARH - BORDH) barwin_draw_text(c->titlebar, (c->frame_geo.width >> 1) - (textw(c->title) >> 1), - ((font->height - font->descent) + ((TBARH - font->height) >> 1)), + ((font.height - font.de) + ((TBARH - font.height) >> 1)), c->title); return; diff --git a/src/init.c b/src/init.c index 56ab57f..11f8774 100644 --- a/src/init.c +++ b/src/init.c @@ -60,12 +60,46 @@ const func_name_list_t layout_list[] = static void init_font(void) { - font = XftFontOpenName(dpy, SCREEN, conf.font); - - if(!font) +#ifdef HAVE_XFT + if(conf.use_xft) { - warnx("WMFS Error: Cannot initialize font"); - font = XftFontOpenName(dpy, SCREEN, "sans-10"); + if(!(font.font = XftFontOpenName(dpy, SCREEN, conf.font))) + { + warnx("WMFS Error: Cannot initialize Xft font"); + font.font = XftFontOpenName(dpy, SCREEN, "sans-10"); + } + + font.de = font.font->descent; + font.as = font.font->ascent; + font.height = font.font->height; + } + else +#endif /* HAVE_XFT */ + { + char **misschar, **names, *defstring; + int d; + XFontStruct **xfs = NULL; + + if(!conf.font) + conf.font = xstrdup("fixed"); + + /* Using Font Set */ + if(!(font.fontset = XCreateFontSet(dpy, conf.font, &misschar, &d, &defstring))) + { + warnx("Can't load font '%s'", conf.font); + font.fontset = XCreateFontSet(dpy, "fixed", &misschar, &d, &defstring); + } + + XFontsOfFontSet(font.fontset, &xfs, &names); + + font.as = xfs[0]->max_bounds.ascent; + font.de = xfs[0]->max_bounds.descent; + font.width = xfs[0]->max_bounds.width; + + font.height = font.as + font.de; + + if(misschar) + XFreeStringList(misschar); } /* Set font in _WMFS_FONT for eventual status tools */ diff --git a/src/structs.h b/src/structs.h index c74a248..0ad5286 100644 --- a/src/structs.h +++ b/src/structs.h @@ -402,6 +402,7 @@ typedef struct /* Misc option */ char *font; + bool use_xft; uint opacity; bool raisefocus; bool focus_fmouse; @@ -533,6 +534,12 @@ typedef struct int nrule; } Conf; +typedef struct +{ + int as, de, width, height; + XftFont *font; + XFontSet fontset; +} FontStruct; typedef struct { int x, y, w, h; diff --git a/src/wmfs.c b/src/wmfs.c index e1f36e0..418d7db 100644 --- a/src/wmfs.c +++ b/src/wmfs.c @@ -98,7 +98,13 @@ quit(void) systray_freeicons(); - XftFontClose(dpy, font); +#ifdef HAVE_XFT + if(conf.use_xft) + XftFontClose(dpy, font.font); + else +#endif /* HAVE_XFT */ + XFreeFontSet(dpy, font.fontset); + for(i = 0; i < CurLast; ++i) XFreeCursor(dpy, cursor[i]); XFreeGC(dpy, gc_stipple); diff --git a/src/wmfs.h b/src/wmfs.h index e17a8ab..afdfbca 100644 --- a/src/wmfs.h +++ b/src/wmfs.h @@ -84,8 +84,8 @@ #define ROOT RootWindow(dpy, SCREEN) #define MAXH DisplayHeight(dpy, DefaultScreen(dpy)) #define MAXW DisplayWidth(dpy, DefaultScreen(dpy)) -#define INFOBARH ((conf.bars.height > 0) ? conf.bars.height : (font->height * 1.5)) -#define FHINFOBAR ((font->height - font->descent) + (((int)INFOBARH - font->height) >> 1)) +#define INFOBARH ((conf.bars.height > 0) ? conf.bars.height : (font.height * 1.5)) +#define FHINFOBAR ((font.height - font.de) + (((int)INFOBARH - font.height) >> 1)) #define SHADH (1) #define BORDH conf.client.borderheight #define TBARH ((conf.titlebar.height < BORDH) ? BORDH : conf.titlebar.height) @@ -457,7 +457,7 @@ int xrandr_event; uint timing; /* Fonts */ -XftFont *font; +FontStruct font; /* Atoms list */ Atom *net_atom; diff --git a/wmfsrc b/wmfsrc index ecf4b8a..aad6c93 100644 --- a/wmfsrc +++ b/wmfsrc @@ -7,6 +7,7 @@ # @include "~/.config/wmfs/menu-wmfsrc" [misc] + use_xft = true font = "dejavu-10" raisefocus = true focus_follow_mouse = true