Embed font in an application

For everything that's not in any way related to PureBasic. General chat etc...
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Embed font in an application

Post 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!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post 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)
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post 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?
maw

Post 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.
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post 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?
maw

Post 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)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
maw

Post 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.
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post by berryoxide »

maw, one word: jättebra! :D
That's amazing and it works!
You're the best coder ever :o
Post Reply