more utf8 #ifdefs



to fix some more issues related to utf8 (GTK_TALKS_UTF8_WE_DONT)
and our previously broken xml files (due to missing encoding)
I've produced the following patch.

to 'try to not silently change the logic, while avoiding crashes'
and because all those char conversion logic is a litte out of my
usual buisiness here are my suggested changes for review.

[
 It fixes #59982 and should reduce all 'missing encoding' 
 complains to only files which may have the problem 
 (=highest bit used)
]

Ok to apply ?
        Hans

diff --exclude-from=c:\util\tool\diff.ign -u -r
from-cvs/dia/app/render_gdk.c my-gtk/dia/app/render_gdk.c
--- from-cvs/dia/app/render_gdk.c       Sat Aug 11 13:46:50 2001
+++ my-gtk/dia/app/render_gdk.c Fri Sep 14 18:44:24 2001
@@ -938,7 +938,15 @@
   ddisplay_transform_coords(ddisp, pos->x, pos->y,
                            &x, &y);
   
+# if defined (GTK_TALKS_UTF8_WE_DONT)
+  {
+    utfchar *utfbuf = charconv_local8_to_utf8(text);
+    iwidth = gdk_string_width(renderer->gdk_font, utfbuf);
+    g_free(utfbuf);
+  }
+# else
   iwidth = gdk_string_width(renderer->gdk_font, text);
+# endif
 
   switch (alignment) {
   case ALIGN_LEFT:
@@ -954,9 +962,19 @@
   color_convert(color, &gdkcolor);
   gdk_gc_set_foreground(gc, &gdkcolor);
       
+# if defined (GTK_TALKS_UTF8_WE_DONT)
+  {
+    utfchar *utfbuf = charconv_local8_to_utf8(text);
+    gdk_draw_string(renderer->pixmap,
+                   renderer->gdk_font, gc,
+                   x,y, utfbuf);
+    g_free(utfbuf);
+  }
+# else
   gdk_draw_string(renderer->pixmap,
                  renderer->gdk_font, gc,
                  x,y, text);
+# endif
 #endif
 }
 
@@ -985,7 +1003,15 @@
 #ifdef HAVE_FREETYPE
   iwidth = freetype_load_string(text, renderer->freetype_font, length);
 #else
+# if defined (GTK_TALKS_UTF8_WE_DONT)
+  {
+    utfchar *utfbuf = charconv_local8_to_utf8(text);
+    iwidth = gdk_string_width(renderer->gdk_font, utfbuf);
+    g_free(utfbuf);
+  }
+# else
   iwidth = gdk_text_width(renderer->gdk_font, text, length);
+# endif
 #endif
 
   return ddisplay_untransform_length(renderer->ddisp, (real) iwidth);
diff --exclude-from=c:\util\tool\diff.ign -u -r from-cvs/dia/lib/charconv.c
my-gtk/dia/lib/charconv.c
--- from-cvs/dia/lib/charconv.c Fri Sep 14 13:11:56 2001
+++ my-gtk/dia/lib/charconv.c   Fri Sep 14 18:53:48 2001
@@ -73,7 +73,11 @@
 #ifdef HAVE_UNICODE 
   local_is_utf8 = unicode_get_charset(charset);
 #else
+# if GLIB_CHECK_VERSION(1,3,0)
+  local_is_utf8 = g_get_charset (charset);
+# else
   *charset = NULL;
+# endif
 #endif
   if (local_is_utf8) {
       this_charset = *charset;
@@ -255,13 +259,21 @@
 extern utfchar *
 charconv_local8_to_utf8(const gchar *local)
 {
+#if GLIB_CHECK_VERSION(1,3,1)
+  return g_locale_to_utf8 (local, -1, NULL, NULL, NULL);
+#else
   return g_strdup(local);
+#endif
 }
 
 extern gchar *
 charconv_utf8_to_local8(const utfchar *utf)
 {
-  return g_strdup(utf);
+#if GLIB_CHECK_VERSION(1,3,1)
+  return g_locale_from_utf8 (utf, -1, NULL, NULL, NULL);
+#else
+  return g_strdup(local);
+#endif
 }
 
 
diff --exclude-from=c:\util\tool\diff.ign -u -r from-cvs/dia/lib/dia_xml.c
my-gtk/dia/lib/dia_xml.c
--- from-cvs/dia/lib/dia_xml.c  Sun Sep 09 16:45:36 2001
+++ my-gtk/dia/lib/dia_xml.c    Fri Sep 14 17:19:22 2001
@@ -77,6 +77,7 @@
   int len;
   gchar *tmp,*res;
   int uf;
+  gboolean well_formed_utf8;
 
   static char magic_xml[] = 
   {0x3c,0x3f,0x78,0x6d,0x6c,0x00}; /* "<?xml" in ASCII */
@@ -125,6 +126,22 @@
     gzclose(zf); /* this file has an encoding string. Good. */
     return filename;
   }
+  /* now let's read the whole file, to see if there are offending bits.
+   * We can call it well formed UTF-8 if the highest isn't used
+   */
+  well_formed_utf8 = TRUE;
+  do {
+    int i;
+    for (i = 0; i < len; i++)
+      if (buf[i] & 0x80)
+        well_formed_utf8 = FALSE;
+    len = gzread(zf,buf,BUFLEN);
+  } while (len > 0 && well_formed_utf8);
+  if (well_formed_utf8) {
+    gzclose(zf); /* this file is utf-8 compatible  */
+    return filename;
+  }
+
   if (0 != strcmp(default_enc,"UTF-8")) {
     message_warning(_("The file %s has no encoding specification;\n"
                       "assuming it is encoded in %s"),filename,default_enc);

-------- Hans "at" Breuer "dot" Org -----------
Tell me what you need, and I'll tell you how to 
get along without it.                -- Dilbert




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]