×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

MFC GDI+ loading ttf font resource

MFC GDI+ loading ttf font resource

MFC GDI+ loading ttf font resource

(OP)
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 --> C++

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):


The second uses a second font resource, but only that one (IDR_FNT_BEBASNEUE_REG):


And here the third example, where I use both fonts:


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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close