On Tue, 2005-08-02 at 22:19 +0200, Jean-Marc Lasgouttes wrote:
> John> Hi, Attached patch hacks around a crash I encountered scrolling
> John> through the user guide in lyx-gtk. It would be nice to track
> John> down what was passing font_metrics::width character counts
> John> longer than the string passed, but one thing at a time :-)

So it turns out the big character counts were actually -1 error return
values being cast to a size_t.  I've added an error check to the
mbstowcs call that was generating them.  It would be nice to handle all
this encoding properly, but my plan is to cover my ears and hum while I
wait for Unicode to happen.  These "invalid" strings don't actually
appear on the screen either, but I'm not sure why that is.

Okay to apply the attached?  I'd rather not assert that the conversion
should be valid, since that in fact isn't the case: the strings are
fine, the gtk/xft code we've got is handling them wrongly I think.

John
? tmp
Index: ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/gtk/ChangeLog,v
retrieving revision 1.131
diff -u -p -r1.131 ChangeLog
--- ChangeLog	31 Jul 2005 22:43:27 -0000	1.131
+++ ChangeLog	2 Aug 2005 22:11:44 -0000
@@ -1,3 +1,8 @@
+2005-07-02  John Spray  <[EMAIL PROTECTED]>
+	* xftFontMetrics.C: Add error checking to call to mbstowcs 
+	in font_metrics::width, estimate width for strings that  
+	mbstowcs doesn't like.
+
 2005-07-31  John Spray  <[EMAIL PROTECTED]>
 	* GToc.C: unbreak viewing lists other than TOC by storing 
 	integer index of entries in Type combobox.
Index: xftFontMetrics.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/gtk/xftFontMetrics.C,v
retrieving revision 1.9
diff -u -p -r1.9 xftFontMetrics.C
--- xftFontMetrics.C	29 Jan 2005 15:09:14 -0000	1.9
+++ xftFontMetrics.C	2 Aug 2005 22:11:44 -0000
@@ -27,6 +27,7 @@
 #include "codeConvert.h"
 
 #include "support/lstrings.h"
+#include "debug.h"
 
 #include <gtkmm.h>
 
@@ -225,15 +226,20 @@ int width(wchar_t c,LyXFont const & f)
 int width(char const * s, size_t n, LyXFont const & f)
 {
 	boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
-	size_t len;
+	int len; // Signed to handle error retvals
 	if (fontLoader.isSpecial(f)) {
 		unsigned char const * us =
 			reinterpret_cast<unsigned char const *>(s);
 		len = n;
 		std::copy(us, us + n, wcs.get());
-	} else
+	} else {
 		len = mbstowcs(wcs.get(), s, n);
-	return width(wcs.get(), len, f);
+		if (len < 0) {
+			lyxerr[Debug::FONT] << "Invalid multibyte encoding! '" << s << "'\n";
+			return n * width("0", 1, f);
+		}
+	}
+	return width(wcs.get(), static_cast<size_t>(len), f);
 }
 
 

Reply via email to