Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Last active August 8, 2023 14:55
Show Gist options
  • Save tmplinshi/7717340 to your computer and use it in GitHub Desktop.
Save tmplinshi/7717340 to your computer and use it in GitHub Desktop.
Load font from file or resource, without needed install to system. https://autohotkey.com/boards/viewtopic.php?f=6&t=813
/*
CustomFont v2.01 (2018-8-25)
---------------------------------------------------------
Description: Load font from file or resource, without needed install to system.
---------------------------------------------------------
Useage Examples:
* Load From File
font1 := New CustomFont("ewatch.ttf")
Gui, Font, s100, ewatch
* Load From Resource
Gui, Add, Text, HWNDhCtrl w400 h200, 12345
font2 := New CustomFont("res:ewatch.ttf", "ewatch", 80) ; <- Add a res: prefix to the resource name.
font2.ApplyTo(hCtrl)
* The fonts will removed automatically when script exits.
To remove a font manually, just clear the variable (e.g. font1 := "").
*/
Class CustomFont
{
static FR_PRIVATE := 0x10
__New(FontFile, FontName="", FontSize=30) {
if RegExMatch(FontFile, "i)res:\K.*", _FontFile)
this.AddFromResource(_FontFile, FontName, FontSize)
else
this.AddFromFile(FontFile)
}
AddFromFile(FontFile) {
if !FileExist(FontFile) {
throw "Unable to find font file: " FontFile
}
DllCall( "AddFontResourceEx", "Str", FontFile, "UInt", this.FR_PRIVATE, "UInt", 0 )
this.data := FontFile
}
AddFromResource(ResourceName, FontName, FontSize = 30) {
static FW_NORMAL := 400, DEFAULT_CHARSET := 0x1
nSize := this.ResRead(fData, ResourceName)
fh := DllCall( "AddFontMemResourceEx", "Ptr", &fData, "UInt", nSize, "UInt", 0, "UIntP", nFonts )
hFont := DllCall( "CreateFont", Int,FontSize, Int,0, Int,0, Int,0, UInt,FW_NORMAL, UInt,0
, Int,0, Int,0, UInt,DEFAULT_CHARSET, Int,0, Int,0, Int,0, Int,0, Str,FontName )
this.data := {fh: fh, hFont: hFont}
}
ApplyTo(hCtrl) {
SendMessage, 0x30, this.data.hFont, 1,, ahk_id %hCtrl%
}
__Delete() {
if IsObject(this.data) {
DllCall( "RemoveFontMemResourceEx", "UInt", this.data.fh )
DllCall( "DeleteObject" , "UInt", this.data.hFont )
} else {
DllCall( "RemoveFontResourceEx" , "Str", this.data, "UInt", this.FR_PRIVATE, "UInt", 0 )
}
}
; ResRead() By SKAN, from http://www.autohotkey.com/board/topic/57631-crazy-scripting-resource-only-dll-for-dummies-36l-v07/?p=609282
ResRead( ByRef Var, Key ) {
VarSetCapacity( Var, 128 ), VarSetCapacity( Var, 0 )
If ! ( A_IsCompiled ) {
FileGetSize, nSize, %Key%
FileRead, Var, *c %Key%
Return nSize
}
If hMod := DllCall( "GetModuleHandle", UInt,0 )
If hRes := DllCall( "FindResource", UInt,hMod, Str,Key, UInt,10 )
If hData := DllCall( "LoadResource", UInt,hMod, UInt,hRes )
If pData := DllCall( "LockResource", UInt,hData )
Return VarSetCapacity( Var, nSize := DllCall( "SizeofResource", UInt,hMod, UInt,hRes ) )
, DllCall( "RtlMoveMemory", Str,Var, UInt,pData, UInt,nSize )
Return 0
}
}
#Include Class_CustomFont.ahk
font1 := New CustomFont("CHOCD TRIAL___.otf")
Gui, Margin, 30, 10
Gui, Color, DECFB2
Gui, Font, s100 c510B01, Chocolate Dealer
Gui, Add, Text, w400, Chocolate
Gui, Show
Return
GuiClose:
ExitApp
#Include Class_CustomFont.ahk
font1 := New CustomFont("res:moonhouse.ttf", "moonhouse", 50)
font2 := New CustomFont("res:Selavy.otf", "Selavy-Regular", 20)
Gui, Color, Black
Gui, Add, Text, hwndhText1 w500 h50 c00FF00 Center, AutoHotkey
Gui, Add, Text, hwndhText2 wp hp cWhite Center, https://www.autohotkey.com/
Gui, Add, Button, xm gRemoveFont1, Remove Font1
font1.applyTo(hText1)
font2.applyTo(hText2)
Gui, Show
Return
RemoveFont1:
font1 := ""
WinSet, Redraw,, A
Return
GuiClose:
ExitApp
FileInstall, moonhouse.ttf, -
FileInstall, Selavy.otf, -
@KRG-23
Copy link

KRG-23 commented Aug 1, 2018

== solved in https://autohotkey.com/boards/viewtopic.php?p=235312#p235312 ==
Hello and thank you very much for this script.
However I am facing a problem when the script after sometime is not working, the default system font displays.
It worked for a couple of time and then the loaded font disappeared.
How should I request some help/support, here on in the boards? Thank you for help!

@Animan8000
Copy link

Are you planning to make a v2 version of this? Would be really useful for GUIs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment