1

I'm trying to migrate my Graphics.DrawString calls (.NET1) to TextRenderer.DrawText (new in .NET 2.0) to get the advantages of the ClearType rendering.

The problem is that TextRenderer does not print occidental characters correctly (korean, japanese, etc...)

Here is an example that shows the issue:

enter image description here

  • Do you know why the korean chars are not seen when using TextRenderer.DrawText?
  • Do you know how fix this issue?

I'm drawing the strings using the following two methods:

    private void DrawGraphicsString(
        Graphics g, string text, Font font, Point p)
    {
        g.DrawString(
            text, font, text_brush, p.X, p.Y, mStringFormat);
        // mStringFormat is
        // StringFormat.GenericTypographic | 
        // StringFormatFlags.MeasureTrailingSpaces

    }

    private void DrawTextRendererString(
         Graphics g, string text, Font font, int x, int y)        
    {
        TextRenderer.DrawText(
            g, text, font, p, this.ForeColor, this.BackColor, mTextFormatFlags);

        // mTextFormatFlags are
        // StringFormat.GenericTypographic + StringFormatFlags.MeasureTrailingSpaces
        // mTextFormatFlags = 
        //    TextFormatFlags.NoPrefix |
        //    TextFormatFlags.NoPadding |
        //    TextFormatFlags.WordBreak |
        //    TextFormatFlags.TextBoxControl |
        //    TextFormatFlags.NoClipping |
        //    TextFormatFlags.ExpandTabs;
    }

EDIT: Using other font it works correctly (used font Malgun Gothic)

enter image description here

So now my questions are:

  • Why Graphics.Drawtext draws korean chars even if the font does not support them?
  • I pasted the corean text in the Visual Studio editor, that uses "Consolas" font and it is drawn correctly. So, why Visual Studio editor can show korean characters, and a textbox cannot?
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
  • 4
    Seeing rectangles means you're using a font that doesn't support the glyphs. Odd problem to have on Vista+, especially since you do appear to have at least one font that supports them. Can't reverse-engineer code from a screenshot. – Hans Passant Mar 15 '12 at 16:58
  • @HansPassant: The font is the same for the two cases, so I not sure if there is a font problem. – Daniel Peñalba Mar 15 '12 at 17:21
  • Like Hans, I'm not convinced that just because one function displays the font properly it means the other should, too. Why don't run through the fonts on your machine and see what affect that has? – Brad Rem Mar 15 '12 at 17:46
  • You are right, it was the font, but I cannot understand why Visual Studio can draw korean text using Consolas, and my textbox cannot – Daniel Peñalba Mar 15 '12 at 17:55

1 Answers1

2

The fact is that editors change font for their default Unicode font when having unsupported characters from a Unicode range they know (in your case, the CJK Unicode definition, that probably calls for Arial Unicode Sans MS or MS-Mincho). Meanwhile, force-rendering with a certain font don't allow this font-switch.

That's why you must know before compiling if you need Unicode and which font you want to use in this case.

So for your other question, why DrawString success to do the font-switch while DrawText wasn't able to. The secret is in your StringFormat.GenericTypographic flag you setup. GenericTypographics default contains Language ID set to neutral language,

which means that the current language associated with the calling thread is used. [1]

Since you are using a CJK input string, probably your calling thread knows the language set to a CJK language set, and then, ensure your display show the correct symbols by having it's generic unicode font switch.

Miraino Hikari
  • 266
  • 3
  • 12