drw.c 11 KB

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