Page 1 of 1
How to include specific Font i my software ...
Posted: Mon Feb 05, 2018 8:29 pm
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
Re: How to include specific Font i my software ...
Posted: Mon Feb 05, 2018 8:43 pm
by eJan
Jou can include it in DataSection then load it, thanks to netmaestro & Num3:
http://www.purebasic.fr/english/viewtop ... 30#p234030
Re: How to include specific Font i my software ...
Posted: Mon Feb 05, 2018 9:37 pm
by kenmo
RegisterFontFile() added in PB 5.50
Re: How to include specific Font i my software ...
Posted: Tue Feb 06, 2018 9:53 am
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

Re: How to include specific Font i my software ...
Posted: Tue Feb 06, 2018 12:20 pm
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
marc
Re: How to include specific Font i my software ...
Posted: Tue Feb 06, 2018 12:26 pm
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! :)
Re: How to include specific Font i my software ...
Posted: Tue Feb 06, 2018 11:48 pm
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)
Re: How to include specific Font i my software ...
Posted: Thu Feb 08, 2018 12:22 pm
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