Text Mess in .NET

6/12/2006 11:16:48 PM

Text Mess in .NET

A major weakness in GDI+ is text handling. The GDI+ “undocumentation” in MSDN has been the primary source of my grievances.

About the only good features in GDI+’s text support are device independence and floating-point coordinates. Vanilla GDI includes more API calls, performs much faster and enables complex scripts. GDI use different, incompatible layout algorithms from GDI+, so that text drawn in GDI also looks different from it GDI+ counterpart. .NET 2.0 includes a new class System.Windows.Forms.TextRenderer for rendering consistent-looking text in system controls.

The text drawing functions in GDI+ are limited in number and require a string copy for each call, making it difficult to create a performant text-layout engine. The standard method MeasureCharacterRanges allocates a number of heavy objects; this is quite expensive for operation that is likely to be called frequently. There are a few ways out by using MeasureString such as pinvoking to gdiplus.dll, writing unsafe code, or using reflection to obtain the private backing string of a StringBuilder. In the latter case, if the string is sufficiently large, it effectively becomes a mutable string which can be manipulated through the StringBuilder’s methods; this avoids a lot of copying when measuring text; however, this approach may break in future versions of the framework.

The default settings for GDI+ are unsuitable for advanced text layout. The casual user will discover that MeasureText does not actually measure text correctly. One must always use a string format derived from StringFormat.GenericTypographic and turn on anti-aliasing text rendering. The only source of this information use to be this KnowledgeBase article.

Comments

 

Navigation

Categories

About

SoftPerson develops innovative new desktop software applications by incorporating artificial intelligence and natural language technologies to bring human-like intelligence to everyday applications.

Social Media