简体   繁体   English

Graphics.DrawString与TextRenderer.DrawText绘制朝鲜语字符

[英]Graphics.DrawString vs TextRenderer.DrawText drawing korean characters

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. 我试图将我的Graphics.DrawString调用(.NET1)迁移到TextRenderer.DrawText (.NET 2.0中的新增功能),以获取ClearType呈现的优点。

The problem is that TextRenderer does not print occidental characters correctly (korean, japanese, etc...) 问题是TextRenderer无法正确打印西方字符(韩文,日文等)。

Here is an example that shows the issue: 这是显示问题的示例:

在此处输入图片说明

  • Do you know why the korean chars are not seen when using TextRenderer.DrawText? 您知道使用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) 编辑:使用其他字体可以正常工作(使用的字体Malgun Gothic)

在此处输入图片说明

So now my questions are: 所以现在我的问题是:

  • Why Graphics.Drawtext draws korean chars even if the font does not support them? 为什么Graphics.Drawtext即使字体不支持韩式字符,也会绘制韩式字符?
  • I pasted the corean text in the Visual Studio editor, that uses "Consolas" font and it is drawn correctly. 我将corean文本粘贴到Visual Studio编辑器中,该编辑器使用“ Consolas”字体,并且可以正确绘制。 So, why Visual Studio editor can show korean characters, and a textbox cannot? 那么,为什么Visual Studio编辑器可以显示朝鲜语字符,而文本框却不能呢?

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). 事实是,当编辑者知道他们知道的Unicode范围中不支持的字符时,便会更改其默认Unicode字体的字体(在您的情况下,可能是Arial Unicode Sans MS或MS-Mincho需要CJK Unicode定义)。 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. 这就是为什么在编译之前必须知道是否需要Unicode以及在这种情况下要使用哪种字体的原因。

So for your other question, why DrawString success to do the font-switch while DrawText wasn't able to. 因此,对于您的另一个问题,为什么DrawString成功地完成了DrawText不能执行的字体切换。 The secret is in your StringFormat.GenericTypographic flag you setup. 秘密在您设置的StringFormat.GenericTypographic标志中。 GenericTypographics default contains Language ID set to neutral language, GenericTypographics默认包含将Language ID设置为中性语言,

which means that the current language associated with the calling thread is used. 这意味着将使用与调用线程关联的当前语言。 [1] [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. 由于您使用的是CJK输入字符串,因此您的调用线程可能知道将语言设置为CJK语言集,然后通过使用通用的Unicode字体开关来确保显示正确的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM