|
-
July 23rd, 2010, 04:21 PM
#1
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:
 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.
-
July 23rd, 2010, 05:17 PM
#2
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."
-
July 23rd, 2010, 05:53 PM
#3
Re: Need clarification about CreateFontIndirect
 Originally Posted by ahmd
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).
-
July 23rd, 2010, 05:59 PM
#4
Re: Need clarification about CreateFontIndirect
Thank you both.
 Originally Posted by Arjay
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?
-
July 23rd, 2010, 08:24 PM
#5
Re: Need clarification about CreateFontIndirect
 Originally Posted by ahmd
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?
-
July 23rd, 2010, 09:23 PM
#6
Re: Need clarification about CreateFontIndirect
 Originally Posted by Arjay
Are you hard coding the fonts?
Yes. Is it bad?
 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.
-
July 23rd, 2010, 10:35 PM
#7
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.
-
July 23rd, 2010, 11:52 PM
#8
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?
-
July 24th, 2010, 12:57 AM
#9
Re: Need clarification about CreateFontIndirect
 Originally Posted by ahmd
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|