+/* get next utf8 char from s return its codepoint and set next_ret to pointer to end of character */
+static FcChar32
+getnextutf8char(const char *s, const char **next_ret)
+{
+ static const unsigned char utfbyte[] = {0x80, 0x00, 0xC0, 0xE0, 0xF0};
+ static const unsigned char utfmask[] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
+ static const FcChar32 utfmin[] = {0, 0x00, 0x80, 0x800, 0x10000};
+ static const FcChar32 utfmax[] = {0, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
+ /* 0xFFFD is the replacement character, used to represent unknown characters */
+ static const FcChar32 unknown = 0xFFFD;
+ FcChar32 ucode; /* FcChar32 type holds 32 bits */
+ size_t usize = 0; /* n' of bytes of the utf8 character */
+ size_t i;
+
+ *next_ret = s+1;
+
+ /* get code of first byte of utf8 character */
+ for (i = 0; i < sizeof utfmask; i++) {
+ if (((unsigned char)*s & utfmask[i]) == utfbyte[i]) {
+ usize = i;
+ ucode = (unsigned char)*s & ~utfmask[i];
+ break;
+ }
+ }
+
+ /* if first byte is a continuation byte or is not allowed, return unknown */
+ if (i == sizeof utfmask || usize == 0)
+ return unknown;
+
+ /* check the other usize-1 bytes */
+ s++;
+ for (i = 1; i < usize; i++) {
+ *next_ret = s+1;
+ /* if byte is nul or is not a continuation byte, return unknown */
+ if (*s == '\0' || ((unsigned char)*s & utfmask[0]) != utfbyte[0])
+ return unknown;
+ /* 6 is the number of relevant bits in the continuation byte */
+ ucode = (ucode << 6) | ((unsigned char)*s & ~utfmask[0]);
+ s++;
+ }
+
+ /* check if ucode is invalid or in utf-16 surrogate halves */
+ if (!BETWEEN(ucode, utfmin[usize], utfmax[usize])
+ || BETWEEN (ucode, 0xD800, 0xDFFF))
+ return unknown;
+
+ return ucode;
+}
+
+/* get which font contains a given code point */
+static XftFont *
+getfontucode(FcChar32 ucode)
+{
+ FcCharSet *fccharset = NULL;
+ FcPattern *fcpattern = NULL;
+ FcPattern *match = NULL;
+ XftFont *retfont = NULL;
+ XftResult result;
+ size_t i;
+
+ for (i = 0; i < dc.nfonts; i++)
+ if (XftCharExists(dpy, dc.fonts[i], ucode) == FcTrue)
+ return dc.fonts[i];
+
+ /* create a charset containing our code point */
+ fccharset = FcCharSetCreate();
+ FcCharSetAddChar(fccharset, ucode);
+
+ /* create a pattern akin to the dc.pattern but containing our charset */
+ if (fccharset) {
+ fcpattern = FcPatternDuplicate(dc.pattern);
+ FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
+ }
+
+ /* find pattern matching fcpattern */
+ if (fcpattern) {
+ FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
+ FcDefaultSubstitute(fcpattern);
+ match = XftFontMatch(dpy, screen, fcpattern, &result);
+ }
+
+ /* if found a pattern, open its font */
+ if (match) {
+ retfont = XftFontOpenPattern(dpy, match);
+ if (retfont && XftCharExists(dpy, retfont, ucode) == FcTrue) {
+ if ((dc.fonts = realloc(dc.fonts, dc.nfonts+1)) == NULL)
+ err(1, "realloc");
+ dc.fonts[dc.nfonts] = retfont;
+ return dc.fonts[dc.nfonts++];
+ } else {
+ XftFontClose(dpy, retfont);
+ }
+ }
+
+ /* in case no fount was found, return the first one */
+ return dc.fonts[0];
+}
+
+/* draw text into XftDraw, return width of text glyphs */
+static int
+drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text)
+{
+ int textwidth = 0;
+
+ while (*text) {
+ XftFont *currfont;
+ XGlyphInfo ext;
+ FcChar32 ucode;
+ const char *next;
+ size_t len;
+
+ ucode = getnextutf8char(text, &next);
+ currfont = getfontucode(ucode);
+
+ len = next - text;
+ XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)text, len, &ext);
+ textwidth += ext.xOff;
+
+ if (draw) {
+ int texty;
+
+ texty = y + (h - (currfont->ascent + currfont->descent))/2 + currfont->ascent;
+ XftDrawStringUtf8(draw, color, currfont, x, texty, (XftChar8 *)text, len);
+ x += ext.xOff;
+ }
+
+ text = next;
+ }
+
+ return textwidth;
+}
+
+/* setup the height, width and icon of the items of a menu */