Results 1 to 7 of 7

Thread: [RESOLVED] Charsets, Fonts and Codepage questions

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [RESOLVED] Charsets, Fonts and Codepage questions

    Hello everybody,

    I have a question about charsets. I have an application in which on load I initialize the fonts the form and controls using whatever fonts are set in the users windows. Now I am porting this application to use arabic fonts. I am able to this by simply calling the charset property


    obj.Font.Charset = 178 'Arabic Charset
    My question is this, what if the current font doesn't support arabic script? I can always set the font name

    obj.Font.Name = "Traditional Arabic" 'Some arabic font
    but would like to use the font that is set in the users windows interface.

    Here are my questions

    1. Is there way of knowing if the current font can display arabic script or not? And if so use it, and if not then use a specfic arabic font?

    2. What is the best way of determining if a system can display arabic fonts? Is it GetSystemMetrics(SM_MIDEASTENABLD) the only way or is there something else?

    3. Should the codepage be set? Or by setting the charset that is enough?

    4. I read this on the internet

    cmdMyButton.Font.Charset = charsetCyrillic
    cmdMyButton.Font.Name = ""
    cmdMyButton.Caption = "Чао а тутто ил мондо!"

    Setting the font name to an empty strings causes the Font object to choose the default font for the control's code page. Of course, you can also explicitly set a font name that you know supports that code page.
    Does this mean that I just use the above code and it will select the default font that works with that specfic charset?

    Thanks for your assistance.
    Last edited by Hassan Basri; Sep 11th, 2009 at 01:43 PM.

  2. #2
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: Charsets, Fonts and Codepage questions

    Different parts of the GUI use different fonts.
    Code:
    Control Panel -> Display -> Appearance -> Advanced
    However, I only see SPI_GETICONTITLELOGFONT for the SPI API:

    http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx

    Quote Originally Posted by MSDN
    SPI_GETICONTITLELOGFONT Retrieves the logical font information for the current icon-title font. The uiParam parameter specifies the size of a LOGFONT structure, and the pvParam parameter must point to the LOGFONT structure to fill in.
    So, you need to use Theme based API, GetThemeFont.

    http://msdn.microsoft.com/en-us/libr...45(VS.85).aspx
    With Font based PropertyIdentifiers:

    Code:
    TMT_FONT
    
    TMT_BODYFONT  
    TMT_CAPTIONFONT  
    TMT_GLYPHFONT
    TMT_HEADING1FONT  
    TMT_HEADING2FONT  
    TMT_ICONTITLEFONT  
    TMT_MENUFONT  
    TMT_MSGBOXFONT  
    TMT_SMALLCAPTIONFONT  
    TMT_STATUSFONT
    By using GetThemeFont to acquire the LOGFONT, you can check the charset member:

    Member
    Code:
    lfCharSet
    Relevant Values
    Code:
    Middle East language edition of Windows:
    ARABIC_CHARSET 
    HEBREW_CHARSET
    3. Should the codepage be set? Or by setting the charset that is enough?
    Where?

    Yes, Question 4 is true. From MSDN LOGFONT:
    .lfFaceName
    A null-terminated string that specifies the typeface name of the font. The length of this string must not exceed 32 TCHAR values, including the terminating NULL. The EnumFontFamiliesEx function can be used to enumerate the typeface names of all currently available fonts. If lfFaceName is an empty string, GDI uses the first font that matches the other specified attributes.
    Hope this helps
    Last edited by Xiphias3; Sep 11th, 2009 at 02:40 PM. Reason: Stupid type errors....

  3. #3

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Charsets, Fonts and Codepage questions

    Thank you for your answers.

    I get the system font and then apply that font to all the controls and form. For example when I get my current font it is Tahoma.

    If I assign the charset

    obj.Font.Charset = 178

    under the system font (which in my case happened to be Tahoma) it works correctly. However let us say the current system font wasn't compatible with the arabic script. Just assigning the charset will not work.

    Basically in pseudo-code I would like to do the following:

    Code:
    IF SystemFontIsEnabledForArabicScript Then
         obj.Font.Charset = 178
         obj.Font.Name = "SystemFont"
    ELSE
         obj.Font.Charset = 178
         obj.Font.Name = ""    'Which will take the first compatible font
    END IF
    I don't know how to determine if current system font is enabled for arabic script or not.

    Also if I enter into the ELSE statement what if there isn't any fonts on the system compatible with the arabic script? Is my GetSystemMetrics(SM_MIDEASTENABLD) enough to tell me that I cannot display arabic?

    Thanks again for your assistance.
    Last edited by Hassan Basri; Sep 11th, 2009 at 07:00 PM.

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Charsets, Fonts and Codepage questions

    Maybe EnumFontFamiliesEx Function?

    Oops, already mentioned. Sorry!

  5. #5
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: Charsets, Fonts and Codepage questions

    Quote Originally Posted by Hassan Basri
    I get the system font and then apply that font to all the controls and form.
    How do you get the "System" font? What function(s) do you use? Or do you just create a new StdFont Object and let VB locate the default font? (in my case, MS Sans Serif. Never saw that one coming! lol), or do you reference a font of a control on application start? I hope you dont mean the bitmap font System.

    Quote Originally Posted by Hassan Basri
    However let us say the current system font wasn't compatible with the arabic script. Just assigning the charset will not work.
    Create a StdFont object from scratch.

    vb Code:
    1. Dim f As StdFont
    2.  
    3. Set f = New StdFont  ' MS Sans Serif (NOT Arabic Compatible)
    4. f.Name = "FixedSys"   ' FixedSys (NOT Arabic Compatible)
    5. f.Charset = 178         ' Arial (Arabic Compatible)
    6. f.Charset = 0            ' Arial (Arabic Compatible)
    7. f.Name = "Tahoma"    ' Tahoma (Arabic Compatible)
    8. f.Charset = 178         ' Tahoma (Arabic Compatible)
    9. f.Name = ""               ' Arial (Arabic Compatible)

    Quote Originally Posted by Hassan Basri
    I don't know how to determine if current system font is enabled for arabic script or not.
    Find the "System" font and check the Charset member.

    vb Code:
    1. Private Declare Function EnumFontFamiliesEx Lib "gdi32.dll" Alias "EnumFontFamiliesExA" (ByVal hdc As Long, ByRef lpLogFont As LOGFONT, ByVal lpEnumFontProc As Long, ByVal lParam As Long, ByVal dw As Long) As Long

    When calling, remember that the LOGFONT struct you're passing can make the function "specialized"
    lfCharset
    If set to DEFAULT_CHARSET (0), the function enumerates all fonts in all character sets. If set to a valid character set value (Like Arabic, 178), the function enumerates only fonts in the specified character set.
    Quote Originally Posted by Hassan Basri
    Is GetSystemMetrics(SM_MIDEASTENABLD) enough to tell me that I cannot display arabic?
    I guess. Check the locale.
    vb Code:
    1. Const LOCALE_USER_DEFAULT = &H400
    2. Const LOCALE_SENGCOUNTRY = &H1002 '  English name of country
    3. Const LOCALE_SENGLANGUAGE = &H1001  '  English name of language
    4. Const LOCALE_SNATIVELANGNAME = &H4  '  native name of language
    5. Const LOCALE_SNATIVECTRYNAME = &H8  '  native name of country
    6. Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
    7. Private Sub Form_Load()
    8.     'KPD-Team 2001
    9.     'URL: [url]http://www.allapi.net/[/url]
    10.     'E-Mail: [email][email protected][/email]
    11.     MsgBox "You live in " & GetInfo(LOCALE_SENGCOUNTRY) & " (" & GetInfo(LOCALE_SNATIVECTRYNAME) & ")," & vbCrLf & "and you speak " & GetInfo(LOCALE_SENGLANGUAGE) & " (" & GetInfo(LOCALE_SNATIVELANGNAME) & ").", vbInformation
    12. End Sub
    13. Public Function GetInfo(ByVal lInfo As Long) As String
    14.     Dim Buffer As String, Ret As String
    15.     Buffer = String$(256, 0)
    16.     Ret = GetLocaleInfo(LOCALE_USER_DEFAULT, lInfo, Buffer, Len(Buffer))
    17.     If Ret > 0 Then
    18.         GetInfo = Left$(Buffer, Ret - 1)
    19.     Else
    20.         GetInfo = ""
    21.     End If
    22. End Function

    Quote Originally Posted by Hassan Basri
    Thanks again for your assistance.
    No problem
    Last edited by Xiphias3; Sep 12th, 2009 at 12:01 AM.

  6. #6

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Charsets, Fonts and Codepage questions

    Thanks again for your assistance,

    I have a class that gets the system font. If you are interested I can post it.

    In any case, I found a very simple solution to the problem. Basically, I get the system font. Then I set the charset, THEN I set the font name. If the charset remains what I set, I know that that font is compatible. Otherwise I use the first default font that is compatible. Here is the code:

    Code:
    'I already got my default font and set it for the form
    
    'Here I store the values
    intCharset = frm.Font.Charset
    strFontName = frm.Font.Name
    
    'Here I set the charset and font
    frm.Font.Charset = m_intFontCharset
    frm.Font.Name = strFontName
    
    'Now I check did the charset change or remain the same?
    If frm.Font.Charset <> m_intFontCharset Then
        'Charset doesn't work with current font so I use the default font by
        'sending an empty string
        frm.Font.Charset = m_intFontCharset
        frm.Font.Name = ""
    Else
        'Current font is compatible with the charset
    End If

  7. #7
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: Charsets, Fonts and Codepage questions

    Quote Originally Posted by Hassan Basri View Post
    Thanks again for your assistance,

    I have a class that gets the system font. If you are interested I can post it.

    In any case, I found a very simple solution to the problem. Basically, I get the system font. Then I set the charset, THEN I set the font name. If the charset remains what I set, I know that that font is compatible. Otherwise I use the first default font that is compatible. Here is the code:

    Code:
    'I already got my default font and set it for the form
    
    'Here I store the values
    intCharset = frm.Font.Charset
    strFontName = frm.Font.Name
    
    'Here I set the charset and font
    frm.Font.Charset = m_intFontCharset
    frm.Font.Name = strFontName
    
    'Now I check did the charset change or remain the same?
    If frm.Font.Charset <> m_intFontCharset Then
        'Charset doesn't work with current font so I use the default font by
        'sending an empty string
        frm.Font.Charset = m_intFontCharset
        frm.Font.Name = ""
    Else
        'Current font is compatible with the charset
    End If

    I was implying that here, but not in so many words:
    vb Code:
    1. Dim f As StdFont
    2.  
    3. Set f = New StdFont  ' MS Sans Serif (NOT Arabic Compatible)
    4. f.Name = "FixedSys"   ' FixedSys (NOT Arabic Compatible)
    5. f.Charset = 178         ' Arial (Arabic Compatible)
    6. f.Charset = 0            ' Arial (Arabic Compatible)
    7. f.Name = "Tahoma"    ' Tahoma (Arabic Compatible)
    8. f.Charset = 178         ' Tahoma (Arabic Compatible)
    9. f.Name = ""               ' Arial (Arabic Compatible)

    Glad to be of help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width