Wmfs: Make Xft font optional #1

This commit is contained in:
Martin Duquesnoy 2011-06-12 17:10:10 +02:00
parent 9bda577718
commit 5d208730cd
8 changed files with 106 additions and 28 deletions

View File

@ -166,13 +166,19 @@ mouse_section(MouseBinding mb[], struct conf_sec **sec)
static void static void
conf_misc_section(void) conf_misc_section(void)
{ {
bool xft = False;
int pad = 12; int pad = 12;
uint opacity = 255; uint opacity = 255;
struct conf_sec *sec; struct conf_sec *sec;
sec = fetch_section_first(NULL, "misc"); 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.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.raisefocus = fetch_opt_first(sec, "false", "raisefocus").boolean;
conf.focus_fmouse = fetch_opt_first(sec, "true", "focus_follow_mouse").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; 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) if(pad > 24 || pad < 1)
{ {
warnx("configuration : pad value (%d) incorrect.", pad); warnx("configuration : pad value (%d) incorrect.", pad);
pad = 12; pad = 12;
} }

View File

@ -90,8 +90,6 @@ draw_text(Drawable d, int x, int y, char* fg, char *str)
void void
draw_image_ofset_text(Drawable d, int x, int y, char* fg, char *str, int x_image_ofset, int y_image_ofset) 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 #ifdef HAVE_IMLIB
char *ostr = NULL; char *ostr = NULL;
int i, ni, sw = 0; 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 */ /* To draw image everywhere we can draw text */
#ifdef HAVE_IMLIB #ifdef HAVE_IMLIB
ostr = xstrdup(str); ostr = xstrdup(str);
textlen = strlen(ostr); 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 */ #endif /* HAVE_IMLIB */
/* Transform X Drawable -> Xft Drawable */ #ifdef HAVE_XFT
xftd = XftDrawCreate(dpy, d, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN)); if(conf.use_xft)
{
XftColor xftcolor;
XftDraw *xftd;
/* Alloc text color */ /* Transform X Drawable -> Xft Drawable */
XftColorAllocName(dpy, DefaultVisual(dpy, SCREEN), xftd = XftDrawCreate(dpy, d, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN));
DefaultColormap(dpy, SCREEN), fg, &xftcolor);
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 */ XftDrawStringUtf8(xftd, &xftcolor, font.font, x, y, (FcChar8 *)str, strlen(str));
XftColorFree(dpy, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN), &xftcolor);
/* 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 #ifdef HAVE_IMLIB
if(strstr(ostr, "i[")) 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 ushort
textw(char *text) textw(char *text)
{ {
XGlyphInfo gl; ushort ret = 0;
#ifdef HAVE_IMLIB #ifdef HAVE_IMLIB
char *ostr = NULL; char *ostr = NULL;
ImageAttr im[128]; ImageAttr im[128];
@ -209,9 +221,7 @@ textw(char *text)
if(!text) if(!text)
return 0; return 0;
#ifdef HAVE_IMLIB #ifdef HAVE_IMLIB
ostr = xstrdup(text); ostr = xstrdup(text);
textlen = strlen(ostr); textlen = strlen(ostr);
@ -219,7 +229,22 @@ textw(char *text)
parse_image_block(im, text); parse_image_block(im, text);
#endif /* HAVE_IMLIB */ #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 #ifdef HAVE_IMLIB
if(strstr(ostr, "i[")) if(strstr(ostr, "i["))
@ -228,5 +253,5 @@ textw(char *text)
free(ostr); free(ostr);
#endif /* HAVE_IMLIB */ #endif /* HAVE_IMLIB */
return gl.width + font->descent; return ret;
} }

View File

@ -313,7 +313,7 @@ frame_update(Client *c)
if(TBARH - BORDH) if(TBARH - BORDH)
barwin_draw_text(c->titlebar, barwin_draw_text(c->titlebar,
(c->frame_geo.width >> 1) - (textw(c->title) >> 1), (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); c->title);
return; return;

View File

@ -60,12 +60,46 @@ const func_name_list_t layout_list[] =
static void static void
init_font(void) init_font(void)
{ {
font = XftFontOpenName(dpy, SCREEN, conf.font); #ifdef HAVE_XFT
if(conf.use_xft)
if(!font)
{ {
warnx("WMFS Error: Cannot initialize font"); if(!(font.font = XftFontOpenName(dpy, SCREEN, conf.font)))
font = XftFontOpenName(dpy, SCREEN, "sans-10"); {
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 */ /* Set font in _WMFS_FONT for eventual status tools */

View File

@ -402,6 +402,7 @@ typedef struct
/* Misc option */ /* Misc option */
char *font; char *font;
bool use_xft;
uint opacity; uint opacity;
bool raisefocus; bool raisefocus;
bool focus_fmouse; bool focus_fmouse;
@ -533,6 +534,12 @@ typedef struct
int nrule; int nrule;
} Conf; } Conf;
typedef struct
{
int as, de, width, height;
XftFont *font;
XFontSet fontset;
} FontStruct;
typedef struct typedef struct
{ {
int x, y, w, h; int x, y, w, h;

View File

@ -98,7 +98,13 @@ quit(void)
systray_freeicons(); 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) for(i = 0; i < CurLast; ++i)
XFreeCursor(dpy, cursor[i]); XFreeCursor(dpy, cursor[i]);
XFreeGC(dpy, gc_stipple); XFreeGC(dpy, gc_stipple);

View File

@ -84,8 +84,8 @@
#define ROOT RootWindow(dpy, SCREEN) #define ROOT RootWindow(dpy, SCREEN)
#define MAXH DisplayHeight(dpy, DefaultScreen(dpy)) #define MAXH DisplayHeight(dpy, DefaultScreen(dpy))
#define MAXW DisplayWidth(dpy, DefaultScreen(dpy)) #define MAXW DisplayWidth(dpy, DefaultScreen(dpy))
#define INFOBARH ((conf.bars.height > 0) ? conf.bars.height : (font->height * 1.5)) #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 FHINFOBAR ((font.height - font.de) + (((int)INFOBARH - font.height) >> 1))
#define SHADH (1) #define SHADH (1)
#define BORDH conf.client.borderheight #define BORDH conf.client.borderheight
#define TBARH ((conf.titlebar.height < BORDH) ? BORDH : conf.titlebar.height) #define TBARH ((conf.titlebar.height < BORDH) ? BORDH : conf.titlebar.height)
@ -457,7 +457,7 @@ int xrandr_event;
uint timing; uint timing;
/* Fonts */ /* Fonts */
XftFont *font; FontStruct font;
/* Atoms list */ /* Atoms list */
Atom *net_atom; Atom *net_atom;

1
wmfsrc
View File

@ -7,6 +7,7 @@
# @include "~/.config/wmfs/menu-wmfsrc" # @include "~/.config/wmfs/menu-wmfsrc"
[misc] [misc]
use_xft = true
font = "dejavu-10" font = "dejavu-10"
raisefocus = true raisefocus = true
focus_follow_mouse = true focus_follow_mouse = true