Fixed 'missing glyph doesn't use fontconfig config substitutions' bug
[st.git] / st.c
diff --git a/st.c b/st.c
index c67623f..031272f 100644 (file)
--- a/st.c
+++ b/st.c
@@ -354,6 +354,8 @@ typedef struct {
        int width;
        int ascent;
        int descent;
+       int badslant;
+       int badweight;
        short lbearing;
        short rbearing;
        XftFont *match;
@@ -2488,22 +2490,22 @@ csidump(void)
        int i;
        uint c;
 
-       printf("ESC[");
+       fprintf(stderr, "ESC[");
        for (i = 0; i < csiescseq.len; i++) {
                c = csiescseq.buf[i] & 0xff;
                if (isprint(c)) {
-                       putchar(c);
+                       putc(c, stderr);
                } else if (c == '\n') {
-                       printf("(\\n)");
+                       fprintf(stderr, "(\\n)");
                } else if (c == '\r') {
-                       printf("(\\r)");
+                       fprintf(stderr, "(\\r)");
                } else if (c == 0x1b) {
-                       printf("(\\e)");
+                       fprintf(stderr, "(\\e)");
                } else {
-                       printf("(%02x)", c);
+                       fprintf(stderr, "(%02x)", c);
                }
        }
-       putchar('\n');
+       putc('\n', stderr);
 }
 
 void
@@ -2592,24 +2594,25 @@ strdump(void)
        int i;
        uint c;
 
-       printf("ESC%c", strescseq.type);
+       fprintf(stderr, "ESC%c", strescseq.type);
        for (i = 0; i < strescseq.len; i++) {
                c = strescseq.buf[i] & 0xff;
                if (c == '\0') {
+                       putc('\n', stderr);
                        return;
                } else if (isprint(c)) {
-                       putchar(c);
+                       putc(c, stderr);
                } else if (c == '\n') {
-                       printf("(\\n)");
+                       fprintf(stderr, "(\\n)");
                } else if (c == '\r') {
-                       printf("(\\r)");
+                       fprintf(stderr, "(\\r)");
                } else if (c == 0x1b) {
-                       printf("(\\e)");
+                       fprintf(stderr, "(\\e)");
                } else {
-                       printf("(%02x)", c);
+                       fprintf(stderr, "(%02x)", c);
                }
        }
-       printf("ESC\\\n");
+       fprintf(stderr, "ESC\\\n");
 }
 
 void
@@ -3370,25 +3373,64 @@ xgeommasktogravity(int mask)
 int
 xloadfont(Font *f, FcPattern *pattern)
 {
+       FcPattern *configured;
        FcPattern *match;
        FcResult result;
        XGlyphInfo extents;
+       int wantattr, haveattr;
 
-       match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
-       if (!match)
+       /*
+        * Manually configure instead of calling XftMatchFont
+        * so that we can use the configured pattern for
+        * "missing glyph" lookups.
+        */
+       configured = FcPatternDuplicate(pattern);
+       if (!configured)
                return 1;
 
+       FcConfigSubstitute(NULL, configured, FcMatchPattern);
+       XftDefaultSubstitute(xw.dpy, xw.scr, configured);
+
+       match = FcFontMatch(NULL, configured, &result);
+       if (!match) {
+               FcPatternDestroy(configured);
+               return 1;
+       }
+
        if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
+               FcPatternDestroy(configured);
                FcPatternDestroy(match);
                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,
                (const FcChar8 *) ascii_printable,
                strlen(ascii_printable), &extents);
 
        f->set = NULL;
-       f->pattern = FcPatternDuplicate(pattern);
+       f->pattern = configured;
 
        f->ascent = f->match->ascent;
        f->descent = f->match->descent;
@@ -3446,6 +3488,9 @@ xloadfonts(char *fontstr, double fontsize)
        if (usedfontsize < 0) {
                FcPatternGetDouble(dc.font.match->pattern,
                                   FC_PIXEL_SIZE, 0, &fontval);
+               FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fontval);
+               if (xloadfont(&dc.font, pattern))
+                       die("st: can't open font %s\n", fontstr);
                usedfontsize = fontval;
                if (fontsize == 0)
                        defaultfontsize = fontval;
@@ -3780,14 +3825,13 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i
        XRenderColor colfg, colbg;
        XRectangle r;
 
-       /* Determine foreground and background colors based on mode. */
-       if (base.fg == defaultfg) {
-               if (base.mode & ATTR_ITALIC)
-                       base.fg = defaultitalic;
-               else if ((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD))
-                       base.fg = defaultitalic;
-               else if (base.mode & ATTR_UNDERLINE)
-                       base.fg = defaultunderline;
+       /* Fallback on color display for attributes not supported by the font */
+       if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
+               if (dc.ibfont.badslant || dc.ibfont.badweight)
+                       base.fg = defaultattr;
+       } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
+           (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
+               base.fg = defaultattr;
        }
 
        if (IS_TRUECOL(base.fg)) {