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