Jump to content

[Solved] How to load a font


Recommended Posts

Hi,

how can I load a font, e.g. TTF font located in @scriptdir and use it in GDI+?

I don't want to install the font - just wanna use it!

I found this but how can I use it in GDI+?

Func _LoadFont($Fontname)
         $s_p = DllStructCreate("char[" & StringLen($Fontname) + 1 & "]")
         DllStructSetData($s_p, 1, $Fontname)
         $msg = DllCall("gdi32.dll", "int", "AddFontResource", "ptr", DllStructGetPtr($s_p))
  ;~     DllStructDelete($s_p)
         If @error = 1 Then MsgBox(0, "DLLcall", "unable to use the DLL file")
         If @error = 2 Then MsgBox(0, "DLLcall", "unknown return type")
         Return $msg[0]
     EndFunc;==>_LoadFont

Any suggestion?

Thanks,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Link to comment
Share on other sites

After calling this function, you can use the font as if it were installed in the system. It works until the first reboot.

_LoadFont("MyFont.ttf")
_SendMessage(0xFFFF, 0x001D, 0, 0)
Thanks for your reply but I'm currently coding a game and wanna use a ttf font to look more authentic.

But it shouldn't be installed to the system in anyway.

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks for your reply but I'm currently coding a game and wanna use a ttf font to look more authentic.

But it should be installed to the system in anyway.

UEZ

Sorry, but I will not quite understand. To delete a font from your system (a table of fonts), use the following function.

DllCall('gdi32.dll', 'int', 'RemoveFontResource', 'str', 'MyFont.ttf')
Edited by Yashied
Link to comment
Share on other sites

Sorry, but I will not quite understand. To delete a font from your system (a table of fonts), use the following function.

DllCall('gdi32.dll', 'int', 'RemoveFontResource', 'str', 'MyFont.ttf')
That means, everytime I'm starting the game the font should be copied and used and when I end the game the font should be removed again. :)

No other way possible?

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

That means, everytime I'm starting the game the font should be copied and used and when I end the game the font should be removed again. :)

No other way possible?

UEZ

Font will not be copied or where it just would be loaded into memory and stay there as long as you do not restart your computer or do not call RemoveFontResource() function. This font not will be copied into the folder Windows\Fonts. You are wanted this, as the title topic? Other way, I do not see.
Link to comment
Share on other sites

Font will not be copied or where it just would be loaded into memory and stay there as long as you do not restart your computer or do not call RemoveFontResource() function. This font not will be copied into the folder Windows\Fonts. You are wanted this, as the title topic? Other way, I do not see.

Thanks Yashied but I thought that there is a different way to load and use a font like loading a DLL.

So that means, when I load it with _LoadFont("MyFont.ttf") than I should be able to use it this way:

Example from Help

#include <GuiConstantsEx.au3>
 #include <GDIPlus.au3>
 
 Opt('MustDeclareVars', 1)
 
 _Main()
 
 Func  _Main()
     Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout
     Local $sString = "Hello world", $aInfo
 
    ; Create GUI
     $hGUI = GUICreate("GDI+", 400, 300)
     $hWnd = WinGetHandle("GDI+")
     GUISetState()
 
    ; Draw a string
     _GDIPlus_Startup ()
     $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
     $hBrush = _GDIPlus_BrushCreateSolid  (0xFF00007F)
     $hFormat = _GDIPlus_StringFormatCreate ()
     $hFamily = _GDIPlus_FontFamilyCreate  ("My Font")
     $hFont = _GDIPlus_FontCreate ($hFamily, 12,  2)
      $tLayout = _GDIPlus_RectFCreate (140,  110,  0,  0)
      $aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, $sString, $hFont, $tLayout, $hFormat)
     _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sString, $hFont, $aInfo[0],  $hFormat,  $hBrush)
 
   ; Loop until user exits
     Do
     Until GUIGetMsg() =  $GUI_EVENT_CLOSE
 
    ; Clean up resources
      _GDIPlus_FontDispose ($hFont)
     _GDIPlus_FontFamilyDispose ($hFamily)
     _GDIPlus_StringFormatDispose ($hFormat)
     _GDIPlus_BrushDispose  ($hBrush)
     _GDIPlus_GraphicsDispose ($hGraphic)
     _GDIPlus_Shutdown  ()
 
 EndFunc ;==>_Main

Or?

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks Yashied but I thought that there is a different way to load and use a font like loading a DLL.

So that means, when I load it with _LoadFont("MyFont.ttf") than I should be able to use it this way:

