title: Accurately measuring, rendering, hit testing and printing text in a WinForms application
body: <p>We need to:</p>
  
  <ol>
  <li>Measure text accurately.</li>
  <li>Render text line by line to a screen graphics context in the presence of translation and scaling transforms applied to the graphics context.</li>
  <li>Hit testing: allow text to be selected precisely with the mouse or via a displayed caret.</li>
  <li>Print the result if needed, and as accurately as possible, with a printer. <em>Note:</em> this is secondary. Screen rendering and hit testing are primary.</li>
  <li>Run on Windows XP and higher operating systems.</li>
  </ol>
  
  <p>within a WinForms application that is also rendering graphics and images to the same graphics context.</p>
  
  <p>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.</p>
  
  <h2>GDI+</h2>
  
  <p>Purportedly resolution-independent text. However according to <a href="https://stackoverflow.com/questions/7268238/drawing-text-in-net/7269191">this question</a> - and other sources - this technology is to be avoided because of quality issues. </p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/6xe5hazb(VS.80).aspx" rel="nofollow noreferrer">MSDN</a> states that calling Graphics.MeasureString along with  <code>StringFormat.GenericTypographic</code> and <code>TextRenderingHint.AntiAlias</code> produces accurate string measurement. However, in our experience, and that of others, this is not the case - we do not get accurate string measurements.</p>
  
  <ul>
  <li>Pros: Fast</li>
  <li>Cons: inaccurate string measurement.</li>
  </ul>
  
  <p>Result: unusable because of inaccurate string measurement.</p>
  
  <h2>GDI via TextRenderer</h2>
  
  <p>This was introduced to overcome the limitations of GDI+. However this introduced limitations of its own:</p>
  
  <ul>
  <li>Very slow</li>
  <li>Does <a href="http://social.msdn.microsoft.com/Forums/zh/winforms/thread/9a2d277d-340b-4d39-9cd0-f59fa9719aa1" rel="nofollow noreferrer">not work with</a> graphics transforms</li>
  </ul>
  
  <p>Result: unusuable for these reasons</p>
  
  <h2>GDI via p/invoke</h2>
  
  <p>Calling <code>GetTextExtentExPoint</code> for text measurement and <code>DrawText</code> / <code>DrawTextEx</code> / <code>ExtTextOut</code> for rendering.</p>
  
  <p>We haven't tried this yet.</p>
  
  <h2>DirectWrite</h2>
  
  <p>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. </p>
  
  <h1>Question</h1>
  
  <p>Which of these technologies can be made to work given the requirements? </p>
  
  <p><em>Note:</em>  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.</p>