none
How can I get the language of the fontfamily? RRS feed

  • Question

  • I wanna create a ComboBox to show all the font on the computer.

    And I want it to order by the language of the font in the ComboBox.

    For example, the list will show the English font first, and then show the German font, and then show the Chinese font and so on. Each language font will separate but no mix together.

    Here is a sample of Photoshop:

    I found that the Adobe Photoshop/Microsoft Word/Windows Notepad and many software can do this.

    Now the question is how can I do this in C# or WPF? Would you please help me? Thank you.

    Someone said that font has not any language, but how can Microsoft&Adobe achieve this? Adobe won't tell me any but I want official of Microsoft tells me more about this.

    Saturday, April 6, 2019 5:39 AM

All replies

  • You can use FontFamily.FamilyNames and enumerate LanguageSpecificStringDictionary


    Saturday, April 6, 2019 9:29 AM
  • You can use FontFamily.FamilyNames and enumerate LanguageSpecificStringDictionary


    I tried what you said, but the keys of the LanguageSpecificStringDictionary is not always right. Sometimes the key of a Chinese font is "en-us". I think that's because the author of the font has not set it right before.
    Saturday, April 6, 2019 1:40 PM
  • I had tested with some chinese/japanese fonts and I get other languages

    For example :

    [en-us, MS UI Gothic]
    [ja-jp, MS UI Gothic]

    [en-us, Microsoft JhengHei UI]
    [zh-hk, Microsoft JhengHei UI]
    [zh-tw, Microsoft JhengHei UI]

    Saturday, April 6, 2019 2:05 PM
  • I had tested with some chinese/japanese fonts and I get other languages

    For example :

    [en-us, MS UI Gothic]
    [ja-jp, MS UI Gothic]

    [en-us, Microsoft JhengHei UI]
    [zh-hk, Microsoft JhengHei UI]
    [zh-tw, Microsoft JhengHei UI]

    Fonts which make by Microsoft will strict to this. But the font does not make by Microsoft or just DIY by someone always set it to en-us. I am afraid of this.
    Saturday, April 6, 2019 3:33 PM
  • The list of the fonts can be obtained with FontFamily.FontFamilies.

    In order to determine the available character sets for each font, try a code converted from C++. An example for “Dubai” font:

     

    LOGFONTW lf = new LOGFONTW( );

    lf.lfCharSet = 1;

    lf.lfPitchAndFamily = 0;

    lf.lfFaceName = "Dubai";

     

    EnumFontFamiliesExDelegate d = ( ref LOGFONTW lplf, IntPtr lpntme, UInt32 FontType, IntPtr lParam ) =>

    {

        Console.WriteLine( lplf.lfFaceName + ", CharSet: " + lplf.lfCharSet );

     

        return 1;

    };

     

    IntPtr hdc = GetDC( IntPtr.Zero );

    var r = EnumFontFamiliesExW( hdc, ref lf, d, IntPtr.Zero, 0 );

    ReleaseDC( IntPtr.Zero, hdc );

     

    Also add the definitions:

     

    [StructLayout( LayoutKind.Sequential, CharSet = CharSet.Unicode )]

    public struct LOGFONTW

    {

        public int lfHeight;

        public int lfWidth;

        public int lfEscapement;

        public int lfOrientation;

        public int lfWeight;

        public byte lfItalic;

        public byte lfUnderline;

        public byte lfStrikeOut;

        public byte lfCharSet;

        public byte lfOutPrecision;

        public byte lfClipPrecision;

        public byte lfQuality;

        public byte lfPitchAndFamily;

        [MarshalAs( UnmanagedType.ByValTStr, SizeConst = 32 )]

        public string lfFaceName;

    }

     

    public delegate int EnumFontFamiliesExDelegate( ref LOGFONTW lplf, IntPtr lpntme, UInt32 FontType, IntPtr lParam );

     

    [DllImport( "gdi32", CharSet = CharSet.Unicode )]

    static extern int EnumFontFamiliesExW( IntPtr hdc, ref LOGFONTW lpLogfont, EnumFontFamiliesExDelegate lpEnumFontFamExProc, IntPtr lParam, UInt32 dwFlags );

     

    [DllImport( "user32" )]

    static extern IntPtr GetDC( IntPtr hWnd );

     

    [DllImport( "user32" )]

    static extern IntPtr ReleaseDC( IntPtr hWnd, IntPtr hdc );

     

    It will display character sets, such as 0 (ANSI) and 178 (Arabic). The meaning can be found in documentation for Font.GdiCharSet and in <wingdi.h> file of Windows API.



    • Edited by Viorel_MVP Saturday, April 6, 2019 7:40 PM
    Saturday, April 6, 2019 7:36 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 8, 2019 2:42 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Someone tell me to use the LanguageSpecificStringDictionary.Keys to get it.

    However, I tested it and found many fonts output en-us even it is not an English font.

    I have a strange idea that the font name of a Chinese font must contain Chinese, and the font name of a Japanese font must contain Japanese. So I can get its language via its font name by whether contain a character of their country.

    I know it is not a standard way, but I have no other way to do it yet. 

    It seems you are the engineer of Microsoft. Would you please tell me how Microsoft Word achieve this? Thank you.


    Saturday, April 13, 2019 3:25 PM
  • However, I tested it and found many fonts output en-us even it is not an English font.

    Which fonts ouput only en-us ?

    They outpout en-us + chinese languages

    Saturday, April 13, 2019 4:04 PM
  • However, I tested it and found many fonts output en-us even it is not an English font.

    Which fonts ouput only en-us ?

    They outpout en-us + chinese languages

    I tested several fonts, especially third-party(most of them are made by individuals studios but not a company) fonts. I think it is not the problem of the code but the author of the font do not set the right LanguageSpecificString when they make the font.

    Whereas, Adobe/Microsoft still can get the right language of these third-party. I doubt if they are using the same way as I think which by checking the language of the font name.

    Sunday, April 14, 2019 4:09 AM
  • Hi

    Thanks for the feedback.

    Based on your description, you want to create a ComboBox to show all the font on the computer. However, a combox can only store one font. So, I create the label to get your goal.

    You could try the following code.

     private void button1_Click(object sender, EventArgs e)
            {
                int count = 0;
    
                int num = 0;
                int b = 0;
                foreach (FontFamily font in FontFamily.Families)
                {
                    b++;
                }
                for (int i = 0; i < b-1; i++)
                {
    
                    Label text = new Label();
    
                    if (count >= 10)
    
                    {
    
                        count = 0;
    
                        num++;
    
                    }
    
                    text.Location = new Point(count * text.Width, num * 20);
    
                    count++;
    
                    Controls.Add(text);
    
                }
                List<string> list = new List<string>();
               
                    foreach (FontFamily font in FontFamily.Families)
                    {
                    list.Add(font.Name);
                    }
                int p =1;
                foreach (Control item in Controls)
                {
                    if(item is Label)
                    {
                        item.Text = list[p];
                        item.Font = new Font(list[p], 10);
                        p++;
                    }
                }  
                
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 25, 2019 9:58 AM
    Moderator