About

I am a software developer in Seattle, building a new AI software company.

Ads

April 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

Categories

Ads


« MSDN Wiki Post | Main | Software Reliability »

June 12, 2006

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

Wait until you try getting performance out of WPF Text.

This reminds me of a document I read which states that text in Avalon are rendered as collections of triangles sent to GPU.

What?! Text in WPF are rendered as triangles from the GPU? That would be 2d triangles, right? (I would hope!)

The comments to this entry are closed.