drw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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;
  58. drw = ecalloc(1, sizeof(Drw));
  59. drw->dpy = dpy;
  60. drw->screen = screen;
  61. drw->root = root;
  62. drw->w = w;
  63. drw->h = h;
  64. drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
  65. drw->gc = XCreateGC(dpy, root, 0, NULL);
  66. drw->fontcount = 0;
  67. XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  68. return drw;
  69. }
  70. void
  71. drw_resize(Drw *drw, unsigned int w, unsigned int h)
  72. {
  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. size_t i;
  83. for (i = 0; i < drw->fontcount; i++)
  84. drw_font_free(drw->fonts[i]);
  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_font_create instead.
  91. */
  92. static Fnt *
  93. drw_font_xcreate(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 same
  100. * the 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
  103. * missing-character-rectangles being drawn, at least with some fonts.
  104. */
  105. if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
  106. fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
  107. return NULL;
  108. }
  109. if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
  110. fprintf(stderr, "error, cannot load font: '%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 pattern.\n");
  117. return NULL;
  118. }
  119. } else {
  120. die("no font specified.\n");
  121. }
  122. font = ecalloc(1, sizeof(Fnt));
  123. font->xfont = xfont;
  124. font->pattern = pattern;
  125. font->ascent = xfont->ascent;
  126. font->descent = xfont->descent;
  127. font->h = font->ascent + font->descent;
  128. font->dpy = drw->dpy;
  129. return font;
  130. }
  131. Fnt*
  132. drw_font_create(Drw *drw, const char *fontname)
  133. {
  134. return drw_font_xcreate(drw, fontname, NULL);
  135. }
  136. void
  137. drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount)
  138. {
  139. size_t i;
  140. Fnt *font;
  141. for (i = 0; i < fontcount; i++) {
  142. if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
  143. die("font cache exhausted.\n");
  144. } else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) {
  145. drw->fonts[drw->fontcount++] = font;
  146. }
  147. }
  148. }
  149. void
  150. drw_font_free(Fnt *font)
  151. {
  152. if (!font)
  153. return;
  154. if (font->pattern)
  155. FcPatternDestroy(font->pattern);
  156. XftFontClose(font->dpy, font->xfont);
  157. free(font);
  158. }
  159. Clr *
  160. drw_clr_create(Drw *drw, const char *clrname)
  161. {
  162. Clr *clr;
  163. clr = ecalloc(1, sizeof(Clr));
  164. if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
  165. DefaultColormap(drw->dpy, drw->screen),
  166. clrname, &clr->rgb))
  167. die("error, cannot allocate color '%s'\n", clrname);
  168. clr->pix = clr->rgb.pixel;
  169. return clr;
  170. }
  171. void
  172. drw_clr_free(Clr *clr)
  173. {
  174. free(clr);
  175. }
  176. void
  177. drw_setscheme(Drw *drw, ClrScheme *scheme)
  178. {
  179. drw->scheme = scheme;
  180. }
  181. int
  182. drw_get_width(Drw *drw, int numcolors, const char *text)
  183. {
  184. int i;
  185. Fnt *curfont = drw->fonts[0];
  186. int w = drw_text(drw, 0, 0, 0, 0, text, 0) + curfont->h;
  187. for (i = 0; i < strlen(text); i++) {
  188. if (text[i] > 0 && text[i] <= numcolors) {
  189. /* we found a color code
  190. * drw_text counted it as a normal character and added one character's width
  191. * we aren't going to render this character, so we remove one character's width */
  192. w -= curfont->xfont->max_advance_width;
  193. if (i == 0 || i + 1 == strlen(text)) {
  194. /* we're on the first or the last character of the string
  195. * drw_text already added one character's height (divided by 2) as padding to the beginning and end
  196. * we don't want to double this padding, so we skip this character */
  197. continue;
  198. }
  199. if (text[i - 1] > 0 && text[i - 1] <= numcolors) {
  200. /* the previous character was also a color code
  201. * we already added padding in the previous iteration
  202. * we don't want to double this padding, so we skip this character */
  203. continue;
  204. }
  205. /* we are somewhere in the middle of the string and the color has changed
  206. * we want to add one character's height (divided by 2) as padding to the end of the previous colored text
  207. * and to the beginning of the new colored text */
  208. w += curfont->h;
  209. }
  210. }
  211. return w;
  212. }
  213. void
  214. drw_colored_text(Drw *drw, ClrScheme *scheme, int numcolors, int x, int y, unsigned int w, unsigned int h, char *text)
  215. {
  216. if (!drw || !drw->fontcount || !drw->scheme)
  217. return;
  218. char *buf = text, *ptr = buf, c = 1;
  219. int i;
  220. while (*ptr) {
  221. for (i = 0; *ptr < 0 || *ptr > numcolors; i++, ptr++);
  222. if (!*ptr)
  223. break;
  224. c = *ptr;
  225. *ptr = 0;
  226. if (i)
  227. x = drw_text(drw, x, y, w, h, buf, 0) + drw->fonts[0]->h;
  228. *ptr = c;
  229. drw_setscheme(drw, &scheme[c-1]);
  230. buf = ++ptr;
  231. }
  232. drw_text(drw, x, y, w, h, buf, 0);
  233. }
  234. void
  235. drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert)
  236. {
  237. if (!drw->scheme)
  238. return;
  239. XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix);
  240. if (filled)
  241. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1);
  242. else if (empty)
  243. XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  244. }
  245. int
  246. drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert)
  247. {
  248. char buf[1024];
  249. int tx, ty, th;
  250. Extnts tex;
  251. XftDraw *d = NULL;
  252. Fnt *curfont, *nextfont;
  253. size_t i, len;
  254. int utf8strlen, utf8charlen, render;
  255. long utf8codepoint = 0;
  256. const char *utf8str;
  257. FcCharSet *fccharset;
  258. FcPattern *fcpattern;
  259. FcPattern *match;
  260. XftResult result;
  261. int charexists = 0;
  262. if (!drw->scheme || !drw->fontcount)
  263. return 0;
  264. if (!(render = x || y || w || h)) {
  265. w = ~w;
  266. } else {
  267. XSetForeground(drw->dpy, drw->gc, invert ?
  268. drw->scheme->fg->pix : drw->scheme->bg->pix);
  269. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  270. d = XftDrawCreate(drw->dpy, drw->drawable,
  271. DefaultVisual(drw->dpy, drw->screen),
  272. DefaultColormap(drw->dpy, drw->screen));
  273. }
  274. curfont = drw->fonts[0];
  275. while (1) {
  276. utf8strlen = 0;
  277. utf8str = text;
  278. nextfont = NULL;
  279. while (*text) {
  280. utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
  281. for (i = 0; i < drw->fontcount; i++) {
  282. charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint);
  283. if (charexists) {
  284. if (drw->fonts[i] == curfont) {
  285. utf8strlen += utf8charlen;
  286. text += utf8charlen;
  287. } else {
  288. nextfont = drw->fonts[i];
  289. }
  290. break;
  291. }
  292. }
  293. if (!charexists || (nextfont && nextfont != curfont))
  294. break;
  295. else
  296. charexists = 0;
  297. }
  298. if (utf8strlen) {
  299. drw_font_getexts(curfont, utf8str, utf8strlen, &tex);
  300. /* shorten text if necessary */
  301. for (len = MIN(utf8strlen, (sizeof buf) - 1); len && (tex.w > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--)
  302. drw_font_getexts(curfont, utf8str, len, &tex);
  303. if (len) {
  304. memcpy(buf, utf8str, len);
  305. buf[len] = '\0';
  306. if (len < utf8strlen)
  307. for (i = len; i && i > len - 3; buf[--i] = '.');
  308. if (render) {
  309. th = curfont->ascent + curfont->descent;
  310. ty = y + (h / 2) - (th / 2) + curfont->ascent;
  311. tx = x + (h / 2);
  312. XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
  313. }
  314. x += tex.w;
  315. w -= tex.w;
  316. }
  317. }
  318. if (!*text) {
  319. break;
  320. } else if (nextfont) {
  321. charexists = 0;
  322. curfont = nextfont;
  323. } else {
  324. /* Regardless of whether or not a fallback font is found, the
  325. * character must be drawn.
  326. */
  327. charexists = 1;
  328. if (drw->fontcount >= DRW_FONT_CACHE_SIZE)
  329. continue;
  330. fccharset = FcCharSetCreate();
  331. FcCharSetAddChar(fccharset, utf8codepoint);
  332. if (!drw->fonts[0]->pattern) {
  333. /* Refer to the comment in drw_font_xcreate for more
  334. * information. */
  335. die("the first font in the cache must be loaded from a font string.\n");
  336. }
  337. fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern);
  338. FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  339. FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  340. FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  341. FcDefaultSubstitute(fcpattern);
  342. match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  343. FcCharSetDestroy(fccharset);
  344. FcPatternDestroy(fcpattern);
  345. if (match) {
  346. curfont = drw_font_xcreate(drw, NULL, match);
  347. if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) {
  348. drw->fonts[drw->fontcount++] = curfont;
  349. } else {
  350. drw_font_free(curfont);
  351. curfont = drw->fonts[0];
  352. }
  353. }
  354. }
  355. }
  356. if (d)
  357. XftDrawDestroy(d);
  358. return x;
  359. }
  360. void
  361. drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  362. {
  363. XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  364. XSync(drw->dpy, False);
  365. }
  366. void
  367. drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex)
  368. {
  369. XGlyphInfo ext;
  370. XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  371. tex->h = font->h;
  372. tex->w = ext.xOff;
  373. }
  374. unsigned int
  375. drw_font_getexts_width(Fnt *font, const char *text, unsigned int len)
  376. {
  377. Extnts tex;
  378. drw_font_getexts(font, text, len, &tex);
  379. return tex.w;
  380. }
  381. Cur *
  382. drw_cur_create(Drw *drw, int shape)
  383. {
  384. Cur *cur;
  385. cur = ecalloc(1, sizeof(Cur));
  386. cur->cursor = XCreateFontCursor(drw->dpy, shape);
  387. return cur;
  388. }
  389. void
  390. drw_cur_free(Drw *drw, Cur *cursor)
  391. {
  392. if (!cursor)
  393. return;
  394. XFreeCursor(drw->dpy, cursor->cursor);
  395. free(cursor);
  396. }