Re: [PATCH] if compiling with xft, hardcode encoding to utf-8

[email protected] (Jérémie Courrèges -Anglas)
Newsgroups gmane.comp.window-managers.ratpoison.devel
Message-ID <[email protected]>
Hi.

"Bernhard R. Link" <[email protected]> writes:

> * Jérémie Courrèges-Anglas <[email protected]> [120630 18:39]:
>> > Unlike the X primitives, Xft does not seem to support
>> > utf-8 when drawing a string encoded according to the current
>> > locale. So if compiling with Xft, hardcode the encoding to be
>> > used to utf-8.
>>
>> This has the undesirable effect to break existing setups.  People using
>> for example latin1 wouldn't be able to input and echo latin1 characters.
>> I'm no unicode expert, but if ratpoison has to get unicode support, then
>> it should probably do it correctly, right?
>
> without Xft fonts, UTF-8 works correctly. With Xft one has to use a
> single-character encoding or use utf8. do you know any way to know in
> which case libX11 currently is so one can call the correct Xft function?

My strategy would be to detect whether we're using an UTF-8 locale, and
then act accordingly.  But it seems this would only be useful when using
Xft, so, make that detection present only when using Xft.

I've made the use of Xutf8* conditional, since I don't know what's the
current state of this extension.  If Xutf8* aren't available, just
revert to Xmb*.  Has it (that stuff seems to be related to UTF8_STRING)
really been included in the X standard?  If so, how portable is it?

I have not yet found out how much we should bother about with the getsel
code.  Also the input editing should be modified to deal with multibytes
characters.

Honestly, I have no idea whether this is the cleanest way to do it
(while keeping it simple).  Please test the attached patch, whether you
use ISO8859-1 or UTF-8 or anything else. :)

Regards.

PS: I've dropped the AC_TYPE_SIGNAL macro call since RETSIGTYPE wasn't
used anywhere in the code and we assume ANSI C anyway.


-- 
Jérémie Courrèges-Anglas
GPG fingerprint: 61DB D9A0 00A4 67CF 2A90 8961 6191 8FBF 06A1 1494

_______________________________________________
Ratpoison-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/ratpoison-devel
xft-utf8.diff (text/x-patch, 5.5 KB)
diff --git a/autogen.sh b/autogen.sh
index 5a1be80..d6cbd45 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -28,4 +28,4 @@ if [ x"$1" = x-f ]
   else         am_opt='--copy'
 fi
 
-aclocal && autoheader && automake -a $am_opt && autoconf
+aclocal -I m4 && autoheader && automake -a $am_opt && autoconf
diff --git a/configure.in b/configure.in
index 08f4ee8..51cf3ad 100644
--- a/configure.in
+++ b/configure.in
@@ -148,6 +148,6 @@ dnl Checks for typedefs, structures, and compiler characteristics.
 dnl Checks for library functions.
 AC_CHECK_FUNCS(getopt getopt_long setsid setpgid setpgrp putenv vsnprintf usleep getline)
 
-AC_TYPE_SIGNAL
+AM_LANGINFO_CODESET
 
 AC_OUTPUT(Makefile doc/Makefile src/Makefile contrib/Makefile)
diff --git a/m4/codeset.m4 b/m4/codeset.m4
new file mode 100644
index 0000000..a53c042
--- /dev/null
+++ b/m4/codeset.m4
@@ -0,0 +1,21 @@
+# codeset.m4 serial 4 (gettext-0.18)
+dnl Copyright (C) 2000-2002, 2006, 2008-2010 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl From Bruno Haible.
+
+AC_DEFUN([AM_LANGINFO_CODESET],
+[
+  AC_CACHE_CHECK([for nl_langinfo and CODESET], [am_cv_langinfo_codeset],
+    [AC_TRY_LINK([#include <langinfo.h>],
+      [char* cs = nl_langinfo(CODESET); return !cs;],
+      [am_cv_langinfo_codeset=yes],
+      [am_cv_langinfo_codeset=no])
+    ])
+  if test $am_cv_langinfo_codeset = yes; then
+    AC_DEFINE([HAVE_LANGINFO_CODESET], [1],
+      [Define if you have <langinfo.h> and nl_langinfo(CODESET).])
+  fi
+])
diff --git a/src/data.h b/src/data.h
index 541ca9e..4251d3f 100644
--- a/src/data.h
+++ b/src/data.h
@@ -235,6 +235,9 @@ struct rp_defaults
 
   XFontSet font;
   char *font_string;
+#ifdef USE_XFT_FONT
+  int utf8_xft;
+#endif
 
   char *fgcolor_string;
   char *bgcolor_string;
diff --git a/src/globals.c b/src/globals.c
index 3029aef..7b679bf 100644
--- a/src/globals.c
+++ b/src/globals.c
@@ -289,15 +289,23 @@ rp_draw_string (rp_screen *s, Drawable d, int style, int x, int y, char *string,
                             DefaultColormap (dpy, s->screen_num));
       if (draw)
         {
-          XftDrawString8 (draw, style == STYLE_NORMAL ? &s->xft_fg_color:&s->xft_bg_color, s->xft_font, x, y, (FcChar8*) string, length);
+	  if (defaults.utf8_xft)
+	    XftDrawStringUtf8 (draw, style == STYLE_NORMAL ? &s->xft_fg_color:&s->xft_bg_color, s->xft_font, x, y, (FcChar8*) string, length);
+	  else
+	    XftDrawString8 (draw, style == STYLE_NORMAL ? &s->xft_fg_color:&s->xft_bg_color, s->xft_font, x, y, (FcChar8*) string, length);
           XftDrawDestroy (draw);
         }
       else
         PRINT_ERROR(("Failed to allocate XftDraw object\n"));
     }
   else
