/* * Copyright 2010 Johan Veenhuizen */ #include #include #include #include "wind.h" #define DEFAULT "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*" struct fontcolor { GC gc; }; struct font *ftload(const char *name) { XFontSet fontset = NULL; char **missingcharsetlist; int missingcharsetcount; char *defstring; if (name != NULL) { fontset = XCreateFontSet(dpy, name, &missingcharsetlist, &missingcharsetcount, &defstring); if (fontset == NULL) errorf("cannot not load fontset %s", name); } if (fontset == NULL) fontset = XCreateFontSet(dpy, DEFAULT, &missingcharsetlist, &missingcharsetcount, &defstring); if (fontset == NULL) fontset = XCreateFontSet(dpy, "fixed", &missingcharsetlist, &missingcharsetcount, &defstring); if (fontset == NULL) return NULL; XFontSetExtents *extents = XExtentsOfFontSet(fontset); struct font *f = xmalloc(sizeof *f); f->data = fontset; f->ascent = -extents->max_logical_extent.y; f->descent = extents->max_logical_extent.height - f->ascent; f->size = f->ascent + f->descent; return f; } void ftfree(struct font *f) { XFreeFontSet(dpy, f->data); free(f); } struct fontcolor *ftloadcolor(const char *name) { unsigned long pixel = getpixel(name); struct fontcolor *c = xmalloc(sizeof *c); c->gc = XCreateGC(dpy, root, GCForeground, &(XGCValues){ .foreground = pixel }); return c; } void ftfreecolor(struct fontcolor *c) { XFreeGC(dpy, c->gc); free(c); } void ftdrawstring(Drawable d, struct font *f, struct fontcolor *c, int x, int y, const char *s) { XFontSet fontset = f->data; XmbDrawString(dpy, d, fontset, c->gc, x, y, s, strlen(s)); } void ftdrawstring_utf8(Drawable d, struct font *f, struct fontcolor *c, int x, int y, const char *s) { #ifdef X_HAVE_UTF8_STRING XFontSet fontset = f->data; Xutf8DrawString(dpy, d, fontset, c->gc, x, y, s, strlen(s)); #else // This is not correct, but might be better than doing nothing. ftdrawstring(d, f, c, x, y, s); #endif } int fttextwidth(struct font *f, const char *s) { XRectangle r = { .x = 0, .y = 0, .width = 0, .height = 0 }; XFontSet fontset = f->data; XmbTextExtents(fontset, s, strlen(s), &r, NULL); return r.x + r.width; } int fttextwidth_utf8(struct font *f, const char *s) { #ifdef X_HAVE_UTF8_STRING XRectangle r = { .x = 0, .y = 0, .width = 0, .height = 0 }; XFontSet fontset = f->data; Xutf8TextExtents(fontset, s, strlen(s), &r, NULL); return r.x + r.width; #else // This is not correct, but might be better than doing nothing. return fttextwidth(f, s); #endif }