Do not use color when font attributes are supported

If fontconfig gives us a font without the attributes we asked for,
display an alternative color instead.
master
Quentin Rameau 2016-10-22 16:36:10 +02:00
parent 7854fde1ff
commit 8c99915608
2 changed files with 35 additions and 13 deletions

View File

@ -138,12 +138,10 @@ static unsigned int mousefg = 7;
static unsigned int mousebg = 0; static unsigned int mousebg = 0;
/* /*
* Colors used, when the specific fg == defaultfg. So in reverse mode this * Color used to display font attributes when fontconfig selected a font which
* will reverse too. Another logic would only make the simple feature too * doesn't match the ones requested.
* complex.
*/ */
static unsigned int defaultitalic = 11; static unsigned int defaultattr = 11;
static unsigned int defaultunderline = 7;
/* /*
* Internal mouse shortcuts. * Internal mouse shortcuts.

40
st.c
View File

@ -354,6 +354,8 @@ typedef struct {
int width; int width;
int ascent; int ascent;
int descent; int descent;
int badslant;
int badweight;
short lbearing; short lbearing;
short rbearing; short rbearing;
XftFont *match; XftFont *match;
@ -3373,6 +3375,7 @@ xloadfont(Font *f, FcPattern *pattern)
FcPattern *match; FcPattern *match;
FcResult result; FcResult result;
XGlyphInfo extents; XGlyphInfo extents;
int wantattr, haveattr;
match = XftFontMatch(xw.dpy, xw.scr, pattern, &result); match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
if (!match) if (!match)
@ -3383,6 +3386,28 @@ xloadfont(Font *f, FcPattern *pattern)
return 1; return 1;
} }
if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
XftResultMatch)) {
/*
* Check if xft was unable to find a font with the appropriate
* slant but gave us one anyway. Try to mitigate.
*/
if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
&haveattr) != XftResultMatch) || haveattr < wantattr) {
f->badslant = 1;
fputs("st: font slant does not match\n", stderr);
}
}
if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
XftResultMatch)) {
if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
&haveattr) != XftResultMatch) || haveattr != wantattr) {
f->badweight = 1;
fputs("st: font weight does not match\n", stderr);
}
}
XftTextExtentsUtf8(xw.dpy, f->match, XftTextExtentsUtf8(xw.dpy, f->match,
(const FcChar8 *) ascii_printable, (const FcChar8 *) ascii_printable,
strlen(ascii_printable), &extents); strlen(ascii_printable), &extents);
@ -3780,14 +3805,13 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i
XRenderColor colfg, colbg; XRenderColor colfg, colbg;
XRectangle r; XRectangle r;
/* Determine foreground and background colors based on mode. */ /* Fallback on color display for attributes not supported by the font */
if (base.fg == defaultfg) { if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
if (base.mode & ATTR_ITALIC) if (dc.ibfont.badslant || dc.ibfont.badweight)
base.fg = defaultitalic; base.fg = defaultattr;
else if ((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD)) } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
base.fg = defaultitalic; (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
else if (base.mode & ATTR_UNDERLINE) base.fg = defaultattr;
base.fg = defaultunderline;
} }
if (IS_TRUECOL(base.fg)) { if (IS_TRUECOL(base.fg)) {