We need to:
- Measure text accurately.
- Render text line by line to a screen graphics context in the presence of translation and scaling transforms applied to the graphics context.
- Hit testing: allow text to be selected precisely with the mouse or via a displayed caret.
- Print the result if needed, and as accurately as possible, with a printer. Note: this is secondary. Screen rendering and hit testing are primary.
- Run on Windows XP and higher operating systems.
within a WinForms application that is also rendering graphics and images to the same graphics context.
There are four technologies that we've encountered. We've tried using the first two, and ran into the issues described, over the course of several months.
GDI+
Purportedly resolution-independent text. However according to this question - and other sources - this technology is to be avoided because of quality issues.
MSDN states that calling Graphics.MeasureString along with StringFormat.GenericTypographic
and TextRenderingHint.AntiAlias
produces accurate string measurement. However, in our experience, and that of others, this is not the case - we do not get accurate string measurements.
- Pros: Fast
- Cons: inaccurate string measurement.
Result: unusable because of inaccurate string measurement.
GDI via TextRenderer
This was introduced to overcome the limitations of GDI+. However this introduced limitations of its own:
- Very slow
- Does not work with graphics transforms
Result: unusuable for these reasons
GDI via p/invoke
Calling GetTextExtentExPoint
for text measurement and DrawText
/ DrawTextEx
/ ExtTextOut
for rendering.
We haven't tried this yet.
DirectWrite
This seems promising, since it interoperates with other technologies including GDI/GDI+, so presumably the rest of our graphics rendering wouldn't change. However it is only available for Windows Vista and more recent Windows versions. This is presently a problem since Windows XP still has a significant installed base.
Question
Which of these technologies can be made to work given the requirements?
Note: There's much misinformation about this topic floating around, so please answer this question only if you have expertise in this area. Also, please don't suggest WPF - that isn't something we're considering using.