How to embed fonts into your Windows programs. Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

How to embed fonts into your Windows programs.

Post by Dark Alchemist » Mon Aug 02, 2010 7:10 am

Just thought I would let others know of this since I have seen a few questions asking about this over the years and having just figured this out myself.

First take a program that converts any binary to C (bin2c but I wrote my own for inhouse work) and write out your binary into C code (unsigned char).

Make sure you know the real font's name because Windows isn't going to let you write in the font if you don't know its real name.

Now with that done and having the info ready here is the magical Windows' code

Code: Select all

    DWORD nFonts;
    size_t len = sizeof(FONT_FROM_THE_C_FILE_WE_CONVERTED);
    void *data = NAME_OF_THE_FONT_FROM_THE_C_FILE_WE_CONVERTED;
    
    void *m_fonthandle = AddFontMemResourceEx(data, len, NULL, &nFonts);
    if(m_fonthandle==0)
	wxLogError("Font add failed.");
You should call RemoveFontMemResourceEx before the application exits to be on the safe side but the system should unload the font even if you don't call RemoveFontMemResourceEx but always better to be safe and just call it (it uses the same info we fed into the AddFontMemResourceEX).

Basically, I just write out the binary of the .ttf into an unsigned char that gets placed into a .cpp file then I include that inside my main .cpp so that my application will then have the font embedded into it and I don't need it on the end user's hard drive nor in a cumbersome resource.

I hope this helps someone out there. :)

Azagaros
Experienced Solver
Experienced Solver
Posts: 59
Joined: Sat Feb 20, 2010 6:26 pm

Post by Azagaros » Mon Aug 02, 2010 10:01 pm

You can't embed a font in a app. You can link the file to a binary and have extraction routine in you app. However on windows installing the font with out user rights might be a little bit of a problem, and accessing it from the install directory might cause problems. Not to mention extracting it to fill might raise a few red flags on a virus scanner.

An easier solution would be to convert to bitmaps and embed in your app, it wouldn't cause the same issues on the user side but it makes programming a little harder.

The easiest solution is ship the font in installation packages and stop being so protective of your font. I download many freely out there.

food for thought
17+ years of c++, currently open watcom and wxWidgets, MFC experience. C::B user.
The world begins again. Are you going to be one of the 2 billion left?

Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist » Tue Aug 03, 2010 7:56 am

Azagaros wrote:You can't embed a font in a app. You can link the file to a binary and have extraction routine in you app. However on windows installing the font with out user rights might be a little bit of a problem, and accessing it from the install directory might cause problems. Not to mention extracting it to fill might raise a few red flags on a virus scanner.

An easier solution would be to convert to bitmaps and embed in your app, it wouldn't cause the same issues on the user side but it makes programming a little harder.

The easiest solution is ship the font in installation packages and stop being so protective of your font. I download many freely out there.

food for thought
My application works on W7, Vista and XP and not a single user has said they even had a prompt complaining or that they can't see the font. I am installing nothing on their machine which is why I like doing it like this (I hate resources for the last 10 years I have been using MSVC) because I have some really idiotic users that will snap me in my butt due their own ineptness from saying no to copying anything. :/

Basically the font lives for as long as my program is running and (according to MSDN) it can't be used by any other applications.

To be honest I am doing absolutely nothing differently from using the resource (this is why the resource commands exists) except I am not using MSVC's resource I am using my code. It is done exactly as people do to load up built in bitmaps, jpgs, pngs, etc...

The thing is Microsoft put in the functionality with the LoadResource stuff, and I am doing nothing differently than they do except mine is more raw and because of that I eliminate one or two lines of code plus the headaches associated with resources in general.

User avatar
Deluge
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 30, 2010 4:52 am
Location: USA
Contact:

Re: How to embed fonts into your Windows programs.

Post by Deluge » Mon Jul 01, 2019 9:54 pm

Thank you Dark Alchemist. This worked for me. I had also tried using AddFontResource, but couldn't get that one to work. I like this better anyway because the font is actually embedded into the executable.

For anyone using wxWidgets 3.1.1, the static method wxFont::AddPrivateFont has been added. Unfortunately, I don't think it allows loading embedded fonts from memory. It has to be an external file. And I think it only supports TrueType fonts.

At the time of writing this, 3.1 isn't considered the stable branch. My system (Windows w/ MSYS2/MinGW) only has 3.0.4 available.

--- Edit ---

FYI, I used Binary to Header to create the resource (plugging my own little app ;)).
Projects:
Debreate
MyABCs
Stendhal

OSes:
Windows 10 Home (missing my Linux & Freebsd :()

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3464
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to embed fonts into your Windows programs.

Post by ONEEYEMAN » Tue Jul 02, 2019 2:32 pm

Hi,
If you use latest release of wxWidgets you can add the user defined font to your program by loading it.

There was a very old thread which was updated yesterday.

I'm not sure whether this functionality is cross-platform or not.

Thank you.

Post Reply