drw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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)
  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->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
  64. drw->gc = XCreateGC(dpy, root, 0, NULL);
  65. XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  66. return drw;
  67. }
  68. void
  69. drw_resize(Drw *drw, unsigned int w, unsigned int h)
  70. {
  71. if (!drw)
  72. return;
  73. drw->w = w;
  74. drw->h = h;
  75. if (drw->drawable)
  76. XFreePixmap(drw->dpy, drw->drawable);
  77. drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
  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. /* This function is an implementation detail. Library users should use
  88. * drw_fontset_create instead.
  89. */
  90. static Fnt *
  91. xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
  92. {
  93. Fnt *font;
  94. XftFont *xfont = NULL;
  95. FcPattern *pattern = NULL;
  96. if (fontname) {
  97. /* Using the pattern found at font->xfont->pattern does not yield the
  98. * same substitution results as using the pattern returned by
  99. * FcNameParse; using the latter results in the desired fallback
  100. * behaviour whereas the former just results in missing-character
  101. * rectangles being drawn, at least with some fonts. */
  102. if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
  103. fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
  104. return NULL;
  105. }
  106. if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
  107. fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
  108. XftFontClose(drw->dpy, xfont);
  109. return NULL;
  110. }
  111. } else if (fontpattern) {
  112. if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
  113. fprintf(stderr, "error, cannot load font from pattern.\n");
  114. return NULL;
  115. }
  116. } else {
  117. die("no font specified.");
  118. }
  119. font = ecalloc(1, sizeof(Fnt));
  120. font->xfont = xfont;
  121. font->pattern = pattern;
  122. font->h = xfont->ascent + xfont->descent;
  123. font->dpy = drw->dpy;
  124. return font;
  125. }
  126. static void
  127. xfont_free(Fnt *font)
  128. {
  129. if (!font)
  130. return;
  131. if (font->pattern)
  132. FcPatternDestroy(font->pattern);
  133. XftFontClose(font->dpy, font->xfont);
  134. free(font);
  135. }
  136. Fnt*
  137. drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
  138. {
  139. Fnt *cur, *ret = NULL;
  140. size_t i;
  141. if (!drw || !fonts)
  142. return NULL;
  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. void
  152. drw_fontset_free(Fnt *font)
  153. {
  154. if (font) {
  155. drw_fontset_free(font->next);
  156. xfont_free(font);
  157. }
  158. }
  159. void
  160. drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
  161. {
  162. if (!drw || !dest || !clrname)
  163. return;
  164. if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
  165. DefaultColormap(drw->dpy, drw->screen),
  166. clrname, dest))
  167. die("error, cannot allocate color '%s'", clrname);
  168. }
  169. /* Wrapper to create color schemes. The caller has to call free(3) on the
  170. * returned color scheme when done using it. */
  171. Clr *
  172. drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
  173. {
  174. size_t i;
  175. Clr *ret;
  176. /* need at least two colors for a scheme */
  177. if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
  178. return NULL;
  179. for (i = 0; i < clrcount; i++)
  180. drw_clr_create(drw, &ret[i], clrnames[i]);
  181. return ret;
  182. }
  183. void
  184. drw_setfontset(Drw *drw, Fnt *set)
  185. {
  186. if (drw)
  187. drw->fonts = set;
  188. }
  189. void
  190. drw_setscheme(Drw *drw, Clr *scm)
  191. {
  192. if (drw)
  193. drw->scheme = scm;
  194. }
  195. void
  196. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
  197. {
  198. if (!drw || !drw->scheme)
  199. return;
  200. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
  201. if (filled)
  202. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  203. else
  204. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
  205. }
  206. int
  207. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
  208. {
  209. int i, ty, ellipsis_x = 0;
  210. unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len;
  211. XftDraw *d = NULL;
  212. Fnt *usedfont, *curfont, *nextfont;
  213. int utf8strlen, utf8charlen, render = x || y || w || h;
  214. long utf8codepoint = 0;
  215. const char *utf8str;
  216. FcCharSet *fccharset;
  217. FcPattern *fcpattern;
  218. FcPattern *match;
  219. XftResult result;
  220. int charexists = 0, overflow = 0;
  221. /* keep track of a couple codepoints for which we have no match. */
  222. enum { nomatches_len = 64 };
  223. static struct { long codepoint[nomatches_len]; unsigned int idx; } nomatches;
  224. static unsigned int ellipsis_width = 0;
  225. if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
  226. return 0;
  227. if (!render) {
  228. w = invert ? invert : ~invert;
  229. } else {
  230. XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
  231. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  232. d = XftDrawCreate(drw->dpy, drw->drawable,
  233. DefaultVisual(drw->dpy, drw->screen),
  234. DefaultColormap(drw->dpy, drw->screen));
  235. x += lpad;
  236. w -= lpad;
  237. }
  238. usedfont = drw->fonts;
  239. if (!ellipsis_width && render)
  240. ellipsis_width = drw_fontset_getwidth(drw, "...");
  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)
  291. drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", 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. for (i = 0; i < nomatches_len; ++i) {
  302. /* avoid calling XftFontMatch if we know we won't find a match */
  303. if (utf8codepoint == nomatches.codepoint[i])
  304. goto no_match;
  305. }
  306. fccharset = FcCharSetCreate();
  307. FcCharSetAddChar(fccharset, utf8codepoint);
  308. if (!drw->fonts->pattern) {
  309. /* Refer to the comment in xfont_create for more information. */
  310. die("the first font in the cache must be loaded from a font string.");
  311. }
  312. fcpattern = FcPatternDuplicate(drw->fonts->pattern);
  313. FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  314. FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  315. FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  316. FcDefaultSubstitute(fcpattern);
  317. match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  318. FcCharSetDestroy(fccharset);
  319. FcPatternDestroy(fcpattern);
  320. if (match) {
  321. usedfont = xfont_create(drw, NULL, match);
  322. if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
  323. for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
  324. ; /* NOP */
  325. curfont->next = usedfont;
  326. } else {
  327. xfont_free(usedfont);
  328. nomatches.codepoint[++nomatches.idx % nomatches_len] = utf8codepoint;
  329. no_match:
  330. usedfont = drw->fonts;
  331. }
  332. }
  333. }
  334. }
  335. if (d)
  336. XftDrawDestroy(d);
  337. return x + (render ? w : 0);
  338. }
  339. void
  340. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  341. {
  342. if (!drw)
  343. return;
  344. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  345. XSync(drw->dpy, False);
  346. }
  347. unsigned int
  348. drw_fontset_getwidth(Drw *drw, const char *text)
  349. {
  350. if (!drw || !drw->fonts || !text)
  351. return 0;
  352. return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
  353. }
  354. unsigned int
  355. drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n)
  356. {
  357. unsigned int tmp = 0;
  358. if (drw && drw->fonts && text && n)
  359. tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n);
  360. return MIN(n, tmp);
  361. }
  362. void
  363. drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
  364. {
  365. XGlyphInfo ext;
  366. if (!font || !text)
  367. return;
  368. XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  369. if (w)
  370. *w = ext.xOff;
  371. if (h)
  372. *h = font->h;
  373. }
  374. Cur *
  375. drw_cur_create(Drw *drw, int shape)
  376. {
  377. Cur *cur;
  378. if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
  379. return NULL;
  380. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  381. return cur;
  382. }
  383. void
  384. drw_cur_free(Drw *drw, Cur *cursor)
  385. {
  386. if (!cursor)
  387. return;
  388. XFreeCursor(drw->dpy, cursor->cursor);
  389. free(cursor);
  390. }