drw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. free(drw);
  88. }
  89. /* This function is an implementation detail. Library users should use
  90. * drw_fontset_create instead.
  91. */
  92. static Fnt *
  93. xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
  94. {
  95. Fnt *font;
  96. XftFont *xfont = NULL;
  97. FcPattern *pattern = NULL;
  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. /* Do not allow using color fonts. This is a workaround for a BadLength
  122. * error from Xft with color glyphs. Modelled on the Xterm workaround. See
  123. * https://bugzilla.redhat.com/show_bug.cgi?id=1498269
  124. * https://lists.suckless.org/dev/1701/30932.html
  125. * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916349
  126. * and lots more all over the internet.
  127. */
  128. FcBool iscol;
  129. if(FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) {
  130. XftFontClose(drw->dpy, xfont);
  131. return NULL;
  132. }
  133. font = ecalloc(1, sizeof(Fnt));
  134. font->xfont = xfont;
  135. font->pattern = pattern;
  136. font->h = xfont->ascent + xfont->descent;
  137. font->dpy = drw->dpy;
  138. return font;
  139. }
  140. static void
  141. xfont_free(Fnt *font)
  142. {
  143. if (!font)
  144. return;
  145. if (font->pattern)
  146. FcPatternDestroy(font->pattern);
  147. XftFontClose(font->dpy, font->xfont);
  148. free(font);
  149. }
  150. Fnt*
  151. drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
  152. {
  153. Fnt *cur, *ret = NULL;
  154. size_t i;
  155. if (!drw || !fonts)
  156. return NULL;
  157. for (i = 1; i <= fontcount; i++) {
  158. if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
  159. cur->next = ret;
  160. ret = cur;
  161. }
  162. }
  163. return (drw->fonts = ret);
  164. }
  165. void
  166. drw_fontset_free(Fnt *font)
  167. {
  168. if (font) {
  169. drw_fontset_free(font->next);
  170. xfont_free(font);
  171. }
  172. }
  173. void
  174. drw_clr_create(Drw *drw, Clr *dest, char *clrname, unsigned int alpha)
  175. {
  176. if (!drw || !dest || !clrname)
  177. return;
  178. if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap,
  179. clrname, dest))
  180. die("error, cannot allocate color '%s'", clrname);
  181. dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24);
  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, char *clrnames[], const unsigned int alphas[], size_t clrcount)
  187. {
  188. size_t i;
  189. Clr *ret;
  190. /* need at least two colors for a scheme */
  191. if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
  192. return NULL;
  193. for (i = 0; i < clrcount; i++)
  194. drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]);
  195. return ret;
  196. }
  197. void
  198. drw_setfontset(Drw *drw, Fnt *set)
  199. {
  200. if (drw)
  201. drw->fonts = set;
  202. }
  203. void
  204. drw_setscheme(Drw *drw, Clr *scm)
  205. {
  206. if (drw)
  207. drw->scheme = scm;
  208. }
  209. void
  210. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
  211. {
  212. if (!drw || !drw->scheme)
  213. return;
  214. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
  215. if (filled)
  216. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  217. else
  218. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
  219. }
  220. int
  221. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
  222. {
  223. char buf[1024];
  224. int ty;
  225. unsigned int ew;
  226. XftDraw *d = NULL;
  227. Fnt *usedfont, *curfont, *nextfont;
  228. size_t i, len;
  229. int utf8strlen, utf8charlen, render = x || y || w || h;
  230. long utf8codepoint = 0;
  231. const char *utf8str;
  232. FcCharSet *fccharset;
  233. FcPattern *fcpattern;
  234. FcPattern *match;
  235. XftResult result;
  236. int charexists = 0;
  237. if (!drw || (render && !drw->scheme) || !text || !drw->fonts)
  238. return 0;
  239. if (!render) {
  240. w = ~w;
  241. } else {
  242. XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
  243. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  244. d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
  245. x += lpad;
  246. w -= lpad;
  247. }
  248. usedfont = drw->fonts;
  249. while (1) {
  250. utf8strlen = 0;
  251. utf8str = text;
  252. nextfont = NULL;
  253. while (*text) {
  254. utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
  255. for (curfont = drw->fonts; curfont; curfont = curfont->next) {
  256. charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
  257. if (charexists) {
  258. if (curfont == usedfont) {
  259. utf8strlen += utf8charlen;
  260. text += utf8charlen;
  261. } else {
  262. nextfont = curfont;
  263. }
  264. break;
  265. }
  266. }
  267. if (!charexists || nextfont)
  268. break;
  269. else
  270. charexists = 0;
  271. }
  272. if (utf8strlen) {
  273. drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
  274. /* shorten text if necessary */
  275. for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
  276. drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
  277. if (len) {
  278. memcpy(buf, utf8str, len);
  279. buf[len] = '\0';
  280. if (len < utf8strlen)
  281. for (i = len; i && i > len - 3; buf[--i] = '.')
  282. ; /* NOP */
  283. if (render) {
  284. ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
  285. XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
  286. usedfont->xfont, x, ty, (XftChar8 *)buf, len);
  287. }
  288. x += ew;
  289. w -= ew;
  290. }
  291. }
  292. if (!*text) {
  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. fccharset = FcCharSetCreate();
  302. FcCharSetAddChar(fccharset, utf8codepoint);
  303. if (!drw->fonts->pattern) {
  304. /* Refer to the comment in xfont_create for more information. */
  305. die("the first font in the cache must be loaded from a font string.");
  306. }
  307. fcpattern = FcPatternDuplicate(drw->fonts->pattern);
  308. FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  309. FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  310. FcPatternAddBool(fcpattern, FC_COLOR, FcFalse);
  311. FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  312. FcDefaultSubstitute(fcpattern);
  313. match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  314. FcCharSetDestroy(fccharset);
  315. FcPatternDestroy(fcpattern);
  316. if (match) {
  317. usedfont = xfont_create(drw, NULL, match);
  318. if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
  319. for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
  320. ; /* NOP */
  321. curfont->next = usedfont;
  322. } else {
  323. xfont_free(usedfont);
  324. usedfont = drw->fonts;
  325. }
  326. }
  327. }
  328. }
  329. if (d)
  330. XftDrawDestroy(d);
  331. return x + (render ? w : 0);
  332. }
  333. void
  334. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  335. {
  336. if (!drw)
  337. return;
  338. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  339. XSync(drw->dpy, False);
  340. }
  341. unsigned int
  342. drw_fontset_getwidth(Drw *drw, const char *text)
  343. {
  344. if (!drw || !drw->fonts || !text)
  345. return 0;
  346. return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
  347. }
  348. void
  349. drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
  350. {
  351. XGlyphInfo ext;
  352. if (!font || !text)
  353. return;
  354. XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  355. if (w)
  356. *w = ext.xOff;
  357. if (h)
  358. *h = font->h;
  359. }
  360. Cur *
  361. drw_cur_create(Drw *drw, int shape)
  362. {
  363. Cur *cur;
  364. if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
  365. return NULL;
  366. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  367. return cur;
  368. }
  369. void
  370. drw_cur_free(Drw *drw, Cur *cursor)
  371. {
  372. if (!cursor)
  373. return;
  374. XFreeCursor(drw->dpy, cursor->cursor);
  375. free(cursor);
  376. }