-#endif
-    XmbDrawString (dpy, d, defaults.font, style == STYLE_NORMAL ? s->normal_gc:s->inverse_gc, x, y, string, length);
+# ifdef X_HAVE_UTF8_STRING
+    if (defaults.utf8_xft)
+      Xutf8DrawString (dpy, d, defaults.font, style == STYLE_NORMAL ? s->normal_gc:s->inverse_gc, x, y, string, length);
+    else
+# endif /* X_HAVE_UTF8_STRING */
+#endif /* USE_XFT_FONT */
+      XmbDrawString (dpy, d, defaults.font, style == STYLE_NORMAL ? s->normal_gc:s->inverse_gc, x, y, string, length);
 }
 
 int
@@ -314,11 +322,19 @@ rp_text_width (rp_screen *s UNUSED, XFontSet font, char *string, int count)
   if (s->xft_font)
     {
       XGlyphInfo extents;
-      XftTextExtents8 (dpy, s->xft_font, (FcChar8*) string, count, &extents);
+      if (defaults.utf8_xft)
+        XftTextExtentsUtf8 (dpy, s->xft_font, (FcChar8*) string, count, &extents);
+      else
+        XftTextExtents8 (dpy, s->xft_font, (FcChar8*) string, count, &extents);
       return extents.xOff;
     }
   else
-#endif
-    return XmbTextEscapement (font, string, count);
+# ifdef X_HAVE_UTF8_STRING
+    if (defaults.utf8_xft)
+      return Xutf8TextEscapement (font, string, count);
+    else
+# endif /* X_HAVE_UTF8_STRING */
+#endif /* USE_XFT_FONT */
+      return XmbTextEscapement (font, string, count);
 }
 
diff --git a/src/main.c b/src/main.c
index 417fc1a..688a355 100644
--- a/src/main.c
+++ b/src/main.c
@@ -38,6 +38,10 @@
 
 #include "ratpoison.h"
 
+#ifdef HAVE_LANGINFO_CODESET
+# include <langinfo.h>
+#endif
+
 /* Several systems seem not to have WAIT_ANY defined, so define it if
    it isn't. */
 #ifndef WAIT_ANY
@@ -169,7 +173,7 @@ strtok_ws (char *s)
 
   if (s)
     pointer = s;
-  
+
   /* skip to first non-whitespace char. */
   while (*pointer && isspace (*pointer)) pointer++;
 
@@ -528,6 +532,9 @@ init_defaults (void)
 
 #ifdef USE_XFT_FONT
   defaults.font_string = xstrdup (DEFAULT_XFT_FONT);
+# ifdef HAVE_LANGINFO_CODESET
+  defaults.utf8_xft    = !strcmp (nl_langinfo (CODESET), "UTF-8");
+# endif /* default to 0 if nl_langinfo is unavailable */
 #else
   /* Attempt to load a font */
   defaults.font = load_query_font_set (dpy, DEFAULT_FONT);
diff --git a/src/manage.c b/src/manage.c
index f822c53..d9eb630 100644
--- a/src/manage.c
+++ b/src/manage.c
@@ -204,7 +204,12 @@ get_wmname (Window w)
   char** cl;
 
   if (XGetWMName(dpy, w, &text_prop) != 0) {
-    status = XmbTextPropertyToTextList(dpy, &text_prop, &cl, &n);
+#if defined (USE_XFT_FONT) && defined (X_HAVE_UTF8_STRING)
+    if (defaults.utf8_xft)
+      status = Xutf8TextPropertyToTextList (dpy, &text_prop, &cl, &n);
+    else
+#endif
+      status = XmbTextPropertyToTextList(dpy, &text_prop, &cl, &n);
     if (status == Success && cl && n > 0) {
       name = xstrdup(cl[0]);
       XFreeStringList(cl);
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (OpenBSD)

iQIcBAEBCgAGBQJP8cLAAAoJEGGRj78GoRSUPQwP/R8rVv3Z69Zqqt08aj01V7zD
wi2NJNJQmEJF/dKJQm9uPDvGC3btiUGHDqskzxygWKFI/Rx5eWAlby3RYJG4ThlG
5PaltUxxamtCkK2+ubTNS2L+Xi5d/6xgkB8y0nqueDW8ZKz4txOiy949HU89lS9I
zapKLnKl7tmAuAWTiqFCqJTxC8kUYbVpvoRddojaT2lNbteI4ZYlGE5Q27B8D3rI
rpjf/Rhe5xhWHDI9V2d0XnL9Eaem0wRZFkGkD4XJigWP4RNKuHj1wJ7HLRHinTn+
64BpAqA2EtthMum31rH5mgTxG+TXdpXgk3V/0M6OIBtcB0joYV6bb/sASSv3lWg+
TZ97Tl/qenxeT8WoVme5MYDipiJZysCLP2DsMw+UKTS8b1QDVWxbbS1v8KMRH76/
TsAHvy27lqdJVYsUhGEck6dnduwoUOdYX6VFaFsgu2p9YFK/RjzhvR5V22JDN2BF
X6mco8yeO+MbLNoAVduj6jrcF2PgAiUlQe2AzaQTGfa0LgDCe4zkCe1n8NRFItmf
QQ11MLQWnfQMgiTEdXHJ5Ecg/jUQy9TzK5+ukQ5Fej929XtAlIeSMPUUhHP2ATHj
olERI6zuk07/CRmS69P623voaFH3e2gxrInjBh11ISREzU8InYgOgAr2QgBl5YCk
ri0ykY+liTkW7F2B+LXj
=FW/q
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.