Index: app/text/gimpfontlist.c
===================================================================
RCS file: /cvs/gnome/gimp/app/text/gimpfontlist.c,v
retrieving revision 1.11
diff -u -2 -r1.11 gimpfontlist.c
--- app/text/gimpfontlist.c	15 Jan 2004 22:02:30 -0000	1.11
+++ app/text/gimpfontlist.c	28 Mar 2004 06:04:14 -0000
@@ -151,6 +151,7 @@
                          PangoFontDescription *desc)
 {
-  GimpFont *font;
-  gchar    *name;
+  GimpFont    *font;
+  gchar       *name;
+  const gchar *end;
 
   if (desc == NULL)
@@ -159,4 +160,49 @@
   name = pango_font_description_to_string (desc);
 
+  /* This code block probably should be made into an utility function
+   * in a library.
+   */
+  if (!g_utf8_validate (name, -1, &end))
+    {
+      /* The font name isn't valid UTF-8. Use a heuristic: If there
+       * seems just to be a few junk bytes at the end of a long enough
+       * ASCII string, simply drop those junk bytes.
+       */
+      gchar *p;
+
+      for (p = name; p < end; p++)
+	if (((guchar) *p) >= 0x80)
+	  break;
+      if (p >= name + 4 && name + strlen (name) <= p + 4)
+	{
+	  /* At least 4 ASCII chars followed by some junk. Use the
+	   * ASCII chars.
+	   */
+	  gchar *oldname = name;
+	  name = g_strndup (oldname, p - oldname);
+	  g_free (oldname);
+	}
+      else
+	{
+	  /* Seems rather random. Some unknown charset and encoding.
+	   * Try convert from current locale.
+	   */
+	  gchar *newname = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
+
+	  if (newname != NULL)
+	    {
+	      g_free (name);
+	      name = newname;
+	    }
+	  else
+	    {
+	      /* Not current locale. Oh well, use g_strescape. */
+	      newname = g_strescape (name, "\\\"");
+	      g_free (name);
+	      name = newname;
+	    }
+	}
+    }
+
   font = g_object_new (GIMP_TYPE_FONT,
                        "name",          name,
