drw.c 11 KB

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