1 /* See LICENSE file for copyright and license details. */
6 #include <X11/Xft/Xft.h>
11 #define UTF_INVALID 0xFFFD
14 utf8decode(const char *s_in
, long *u
, int *err
)
16 static const unsigned char lens
[] = {
17 /* 0XXXX */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18 /* 10XXX */ 0, 0, 0, 0, 0, 0, 0, 0, /* invalid */
19 /* 110XX */ 2, 2, 2, 2,
22 /* 11111 */ 0, /* invalid */
24 static const unsigned char leading_mask
[] = { 0x7F, 0x1F, 0x0F, 0x07 };
25 static const unsigned int overlong
[] = { 0x0, 0x80, 0x0800, 0x10000 };
27 const unsigned char *s
= (const unsigned char *)s_in
;
28 int len
= lens
[*s
>> 3];
34 long cp
= s
[0] & leading_mask
[len
- 1];
35 for (int i
= 1; i
< len
; ++i
) {
36 if (s
[i
] == '\0' || (s
[i
] & 0xC0) != 0x80)
38 cp
= (cp
<< 6) | (s
[i
] & 0x3F);
40 /* out of range, surrogate, overlong encoding */
41 if (cp
> 0x10FFFF || (cp
>> 11) == 0x1B || cp
< overlong
[len
- 1])
50 drw_create(Display
*dpy
, int screen
, Window root
, unsigned int w
, unsigned int h
)
52 Drw
*drw
= ecalloc(1, sizeof(Drw
));
59 drw
->drawable
= XCreatePixmap(dpy
, root
, w
, h
, DefaultDepth(dpy
, screen
));
60 drw
->gc
= XCreateGC(dpy
, root
, 0, NULL
);
61 XSetLineAttributes(dpy
, drw
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
67 drw_resize(Drw
*drw
, unsigned int w
, unsigned int h
)
75 XFreePixmap(drw
->dpy
, drw
->drawable
);
76 drw
->drawable
= XCreatePixmap(drw
->dpy
, drw
->root
, w
, h
, DefaultDepth(drw
->dpy
, drw
->screen
));
82 XFreePixmap(drw
->dpy
, drw
->drawable
);
83 XFreeGC(drw
->dpy
, drw
->gc
);
84 drw_fontset_free(drw
->fonts
);
88 /* This function is an implementation detail. Library users should use
89 * drw_fontset_create instead.
92 xfont_create(Drw
*drw
, const char *fontname
, FcPattern
*fontpattern
)
95 XftFont
*xfont
= NULL
;
96 FcPattern
*pattern
= NULL
;
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
);
108 if (!(pattern
= FcNameParse((FcChar8
*) fontname
))) {
109 fprintf(stderr
, "error, cannot parse font name to pattern: '%s'\n", fontname
);
110 XftFontClose(drw
->dpy
, xfont
);
113 } else if (fontpattern
) {
114 if (!(xfont
= XftFontOpenPattern(drw
->dpy
, fontpattern
))) {
115 fprintf(stderr
, "error, cannot load font from pattern.\n");
119 die("no font specified.");
122 font
= ecalloc(1, sizeof(Fnt
));
124 font
->pattern
= pattern
;
125 font
->h
= xfont
->ascent
+ xfont
->descent
;
126 font
->dpy
= drw
->dpy
;
132 xfont_free(Fnt
*font
)
137 FcPatternDestroy(font
->pattern
);
138 XftFontClose(font
->dpy
, font
->xfont
);
143 drw_fontset_create(Drw
* drw
, const char *fonts
[], size_t fontcount
)
145 Fnt
*cur
, *ret
= NULL
;
151 for (i
= 1; i
<= fontcount
; i
++) {
152 if ((cur
= xfont_create(drw
, fonts
[fontcount
- i
], NULL
))) {
157 return (drw
->fonts
= ret
);
161 drw_fontset_free(Fnt
*font
)
164 drw_fontset_free(font
->next
);
170 drw_clr_create(Drw
*drw
, Clr
*dest
, const char *clrname
)
172 if (!drw
|| !dest
|| !clrname
)
175 if (!XftColorAllocName(drw
->dpy
, DefaultVisual(drw
->dpy
, drw
->screen
),
176 DefaultColormap(drw
->dpy
, drw
->screen
),
178 die("error, cannot allocate color '%s'", clrname
);
181 /* Wrapper to create color schemes. The caller has to call free(3) on the
182 * returned color scheme when done using it. */
184 drw_scm_create(Drw
*drw
, const char *clrnames
[], size_t clrcount
)
189 /* need at least two colors for a scheme */
190 if (!drw
|| !clrnames
|| clrcount
< 2 || !(ret
= ecalloc(clrcount
, sizeof(XftColor
))))
193 for (i
= 0; i
< clrcount
; i
++)
194 drw_clr_create(drw
, &ret
[i
], clrnames
[i
]);
199 drw_setfontset(Drw
*drw
, Fnt
*set
)
206 drw_setscheme(Drw
*drw
, Clr
*scm
)
213 drw_rect(Drw
*drw
, int x
, int y
, unsigned int w
, unsigned int h
, int filled
, int invert
)
215 if (!drw
|| !drw
->scheme
)
217 XSetForeground(drw
->dpy
, drw
->gc
, invert
? drw
->scheme
[ColBg
].pixel
: drw
->scheme
[ColFg
].pixel
);
219 XFillRectangle(drw
->dpy
, drw
->drawable
, drw
->gc
, x
, y
, w
, h
);
221 XDrawRectangle(drw
->dpy
, drw
->drawable
, drw
->gc
, x
, y
, w
- 1, h
- 1);
225 drw_text(Drw
*drw
, int x
, int y
, unsigned int w
, unsigned int h
, unsigned int lpad
, const char *text
, int invert
)
227 int ty
, ellipsis_x
= 0;
228 unsigned int tmpw
, ew
, ellipsis_w
= 0, ellipsis_len
, hash
, h0
, h1
;
230 Fnt
*usedfont
, *curfont
, *nextfont
;
231 int utf8strlen
, utf8charlen
, utf8err
, render
= x
|| y
|| w
|| h
;
232 long utf8codepoint
= 0;
234 FcCharSet
*fccharset
;
235 FcPattern
*fcpattern
;
238 int charexists
= 0, overflow
= 0;
239 /* keep track of a couple codepoints for which we have no match. */
240 static unsigned int nomatches
[128], ellipsis_width
, invalid_width
;
241 static const char invalid
[] = "�";
243 if (!drw
|| (render
&& (!drw
->scheme
|| !w
)) || !text
|| !drw
->fonts
)
247 w
= invert
? invert
: ~invert
;
249 XSetForeground(drw
->dpy
, drw
->gc
, drw
->scheme
[invert
? ColFg
: ColBg
].pixel
);
250 XFillRectangle(drw
->dpy
, drw
->drawable
, drw
->gc
, x
, y
, w
, h
);
253 d
= XftDrawCreate(drw
->dpy
, drw
->drawable
,
254 DefaultVisual(drw
->dpy
, drw
->screen
),
255 DefaultColormap(drw
->dpy
, drw
->screen
));
260 usedfont
= drw
->fonts
;
261 if (!ellipsis_width
&& render
)
262 ellipsis_width
= drw_fontset_getwidth(drw
, "...");
263 if (!invalid_width
&& render
)
264 invalid_width
= drw_fontset_getwidth(drw
, invalid
);
266 ew
= ellipsis_len
= utf8err
= utf8charlen
= utf8strlen
= 0;
270 utf8charlen
= utf8decode(text
, &utf8codepoint
, &utf8err
);
271 for (curfont
= drw
->fonts
; curfont
; curfont
= curfont
->next
) {
272 charexists
= charexists
|| XftCharExists(drw
->dpy
, curfont
->xfont
, utf8codepoint
);
274 drw_font_getexts(curfont
, text
, utf8charlen
, &tmpw
, NULL
);
275 if (ew
+ ellipsis_width
<= w
) {
276 /* keep track where the ellipsis still fits */
279 ellipsis_len
= utf8strlen
;
284 /* called from drw_fontset_getwidth_clamp():
285 * it wants the width AFTER the overflow
290 utf8strlen
= ellipsis_len
;
291 } else if (curfont
== usedfont
) {
293 utf8strlen
+= utf8err
? 0 : utf8charlen
;
294 ew
+= utf8err
? 0 : tmpw
;
302 if (overflow
|| !charexists
|| nextfont
|| utf8err
)
310 ty
= y
+ (h
- usedfont
->h
) / 2 + usedfont
->xfont
->ascent
;
311 XftDrawStringUtf8(d
, &drw
->scheme
[invert
? ColBg
: ColFg
],
312 usedfont
->xfont
, x
, ty
, (XftChar8
*)utf8str
, utf8strlen
);
317 if (utf8err
&& (!render
|| invalid_width
< w
)) {
319 drw_text(drw
, x
, y
, w
, h
, 0, invalid
, invert
);
323 if (render
&& overflow
)
324 drw_text(drw
, ellipsis_x
, y
, ellipsis_w
, h
, 0, "...", invert
);
326 if (!*text
|| overflow
) {
328 } else if (nextfont
) {
332 /* Regardless of whether or not a fallback font is found, the
333 * character must be drawn. */
336 hash
= (unsigned int)utf8codepoint
;
337 hash
= ((hash
>> 16) ^ hash
) * 0x21F0AAAD;
338 hash
= ((hash
>> 15) ^ hash
) * 0xD35A2D97;
339 h0
= ((hash
>> 15) ^ hash
) % LENGTH(nomatches
);
340 h1
= (hash
>> 17) % LENGTH(nomatches
);
341 /* avoid expensive XftFontMatch call when we know we won't find a match */
342 if (nomatches
[h0
] == utf8codepoint
|| nomatches
[h1
] == utf8codepoint
)
345 fccharset
= FcCharSetCreate();
346 FcCharSetAddChar(fccharset
, utf8codepoint
);
348 if (!drw
->fonts
->pattern
) {
349 /* Refer to the comment in xfont_create for more information. */
350 die("the first font in the cache must be loaded from a font string.");
353 fcpattern
= FcPatternDuplicate(drw
->fonts
->pattern
);
354 FcPatternAddCharSet(fcpattern
, FC_CHARSET
, fccharset
);
355 FcPatternAddBool(fcpattern
, FC_SCALABLE
, FcTrue
);
357 FcConfigSubstitute(NULL
, fcpattern
, FcMatchPattern
);
358 FcDefaultSubstitute(fcpattern
);
359 match
= XftFontMatch(drw
->dpy
, drw
->screen
, fcpattern
, &result
);
361 FcCharSetDestroy(fccharset
);
362 FcPatternDestroy(fcpattern
);
365 usedfont
= xfont_create(drw
, NULL
, match
);
366 if (usedfont
&& XftCharExists(drw
->dpy
, usedfont
->xfont
, utf8codepoint
)) {
367 for (curfont
= drw
->fonts
; curfont
->next
; curfont
= curfont
->next
)
369 curfont
->next
= usedfont
;
371 xfont_free(usedfont
);
372 nomatches
[nomatches
[h0
] ? h1
: h0
] = utf8codepoint
;
374 usedfont
= drw
->fonts
;
382 return x
+ (render
? w
: 0);
386 drw_map(Drw
*drw
, Window win
, int x
, int y
, unsigned int w
, unsigned int h
)
391 XCopyArea(drw
->dpy
, drw
->drawable
, win
, drw
->gc
, x
, y
, w
, h
, x
, y
);
392 XSync(drw
->dpy
, False
);
396 drw_fontset_getwidth(Drw
*drw
, const char *text
)
398 if (!drw
|| !drw
->fonts
|| !text
)
400 return drw_text(drw
, 0, 0, 0, 0, 0, text
, 0);
404 drw_fontset_getwidth_clamp(Drw
*drw
, const char *text
, unsigned int n
)
406 unsigned int tmp
= 0;
407 if (drw
&& drw
->fonts
&& text
&& n
)
408 tmp
= drw_text(drw
, 0, 0, 0, 0, 0, text
, n
);
413 drw_font_getexts(Fnt
*font
, const char *text
, unsigned int len
, unsigned int *w
, unsigned int *h
)
420 XftTextExtentsUtf8(font
->dpy
, font
->xfont
, (XftChar8
*)text
, len
, &ext
);
428 drw_cur_create(Drw
*drw
, int shape
)
432 if (!drw
|| !(cur
= ecalloc(1, sizeof(Cur
))))
435 cur
->cursor
= XCreateFontCursor(drw
->dpy
, shape
);
441 drw_cur_free(Drw
*drw
, Cur
*cursor
)
446 XFreeCursor(drw
->dpy
, cursor
->cursor
);