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