AddFontMemResourceEx & D3DXCreateFont

Started by
-1 comments, last by Indifferent 9 years, 11 months ago

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.

This topic is closed to new replies.

Advertisement