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

From: Ryusei Yamaguchi <mandel59_AT_gmail.com>
Date: Mon, 7 Mar 2016 15:06:53 +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>
---
 st.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/st.c b/st.c
index 0536b6f..fc49e67 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_@ -3289,6 +3290,7 @@ xloadfont(Font *f, FcPattern *pattern)
 {
 	FcPattern *match;
 	FcResult result;
+	XGlyphInfo extents;
 
 	match = FcFontMatch(NULL, pattern, &result);
 	if (!match)
_AT_@ -3299,6 +3301,15 @@ xloadfont(Font *f, FcPattern *pattern)
 		return 1;
 	}
 
+#define SINGLE_WIDE_CHARS \
+	" !\"#$%&'()*+,-./0123456789:;<=>?" \
+	"_AT_ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" \
+	"`abcdefghijklmnopqrstuvwxyz{|}~"
+
+	XftTextExtentsUtf8(xw.dpy, f->match,
+		(const FcChar8 *) SINGLE_WIDE_CHARS,
+		LEN(SINGLE_WIDE_CHARS), &extents);
+
 	f->set = NULL;
 	f->pattern = FcPatternDuplicate(pattern);
 
_AT_@ -3308,7 +3319,9 @@ 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(SINGLE_WIDE_CHARS));
+
+#undef SINGLE_WIDE_CHARACTERS
 
 	return 0;
 }
-- 
1.9.1
Received on Mon Mar 07 2016 - 07:06:53 CET

This archive was generated by hypermail 2.3.0 : Mon Mar 07 2016 - 07:24:12 CET