Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MFC GDI+ loading ttf font resource

Status
Not open for further replies.

hundsmiachn

Programmer
Nov 3, 2019
1
AT
Hi

I am using VS 2012 and making a app which displays several texts via GDI+ DrawText. When using ARIAL font, text looks okay, no problems.
But I wanted to use my own ttf fonts for the texts. So I added the ttf files to the resources and made an BINARY Object out of this ttf files.
When I use one font only all text are displayed ok. But when I use different fonts in the same WM_PAINT loop, the text looks completely garbage, strange letters are displayed.

Here is my code for displaying the text, it is called in my own method (DrawText) and is called for every text element, so local variables are destroyed.

Does the LoadResource/LockResource need a unlock or something???? I think it allocates some wrong memory when loading a second font..??

Code:
        Gdiplus::PrivateFontCollection fntcol;
	HINSTANCE hResInstance = (HINSTANCE)GetModuleHandle(NULL);
	HRSRC res = ::FindResource(hResInstance, MAKEINTRESOURCE(font.id), L"BINARY");
	if (res) {
		HGLOBAL mem = ::LoadResource(hResInstance, res);
		void *data = ::LockResource(mem);
		size_t len = ::SizeofResource(hResInstance, res);
		Gdiplus::Status nResults = fntcol.AddMemoryFont(data,len);
	}

	FontFamily fontFamily;
	int nNumFound=0;
	Gdiplus::Status nResults = fntcol.GetFamilies(1,&fontFamily, &nNumFound);
	Gdiplus::Font fnt(&fontFamily,(float)font.size,FontStyleRegular,UnitPixel);
	
	LinearGradientBrush brush(Rect(x,y, 1000, (int)fnt.GetHeight(&rgraphics)), col1, col2, LinearGradientModeVertical);
	rgraphics.DrawString(text, -1, &fnt, origin, &fmt, &brush);

Here are 3 examples images:

The first uses only one font resource (IDR_FNT_ROBOTO_COND_BOLD):
1._modjsa.png


The second uses a second font resource, but only that one (IDR_FNT_BEBASNEUE_REG):
2_gtw9st.png


And here the third example, where I use both fonts:
3_kpeo7m.png


As you can see, every font alone works fine, but when I use more than one, the output text is garbage.

thanks for any help.
regards
Erich
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top