drw.c (9999B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <X11/Xlib.h> 6 #include <X11/Xft/Xft.h> 7 8 #include "drw.h" 9 #include "util.h" 10 11 #define UTF_INVALID 0xFFFD 12 #define UTF_SIZ 4 13 14 static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0}; 15 static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; 16 static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000}; 17 static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; 18 19 static long 20 utf8decodebyte(const char c, size_t *i) 21 { 22 for (*i = 0; *i < (UTF_SIZ + 1); ++(*i)) 23 if (((unsigned char)c & utfmask[*i]) == utfbyte[*i]) 24 return (unsigned char)c & ~utfmask[*i]; 25 return 0; 26 } 27 28 static size_t 29 utf8validate(long *u, size_t i) 30 { 31 if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF)) 32 *u = UTF_INVALID; 33 for (i = 1; *u > utfmax[i]; ++i) 34 ; 35 return i; 36 } 37 38 static size_t 39 utf8decode(const char *c, long *u, size_t clen) 40 { 41 size_t i, j, len, type; 42 long udecoded; 43 44 *u = UTF_INVALID; 45 if (!clen) 46 return 0; 47 udecoded = utf8decodebyte(c[0], &len); 48 if (!BETWEEN(len, 1, UTF_SIZ)) 49 return 1; 50 for (i = 1, j = 1; i < clen && j < len; ++i, ++j) { 51 udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type); 52 if (type) 53 return j; 54 } 55 if (j < len) 56 return 0; 57 *u = udecoded; 58 utf8validate(u, len); 59 60 return len; 61 } 62 63 Drw * 64 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap) 65 { 66 Drw *drw = ecalloc(1, sizeof(Drw)); 67 68 drw->dpy = dpy; 69 drw->screen = screen; 70 drw->root = root; 71 drw->w = w; 72 drw->h = h; 73 drw->visual = visual; 74 drw->depth = depth; 75 drw->cmap = cmap; 76 drw->drawable = XCreatePixmap(dpy, root, w, h, depth); 77 drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL); 78 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); 79 80 return drw; 81 } 82 83 void 84 drw_resize(Drw *drw, unsigned int w, unsigned int h) 85 { 86 if (!drw) 87 return; 88 89 drw->w = w; 90 drw->h = h; 91 if (drw->drawable) 92 XFreePixmap(drw->dpy, drw->drawable); 93 drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth); 94 } 95 96 void 97 drw_free(Drw *drw) 98 { 99 XFreePixmap(drw->dpy, drw->drawable); 100 XFreeGC(drw->dpy, drw->gc); 101 drw_fontset_free(drw->fonts); 102 free(drw); 103 } 104 105 /* This function is an implementation detail. Library users should use 106 * drw_fontset_create instead. 107 */ 108 static Fnt * 109 xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern) 110 { 111 Fnt *font; 112 XftFont *xfont = NULL; 113 FcPattern *pattern = NULL; 114 115 if (fontname) { 116 /* Using the pattern found at font->xfont->pattern does not yield the 117 * same substitution results as using the pattern returned by 118 * FcNameParse; using the latter results in the desired fallback 119 * behaviour whereas the former just results in missing-character 120 * rectangles being drawn, at least with some fonts. */ 121 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) { 122 fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname); 123 return NULL; 124 } 125 if (!(pattern = FcNameParse((FcChar8 *) fontname))) { 126 fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname); 127 XftFontClose(drw->dpy, xfont); 128 return NULL; 129 } 130 } else if (fontpattern) { 131 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) { 132 fprintf(stderr, "error, cannot load font from pattern.\n"); 133 return NULL; 134 } 135 } else { 136 die("no font specified."); 137 } 138 139 font = ecalloc(1, sizeof(Fnt)); 140 font->xfont = xfont; 141 font->pattern = pattern; 142 font->h = xfont->ascent + xfont->descent; 143 font->dpy = drw->dpy; 144 145 return font; 146 } 147 148 static void 149 xfont_free(Fnt *font) 150 { 151 if (!font) 152 return; 153 if (font->pattern) 154 FcPatternDestroy(font->pattern); 155 XftFontClose(font->dpy, font->xfont); 156 free(font); 157 } 158 159 Fnt* 160 drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount) 161 { 162 Fnt *cur, *ret = NULL; 163 size_t i; 164 165 if (!drw || !fonts) 166 return NULL; 167 168 for (i = 1; i <= fontcount; i++) { 169 if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) { 170 cur->next = ret; 171 ret = cur; 172 } 173 } 174 return (drw->fonts = ret); 175 } 176 177 void 178 drw_fontset_free(Fnt *font) 179 { 180 if (font) { 181 drw_fontset_free(font->next); 182 xfont_free(font); 183 } 184 } 185 186 void 187 drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha) 188 { 189 if (!drw || !dest || !clrname) 190 return; 191 192 if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap, 193 clrname, dest)) 194 die("error, cannot allocate color '%s'", clrname); 195 196 dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24); 197 } 198 199 /* Wrapper to create color schemes. The caller has to call free(3) on the 200 * returned color scheme when done using it. */ 201 Clr * 202 drw_scm_create(Drw *drw, char *clrnames[], const unsigned int alphas[], size_t clrcount) 203 { 204 size_t i; 205 Clr *ret; 206 207 /* need at least two colors for a scheme */ 208 if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor)))) 209 return NULL; 210 211 for (i = 0; i < clrcount; i++) 212 drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]); 213 return ret; 214 } 215 216 void 217 drw_setfontset(Drw *drw, Fnt *set) 218 { 219 if (drw) 220 drw->fonts = set; 221 } 222 223 void 224 drw_setscheme(Drw *drw, Clr *scm) 225 { 226 if (drw) 227 drw->scheme = scm; 228 } 229 230 void 231 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert) 232 { 233 if (!drw || !drw->scheme) 234 return; 235 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel); 236 if (filled) 237 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 238 else 239 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1); 240 } 241 242 int 243 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert) 244 { 245 char buf[1024]; 246 int ty; 247 unsigned int ew; 248 XftDraw *d = NULL; 249 Fnt *usedfont, *curfont, *nextfont; 250 size_t i, len; 251 int utf8strlen, utf8charlen, render = x || y || w || h; 252 long utf8codepoint = 0; 253 const char *utf8str; 254 FcCharSet *fccharset; 255 FcPattern *fcpattern; 256 FcPattern *match; 257 XftResult result; 258 int charexists = 0; 259 260 if (!drw || (render && !drw->scheme) || !text || !drw->fonts) 261 return 0; 262 263 if (!render) { 264 w = ~w; 265 } else { 266 XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); 267 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 268 d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap); 269 x += lpad; 270 w -= lpad; 271 } 272 273 usedfont = drw->fonts; 274 while (1) { 275 utf8strlen = 0; 276 utf8str = text; 277 nextfont = NULL; 278 while (*text) { 279 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ); 280 for (curfont = drw->fonts; curfont; curfont = curfont->next) { 281 charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint); 282 if (charexists) { 283 if (curfont == usedfont) { 284 utf8strlen += utf8charlen; 285 text += utf8charlen; 286 } else { 287 nextfont = curfont; 288 } 289 break; 290 } 291 } 292 293 if (!charexists || nextfont) 294 break; 295 else 296 charexists = 0; 297 } 298 299 if (utf8strlen) { 300 drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL); 301 /* shorten text if necessary */ 302 for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--) 303 drw_font_getexts(usedfont, utf8str, len, &ew, NULL); 304 305 if (len) { 306 memcpy(buf, utf8str, len); 307 buf[len] = '\0'; 308 if (len < utf8strlen) 309 for (i = len; i && i > len - 3; buf[--i] = '.') 310 ; /* NOP */ 311 312 if (render) { 313 ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent; 314 XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg], 315 usedfont->xfont, x, ty, (XftChar8 *)buf, len); 316 } 317 x += ew; 318 w -= ew; 319 } 320 } 321 322 if (!*text) { 323 break; 324 } else if (nextfont) { 325 charexists = 0; 326 usedfont = nextfont; 327 } else { 328 /* Regardless of whether or not a fallback font is found, the 329 * character must be drawn. */ 330 charexists = 1; 331 332 fccharset = FcCharSetCreate(); 333 FcCharSetAddChar(fccharset, utf8codepoint); 334 335 if (!drw->fonts->pattern) { 336 /* Refer to the comment in xfont_create for more information. */ 337 die("the first font in the cache must be loaded from a font string."); 338 } 339 340 fcpattern = FcPatternDuplicate(drw->fonts->pattern); 341 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); 342 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue); 343 FcPatternAddBool(fcpattern, FC_COLOR, FcFalse); 344 345 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern); 346 FcDefaultSubstitute(fcpattern); 347 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result); 348 349 FcCharSetDestroy(fccharset); 350 FcPatternDestroy(fcpattern); 351 352 if (match) { 353 usedfont = xfont_create(drw, NULL, match); 354 if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) { 355 for (curfont = drw->fonts; curfont->next; curfont = curfont->next) 356 ; /* NOP */ 357 curfont->next = usedfont; 358 } else { 359 xfont_free(usedfont); 360 usedfont = drw->fonts; 361 } 362 } 363 } 364 } 365 if (d) 366 XftDrawDestroy(d); 367 368 return x + (render ? w : 0); 369 } 370 371 void 372 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) 373 { 374 if (!drw) 375 return; 376 377 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y); 378 XSync(drw->dpy, False); 379 } 380 381 unsigned int 382 drw_fontset_getwidth(Drw *drw, const char *text) 383 { 384 if (!drw || !drw->fonts || !text) 385 return 0; 386 return drw_text(drw, 0, 0, 0, 0, 0, text, 0); 387 } 388 389 void 390 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) 391 { 392 XGlyphInfo ext; 393 394 if (!font || !text) 395 return; 396 397 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext); 398 if (w) 399 *w = ext.xOff; 400 if (h) 401 *h = font->h; 402 } 403 404 Cur * 405 drw_cur_create(Drw *drw, int shape) 406 { 407 Cur *cur; 408 409 if (!drw || !(cur = ecalloc(1, sizeof(Cur)))) 410 return NULL; 411 412 cur->cursor = XCreateFontCursor(drw->dpy, shape); 413 414 return cur; 415 } 416 417 void 418 drw_cur_free(Drw *drw, Cur *cursor) 419 { 420 if (!cursor) 421 return; 422 423 XFreeCursor(drw->dpy, cursor->cursor); 424 free(cursor); 425 }