Page 1 of 1

Embedding font in PB code?

Posted: Fri Oct 24, 2014 9:52 pm
by davido
Is it possible to embed a font in PureBasic code, even though it not installed in the OS?

Re: Embedding font in PB code?

Posted: Fri Oct 24, 2014 10:00 pm
by RASHAD
Hi davido
Code by NUM3
I did not test it but Some how I am sure that it is OK :)

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

Re: Embedding font in PB code?

Posted: Fri Oct 24, 2014 10:47 pm
by RASHAD
Tested works like a charm
PB 5.30 x86 Win 8.1 x64

Code: Select all

Global fontid

;OpenLibrary(0,"gdi32.dll")
fontid=AddFontMemResourceEx_(?font,?end_font-?font,0,@"1")
;CloseLibrary(0)
LoadFont(0,"SPACECRAFT",14)
  
OpenWindow(0,0,0,400,300,"Test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered| #PB_Window_MaximizeGadget| #PB_Window_SizeGadget)
TextGadget(0,10,10,300,40,"This is a TEST",#SS_CENTERIMAGE | #SS_CENTER| #WS_BORDER)
SetGadgetFont(0,FontID(0))
SetGadgetColor(0,#PB_Gadget_FrontColor,#Red)
SetGadgetColor(0,#PB_Gadget_BackColor,#Yellow)
    

Repeat
           
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            RemoveFontMemResourceEx_(fontid)
            Quit = 1 
             
  EndSelect 

Until Quit = 1
End

DataSection
  font:
  IncludeBinary "SPACECRAFT.ttf"
  end_font:
EndDataSection


Re: Embedding font in PB code?

Posted: Sat Oct 25, 2014 8:09 pm
by davido
Hi RASHAD,

Great! Thank you for going to all this trouble. :D

Your code always works, so when I couldn't get it to work first time I wondered what I could possibly be doing wrong.
After a while I found I was entering the font-file name - not the name of the font! doh :oops:

Re: Embedding font in PB code?

Posted: Sat Oct 25, 2014 8:51 pm
by RASHAD
Hi davido
You should be punished for that :mrgreen:
We always use the font name not the font file name to LoadFont()
Be careful Dbl Click the font file and read the font name on top because sometimes they use a phony name for the file

Re: Embedding font in PB code?

Posted: Tue Oct 28, 2014 6:47 am
by heartbone
Does anyone have da codes to do this wid de Linux?

Re: Embedding font in PB code?

Posted: Tue Oct 28, 2014 7:22 pm
by davido
heartbone wrote:Does anyone have da codes to do this wid de Linux?
@heartbone, good thinking.

Perhaps someone knows the Mac OSX equivalents, too.

Re: Embedding font in PB code?

Posted: Tue Oct 28, 2014 8:29 pm
by Danilo
davido wrote:Perhaps someone knows the Mac OSX equivalents, too.
On Mac OS X you can embed a font into the app resources, see here.

Re: Embedding font in PB code?

Posted: Thu Oct 30, 2014 8:31 pm
by davido
@Danilo,
As per usual: Excellent.
Thank you, very much. :D

Re: Embedding font in PB code?

Posted: Sun May 07, 2017 11:45 am
by Manuel
Note for future reference: the font name in LoadFont() must be the internal font name of the TTF file (seen when double-clicking it to preview it); and NOT be the actual TTF filename itself. I just spent hours trying to work out why this wasn't working, and that was the reason. :?

Re: Embedding font in PB code?

Posted: Sun May 07, 2017 1:59 pm
by Bisonte
Since PB 5.60 the native function for all OS is : RegisterFontFile()