Embedding font in PB code?

Just starting out? Need help? Post your questions and find answers here.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Embedding font in PB code?

Post by davido »

Is it possible to embed a font in PureBasic code, even though it not installed in the OS?
DE AA EB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Embedding font in PB code?

Post 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
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Embedding font in PB code?

Post 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

Egypt my love
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Embedding font in PB code?

Post 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:
DE AA EB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Embedding font in PB code?

Post 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
Egypt my love
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Embedding font in PB code?

Post by heartbone »

Does anyone have da codes to do this wid de Linux?
Keep it BASIC.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Embedding font in PB code?

Post 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.
DE AA EB
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Embedding font in PB code?

Post 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.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Embedding font in PB code?

Post by davido »

@Danilo,
As per usual: Excellent.
Thank you, very much. :D
DE AA EB
Manuel
User
User
Posts: 14
Joined: Thu Mar 02, 2017 11:15 pm
Location: Barcelona

Re: Embedding font in PB code?

Post 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. :?
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Embedding font in PB code?

Post by Bisonte »

Since PB 5.60 the native function for all OS is : RegisterFontFile()
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Post Reply