Jump to content
  • Advertisement
Sign in to follow this  
Indifferent

AddFontMemResourceEx & D3DXCreateFont

This topic is 1584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to load a font from a buffer using AddFontMemResourceEx and then creating a D3D font with D3DXCreateFont. This works fine on every platform except Windows XP, where it causes the wrong characters to be printed.

 

The problem seems to occur with OpenType (.otf) fonts whereas TrueType (.ttf) seem to work fine.

 

Here's the relevant code (modified slightly for brevity):

char* fontName = "FontName";

HANDLE hFont = AddFontMemResourceEx(data, size, 0, &loaded);

if(!hFont || !loaded) {
    //error handling
}

HRESULT res = D3DXCreateFont(device, 30, 0, FW_NORMAL, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
    ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontName, &font);

if(res != S_OK) {
    //error handling
}

...some code later...
//DrawTextA/W, makes no difference to the result
font->DrawTextW(NULL, message, -1, &rect, DT_LEFT | DT_NOCLIP, colour);

Here's an example render:

 

vRKatkT.jpg

 

Interestingly, if I make a small change to the code to save the font to disk and then reload it with another API function, everything works fine:

char* fontName= "FontName";
char* fileName = "FontName.otf";

FILE* file = fopen(fileName, "w+b");
fwrite(data, size, 1, file);
fclose(file);

//non-ex version works fine too
AddFontResourceEx(fileName, FR_PRIVATE, 0);

HRESULT res = D3DXCreateFont(device, 30, 0, FW_NORMAL, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
    ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontName, &font);

if(res != S_OK) {
    //error handling
}

...some code later...
//DrawTextA/W, makes no difference to the result
font->DrawTextW(NULL, message, -1, &rect, DT_LEFT | DT_NOCLIP, colour);

It seems this is likely some issue with XP's API but before I go converting to TTF, I'd be happy to hear any ideas/theories on what could be going wrong.

 

Edit: Seems I was misled into believing that converting to a TTF was a possible solution. After using AddFontResource(Ex) in prior testing, the font hadn't been freed correctly and was still being silently used rather than the TTF version I was loading with AddFontMemResourceEx.

 

Second edit: After going through the same tests again, ensuring it wasn't quietly using an already opened font without my knowing, it seems setting the FR_NOT_ENUM flag in AddFontResourceEx causes the same corruption issue under XP. If providing a name to load isn't valid when using the flag, I'm not entirely sure:

  • Why it'd work fine on later versions of Windows.
  • How you'd ever make use of the font.
  • Why, rather than failing, it'd find the font but display the incorrect glyphs.
Edited by Indifferent

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!