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
, Visual
*visual
, unsigned int depth
, Colormap cmap
)
68 drw
= ecalloc(1, sizeof(Drw
));
77 drw
->drawable
= XCreatePixmap(dpy
, root
, w
, h
, depth
);
78 drw
->gc
= XCreateGC(dpy
, drw
->drawable
, 0, NULL
);
80 XSetLineAttributes(dpy
, drw
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
86 drw_resize(Drw
*drw
, unsigned int w
, unsigned int h
)
91 XFreePixmap(drw
->dpy
, drw
->drawable
);
92 drw
->drawable
= XCreatePixmap(drw
->dpy
, drw
->root
, w
, h
, drw
->depth
);
100 for (i
= 0; i
< drw
->fontcount
; i
++)
101 drw_font_free(drw
->fonts
[i
]);
102 XFreePixmap(drw
->dpy
, drw
->drawable
);
103 XFreeGC(drw
->dpy
, drw
->gc
);
107 /* This function is an implementation detail. Library users should use
108 * drw_font_create instead.
111 drw_font_xcreate(Drw
*drw
, const char *fontname
, FcPattern
*fontpattern
)
114 XftFont
*xfont
= NULL
;
115 FcPattern
*pattern
= NULL
;
118 /* Using the pattern found at font->xfont->pattern does not yield same
119 * the same substitution results as using the pattern returned by
120 * FcNameParse; using the latter results in the desired fallback
121 * behaviour whereas the former just results in
122 * missing-character-rectangles being drawn, at least with some fonts.
124 if (!(xfont
= XftFontOpenName(drw
->dpy
, drw
->screen
, fontname
))) {
125 fprintf(stderr
, "error, cannot load font: '%s'\n", fontname
);
128 if (!(pattern
= FcNameParse((FcChar8
*) fontname
))) {
129 fprintf(stderr
, "error, cannot load font: '%s'\n", fontname
);
130 XftFontClose(drw
->dpy
, xfont
);
133 } else if (fontpattern
) {
134 if (!(xfont
= XftFontOpenPattern(drw
->dpy
, fontpattern
))) {
135 fprintf(stderr
, "error, cannot load font pattern.\n");
139 die("no font specified.\n");
142 font
= ecalloc(1, sizeof(Fnt
));
144 font
->pattern
= pattern
;
145 font
->ascent
= xfont
->ascent
;
146 font
->descent
= xfont
->descent
;
147 font
->h
= font
->ascent
+ font
->descent
;
148 font
->dpy
= drw
->dpy
;
154 drw_font_create(Drw
*drw
, const char *fontname
)
156 return drw_font_xcreate(drw
, fontname
, NULL
);
160 drw_load_fonts(Drw
* drw
, const char *fonts
[], size_t fontcount
)
165 for (i
= 0; i
< fontcount
; i
++) {
166 if (drw
->fontcount
>= DRW_FONT_CACHE_SIZE
) {
167 die("font cache exhausted.\n");
168 } else if ((font
= drw_font_xcreate(drw
, fonts
[i
], NULL
))) {
169 drw
->fonts
[drw
->fontcount
++] = font
;
175 drw_font_free(Fnt
*font
)
180 FcPatternDestroy(font
->pattern
);
181 XftFontClose(font
->dpy
, font
->xfont
);
186 drw_clr_create(Drw
*drw
, const char *clrname
, unsigned int alpha
)
190 clr
= ecalloc(1, sizeof(Clr
));
191 if (!XftColorAllocName(drw
->dpy
, drw
->visual
, drw
->cmap
,
193 die("error, cannot allocate color '%s'\n", clrname
);
194 clr
->pix
= (clr
->rgb
.pixel
& 0x00ffffffU
) | (alpha
<< 24);
200 drw_clr_free(Clr
*clr
)
206 drw_setscheme(Drw
*drw
, ClrScheme
*scheme
)
208 drw
->scheme
= scheme
;
212 drw_rect(Drw
*drw
, int x
, int y
, unsigned int w
, unsigned int h
, int filled
, int empty
, int invert
)
216 XSetForeground(drw
->dpy
, drw
->gc
, invert
? drw
->scheme
->bg
->pix
: drw
->scheme
->fg
->pix
);
218 XFillRectangle(drw
->dpy
, drw
->drawable
, drw
->gc
, x
, y
, w
+ 1, h
+ 1);
220 XDrawRectangle(drw
->dpy
, drw
->drawable
, drw
->gc
, x
, y
, w
, h
);
224 drw_text(Drw
*drw
, int x
, int y
, unsigned int w
, unsigned int h
, const char *text
, int invert
)
230 Fnt
*curfont
, *nextfont
;
232 int utf8strlen
, utf8charlen
, render
;
233 long utf8codepoint
= 0;
235 FcCharSet
*fccharset
;
236 FcPattern
*fcpattern
;
241 if (!drw
->scheme
|| !drw
->fontcount
)
244 if (!(render
= x
|| y
|| w
|| h
)) {
247 XSetForeground(drw
->dpy
, drw
->gc
, invert
?
248 drw
->scheme
->fg
->pix
: drw
->scheme
->bg
->pix
);
249 XFillRectangle(drw
->dpy
, drw
->drawable
, drw
->gc
, x
, y
, w
, h
);
250 d
= XftDrawCreate(drw
->dpy
, drw
->drawable
, drw
->visual
, drw
->cmap
);
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
);