search the system for font containing character

This commit is contained in:
phillbush 2020-08-01 15:12:57 -03:00
parent f8d55995df
commit 6d56f2796c

46
xmenu.c
View File

@ -260,12 +260,17 @@ parsefonts(const char *s)
i = 0; i = 0;
while (isspace(*p)) while (isspace(*p))
p++; p++;
while (*p != '\0' && *p != ',') { while (i < sizeof buf && *p != '\0' && *p != ',') {
buf[i++] = *p++; buf[i++] = *p++;
} }
if (i >= sizeof buf)
errx(1, "font name too long");
if (*p == ',') if (*p == ',')
p++; p++;
buf[i] = '\0'; buf[i] = '\0';
if (nfont == 0)
if ((dc.pattern = FcNameParse((FcChar8 *)buf)) == NULL)
errx(1, "the first font in the cache must be loaded from a font string");
if ((dc.fonts[nfont++] = XftFontOpenName(dpy, screen, buf)) == NULL) if ((dc.fonts[nfont++] = XftFontOpenName(dpy, screen, buf)) == NULL)
errx(1, "cannot load font"); errx(1, "cannot load font");
} }
@ -643,12 +648,45 @@ getnextutf8char(const char *s, const char **next_ret)
static XftFont * static XftFont *
getfontucode(FcChar32 ucode) getfontucode(FcChar32 ucode)
{ {
FcCharSet *fccharset;
FcPattern *fcpattern;
FcPattern *match;
XftResult result;
XftFont *retfont;
size_t i; size_t i;
for (i = 0; i < dc.nfonts; i++) for (i = 0; i < dc.nfonts; i++)
if (XftCharExists(dpy, dc.fonts[i], ucode) == FcTrue) if (XftCharExists(dpy, dc.fonts[i], ucode) == FcTrue)
return dc.fonts[i]; return dc.fonts[i];
return NULL;
/* create a charset containing our code point */
fccharset = FcCharSetCreate();
FcCharSetAddChar(fccharset, ucode);
/* create a pattern akin to the dc.pattern but containing our code point */
fcpattern = FcPatternDuplicate(dc.pattern);
FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
/* find pattern matching 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 retfont;
} 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 */ /* draw text into XftDraw, return width of text glyphs */
@ -668,11 +706,9 @@ drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *t
size_t len; size_t len;
ucode = getnextutf8char(text, &next); ucode = getnextutf8char(text, &next);
if ((currfont = getfontucode(ucode)) == NULL) currfont = getfontucode(ucode);
currfont = dc.fonts[0];
len = next - text; len = next - text;
XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)text, len, &ext); XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)text, len, &ext);
textwidth += ext.xOff; textwidth += ext.xOff;