Skip to content

Instantly share code, notes, and snippets.

@holomorph
Last active May 8, 2016 15:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save holomorph/c3e3097fbe9f738530b5 to your computer and use it in GitHub Desktop.
diff --git a/config.mk b/config.mk
index 80dc936..fa19e58 100644
--- a/config.mk
+++ b/config.mk
@@ -10,6 +10,10 @@ MANPREFIX = ${PREFIX}/share/man
X11INC = /usr/X11R6/include
X11LIB = /usr/X11R6/lib
+# Pango
+PANGOLIBS = $(shell pkg-config --libs pango pangoxft)
+PANGOFLAGS = $(shell pkg-config --cflags pango pangoxft)
+
# Xinerama, comment if you don't want it
XINERAMALIBS = -lXinerama
XINERAMAFLAGS = -DXINERAMA
@@ -21,8 +25,8 @@ FREETYPEINC = /usr/include/freetype2
#FREETYPEINC = ${X11INC}/freetype2
# includes and libs
-INCS = -I${X11INC} -I${FREETYPEINC}
-LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS}
+INCS = -I${X11INC} -I${FREETYPEINC} ${PANGOFLAGS}
+LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} ${PANGOLIBS}
# flags
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=2 -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
diff --git a/drw.c b/drw.c
index f49200b..5ad8b7d 100644
--- a/drw.c
+++ b/drw.c
@@ -4,6 +4,9 @@
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
+#include <pango/pango.h>
+#include <pango/pangoxft.h>
+#include <pango/pangofc-fontmap.h>
#include "drw.h"
#include "util.h"
@@ -110,6 +113,10 @@ drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
Fnt *font;
XftFont *xfont = NULL;
FcPattern *pattern = NULL;
+ PangoFontMap *fontmap;
+ PangoContext *context;
+ PangoFontDescription *desc;
+ PangoFontMetrics *metrics;
if (fontname) {
/* Using the pattern found at font->xfont->pattern does not yield same
@@ -139,11 +146,20 @@ drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
font = ecalloc(1, sizeof(Fnt));
font->xfont = xfont;
font->pattern = pattern;
- font->ascent = xfont->ascent;
- font->descent = xfont->descent;
+ fontmap = pango_xft_get_font_map(drw->dpy, drw->screen);
+ context = pango_font_map_create_context(fontmap);
+ desc = pango_fc_font_description_from_pattern(font->xfont->pattern, TRUE);
+ font->layout = pango_layout_new(context);
+ pango_layout_set_font_description(font->layout, desc);
+
+ metrics = pango_context_get_metrics(context, desc, NULL);
+ font->ascent = pango_font_metrics_get_ascent(metrics) / PANGO_SCALE;
+ font->descent = pango_font_metrics_get_descent(metrics) / PANGO_SCALE;
font->h = font->ascent + font->descent;
font->dpy = drw->dpy;
+ pango_font_metrics_unref(metrics);
+ g_object_unref(context);
return font;
}
@@ -175,6 +191,8 @@ drw_font_free(Fnt *font)
return;
if (font->pattern)
FcPatternDestroy(font->pattern);
+ if(font->layout)
+ g_object_unref(font->layout);
XftFontClose(font->dpy, font->xfont);
free(font);
}
@@ -290,9 +308,11 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
if (render) {
th = curfont->ascent + curfont->descent;
- ty = y + (h / 2) - (th / 2) + curfont->ascent;
+ ty = y + (h / 2) - (th / 2);
tx = x + (h / 2);
- XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
+ pango_layout_set_markup(curfont->layout, buf, len);
+ pango_xft_render_layout(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->layout, tx * PANGO_SCALE, ty * PANGO_SCALE);
+ pango_layout_set_attributes(curfont->layout, NULL);
}
x += tex.w;
w -= tex.w;
@@ -361,10 +381,14 @@ void
drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex)
{
XGlyphInfo ext;
+ PangoRectangle r;
XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
- tex->h = font->h;
- tex->w = ext.xOff;
+ pango_layout_set_markup(font->layout, text, len);
+ pango_layout_get_extents(font->layout, 0, &r);
+ pango_layout_set_attributes(font->layout, NULL);
+ tex->h = r.height / PANGO_SCALE;
+ tex->w = r.width / PANGO_SCALE;
}
unsigned int
diff --git a/drw.h b/drw.h
index e3b8515..15ce639 100644
--- a/drw.h
+++ b/drw.h
@@ -17,6 +17,7 @@ typedef struct {
unsigned int h;
XftFont *xfont;
FcPattern *pattern;
+ PangoLayout *layout;
} Fnt;
typedef struct {
diff --git a/dwm.c b/dwm.c
index ff7e096..66df7d4 100644
--- a/dwm.c
+++ b/dwm.c
@@ -40,6 +40,8 @@
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
#include <X11/Xft/Xft.h>
+#include <pango/pango.h>
+#include <pango/pangoxft.h>
#include "drw.h"
#include "util.h"
@@ -236,7 +238,7 @@ static void zoom(const Arg *arg);
/* variables */
static const char broken[] = "broken";
-static char stext[256];
+static char stext[1024];
static int screen;
static int sw, sh; /* X display screen geometry width, height */
static int bh, blw = 0; /* bar geometry */
diff --git a/util.h b/util.h
index cded043..22d7a5e 100644
--- a/util.h
+++ b/util.h
@@ -1,7 +1,11 @@
/* See LICENSE file for copyright and license details. */
+#ifndef MAX
#define MAX(A, B) ((A) > (B) ? (A) : (B))
+#endif /* MAX */
+#ifndef MIN
#define MIN(A, B) ((A) < (B) ? (A) : (B))
+#endif /* MIN */
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
void die(const char *errstr, ...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment