Page 1 of 1

Embed font in an application

Posted: Sat Aug 30, 2008 12:18 am
by berryoxide
Hello!

I have some applications which install special font files to C:\Windows\Fonts. It's the only thing that keeps me from making these applications run form a USB stick. Does anyone know if it is possible to "embed" or cheat the applications so that they load the fonts either from the current directory, or from somewhere in the exe?

Thanks in advance!

Posted: Sat Aug 30, 2008 12:41 am
by ts-soft

Code: Select all

Procedure AddFont(FontFileName.s)
  AddFontResource_(FontFileName.s)
  SendMessage_(#HWND_BROADCAST,#WM_FONTCHANGE, #NUL, #NUL)
EndProcedure

Procedure RemoveFont(FontFileName.s)
  RemoveFontResource_(FontFileName.s)
EndProcedure

Posted: Sat Aug 30, 2008 9:33 am
by Num3
Load from DataSection:

Code: Select all

  OpenLibrary(0,"gdi32.dll")
  fontid=CallFunction(0,"AddFontMemResourceEx",?font,?end_font-?font,0,@"1")
  CloseLibrary(0)
  LoadFont(0,"anorexia",12)

DataSection
  font:
  IncludeBinary "anorexia.fon"
  end_font:
EndDataSection
Load From executable directory:

Code: Select all

 OpenLibrary(0,"gdi32.dll")
  AddFontResource_(GetCurrentDirectory()+"anorexia.fon")
  SendMessage_(#HWND_BROADCAST, #WM_FONTCHANGE, 0, 0)
  CloseLibrary(0)
  LoadFont(0,"anorexia",12)

Posted: Sat Aug 30, 2008 10:46 am
by berryoxide
I should have noted that they are 3rd party applications. As in I have not the source code, that's why I posted in the Off Topic section :)

So is it possible?

Posted: Sat Aug 30, 2008 11:28 am
by maw
You already have the answer above. Use one of Num3's solutions, insert a RunProgram() to start the application and you're done.

Posted: Sat Aug 30, 2008 11:42 am
by berryoxide
maw wrote:You already have the answer above. Use one of Num3's solutions, insert a RunProgram() to start the application and you're done.
I'm still puzzled. Can you give me sample code showing what you are telling me?

So is it possible to make a "launcher" that opens up a 3rd party application, tells it to load the fonts locally and it works?

Posted: Sat Aug 30, 2008 12:50 pm
by maw
berryoxide wrote:So is it possible to make a "launcher" that opens up a 3rd party application, tells it to load the fonts locally and it works?
No, you simply load the font with the launcher which makes it available to all other programs. Just noticed on MSDN that AddFontMemResourceEx makes the font private so you have to stick with the AddFontResource.

Code: Select all

AddFontResource_(fontname$)
SendMessage_(#HWND_BROADCAST, #WM_FONTCHANGE, 0, 0)
RunProgram(program$, "", "", #PB_Program_Wait)
RemoveFontResource_(fontname$)
SendMessage_(#HWND_BROADCAST, #WM_FONTCHANGE, 0, 0)

Posted: Sat Aug 30, 2008 2:40 pm
by PB
>> is it possible to make a "launcher" that opens up a 3rd party application,
>> tells it to load the fonts locally and it works?
>
> No, you simply load the font with the launcher which makes it available to
> all other programs

Well, the answer is yes, then. :) His app loads the font, then launches the
other app, which can then use the new available font -- just like you said.

Posted: Sat Aug 30, 2008 2:49 pm
by maw
PB wrote:>> is it possible to make a "launcher" that opens up a 3rd party application,
>> tells it to load the fonts locally and it works?
>
> No, you simply load the font with the launcher which makes it available to
> all other programs

Well, the answer is yes, then. :) His app loads the font, then launches the
other app, which can then use the new available font -- just like you said.
Not to quibble over semantics but the answer is no :D He asked if you open the application and tell it to load the fonts locally, which you don't do. You load the font before the application, hence the font is already loaded when the application needs it.

Posted: Sat Aug 30, 2008 7:11 pm
by berryoxide
maw, one word: jättebra! :D
That's amazing and it works!
You're the best coder ever :o