LoadFontResource_() API

Everything else that doesn't fall into one of the other PB categories.
User avatar
charvista
Addict
Addict
Posts: 902
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

LoadFontResource_() API

Post by charvista »

Hi
It looks like the LoadFontResource_() API is no longer working? Perhaps since 2018 already, but I have discovered it only now.

Code: Select all

Procedure zFontAdd(FontFileName.s)
    AddFontResource_(FontFileName.s)
    SendMessage_(#HWND_BROADCAST,#WM_FONTCHANGE, #Null, #Null)
EndProcedure
It looks like I need to use LoadFontResourceA_() or LoadFontResourceW_() ? I am not understanding APIs well, so maybe you can help me?
Using PB 5.70 LTS x64 on Windows 10 Home
https://docs.microsoft.com/en-us/window ... tresourcea
Thanks in advance
- Windows 11 Home 64-bit
- PureBasic 6.04 LTS (x64)
- 64 Gb RAM
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: LoadFontResource_() API

Post by Olliv »

I cannot help you greatly. But I can tell you what do xxxA_() and xxxW_() mean.

A and W mean Ascii and Word. This informs you the size of each character in a string.

A = 8 bits per character (1 byte, old standard)
W = 16 bits per character (2 bytes, new standard)

Earlier, "hello" was stored as 'hello' on memory.
Now, on Unicode standard, "hello" is stored as ' h e l l o' on memory. Normally, blank spaces between each character is a zero-8-bits code. This allows to treat far more characters as just the 256 characters of ASCII. Now we can treat a set of 65536 characters (256 * 256 characters), what it allows us to treat asian characters, all european characters, etc...

To check (just one time, not everytime !) if your function is right, you can search in which DLL file, this function is stored. And check its fullname with the Purebasic DLL native library (OpenLibrary(), ExamineLibrary(), etc... ).

If the name of the function contains more than classic characters (as '@' i.e. ) post it here to have the translation. Extra characters in the name of a DLL function means the name is 'decorated' and contains a code which tells us what is the arguments template (how many variables, and which type).
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: LoadFontResource_() API

Post by Mijikai »

I can offer you my code to load fonts from memory:

Code: Select all

;CatchFont() & ReleaseFont()
;Author: Mijikai
;------------------------------------------------------------------------------------------------
;This allows you to load (catch & free) fonts from memory.
;The procedure CatchFont() returns a PureBasic #Font on success.
;It optionally returns the handle to the created resource memory.
;This memory will be freed once the program ends (PureBasic #Fonts act the same).
;So in most cases u wont really need to release the #Font / handle.
;------------------------------------------------------------------------------------------------

Procedure.i ReleaseFont(Font.i,Handle.i = #Null)
  If IsFont(Font)
    FreeFont(Font)
  EndIf
  If Handle
    ProcedureReturn RemoveFontMemResourceEx_(handle)
  EndIf
  ProcedureReturn #Null
EndProcedure

Procedure.i CatchFont(*Buffer,BufferSize.i,FontName.s,FontSize.i,FontStyle.i = #Null,*Handle.Integer = #Null)
  Protected handle.i
  Protected fonts.i
  Protected font.i
  fonts = 1
  handle = AddFontMemResourceEx_(*Buffer,BufferSize,#Null,@fonts)
  If handle
    font = LoadFont(#PB_Any,FontName,FontSize,FontStyle)
    If IsFont(font)
      If *Handle
        *Handle\i = handle
      EndIf
      ProcedureReturn font
    EndIf
    RemoveFontMemResourceEx_(handle)
  EndIf
  ProcedureReturn #Null
EndProcedure

Karellen
User
User
Posts: 82
Joined: Fri Aug 16, 2013 2:52 pm
Location: Germany

Re: LoadFontResource_() API

Post by Karellen »

Hmm, doesn't do the build in command in PureBasic the job?

https://www.purebasic.com/documentation ... tfile.html
Stanley decided to go to the meeting room...
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: LoadFontResource_() API

Post by Mijikai »

Yes, if theres a good reason to write it to disk before using it this will do just fine.
Karellen
User
User
Posts: 82
Joined: Fri Aug 16, 2013 2:52 pm
Location: Germany

Re: LoadFontResource_() API

Post by Karellen »

Mijikai wrote:Yes, if theres a good reason to write it to disk before using it this will do just fine.
Sorry, couldn't get that this was an issue for the orginal question. My native language is not english. Just dropped spontaneously my thought becaus I wanted to help. Sorry again.
Stanley decided to go to the meeting room...
User avatar
charvista
Addict
Addict
Posts: 902
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: LoadFontResource_() API

Post by charvista »

Thank you all :P
The problem is solved, thanks to Karellen, pointing out for the addition of RegisterFontFile(), that solved the problem, a feature added since PB 5.50.
It was not necessary earlier, perhaps that was the reason...

One more question:
As the font has been registered, is there no need to Unregister it? Is the font automatically unregistered when the program ends? Or when the computer is shut down?
This question to know if the memory would not be overloaded when registering several thousands of fonts.
- Windows 11 Home 64-bit
- PureBasic 6.04 LTS (x64)
- 64 Gb RAM
Karellen
User
User
Posts: 82
Joined: Fri Aug 16, 2013 2:52 pm
Location: Germany

Re: LoadFontResource_() API

Post by Karellen »

charvista wrote: As the font has been registered, is there no need to Unregister it?
No, as described in the manual:
The font file is registered for the current program only. This means that the font(s) are not accessible by other programs and will be unregistered when the program ends. No system-wide changes are made by this command.
Stanley decided to go to the meeting room...
User avatar
charvista
Addict
Addict
Posts: 902
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: LoadFontResource_() API

Post by charvista »

Thanks Karellen. It escaped me. :mrgreen:
- Windows 11 Home 64-bit
- PureBasic 6.04 LTS (x64)
- 64 Gb RAM
Post Reply