drw.c (11316B)
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 dest->pixel |= 0xff << 24; 181 } 182 183 /* Wrapper to create color schemes. The caller has to call free(3) on the 184 * returned color scheme when done using it. */ 185 Clr * 186 drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) 187 { 188 size_t i; 189 Clr *ret; 190 191 /* need at least two colors for a scheme */ 192 if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor)))) 193 return NULL; 194 195 for (i = 0; i < clrcount; i++) 196 drw_clr_create(drw, &ret[i], clrnames[i]); 197 return ret; 198 } 199 200 void 201 drw_setfontset(Drw *drw, Fnt *set) 202 { 203 if (drw) 204 drw->fonts = set; 205 } 206 207 void 208 drw_setscheme(Drw *drw, Clr *scm) 209 { 210 if (drw) 211 drw->scheme = scm; 212 } 213 214 void 215 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert) 216 { 217 if (!drw || !drw->scheme) 218 return; 219 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel); 220 if (filled) 221 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 222 else 223 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1); 224 } 225 226 int 227 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert) 228 { 229 int ty, ellipsis_x = 0; 230 unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1; 231 XftDraw *d = NULL; 232 Fnt *usedfont, *curfont, *nextfont; 233 int utf8strlen, utf8charlen, utf8err, render = x || y || w || h; 234 long utf8codepoint = 0; 235 const char *utf8str; 236 FcCharSet *fccharset; 237 FcPattern *fcpattern; 238 FcPattern *match; 239 XftResult result; 240 int charexists = 0, overflow = 0; 241 /* keep track of a couple codepoints for which we have no match. */ 242 static unsigned int nomatches[128], ellipsis_width, invalid_width; 243 static const char invalid[] = "�"; 244 245 if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts) 246 return 0; 247 248 if (!render) { 249 w = invert ? invert : ~invert; 250 } else { 251 XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); 252 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 253 if (w < lpad) 254 return x + w; 255 d = XftDrawCreate(drw->dpy, drw->drawable, 256 DefaultVisual(drw->dpy, drw->screen), 257 DefaultColormap(drw->dpy, drw->screen)); 258 x += lpad; 259 w -= lpad; 260 } 261 262 usedfont = drw->fonts; 263 if (!ellipsis_width && render) 264 ellipsis_width = drw_fontset_getwidth(drw, "..."); 265 if (!invalid_width && render) 266 invalid_width = drw_fontset_getwidth(drw, invalid); 267 while (1) { 268 ew = ellipsis_len = utf8err = utf8charlen = utf8strlen = 0; 269 utf8str = text; 270 nextfont = NULL; 271 while (*text) { 272 utf8charlen = utf8decode(text, &utf8codepoint, &utf8err); 273 for (curfont = drw->fonts; curfont; curfont = curfont->next) { 274 charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint); 275 if (charexists) { 276 drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL); 277 if (ew + ellipsis_width <= w) { 278 /* keep track where the ellipsis still fits */ 279 ellipsis_x = x + ew; 280 ellipsis_w = w - ew; 281 ellipsis_len = utf8strlen; 282 } 283 284 if (ew + tmpw > w) { 285 overflow = 1; 286 /* called from drw_fontset_getwidth_clamp(): 287 * it wants the width AFTER the overflow 288 */ 289 if (!render) 290 x += tmpw; 291 else 292 utf8strlen = ellipsis_len; 293 } else if (curfont == usedfont) { 294 text += utf8charlen; 295 utf8strlen += utf8err ? 0 : utf8charlen; 296 ew += utf8err ? 0 : tmpw; 297 } else { 298 nextfont = curfont; 299 } 300 break; 301 } 302 } 303 304 if (overflow || !charexists || nextfont || utf8err) 305 break; 306 else 307 charexists = 0; 308 } 309 310 if (utf8strlen) { 311 if (render) { 312 ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent; 313 XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg], 314 usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen); 315 } 316 x += ew; 317 w -= ew; 318 } 319 if (utf8err && (!render || invalid_width < w)) { 320 if (render) 321 drw_text(drw, x, y, w, h, 0, invalid, invert); 322 x += invalid_width; 323 w -= invalid_width; 324 } 325 if (render && overflow) 326 drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert); 327 328 if (!*text || overflow) { 329 break; 330 } else if (nextfont) { 331 charexists = 0; 332 usedfont = nextfont; 333 } else { 334 /* Regardless of whether or not a fallback font is found, the 335 * character must be drawn. */ 336 charexists = 1; 337 338 hash = (unsigned int)utf8codepoint; 339 hash = ((hash >> 16) ^ hash) * 0x21F0AAAD; 340 hash = ((hash >> 15) ^ hash) * 0xD35A2D97; 341 h0 = ((hash >> 15) ^ hash) % LENGTH(nomatches); 342 h1 = (hash >> 17) % LENGTH(nomatches); 343 /* avoid expensive XftFontMatch call when we know we won't find a match */ 344 if (nomatches[h0] == utf8codepoint || nomatches[h1] == utf8codepoint) 345 goto no_match; 346 347 fccharset = FcCharSetCreate(); 348 FcCharSetAddChar(fccharset, utf8codepoint); 349 350 if (!drw->fonts->pattern) { 351 /* Refer to the comment in xfont_create for more information. */ 352 die("the first font in the cache must be loaded from a font string."); 353 } 354 355 fcpattern = FcPatternDuplicate(drw->fonts->pattern); 356 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); 357 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue); 358 359 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern); 360 FcDefaultSubstitute(fcpattern); 361 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result); 362 363 FcCharSetDestroy(fccharset); 364 FcPatternDestroy(fcpattern); 365 366 if (match) { 367 usedfont = xfont_create(drw, NULL, match); 368 if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) { 369 for (curfont = drw->fonts; curfont->next; curfont = curfont->next) 370 ; /* NOP */ 371 curfont->next = usedfont; 372 } else { 373 xfont_free(usedfont); 374 nomatches[nomatches[h0] ? h1 : h0] = utf8codepoint; 375 no_match: 376 usedfont = drw->fonts; 377 } 378 } 379 } 380 } 381 if (d) 382 XftDrawDestroy(d); 383 384 return x + (render ? w : 0); 385 } 386 387 void 388 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) 389 { 390 if (!drw) 391 return; 392 393 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y); 394 XSync(drw->dpy, False); 395 } 396 397 unsigned int 398 drw_fontset_getwidth(Drw *drw, const char *text) 399 { 400 if (!drw || !drw->fonts || !text) 401 return 0; 402 return drw_text(drw, 0, 0, 0, 0, 0, text, 0); 403 } 404 405 unsigned int 406 drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n) 407 { 408 unsigned int tmp = 0; 409 if (drw && drw->fonts && text && n) 410 tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n); 411 return MIN(n, tmp); 412 } 413 414 void 415 drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h) 416 { 417 XGlyphInfo ext; 418 419 if (!font || !text) 420 return; 421 422 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext); 423 if (w) 424 *w = ext.xOff; 425 if (h) 426 *h = font->h; 427 } 428 429 Cur * 430 drw_cur_create(Drw *drw, int shape) 431 { 432 Cur *cur; 433 434 if (!drw || !(cur = ecalloc(1, sizeof(Cur)))) 435 return NULL; 436 437 cur->cursor = XCreateFontCursor(drw->dpy, shape); 438 439 return cur; 440 } 441 442 void 443 drw_cur_free(Drw *drw, Cur *cursor) 444 { 445 if (!cursor) 446 return; 447 448 XFreeCursor(drw->dpy, cursor->cursor); 449 free(cursor); 450 }