Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add XFT support to textwidth #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions gadgets/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ X11INC = /usr/X11R6/include
INCS = -I. -I/usr/include -I${X11INC}

X11LIB = /usr/X11R6/lib
LIBS = -L/usr/lib

CFLAGS = -Os ${INCS}
# Configure the features you want to be supported
# Only one of the following options has to be uncommented,
# all others must be commented!
#
# Uncomment: Remove # from the beginning of respective lines
# Comment : Add # to the beginning of the respective lines

## Option 1: No XFT
#LIBS = -L/usr/lib
#CFLAGS = -Os ${INCS}

## Option 2: With XFT
LIBS = -L/usr/lib `pkg-config --libs xft`
CFLAGS = -Os ${INCS} -DDZEN_XFT `pkg-config --cflags xft`

LDFLAGS = ${LIBS}

# compiler and linker
Expand Down
33 changes: 31 additions & 2 deletions gadgets/textwidth.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ THE SOFTWARE.
#include<stdlib.h>
#include<string.h>
#include<X11/Xlib.h>
#ifdef DZEN_XFT
#include<X11/Xft/Xft.h>
#endif

typedef struct _Fnt {
XFontStruct *xfont;
XFontSet set;
int ascent;
int descent;
int height;
#ifdef DZEN_XFT
XftFont *xftfont;
XGlyphInfo *extents;
int width;
#endif
} Fnt;

Fnt font;
Expand All @@ -42,19 +50,25 @@ Display *dpy;
unsigned int
textw(const char *text, unsigned int len) {
XRectangle r;

#ifndef DZEN_XFT
if(font.set) {
XmbTextExtents(font.set, text, len, NULL, &r);
return r.width;
}
return XTextWidth(font.xfont, text, len);
#else
XftTextExtentsUtf8(dpy, font.xftfont, (unsigned const char *) text, strlen(text), font.extents);
if(font.extents->height > font.height)
font.height = font.extents->height;
return font.extents->xOff;
#endif
}

void
setfont(const char *fontstr) {
#ifndef DZEN_XFT
char *def, **missing;
int i, n;

missing = NULL;
if(font.set)
XFreeFontSet(dpy, font.set);
Expand Down Expand Up @@ -88,6 +102,21 @@ setfont(const char *fontstr) {
font.descent = font.xfont->descent;
}
font.height = font.ascent + font.descent;
#else
if(font.xftfont)
XftFontClose(dpy, font.xftfont);
font.xftfont = XftFontOpenXlfd(dpy, DefaultScreen(dpy), fontstr);
if(!font.xftfont)
font.xftfont = XftFontOpenName(dpy, DefaultScreen(dpy), fontstr);
if(!font.xftfont) {
fprintf(stderr, "error, cannot load font: '%s'\n", fontstr);
exit(EXIT_FAILURE);
}
font.extents = malloc(sizeof(XGlyphInfo));
XftTextExtentsUtf8(dpy, font.xftfont, (unsigned const char *) fontstr, strlen(fontstr), font.extents);
font.height = font.xftfont->ascent + font.xftfont->descent;
font.width = (font.extents->width)/strlen(fontstr);
#endif
}

int
Expand Down