Skip to content

Instantly share code, notes, and snippets.

@farhaven
Created October 29, 2012 09:18
Show Gist options
  • Save farhaven/3972547 to your computer and use it in GitHub Desktop.
Save farhaven/3972547 to your computer and use it in GitHub Desktop.
diff -r e9301fef1b63 config.def.h
--- a/config.def.h Sat Oct 27 00:01:15 2012 +0200
+++ b/config.def.h Mon Oct 29 10:20:23 2012 +0100
@@ -1,9 +1,8 @@
static const Bool wmborder = True;
-static const char font[] = "-*-terminus-medium-r-normal-*-14-*-*-*-*-*-*-*";
+static const char font[] = "DejaVu Sans Mono:pixelsize=14:antialias=true:autohint=true";
static const char normbgcolor[] = "#cccccc";
static const char normfgcolor[] = "#000000";
static const char pressbgcolor[] = "#0000cc";
static const char pressfgcolor[] = "#ffffff";
static const char highlightbgcolor[] = "#0000cc";
static const char highlightfgcolor[] = "#ffffff";
-
diff -r e9301fef1b63 config.mk
--- a/config.mk Sat Oct 27 00:01:15 2012 +0200
+++ b/config.mk Mon Oct 29 10:20:23 2012 +0100
@@ -18,9 +18,9 @@
# flags
CPPFLAGS = -DVERSION=\"${VERSION}\" \
- ${XINERAMAFLAGS}
+ ${XINERAMAFLAGS} -I/usr/X11R6/include/freetype2
CFLAGS = -g -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
-LDFLAGS = -g ${LIBS}
+LDFLAGS = -g ${LIBS} -lXft
# Solaris
#CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\"
diff -r e9301fef1b63 svkbd.c
--- a/svkbd.c Sat Oct 27 00:01:15 2012 +0200
+++ b/svkbd.c Mon Oct 29 10:20:23 2012 +0100
@@ -12,6 +12,7 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
+#include <X11/Xft/Xft.h>
#include <X11/extensions/XTest.h>
/* macros */
@@ -27,9 +28,9 @@
typedef unsigned long ulong;
typedef struct {
- ulong norm[ColLast];
- ulong press[ColLast];
- ulong high[ColLast];
+ XftColor norm[ColLast];
+ XftColor press[ColLast];
+ XftColor high[ColLast];
Drawable drawable;
GC gc;
@@ -37,8 +38,7 @@
int ascent;
int descent;
int height;
- XFontSet set;
- XFontStruct *xfont;
+ XftFont *xfont;
} font;
} DC; /* draw context */
@@ -68,7 +68,7 @@
static void drawkey(Key *k);
static void expose(XEvent *e);
static Key *findkey(int x, int y);
-static ulong getcolor(const char *colstr);
+static XftColor getcolor(const char *colstr);
static void initfont(const char *fontstr);
static void leavenotify(XEvent *e);
static void press(Key *k, KeySym mod);
@@ -182,10 +182,6 @@
void
cleanup(void) {
- if(dc.font.set)
- XFreeFontSet(dpy, dc.font.set);
- else
- XFreeFont(dpy, dc.font.xfont);
XFreePixmap(dpy, dc.drawable);
XFreeGC(dpy, dc.gc);
XDestroyWindow(dpy, win);
@@ -242,8 +238,9 @@
drawkey(Key *k) {
int x, y, h, len;
XRectangle r = { k->x, k->y, k->w, k->h};
+ XftDraw *d;
const char *l;
- ulong *col;
+ XftColor *col;
if(k->pressed)
col = dc.press;
@@ -252,13 +249,12 @@
else
col = dc.norm;
- XSetForeground(dpy, dc.gc, col[ColBG]);
+ XSetForeground(dpy, dc.gc, col[ColBG].pixel);
XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
- XSetForeground(dpy, dc.gc, dc.norm[ColFG]);
+ XSetForeground(dpy, dc.gc, dc.norm[ColFG].pixel);
r.height -= 1;
r.width -= 1;
XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
- XSetForeground(dpy, dc.gc, col[ColFG]);
if(k->label) {
l = k->label;
} else {
@@ -268,12 +264,9 @@
h = dc.font.ascent + dc.font.descent;
y = k->y + (k->h / 2) - (h / 2) + dc.font.ascent;
x = k->x + (k->w / 2) - (textnw(l, len) / 2);
- if(dc.font.set) {
- XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, l,
- len);
- } else {
- XDrawString(dpy, dc.drawable, dc.gc, x, y, l, len);
- }
+ d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen));
+ XftDrawStringUtf8(d, &(dc.norm[ColFG]), dc.font.xfont, x, y, (XftChar8 *) l, len);
+ XftDrawDestroy(d);
XCopyArea(dpy, dc.drawable, win, dc.gc, k->x, k->y, k->w, k->h,
k->x, k->y);
}
@@ -300,50 +293,22 @@
return NULL;
}
-ulong
+XftColor
getcolor(const char *colstr) {
- Colormap cmap = DefaultColormap(dpy, screen);
- XColor color;
-
- if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
+ XftColor c;
+ if (!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &c))
die("error, cannot allocate color '%s'\n", colstr);
- return color.pixel;
+ return c;
}
void
initfont(const char *fontstr) {
- char *def, **missing;
- int i, n;
+ if (!(dc.font.xfont = XftFontOpenName(dpy, screen, fontstr))
+ && !(dc.font.xfont = XftFontOpenName(dpy, screen, "fixed")))
+ die("can't load font '%s'\n", fontstr);
- missing = NULL;
- if(dc.font.set)
- XFreeFontSet(dpy, dc.font.set);
- dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
- if(missing) {
- while(n--)
- fprintf(stderr, "svkbd: missing fontset: %s\n", missing[n]);
- XFreeStringList(missing);
- }
- if(dc.font.set) {
- XFontStruct **xfonts;
- char **font_names;
- dc.font.ascent = dc.font.descent = 0;
- n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
- for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
- dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
- dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
- xfonts++;
- }
- } else {
- if(dc.font.xfont)
- XFreeFont(dpy, dc.font.xfont);
- dc.font.xfont = NULL;
- if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
- && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
- die("error, cannot load font: '%s'\n", fontstr);
- dc.font.ascent = dc.font.xfont->ascent;
- dc.font.descent = dc.font.xfont->descent;
- }
+ dc.font.ascent = dc.font.xfont->ascent;
+ dc.font.descent = dc.font.xfont->descent;
dc.font.height = dc.font.ascent + dc.font.descent;
}
@@ -488,14 +453,12 @@
dc.drawable = XCreatePixmap(dpy, root, ww, wh,
DefaultDepth(dpy, screen));
dc.gc = XCreateGC(dpy, root, 0, 0);
- if(!dc.font.set)
- XSetFont(dpy, dc.gc, dc.font.xfont->fid);
for(i = 0; i < LENGTH(keys); i++)
keys[i].pressed = 0;
wa.override_redirect = !wmborder;
- wa.border_pixel = dc.norm[ColFG];
- wa.background_pixel = dc.norm[ColBG];
+ wa.border_pixel = dc.norm[ColFG].pixel;
+ wa.background_pixel = dc.norm[ColBG].pixel;
win = XCreateWindow(dpy, root, wx, wy, ww, wh, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
CWOverrideRedirect | CWBorderPixel |
@@ -540,13 +503,9 @@
int
textnw(const char *text, uint len) {
- XRectangle r;
-
- if(dc.font.set) {
- XmbTextExtents(dc.font.set, text, len, NULL, &r);
- return r.width;
- }
- return XTextWidth(dc.font.xfont, text, len);
+ XGlyphInfo ext;
+ XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext);
+ return ext.xOff;
}
void
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment