drw: fixup font handling

This commit is contained in:
Markus Teich 2015-04-21 22:57:52 +02:00
parent 85d25716b0
commit 4993b300f3
2 changed files with 75 additions and 62 deletions

120
drw.c
View File

@ -11,6 +11,8 @@
#define UTF_INVALID 0xFFFD #define UTF_INVALID 0xFFFD
#define UTF_SIZ 4 #define UTF_SIZ 4
static void drw_xfont_free(Fnt *font);
static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0}; static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000}; static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
@ -68,7 +70,6 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
drw->h = h; drw->h = h;
drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen)); drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
drw->gc = XCreateGC(dpy, root, 0, NULL); drw->gc = XCreateGC(dpy, root, 0, NULL);
drw->fontcount = 0;
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
return drw; return drw;
} }
@ -86,17 +87,14 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h) {
void void
drw_free(Drw *drw) { drw_free(Drw *drw) {
size_t i; drw_fontset_free(drw->fonts);
for (i = 0; i < drw->fontcount; i++) {
drw_font_free(drw->fonts[i]);
}
XFreePixmap(drw->dpy, drw->drawable); XFreePixmap(drw->dpy, drw->drawable);
XFreeGC(drw->dpy, drw->gc); XFreeGC(drw->dpy, drw->gc);
free(drw); free(drw);
} }
/* This function is an implementation detail. Library users should use /* This function is an implementation detail. Library users should use
* drw_font_create instead. * drw_fontset_create instead.
*/ */
static Fnt * static Fnt *
drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) { drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) {
@ -116,7 +114,7 @@ drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) {
* missing-character-rectangles being drawn, at least with some fonts. * missing-character-rectangles being drawn, at least with some fonts.
*/ */
if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) || if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) ||
!(font->pattern = FcNameParse((FcChar8 *) fontname))) { !(font->pattern = FcNameParse((const FcChar8 *) fontname))) {
if (font->xfont) { if (font->xfont) {
XftFontClose(drw->dpy, font->xfont); XftFontClose(drw->dpy, font->xfont);
font->xfont = NULL; font->xfont = NULL;
@ -143,26 +141,8 @@ drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) {
return font; return font;
} }
Fnt*
drw_font_create(Drw *drw, const char *fontname) {
return drw_font_xcreate(drw, fontname, NULL);
}
void void
drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount) { drw_xfont_free(Fnt *font) {
size_t i;
Fnt *font;
for (i = 0; i < fontcount; i++) {
if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
die("Font cache exhausted.\n");
} else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) {
drw->fonts[drw->fontcount++] = font;
}
}
}
void
drw_font_free(Fnt *font) {
if(!font) if(!font)
return; return;
if(font->pattern) if(font->pattern)
@ -171,6 +151,30 @@ drw_font_free(Fnt *font) {
free(font); free(font);
} }
Fnt*
drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount) {
Fnt *ret = NULL;
Fnt *cur = NULL;
ssize_t i;
for (i = fontcount - 1; i >= 0; i--) {
if ((cur = drw_font_xcreate(drw, fonts[i], NULL))) {
cur->next = ret;
ret = cur;
}
}
return ret;
}
void
drw_fontset_free(Fnt *font) {
Fnt *nf = font;
while ((font = nf)) {
nf = font->next;
drw_xfont_free(font);
}
}
Scm * Scm *
drw_scm_create(Drw *drw, const char *fgname, const char *bgname) { drw_scm_create(Drw *drw, const char *fgname, const char *bgname) {
Scm *scm; Scm *scm;
@ -197,6 +201,12 @@ drw_scm_free(Scm *scm) {
free(scm); free(scm);
} }
void
drw_setfontset(Drw *drw, Fnt *set) {
if (drw)
drw->fonts = set;
}
void void
drw_setscheme(Drw *drw, Scm *scm) { drw_setscheme(Drw *drw, Scm *scm) {
if (drw && scm) if (drw && scm)
@ -223,7 +233,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
Colormap cmap; Colormap cmap;
Visual *vis; Visual *vis;
XftDraw *d; XftDraw *d;
Fnt *curfont, *nextfont; Fnt *usedfont, *curfont, *nextfont;
size_t i, len; size_t i, len;
int utf8strlen, utf8charlen, render; int utf8strlen, utf8charlen, render;
long utf8codepoint = 0; long utf8codepoint = 0;
@ -245,7 +255,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
} }
if (!text || !drw->fontcount) { if (!text || !drw->fonts) {
return 0; return 0;
} else if (render) { } else if (render) {
cmap = DefaultColormap(drw->dpy, drw->screen); cmap = DefaultColormap(drw->dpy, drw->screen);
@ -253,27 +263,27 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
d = XftDrawCreate(drw->dpy, drw->drawable, vis, cmap); d = XftDrawCreate(drw->dpy, drw->drawable, vis, cmap);
} }
curfont = drw->fonts[0]; usedfont = drw->fonts;
while (1) { while (1) {
utf8strlen = 0; utf8strlen = 0;
utf8str = text; utf8str = text;
nextfont = NULL; nextfont = NULL;
while (*text) { while (*text) {
utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ); utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
for (i = 0; i < drw->fontcount; i++) { for (curfont = drw->fonts; curfont; curfont = curfont->next) {
charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint); charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
if (charexists) { if (charexists) {
if (drw->fonts[i] == curfont) { if (curfont == usedfont) {
utf8strlen += utf8charlen; utf8strlen += utf8charlen;
text += utf8charlen; text += utf8charlen;
} else { } else {
nextfont = drw->fonts[i]; nextfont = curfont;
} }
break; break;
} }
} }
if (!charexists || (nextfont && nextfont != curfont)) { if (!charexists || nextfont) {
break; break;
} else { } else {
charexists = 0; charexists = 0;
@ -281,10 +291,10 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
} }
if (utf8strlen) { if (utf8strlen) {
drw_font_getexts(curfont, utf8str, utf8strlen, &ew, NULL); drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
/* shorten text if necessary */ /* shorten text if necessary */
for(len = MIN(utf8strlen, (sizeof buf) - 1); len && (ew > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--) for(len = MIN(utf8strlen, (sizeof buf) - 1); len && (ew > w - drw->fonts->h || w < drw->fonts->h); len--)
drw_font_getexts(curfont, utf8str, len, &ew, NULL); drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
if (len) { if (len) {
memcpy(buf, utf8str, len); memcpy(buf, utf8str, len);
@ -293,10 +303,10 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
for(i = len; i && i > len - 3; buf[--i] = '.'); for(i = len; i && i > len - 3; buf[--i] = '.');
if (render) { if (render) {
th = curfont->ascent + curfont->descent; th = usedfont->ascent + usedfont->descent;
ty = y + (h / 2) - (th / 2) + curfont->ascent; ty = y + (h / 2) - (th / 2) + usedfont->ascent;
tx = x + (h / 2); tx = x + (h / 2);
XftDrawStringUtf8(d, invert ? &drw->scheme->bg.rgb : &drw->scheme->fg.rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len); XftDrawStringUtf8(d, invert ? &drw->scheme->bg.rgb : &drw->scheme->fg.rgb, usedfont->xfont, tx, ty, (XftChar8 *)buf, len);
} }
x += ew; x += ew;
@ -308,28 +318,24 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
break; break;
} else if (nextfont) { } else if (nextfont) {
charexists = 0; charexists = 0;
curfont = nextfont; usedfont = nextfont;
} else { } else {
/* Regardless of whether or not a fallback font is found, the /* Regardless of whether or not a fallback font is found, the
* character must be drawn. * character must be drawn.
*/ */
charexists = 1; charexists = 1;
if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
continue;
}
fccharset = FcCharSetCreate(); fccharset = FcCharSetCreate();
FcCharSetAddChar(fccharset, utf8codepoint); FcCharSetAddChar(fccharset, utf8codepoint);
if (!drw->fonts[0]->pattern) { if (!drw->fonts->pattern) {
/* Refer to the comment in drw_font_xcreate for more /* Refer to the comment in drw_font_xcreate for more
* information. * information.
*/ */
die("The first font in the cache must be loaded from a font string.\n"); die("The first font in the cache must be loaded from a font string.\n");
} }
fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern); fcpattern = FcPatternDuplicate(drw->fonts->pattern);
FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue); FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
@ -341,14 +347,14 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *tex
FcPatternDestroy(fcpattern); FcPatternDestroy(fcpattern);
if (match) { if (match) {
curfont = drw_font_xcreate(drw, NULL, match); usedfont = drw_font_xcreate(drw, NULL, match);
if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) { if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
drw->fonts[drw->fontcount++] = curfont; for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
; /* just find the end of the linked list */
curfont->next = usedfont;
} else { } else {
if (curfont) { drw_xfont_free(usedfont);
drw_font_free(curfont); usedfont = drw->fonts;
}
curfont = drw->fonts[0];
} }
} }
} }
@ -369,6 +375,12 @@ drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
XSync(drw->dpy, False); XSync(drw->dpy, False);
} }
unsigned int
drw_fontset_getwidth(Drw *drw, const char *text) {
if (!drw || !drw->fonts || !text)
return 0;
return drw_text(drw, 0, 0, 0, 0, text, 0) + drw->fonts->h;
}
void void
drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) { drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) {

17
drw.h
View File

@ -5,14 +5,16 @@ typedef struct {
Cursor cursor; Cursor cursor;
} Cur; } Cur;
typedef struct { typedef struct Fnt Fnt;
struct Fnt {
Display *dpy; Display *dpy;
int ascent; int ascent;
int descent; int descent;
unsigned int h; unsigned int h;
XftFont *xfont; XftFont *xfont;
FcPattern *pattern; FcPattern *pattern;
} Fnt; Fnt *next;
};
typedef struct { typedef struct {
struct { struct {
@ -29,8 +31,7 @@ typedef struct {
Drawable drawable; Drawable drawable;
GC gc; GC gc;
Scm *scheme; Scm *scheme;
size_t fontcount; Fnt *fonts;
Fnt *fonts[DRW_FONT_CACHE_SIZE];
} Drw; } Drw;
/* Drawable abstraction */ /* Drawable abstraction */
@ -39,9 +40,9 @@ void drw_resize(Drw *drw, unsigned int w, unsigned int h);
void drw_free(Drw *drw); void drw_free(Drw *drw);
/* Fnt abstraction */ /* Fnt abstraction */
Fnt *drw_font_create(Drw *drw, const char *fontname); Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount);
void drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount); void drw_fontset_free(Fnt* set);
void drw_font_free(Fnt *font); unsigned int drw_fontset_getwidth(Drw *drw, const char *text);
void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h);
/* Colorscheme abstraction */ /* Colorscheme abstraction */
@ -53,7 +54,7 @@ Cur *drw_cur_create(Drw *drw, int shape);
void drw_cur_free(Drw *drw, Cur *cursor); void drw_cur_free(Drw *drw, Cur *cursor);
/* Drawing context manipulation */ /* Drawing context manipulation */
void drw_setfont(Drw *drw, Fnt *font); void drw_setfontset(Drw *drw, Fnt *set);
void drw_setscheme(Drw *drw, Scm *scm); void drw_setscheme(Drw *drw, Scm *scm);
/* Drawing functions */ /* Drawing functions */