1 /* See LICENSE file for copyright and license details. */
6 #include <X11/Xft/Xft.h>
11 #define UTF_INVALID 0xFFFD
14 static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
15 static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
16 static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
17 static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
20 utf8decodebyte(const char c, size_t *i)
22 for (*i = 0; *i < (UTF_SIZ + 1); ++(*i))
23 if (((unsigned char)c & utfmask[*i]) == utfbyte[*i])
24 return (unsigned char)c & ~utfmask[*i];
29 utf8validate(long *u, size_t i)
31 if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
33 for (i = 1; *u > utfmax[i]; ++i)
39 utf8decode(const char *c, long *u, size_t clen)
41 size_t i, j, len, type;
47 udecoded = utf8decodebyte(c[0], &len);
48 if (!BETWEEN(len, 1, UTF_SIZ))
50 for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
51 udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
64 drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
68 drw = ecalloc(1, sizeof(Drw));
74 drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
75 drw->gc = XCreateGC(dpy, root, 0, NULL);
77 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
83 drw_resize(Drw *drw, unsigned int w, unsigned int h)
88 XFreePixmap(drw->dpy, drw->drawable);
89 drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
97 for (i = 0; i < drw->fontcount; i++)
98 drw_font_free(drw->fonts[i]);
99 XFreePixmap(drw->dpy, drw->drawable);
100 XFreeGC(drw->dpy, drw->gc);
104 /* This function is an implementation detail. Library users should use
105 * drw_font_create instead.
108 drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern)
111 XftFont *xfont = NULL;
112 FcPattern *pattern = NULL;
115 /* Using the pattern found at font->xfont->pattern does not yield same
116 * the same substitution results as using the pattern returned by
117 * FcNameParse; using the latter results in the desired fallback
118 * behaviour whereas the former just results in
119 * missing-character-rectangles being drawn, at least with some fonts.
121 if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
122 fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
125 if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
126 fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
127 XftFontClose(drw->dpy, xfont);
130 } else if (fontpattern) {
131 if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
132 fprintf(stderr, "error, cannot load font pattern.\n");
136 die("no font specified.\n");
139 font = ecalloc(1, sizeof(Fnt));
141 font->pattern = pattern;
142 font->ascent = xfont->ascent;
143 font->descent = xfont->descent;
144 font->h = font->ascent + font->descent;
145 font->dpy = drw->dpy;
151 drw_font_create(Drw *drw, const char *fontname)
153 return drw_font_xcreate(drw, fontname, NULL);
157 drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount)
162 for (i = 0; i < fontcount; i++) {
163 if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
164 die("font cache exhausted.\n");
165 } else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) {
166 drw->fonts[drw->fontcount++] = font;
172 drw_font_free(Fnt *font)
177 FcPatternDestroy(font->pattern);
178 XftFontClose(font->dpy, font->xfont);
183 drw_clr_create(Drw *drw, const char *clrname)
187 clr = ecalloc(1, sizeof(Clr));
188 if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
189 DefaultColormap(drw->dpy, drw->screen),
191 die("error, cannot allocate color '%s'\n", clrname);
192 clr->pix = clr->rgb.pixel;
198 drw_clr_free(Clr *clr)
204 drw_setscheme(Drw *drw, ClrScheme *scheme)
206 drw->scheme = scheme;
210 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert)
214 XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix);
216 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1);
218 XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
222 drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert)
228 Fnt *curfont, *nextfont;
230 int utf8strlen, utf8charlen, render;
231 long utf8codepoint = 0;
233 FcCharSet *fccharset;
234 FcPattern *fcpattern;
239 if (!drw->scheme || !drw->fontcount)
242 if (!(render = x || y || w || h)) {
245 XSetForeground(drw->dpy, drw->gc, invert ?
246 drw->scheme->fg->pix : drw->scheme->bg->pix);
247 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
248 d = XftDrawCreate(drw->dpy, drw->drawable,
249 DefaultVisual(drw->dpy, drw->screen),
250 DefaultColormap(drw->dpy, drw->screen));
253 curfont = drw->fonts[0];
259 utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
260 for (i = 0; i < drw->fontcount; i++) {
261 charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint);
263 if (drw->fonts[i] == curfont) {
264 utf8strlen += utf8charlen;
267 nextfont = drw->fonts[i];
273 if (!charexists || (nextfont && nextfont != curfont))
280 drw_font_getexts(curfont, utf8str, utf8strlen, &tex);
281 /* shorten text if necessary */
282 for (len = MIN(utf8strlen, (sizeof buf) - 1); len && (tex.w > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--)
283 drw_font_getexts(curfont, utf8str, len, &tex);
286 memcpy(buf, utf8str, len);
288 if (len < utf8strlen)
289 for (i = len; i && i > len - 3; buf[--i] = '.');
292 th = curfont->ascent + curfont->descent;
293 ty = y + (h / 2) - (th / 2) + curfont->ascent;
295 XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
304 } else if (nextfont) {
308 /* Regardless of whether or not a fallback font is found, the
309 * character must be drawn.
313 if (drw->fontcount >= DRW_FONT_CACHE_SIZE)
316 fccharset = FcCharSetCreate();
317 FcCharSetAddChar(fccharset, utf8codepoint);
319 if (!drw->fonts[0]->pattern) {
320 /* Refer to the comment in drw_font_xcreate for more
322 die("the first font in the cache must be loaded from a font string.\n");
325 fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern);
326 FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
327 FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
329 FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
330 FcDefaultSubstitute(fcpattern);
331 match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
333 FcCharSetDestroy(fccharset);
334 FcPatternDestroy(fcpattern);
337 curfont = drw_font_xcreate(drw, NULL, match);
338 if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) {
339 drw->fonts[drw->fontcount++] = curfont;
341 drw_font_free(curfont);
342 curfont = drw->fonts[0];
354 drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
356 XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
357 XSync(drw->dpy, False);
361 drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex)
365 XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
371 drw_font_getexts_width(Fnt *font, const char *text, unsigned int len)
375 drw_font_getexts(font, text, len, &tex);
381 drw_cur_create(Drw *drw, int shape)
385 cur = ecalloc(1, sizeof(Cur));
386 cur->cursor = XCreateFontCursor(drw->dpy, shape);
392 drw_cur_free(Drw *drw, Cur *cursor)
396 XFreeCursor(drw->dpy, cursor->cursor);