[Mono-bugs] [Bug 473758] Calling methods on Graphic after passing unicode text to MeasureString fires errors

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Wed Sep 16 09:18:52 EDT 2009


User mono at esar.org.uk added comment

stephen robinson <mono at esar.org.uk> changed:

           What    |Removed                     |Added
                 CC|                            |mono at esar.org.uk

--- Comment #1 from stephen robinson <mono at esar.org.uk>  2009-09-16 07:18:51 MDT ---
I've been doing some more work on the program of mine that originally found
this bug (or more specifically #468987) and have had a chance to trace the
problem a bit further.

I should warn you, I know almost nothing about unicode so some of the following
might not be quite right.

The problem appears to be that the byte sequence 0xD8 0xD9 doesn't represent a
valid character in UTF-16, the range 0xD800 to 0xDFFF is reserved for surrogate
pairs. Specifically the range 0xD800 to 0xDBFF is reserved for the high bytes
of the pairs and the range 0xDC00 to 0xDFFF is reserved for the low byte of the
pairs. So we can see that the sequence 0xD8 0xD9 is two high bytes and is

This doesn't cause a problem on microsoft's implementation of MeasureString(),
I haven't checked the exact behaviour but it silently handles the bad sequence
and everything just works.

In the mono implementation we get the GDI+ generic error. This error is
produced because MeasureString() calls CalculateStringWidths() in text-cairo.c
in libgdiplus. CalculateStringWidths() calls utf8_encode_ucs2char() which
blindly converts the invalid UTF-16 to UTF-8. CalculateStringWidths() then
calls cairo_text_extents() with the bad string. This results in the cairo
status variable in graphics->ct containing an error code. When cairo_status()
is next called we get the GDI+ generic error. In the example code this is when
FillRectangle() calls fill_graphics_with_brush() which calls cairo_status() at
the end.

I don't know how this should be fixed, whether the bad codes should be stripped
from the string before measuring or if the status code should be reset. I've
run out of time again to look any further so I thought I'd provide the above in
the hope that it helps someone else work out a fix.

Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.

More information about the mono-bugs mailing list