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