RegisterFontFile from Memory

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

RegisterFontFile from Memory

Post by jacdelad »

Hello,
I would like to have the option to include a font file without having to write it to disk before loading (if that's possible).
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
deeproot
Enthusiast
Enthusiast
Posts: 269
Joined: Thu Dec 17, 2009 12:00 pm
Location: Llangadog, Wales, UK
Contact:

Re: RegisterFontFile from Memory

Post by deeproot »

See this thread - - https://www.purebasic.fr/english/viewtopic.php?p=455022

For Windows it's quite straightforward, loading direct from a font file embedded in the data section - simple example:

Code: Select all

;
;  Change the font filename and LoadFont as needed !!!
;
EnableExplicit
Enumeration
  #Win1
  #Text1
  #DejaVuSansBold
EndEnumeration
Define myMemFont

Procedure CatchFont(*Memory, myMemSize)
  ; load font into memory
  Protected myParam1 = 1
  ProcedureReturn AddFontMemResourceEx_(*Memory, myMemSize, 0, @myParam1)
EndProcedure

If OpenWindow(#Win1, 0, 0, 460,100,"Font test", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
  myMemFont = CatchFont(?dejafont, ?end_dejafont - ?dejafont) 
  LoadFont(#DejaVuSansBold, "DejaVu Sans", 12, #PB_Font_Bold)
  TextGadget(#Text1, 20,40,420,25, "The quick brown fox jumps over the lazy dog")  
  SetGadgetFont(#Text1, FontID(#DejaVuSansBold))
EndIf  

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

DataSection
  dejafont:
  IncludeBinary "DejaVuSans-Bold.ttf"
  end_dejafont: 
EndDataSection
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: RegisterFontFile from Memory

Post by jacdelad »

Yes, Windows. My program is meant to run on Windows and Raspi.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: RegisterFontFile from Memory

Post by User_Russian »

AddFontMemResourceEx
It doesn't work with VectorDrawing.
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: RegisterFontFile from Memory

Post by jacdelad »

Yes, the help also tells so. But that's not important for me.
It's just that I need a specific font that almost all systems don't have installed. And I wanted to avoid writing it temporarily onto the harddisk.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply