How to include specific Font i my software ...

Just starting out? Need help? Post your questions and find answers here.
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

How to include specific Font i my software ...

Post by marc_256 »

hi,

I use as a font 'Courier New' and works very well on my own PC.
But if this font is not installed on a user PC, there is another font who is selected.
But this is the problem if I use fixed text width and height zones.

How can I include a font in my software ?
I use ScreenMode, Windows, and PB 561 Final (x86/x64)
Is there a special command for that except LoadFont()

Thanks,
marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Re: How to include specific Font i my software ...

Post by eJan »

Jou can include it in DataSection then load it, thanks to netmaestro & Num3:
http://www.purebasic.fr/english/viewtop ... 30#p234030
Image
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: How to include specific Font i my software ...

Post by kenmo »

RegisterFontFile() added in PB 5.50
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: How to include specific Font i my software ...

Post by Kwai chang caine »

Be carefull MARC, sometime it's not possible to add fonts dynamicaly if you are not admin. :?
I don't know if it's your case, but i had this problem and it's good to know, i have lost several time before understand the problem
For me..impossible to use RegisterFontFile() or even several API who rest without effect :|
ImageThe happiness is a road...
Not a destination
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: How to include specific Font i my software ...

Post by marc_256 »

Hi,

thanks for the info,
Did some tests ...
and I gone use the font in DataSection solution.
So I can use it in multi platform environments.

thanks again :D
marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to include specific Font i my software ...

Post by Dude »

marc_256 wrote:How can I include a font in my software ?
For Windows, here's how one of my apps uses a custom font without it ever being installed on the PC, and doesn't need admin rights to use.

It's a modified version of other code I found on these forums (by Rashad, of course):

Code: Select all

; At the start of my app, I add the font from the exe (never from disk):

Global myfont=AddFontMemResourceEx_(?MyFont_Start,?MyFont_End-?MyFont_Start,0,@"1")
If myfont=0
  MessageRequester("Error","App couldn't start correctly; please try again.")
  End
EndIf

LoadFont(0,"name",size) ; Name = the internal name of the font (not its filename).

; App goes here (main loop, procedures, etc).

; ...

; When app is ready to quit, release the font:

FreeFont(0)
RemoveFontMemResourceEx_(myfont)

End

DataSection
  MyFont_Start:
  IncludeBinary "MyFont.ttf"
  MyFont_End:
EndDataSection

; Done! :)
bytecave
User
User
Posts: 40
Joined: Mon Aug 13, 2012 7:26 am

Re: How to include specific Font i my software ...

Post by bytecave »

This should be cross platform, i.e., doesn't use any Windows API. It writes the font out to the temporary directory (if it doesn't already exist there), but you could as easily write it to the executable directory.

EDIT: I excerpted this from my game, and edited it. Looks like I had the WriteData line wrong. Can't test at the moment, but it should be correct now. Sorry for any inconvenience!

Code: Select all

DataSection
  start_myfont:
    IncludeBinary "myfontfile.ttf"
  end_myfont:
EndDataSection
 
Procedure.i LoadPrivateFont(strFontFile.s, strFontFace.s, *pFontStart, *pFontEnd, iPointSize.i, iFlags.i = 0)
  Protected hFont.i
  Protected hFile.i
  
  strFontFile = GetTemporaryDirectory() + strFontFile
  
  ;if font file doesn't already exist, write it to disk and register it for loading
  If FileSize(strFontFile) = -1 
    hFile = CreateFile(#PB_Any, strFontFile)
    
    If hFile
      WriteData(hFile, *pFontStart, *pFontEnd - *pFontStart)
      CloseFile(hFile)
    EndIf
  EndIf
  
  ;If font file can't be registered, we'll get the default font below
  RegisterFontFile(strFontFile)

  hFont = LoadFont(#PB_Any, strFontFace, iPointSize, iFlags)
  If Not IsFont(hFont)
    hFont = GetGadgetFont(#PB_Default)   ;or just loadfont another default font you'd like
  EndIf
  
  ProcedureReturn hFont
EndProcedure

Define hFont.i
hFont = LoadPrivateFont("myfontfile.ttf", "MyFont", ?start_myfont, ?end_myfont, 16)
Last edited by bytecave on Thu Feb 08, 2018 7:58 pm, edited 2 times in total.
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: How to include specific Font i my software ...

Post by marc_256 »

Hi,

@ Dude,
thanks, but I really need multi platform application.

@ bytecave
Thanks, this is it, I 'm gone test this in the afternoon.

Thanks,
Marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Post Reply