CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Need clarification about CreateFontIndirect

    Say, if I want to create a font:
    Code:
    							LOGFONT lf = {0};
    							lf.lfHeight = 12;
    							lf.lfWeight = FW_THIN;
    							VERIFY(StringCchCopy(lf.lfFaceName, sizeof(lf.lfFaceName) / sizeof(lf.lfFaceName[0]), _T("Calibri")) == S_OK);
    							HFONT hFont = CreateFontIndirect(&lf);
    							if(!hFont)
    							{
    								//Font created OK
    							}
    If I specify pretty much any name as lfFaceName CreateFontIndirect() returns a handle anyway. My understanding was that it will return NULL if it can't create that exact font. Am I correct on that one?

    PS. I'm running this code on Windows 7, so maybe they added some fail-safe mechanism for it already...

    PS 2. I'm kind of confused over this statement:
    Quote Originally Posted by MSDN
    The fonts for many East Asian languages have two typeface names: an English name and a localized name. CreateFont and CreateFontIndirect take the localized typeface name only on a system locale that matches the language, while they take the English typeface name on all other system locales. The best method is to try one name and, on failure, try the other.
    I do not have a machine with East Asian languages, nor that I can speak those languages. What would be the correct way to create a font on those machines?

    The strategy I thought of is to try to create a font that I want, say, "Calibri", and if it can't be created (if it's not installed), try some basic font, like "Arial" for instance. But again, CreateFontIndirect seems to return a handle for any font type name I specify for it.

  2. #2
    Join Date
    Jul 2010
    Location
    Mexico
    Posts
    21

    Re: Need clarification about CreateFontIndirect

    Hi! The font creation functions will *never* fail, because the system will return the handle to a font that is similar to the one you described with the fontfamily, pitch, width, etc... parameters passed to these functions (raw or inside a LOGFONT structure). However, if you pass an invalid parameter, it will fail, but the wrong facename won't cause a failure.

    So you'll always end up with a valid HFONT, no matter what facename you specify... You may try checking if an specific font exists, by enumerating the font families (check EnumFontFamiliesEx on MSDN).

    Hope it helps.
    Regards.
    "A program is never less than 90% complete, and never more than 95% complete."

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need clarification about CreateFontIndirect

    Quote Originally Posted by ahmd View Post
    I do not have a machine with East Asian languages, nor that I can speak those languages. What would be the correct way to create a font on those machines?
    By following what msdn suggests (i.e. check the locale settings and if it matchs the language of the font, use the native language; otherwise, specify the English font name).

  4. #4
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Need clarification about CreateFontIndirect

    Thank you both.

    Quote Originally Posted by Arjay View Post
    By following what msdn suggests (i.e. check the locale settings and if it matchs the language of the font, use the native language; otherwise, specify the English font name).
    Arjay, this line " check the locale settings and if it matchs the language of the font" opens up a whole new can of worms. Plus even if I compare it and it matches, how in the world would I know the native font name?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need clarification about CreateFontIndirect

    Quote Originally Posted by ahmd View Post
    Thank you both.



    Arjay, this line " check the locale settings and if it matchs the language of the font" opens up a whole new can of worms. Plus even if I compare it and it matches, how in the world would I know the native font name?
    Look an api that enums the fonts. See if it returns the english name and localized name.

    Are you hard coding the fonts?

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Need clarification about CreateFontIndirect

    Quote Originally Posted by Arjay View Post
    Are you hard coding the fonts?
    Yes. Is it bad?

    Quote Originally Posted by ahmd
    The strategy I thought of is to try to create a font that I want, say, "Calibri", and if it can't be created (if it's not installed), try some basic font, like "Arial" for instance.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need clarification about CreateFontIndirect

    I wouldn't say 'bad' to hardcode fonts, but given your criteria I would say 'ambitious'.

    If your app is to be globalized, not only will you need the fonts but also the localized resource strings in the language you support.

    Generally folks will use the default font on the system (and also switch language based on the system language).

    If you are to support multiple languages and want to allow the user to choose the font (other than their default system font), I would recommend enumerating the fonts and giving the user a combobox to select the desired font.

    Lastly, you are going to have to test for each language you support with a variety of fonts. Otherwise you are going to get text truncation bugs due to different language/fonts display differently within a control. For obvious reasons, it's a big testing effort.

  8. #8
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Need clarification about CreateFontIndirect

    Thanks. Let me explain why I need this.

    The program I work on creates a small icon at the run-time by combining a default background and a number, say from 1 to N. For that it needs to create a font and use it for DrawText(). I observed that I have to use a certain subset of fonts to make sure that those numbers are easy to read. So what you're saying is that, say, Calibri font will display numbers differently on a machine with Chinese as the default locale?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need clarification about CreateFontIndirect

    Quote Originally Posted by ahmd View Post
    Thanks. Let me explain why I need this.

    The program I work on creates a small icon at the run-time by combining a default background and a number, say from 1 to N. For that it needs to create a font and use it for DrawText(). I observed that I have to use a certain subset of fonts to make sure that those numbers are easy to read. So what you're saying is that, say, Calibri font will display numbers differently on a machine with Chinese as the default locale?
    You implied that you want to change fonts on the fly and support multiple languages, did you not? Your subset of fonts may not be available for different language versions. That's why I recommended that you use the system font.

    At any rate, test it out. Get an english machine, try it. Install a language pack, switch to that language with the IME and try it again.

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

Featured