Adding pango patch (ref. #10)
Text from original patch: Using pango markup for status text Use a single font. Removed some code utf8 code from drw. Created for pango 1.44. Older versions might not have getter for font height, ascent + descent can be used instead. All texts are rendered with pango but only status is with markup. Increased stext size (in case a lot of markup is used). MIN/MAX is already defined (didn't redefine them).
This commit is contained in:
168
drw.c
168
drw.c
@@ -9,6 +9,7 @@
|
||||
#include "drw.h"
|
||||
#include "util.h"
|
||||
|
||||
#if !PANGO_PATCH
|
||||
#define UTF_INVALID 0xFFFD
|
||||
#define UTF_SIZ 4
|
||||
|
||||
@@ -60,6 +61,7 @@ utf8decode(const char *c, long *u, size_t clen)
|
||||
|
||||
return len;
|
||||
}
|
||||
#endif // PANGO_PATCH
|
||||
|
||||
Drw *
|
||||
#if ALPHA_PATCH
|
||||
@@ -116,6 +118,41 @@ drw_free(Drw *drw)
|
||||
free(drw);
|
||||
}
|
||||
|
||||
#if PANGO_PATCH
|
||||
/* This function is an implementation detail. Library users should use
|
||||
* drw_font_create instead.
|
||||
*/
|
||||
static Fnt *
|
||||
xfont_create(Drw *drw, const char *fontname)
|
||||
{
|
||||
Fnt *font;
|
||||
PangoFontMap *fontmap;
|
||||
PangoContext *context;
|
||||
PangoFontDescription *desc;
|
||||
PangoFontMetrics *metrics;
|
||||
|
||||
if (!fontname) {
|
||||
die("no font specified.");
|
||||
}
|
||||
|
||||
font = ecalloc(1, sizeof(Fnt));
|
||||
font->dpy = drw->dpy;
|
||||
|
||||
fontmap = pango_xft_get_font_map(drw->dpy, drw->screen);
|
||||
context = pango_font_map_create_context(fontmap);
|
||||
desc = pango_font_description_from_string(fontname);
|
||||
font->layout = pango_layout_new(context);
|
||||
pango_layout_set_font_description(font->layout, desc);
|
||||
|
||||
metrics = pango_context_get_metrics(context, desc, pango_language_from_string ("en-us"));
|
||||
font->h = pango_font_metrics_get_height(metrics) / PANGO_SCALE;
|
||||
|
||||
pango_font_metrics_unref(metrics);
|
||||
g_object_unref(context);
|
||||
|
||||
return font;
|
||||
}
|
||||
#else
|
||||
/* This function is an implementation detail. Library users should use
|
||||
* drw_fontset_create instead.
|
||||
*/
|
||||
@@ -173,18 +210,38 @@ xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
|
||||
|
||||
return font;
|
||||
}
|
||||
#endif // PANGO_PATCH
|
||||
|
||||
static void
|
||||
xfont_free(Fnt *font)
|
||||
{
|
||||
if (!font)
|
||||
return;
|
||||
#if PANGO_PATCH
|
||||
if (font->layout)
|
||||
g_object_unref(font->layout);
|
||||
#else
|
||||
if (font->pattern)
|
||||
FcPatternDestroy(font->pattern);
|
||||
XftFontClose(font->dpy, font->xfont);
|
||||
#endif // PANGO_PATCH
|
||||
free(font);
|
||||
}
|
||||
|
||||
#if PANGO_PATCH
|
||||
Fnt*
|
||||
drw_font_create(Drw* drw, const char font[])
|
||||
{
|
||||
Fnt *fnt = NULL;
|
||||
|
||||
if (!drw || !font)
|
||||
return NULL;
|
||||
|
||||
fnt = xfont_create(drw, font);
|
||||
|
||||
return (drw->font = fnt);
|
||||
}
|
||||
#else
|
||||
Fnt*
|
||||
drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
|
||||
{
|
||||
@@ -202,7 +259,16 @@ drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
|
||||
}
|
||||
return (drw->fonts = ret);
|
||||
}
|
||||
#endif // PANGO_PATCH
|
||||
|
||||
#if PANGO_PATCH
|
||||
void
|
||||
drw_font_free(Fnt *font)
|
||||
{
|
||||
if (font)
|
||||
xfont_free(font);
|
||||
}
|
||||
#else
|
||||
void
|
||||
drw_fontset_free(Fnt *font)
|
||||
{
|
||||
@@ -211,6 +277,7 @@ drw_fontset_free(Fnt *font)
|
||||
xfont_free(font);
|
||||
}
|
||||
}
|
||||
#endif // PANGO_PATCH
|
||||
|
||||
void
|
||||
drw_clr_create(
|
||||
@@ -279,12 +346,14 @@ drw_scm_create(
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if !PANGO_PATCH
|
||||
void
|
||||
drw_setfontset(Drw *drw, Fnt *set)
|
||||
{
|
||||
if (drw)
|
||||
drw->fonts = set;
|
||||
}
|
||||
#endif // PANGO_PATCH
|
||||
|
||||
void
|
||||
drw_setscheme(Drw *drw, Clr *scm)
|
||||
@@ -305,6 +374,72 @@ drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int
|
||||
XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
|
||||
}
|
||||
|
||||
#if PANGO_PATCH
|
||||
int
|
||||
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, Bool markup)
|
||||
{
|
||||
char buf[1024];
|
||||
int ty;
|
||||
unsigned int ew;
|
||||
XftDraw *d = NULL;
|
||||
size_t i, len;
|
||||
int render = x || y || w || h;
|
||||
|
||||
if (!drw || (render && !drw->scheme) || !text || !drw->font)
|
||||
return 0;
|
||||
|
||||
if (!render) {
|
||||
w = ~w;
|
||||
} else {
|
||||
XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
|
||||
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
||||
#if ALPHA_PATCH
|
||||
d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
|
||||
#else
|
||||
d = XftDrawCreate(drw->dpy, drw->drawable,
|
||||
DefaultVisual(drw->dpy, drw->screen),
|
||||
DefaultColormap(drw->dpy, drw->screen));
|
||||
#endif // ALPHA_PATCH
|
||||
x += lpad;
|
||||
w -= lpad;
|
||||
}
|
||||
|
||||
len = strlen(text);
|
||||
|
||||
if (len) {
|
||||
drw_font_getexts(drw->font, text, len, &ew, NULL, markup);
|
||||
/* shorten text if necessary */
|
||||
for (len = MIN(len, sizeof(buf) - 1); len && ew > w; len--)
|
||||
drw_font_getexts(drw->font, text, len, &ew, NULL, markup);
|
||||
|
||||
if (len) {
|
||||
memcpy(buf, text, len);
|
||||
buf[len] = '\0';
|
||||
if (len < strlen(text))
|
||||
for (i = len; i && i > len - 3; buf[--i] = '.')
|
||||
; /* NOP */
|
||||
|
||||
if (render) {
|
||||
ty = y + (h - drw->font->h) / 2;
|
||||
if (markup)
|
||||
pango_layout_set_markup(drw->font->layout, buf, len);
|
||||
else
|
||||
pango_layout_set_text(drw->font->layout, buf, len);
|
||||
pango_xft_render_layout(d, &drw->scheme[invert ? ColBg : ColFg],
|
||||
drw->font->layout, x * PANGO_SCALE, ty * PANGO_SCALE);
|
||||
if (markup) /* clear markup attributes */
|
||||
pango_layout_set_attributes(drw->font->layout, NULL);
|
||||
}
|
||||
x += ew;
|
||||
w -= ew;
|
||||
}
|
||||
}
|
||||
if (d)
|
||||
XftDrawDestroy(d);
|
||||
|
||||
return x + (render ? w : 0);
|
||||
}
|
||||
#else
|
||||
int
|
||||
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
|
||||
{
|
||||
@@ -439,6 +574,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
|
||||
|
||||
return x + (render ? w : 0);
|
||||
}
|
||||
#endif // PANGO_PATCH
|
||||
|
||||
void
|
||||
drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
|
||||
@@ -450,6 +586,15 @@ drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
|
||||
XSync(drw->dpy, False);
|
||||
}
|
||||
|
||||
#if PANGO_PATCH
|
||||
unsigned int
|
||||
drw_font_getwidth(Drw *drw, const char *text, Bool markup)
|
||||
{
|
||||
if (!drw || !drw->font || !text)
|
||||
return 0;
|
||||
return drw_text(drw, 0, 0, 0, 0, 0, text, 0, markup);
|
||||
}
|
||||
#else
|
||||
unsigned int
|
||||
drw_fontset_getwidth(Drw *drw, const char *text)
|
||||
{
|
||||
@@ -457,7 +602,29 @@ drw_fontset_getwidth(Drw *drw, const char *text)
|
||||
return 0;
|
||||
return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
|
||||
}
|
||||
#endif // PANGO_PATCH
|
||||
|
||||
#if PANGO_PATCH
|
||||
void
|
||||
drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h, Bool markup)
|
||||
{
|
||||
if (!font || !text)
|
||||
return;
|
||||
|
||||
PangoRectangle r;
|
||||
if (markup)
|
||||
pango_layout_set_markup(font->layout, text, len);
|
||||
else
|
||||
pango_layout_set_text(font->layout, text, len);
|
||||
pango_layout_get_extents(font->layout, 0, &r);
|
||||
if (markup) /* clear markup attributes */
|
||||
pango_layout_set_attributes(font->layout, NULL);
|
||||
if (w)
|
||||
*w = r.width / PANGO_SCALE;
|
||||
if (h)
|
||||
*h = font->h;
|
||||
}
|
||||
#else
|
||||
void
|
||||
drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
|
||||
{
|
||||
@@ -472,6 +639,7 @@ drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w,
|
||||
if (h)
|
||||
*h = font->h;
|
||||
}
|
||||
#endif // PANGO_PATCH
|
||||
|
||||
Cur *
|
||||
drw_cur_create(Drw *drw, int shape)
|
||||
|
Reference in New Issue
Block a user