From 827c2337690809e1c663ae0732b8bf510e1fc15f Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 18 Dec 2009 21:17:54 +0000 Subject: [PATCH] Deduce X11 font size from a relevant sample to avoid oversized font cells. Fixes bug #21 (X11 Unicode support). --- caca/driver/x11.c | 29 ++++++++++++++++++++--------- examples/unicode.c | 1 + 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/caca/driver/x11.c b/caca/driver/x11.c index df922b3..f41b0a5 100644 --- a/caca/driver/x11.c +++ b/caca/driver/x11.c @@ -209,12 +209,25 @@ static int x11_init_graphics(caca_display_t *dp) #if defined X_HAVE_UTF8_STRING if (dp->drv.p->font_set) { - XFontSetExtents *ex = XExtentsOfFontSet(dp->drv.p->font_set); + /* Do not trust the fontset, because some fonts may have + * irrelevantly large glyphs. Pango and rxvt use the same method. */ + static wchar_t const test[] = + { + '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', + 0x00cd, 0x00e7, 0x00d5, 0x0114, 0x0177, /* Í ç Õ Ĕ ŷ */ + /* 0x0643, */ /* ﻙ */ + 0x304c, 0x672c, /* が本 */ + 0, + }; + + XRectangle ink, logical; + dp->drv.p->font_width = - XmbTextEscapement(dp->drv.p->font_set, "CAca", 4) / 4; - dp->drv.p->font_height = ex->max_logical_extent.height; - dp->drv.p->font_offset = ex->max_logical_extent.height - + ex->max_logical_extent.y; + (XmbTextEscapement(dp->drv.p->font_set, "CAca", 4) + 2) / 4; + XwcTextExtents(dp->drv.p->font_set, test, sizeof(test) / sizeof(*test), + &ink, &logical); + dp->drv.p->font_height = ink.height; + dp->drv.p->font_offset = ink.height + ink.y; } else #endif @@ -901,8 +914,7 @@ static void x11_put_glyph(caca_display_t *dp, int x, int y, int yoff, if (dp->drv.p->font_set) { wchar_t wch = ch; - XwcDrawString(dpy, px, dp->drv.p->font_set, gc, - x + (fw - w) / 2, yoff, &wch, 1); + XwcDrawString(dpy, px, dp->drv.p->font_set, gc, x, yoff, &wch, 1); } else #endif @@ -922,8 +934,7 @@ static void x11_put_glyph(caca_display_t *dp, int x, int y, int yoff, ch16.byte2 = (uint8_t)ch; } - XDrawString16(dpy, px, gc, - x + (ch16.byte1 ? 0 : (fw - w) / 2), yoff, &ch16, 1); + XDrawString16(dpy, px, gc, x, yoff, &ch16, 1); } } diff --git a/examples/unicode.c b/examples/unicode.c index 2d01a3a..7f6f462 100644 --- a/examples/unicode.c +++ b/examples/unicode.c @@ -44,6 +44,7 @@ int main(int argc, char *argv[]) caca_put_str(cv, 1, 2, "This is ASCII: | abc DEF 123 !@# |"); caca_put_str(cv, 1, 3, "This is Unicode: | äßç δεφ ☺♥♀ ╞╬╗ |"); caca_put_str(cv, 1, 4, "And this is, too: | ἀβϛ ΔЗҒ ᚴᛒᛯ ♩♔✈ |"); + caca_put_str(cv, 41, 4, "Size test: 018adxmygWX'_ÍçÕĔŷ ﻙ が本"); caca_put_str(cv, 1, 5, "If the three lines do not have the same length, there is a bug somewhere.");