[dev] [PATCH] font: allow user specified italic, bold, italic bold fonts

From: Plato Kiorpelidis <kioplato_AT_gmail.com>
Date: Sat, 6 Aug 2022 15:23:58 +0300

Sometimes FcFontMatch does not return the font the user considers to be
the matching italic, bold or italic bold version of the one specified in
config.h. To deal with this, allow the user to specify the exact font
for italic, bold and italic bold versions. If any of these are not
provided, the previous mechanism will be used to heuristically pattern
match config.h user provided font to find the missing version.

The call to FcFontMatch isn't being avoided. It still happens. However,
now the user has the ability to specify the exact font that's expected.

Signed-off-by: Plato Kiorpelidis <kioplato_AT_gmail.com>
---
Hi,
Sorry for not including a patch, I thought my branch was enough.
Here's my patch from within GitHub as well.
https://github.com/kioplato/st/compare/user-specified-fonts
Thanks,
Plato Kiorpelidis
 config.def.h |  9 +++++
 x.c          | 97 +++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 94 insertions(+), 12 deletions(-)
diff --git a/config.def.h b/config.def.h
index 91ab8ca..7ad3b78 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -6,6 +6,15 @@
  * font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html
  */
 static char *font = "Liberation Mono:pixelsize=12:antialias=true:autohint=true";
+/*
+ * Manually specify bold, italic and bold italic fonts.
+ * 'pixelsize' or 'size' must not be specified in these fonts.
+ * The 'pixelsize' or 'size' specified in the above font will be used.
+ */
+static char *ifont = NULL;
+static char *bfont = NULL;
+static char *ibfont = NULL;
+
 static int borderpx = 2;
 
 /*
diff --git a/x.c b/x.c
index 2a3bd38..6397509 100644
--- a/x.c
+++ b/x.c
_AT_@ -1032,20 +1032,93 @@ xloadfonts(const char *fontstr, double fontsize)
 	win.cw = ceilf(dc.font.width * cwscale);
 	win.ch = ceilf(dc.font.height * chscale);
 
-	FcPatternDel(pattern, FC_SLANT);
-	FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
-	if (xloadfont(&dc.ifont, pattern))
-		die("can't open font %s\n", fontstr);
+	if (ifont) {
+		FcPattern *ipattern;
+		if (fontstr[0] == '-')
+			ipattern = XftXlfdParse(ifont, False, False);
+		else
+			ipattern = FcNameParse((const FcChar8 *)ifont);
 
-	FcPatternDel(pattern, FC_WEIGHT);
-	FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
-	if (xloadfont(&dc.ibfont, pattern))
-		die("can't open font %s\n", fontstr);
+		if (!ipattern)
+			die("can't open specified italics font %s\n", ifont);
 
-	FcPatternDel(pattern, FC_SLANT);
-	FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
-	if (xloadfont(&dc.bfont, pattern))
-		die("can't open font %s\n", fontstr);
+		if (FcPatternGetDouble(ipattern, FC_PIXEL_SIZE, 0, &fontval) == FcResultMatch ||
+			FcPatternGetDouble(ipattern, FC_SIZE, 0, &fontval) == FcResultMatch)
+			die("user specified style fonts can't set 'pixelsize' or 'size'\n");
+
+		FcPatternAddDouble(ipattern, FC_PIXEL_SIZE, usedfontsize);
+
+		if (xloadfont(&dc.ifont, ipattern))
+			die("can't open font %s\n", ifont);
+
+		FcPatternDestroy(ipattern);
+	} else {
+		FcPatternDel(pattern, FC_SLANT);
+		FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
+
+		if (xloadfont(&dc.ifont, pattern))
+			die("can't open font %s\n", fontstr);
+	}
+
+	if (ibfont) {
+		FcPattern *ibpattern;
+		if (fontstr[0] == '-')
+			ibpattern = XftXlfdParse(ibfont, False, False);
+		else
+			ibpattern = FcNameParse((const FcChar8 *)ibfont);
+
+		if (!ibpattern)
+			die("can't open specified bold italic font %s\n", ibfont);
+
+		if (FcPatternGetDouble(ibpattern, FC_PIXEL_SIZE, 0, &fontval) == FcResultMatch ||
+			FcPatternGetDouble(ibpattern, FC_SIZE, 0, &fontval) == FcResultMatch)
+			die("user specified style fonts can't set 'pixelsize' or 'size'\n");
+
+		FcPatternAddDouble(ibpattern, FC_PIXEL_SIZE, usedfontsize);
+
+		if (xloadfont(&dc.ibfont, ibpattern))
+			die("can't open font %s\n", ibfont);
+
+		FcPatternDestroy(ibpattern);
+	} else {
+		FcPatternDel(pattern, FC_SLANT);
+		FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
+		FcPatternDel(pattern, FC_WEIGHT);
+		FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
+
+		if (xloadfont(&dc.ibfont, pattern))
+			die("can't open font %s\n", fontstr);
+	}
+
+	if (bfont) {
+		FcPattern *bpattern;
+		if (fontstr[0] == '-')
+			bpattern = XftXlfdParse(bfont, False, False);
+		else
+			bpattern = FcNameParse((const FcChar8 *)bfont);
+
+		if (!bpattern)
+			die("can't open specified bold font %s\n", bfont);
+
+		if (FcPatternGetDouble(bpattern, FC_PIXEL_SIZE, 0, &fontval) == FcResultMatch ||
+			FcPatternGetDouble(bpattern, FC_SIZE, 0, &fontval) == FcResultMatch)
+			die("user specified style fonts can't set 'pixelsize' or 'size'\n");
+
+		FcPatternAddDouble(bpattern, FC_PIXEL_SIZE, usedfontsize);
+
+		if (xloadfont(&dc.bfont, bpattern))
+			die("can't open font %s\n", bfont);
+
+		FcPatternDestroy(bpattern);
+	} else {
+		FcPatternDel(pattern, FC_SLANT);
+		FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
+		FcPatternDel(pattern, FC_WEIGHT);
+		FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
+
+		if (xloadfont(&dc.bfont, pattern))
+			die("can't open font %s\n", fontstr);
+	}
 
 	FcPatternDestroy(pattern);
 }
base-commit: baa9357e96d2478baa52a3301e70ac80a229b726
-- 
2.37.1
Received on Sat Aug 06 2022 - 14:23:58 CEST

This archive was generated by hypermail 2.3.0 : Sat Aug 06 2022 - 14:24:38 CEST