[dev] [PATCH] Measure the single advance width with a heuristic method

From: Ryusei Yamaguchi <mandel59_AT_gmail.com>
Date: Tue, 8 Mar 2016 12:26:04 +0900

This fix is needed to use dual-width fonts, which have double-width
glyphs (e.g. CJK unified ideographs).

Signed-off-by: Ryusei Yamaguchi <mandel59_AT_gmail.com>
---
 config.def.h | 8 ++++++++
 st.c         | 8 +++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/config.def.h b/config.def.h
index fd09d72..a1e7d5a 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -417,3 +417,11 @@ static uint selmasks[] = {
 	[SEL_RECTANGULAR] = Mod1Mask,
 };
 
+/*
+ * Printable characters in ASCII, used to estimate the advance width
+ * of single wide characters.
+ */
+static char ascii_printable[] =
+	" !\"#$%&'()*+,-./0123456789:;<=>?"
+	"_AT_ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
+	"`abcdefghijklmnopqrstuvwxyz{|}~";
diff --git a/st.c b/st.c
index 2473af7..ca126d7 100644
--- a/st.c
+++ b/st.c
_AT_@ -68,6 +68,7 @@ char *argv0;
 #define LEN(a)			(sizeof(a) / sizeof(a)[0])
 #define DEFAULT(a, b)		(a) = (a) ? (a) : (b)
 #define BETWEEN(x, a, b)	((a) <= (x) && (x) <= (b))
+#define DIVCEIL(n, d)		(((n) + ((d) - 1)) / (d))
 #define ISCONTROLC0(c)		(BETWEEN(c, 0, 0x1f) || (c) == '\177')
 #define ISCONTROLC1(c)		(BETWEEN(c, 0x80, 0x9f))
 #define ISCONTROL(c)		(ISCONTROLC0(c) || ISCONTROLC1(c))
_AT_@ -3277,6 +3278,7 @@ xloadfont(Font *f, FcPattern *pattern)
 {
 	FcPattern *match;
 	FcResult result;
+	XGlyphInfo extents;
 
 	match = FcFontMatch(NULL, pattern, &result);
 	if (!match)
_AT_@ -3287,6 +3289,10 @@ xloadfont(Font *f, FcPattern *pattern)
 		return 1;
 	}
 
+	XftTextExtentsUtf8(xw.dpy, f->match,
+		(const FcChar8 *) ascii_printable,
+		LEN(ascii_printable), &extents);
+
 	f->set = NULL;
 	f->pattern = FcPatternDuplicate(pattern);
 
_AT_@ -3296,7 +3302,7 @@ xloadfont(Font *f, FcPattern *pattern)
 	f->rbearing = f->match->max_advance_width;
 
 	f->height = f->ascent + f->descent;
-	f->width = f->lbearing + f->rbearing;
+	f->width = DIVCEIL(extents.xOff, LEN(ascii_printable));
 
 	return 0;
 }
-- 
1.9.1
Received on Tue Mar 08 2016 - 04:26:04 CET

This archive was generated by hypermail 2.3.0 : Tue Mar 08 2016 - 04:36:11 CET