Example from Help

#include <GuiConstantsEx.au3>
 #include <GDIPlus.au3>
 
 Opt('MustDeclareVars', 1)
 
 _Main()
 
 Func  _Main()
     Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout
     Local $sString = "Hello world", $aInfo
 
; Create GUI
     $hGUI = GUICreate("GDI+", 400, 300)
     $hWnd = WinGetHandle("GDI+")
     GUISetState()
 
; Draw a string
     _GDIPlus_Startup ()
     $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
     $hBrush = _GDIPlus_BrushCreateSolid  (0xFF00007F)
     $hFormat = _GDIPlus_StringFormatCreate ()
     $hFamily = _GDIPlus_FontFamilyCreate  ("My Font")
     $hFont = _GDIPlus_FontCreate ($hFamily, 12,  2)
      $tLayout = _GDIPlus_RectFCreate (140,  110,  0,  0)
      $aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, $sString, $hFont, $tLayout, $hFormat)
     _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sString, $hFont, $aInfo[0],  $hFormat,  $hBrush)
 
; Loop until user exits
     Do
     Until GUIGetMsg() =  $GUI_EVENT_CLOSE
 
; Clean up resources
      _GDIPlus_FontDispose ($hFont)
     _GDIPlus_FontFamilyDispose ($hFamily)
     _GDIPlus_StringFormatDispose ($hFormat)
     _GDIPlus_BrushDispose  ($hBrush)
     _GDIPlus_GraphicsDispose ($hGraphic)
     _GDIPlus_Shutdown  ()
 
 EndFunc;==>_Main

Or?

UEZ

You're right, with GDI+ (Posted Image) is really not working. Try this.

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func  _Main()
    Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $hCollection, $tLayout
    Local $sString = "Hello world", $aInfo

; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    $hWnd = WinGetHandle("GDI+")
    GUISetState()

; Draw a string
    _GDIPlus_Startup ()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
    $hBrush = _GDIPlus_BrushCreateSolid  (0xFF00007F)
    $hFormat = _GDIPlus_StringFormatCreate ()
    $hCollection = DllCall($ghGDIPDll, 'int', 'GdipNewPrivateFontCollection', 'ptr*', 0)
    $hCollection = $hCollection[1]
    DllCall($ghGDIPDll, 'int', 'GdipPrivateAddFontFile', 'ptr', $hCollection, 'wstr', '...\MyFont.ttf')
    $hFamily = DllCall($ghGDIPDll, 'int', 'GdipCreateFontFamilyFromName', 'wstr', 'MyFont', 'ptr', $hCollection, 'int*', 0)
    $hFamily = $hFamily[3]
;   $hFamily = _GDIPlus_FontFamilyCreate  ("My Font")
    $hFont = _GDIPlus_FontCreate ($hFamily, 12,  2)
    $tLayout = _GDIPlus_RectFCreate (140,  110,  0,  0)
    $aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, $sString, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx ($hGraphic, $sString, $hFont, $aInfo[0],  $hFormat,  $hBrush)

; Loop until user exits
     Do
     Until GUIGetMsg() =  $GUI_EVENT_CLOSE

; Clean up resources
    _GDIPlus_FontDispose ($hFont)
    _GDIPlus_FontFamilyDispose ($hFamily)
;   DllCall($ghGDIPDll, 'int', 'GdipDeletePrivateFontCollection', 'ptr', $hCollection)
    _GDIPlus_StringFormatDispose ($hFormat)
    _GDIPlus_BrushDispose  ($hBrush)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown  ()

EndFunc;==>_Main
Edited by Yashied
Link to comment
Share on other sites

Thanks you very much YASHIED :)

Without your help I couldn't load the font which make the looking of the game more authentic!

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Link to comment
Share on other sites

There is one small problem. With the release of HCollection script is crashed. I specifically commented out this line. Unfortunately, I do not know what the case here.

Good luck.

Until now I have no crashs!

I will observer it!

Thanks anyway!

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

To Delete the collection, you have to call the func this way (ptr* instead of ptr, since ** is in in the API function-definition)

DllCall($ghGDIPDll, 'int', 'GdipDeletePrivateFontCollection', 'ptr*', $hCollection)
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

To Delete the collection, you have to call the func this way (ptr* instead of ptr, since ** is in in the API function-definition)

DllCall($ghGDIPDll, 'int', 'GdipDeletePrivateFontCollection', 'ptr*', $hCollection)
Yes of course you are right. I missed it. Thanks.

:)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...