Jump to content
  • Advertisement
Sign in to follow this  
Plerion

[C#] Problem with PrivateFontCollection

This topic is 3000 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

Hello!

I have some problems with the PrivateFontCollection. I have a .ttf-file which i added as a resource to my application. Then i use a PrivateFontCollection to load it from memory. Sadly the font i create from that family (which has the correct name) doesnt look how it should. If i create the font using AddFontFile everything works. Here some code:

private void Form1_Load(object sender, EventArgs e)
{
byte[] data = Content.space_age;
IntPtr hGlob = Marshal.AllocCoTaskMem(data.Length);
Marshal.Copy(data, 0, hGlob, data.Length);
PrivateFontCollection coll = new PrivateFontCollection();
//coll.AddFontFile("C:\\Users\\Admin\\Downloads\\space_age\\space_age.ttf");
coll.AddMemoryFont(hGlob, data.Length);
FontFamily fam = coll.Families[0];
Font curFnt = label1.Font;
Font fnt = new Font(fam, 18, curFnt.Style, curFnt.Unit, curFnt.GdiCharSet, curFnt.GdiVerticalFont);
label1.Font = fnt;
Marshal.FreeCoTaskMem(hGlob);
}



The commented version works, the uncommented doesnt. What is the reason for that beahaviour?

Greetings
Plerion

Share this post


Link to post
Share on other sites
Advertisement
Define "doesn't work."

Does it throw an exception? Draw incorrectly? Summon demon monkeys from the netherworld to attack your toes?

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
Summon demon monkeys from the netherworld to attack your toes?


Thats what it does! Really!

Its drawing it in Arial.

Share this post


Link to post
Share on other sites
My first guess would be that you already have the file open someplace else, so the font system can't load it. (Edit: that should throw an exception according to MSDN. So probably not.)

Failing that, try new Font(fam.Name, ...); to see if something is going oddly wrong with the family name string conversion.

Share this post


Link to post
Share on other sites
Ive changed it to the following:

private void Form1_Load(object sender, EventArgs e)
{
byte[] data = Content.space_age;
IntPtr hGlob = Marshal.AllocCoTaskMem(data.Length);
Marshal.Copy(data, 0, hGlob, data.Length);
PrivateFontCollection coll = new PrivateFontCollection();
coll.AddMemoryFont(hGlob, data.Length);
FontFamily fam = coll.Families[0];
Font curFnt = label1.Font;

MessageBox.Show(fam.Name);

Font fnt = new Font(fam.Name, 18, curFnt.Style, curFnt.Unit, curFnt.GdiCharSet, curFnt.GdiVerticalFont);
label1.Font = fnt;
Marshal.FreeCoTaskMem(hGlob);
}




It correctly displays "Space Age" but still renders as arial...

Share this post


Link to post
Share on other sites
Oh, that was my bad, i read that i need to set the TextRendering, but i wrote Application.SetCompatibleTextRenderingDefault(false); instead of true... Its definitely to hot (>35°) :D

Share this post


Link to post
Share on other sites
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!