Question

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?
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top