Skip to content

Instantly share code, notes, and snippets.

@pa-0
Forked from tmplinshi/Class_CustomFont.ahk
Created October 11, 2025 23:24
Show Gist options
  • Save pa-0/4824a2886760323a30d1c65f2fdcff03 to your computer and use it in GitHub Desktop.
Save pa-0/4824a2886760323a30d1c65f2fdcff03 to your computer and use it in GitHub Desktop.

Revisions

  1. @tmplinshi tmplinshi revised this gist Aug 24, 2018. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions Class_CustomFont.ahk
    Original file line number Diff line number Diff line change
    @@ -22,17 +22,16 @@ Class CustomFont
    static FR_PRIVATE := 0x10

    __New(FontFile, FontName="", FontSize=30) {
    if !FileExist(FontFile)
    throw "Unable to find font file: " FontFile

    if RegExMatch(FontFile, "i)res:\K.*", _FontFile) {
    if RegExMatch(FontFile, "i)res:\K.*", _FontFile)
    this.AddFromResource(_FontFile, FontName, FontSize)
    } else {
    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
    }
  2. @tmplinshi tmplinshi revised this gist Aug 24, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Class_CustomFont.ahk
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*
    CustomFont v2.00 (2016-2-24)
    CustomFont v2.01 (2018-8-25)
    ---------------------------------------------------------
    Description: Load font from file or resource, without needed install to system.
    ---------------------------------------------------------
    @@ -22,6 +22,9 @@ Class CustomFont
    static FR_PRIVATE := 0x10

    __New(FontFile, FontName="", FontSize=30) {
    if !FileExist(FontFile)
    throw "Unable to find font file: " FontFile

    if RegExMatch(FontFile, "i)res:\K.*", _FontFile) {
    this.AddFromResource(_FontFile, FontName, FontSize)
    } else {
  3. @tmplinshi tmplinshi revised this gist May 7, 2018. No changes.
  4. @tmplinshi tmplinshi revised this gist May 7, 2018. No changes.
  5. @tmplinshi tmplinshi revised this gist Feb 24, 2016. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,15 @@
    #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, hwndhText w500 h50 c00FF00 Center, AutoHotkey
    font1 := New CustomFont("res:moonhouse.ttf", "moonhouse", 50)
    font1.applyTo(hText)
    Gui, Add, Text, hwndhText wp hp cWhite Center, https://www.autohotkey.com/
    font2 := New CustomFont("res:Selavy.otf", "Selavy-Regular", 20)
    font2.applyTo(hText)
    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

  6. @tmplinshi tmplinshi revised this gist Feb 24, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -20,4 +20,4 @@ GuiClose:
    ExitApp

    FileInstall, moonhouse.ttf, -
    FileInstall, trench100free.otf, -
    FileInstall, Selavy.otf, -
  7. @tmplinshi tmplinshi revised this gist Feb 24, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,11 @@ Gui, Add, Text, hwndhText w500 h50 c00FF00 Center, AutoHotkey
    Gui, Add, Text, hwndhText wp hp cWhite Center, https://www.autohotkey.com/
    font2 := New CustomFont("res:Selavy.otf", "Selavy-Regular", 20)
    font2.applyTo(hText)
    Gui, Add, Button, xm gRemoveFont, Remove Font
    Gui, Add, Button, xm gRemoveFont1, Remove Font1
    Gui, Show
    Return

    RemoveFont:
    RemoveFont1:
    font1 := ""
    WinSet, Redraw,, A
    Return
  8. @tmplinshi tmplinshi revised this gist Feb 24, 2016. No changes.
  9. @tmplinshi tmplinshi revised this gist Feb 24, 2016. 3 changed files with 57 additions and 58 deletions.
    80 changes: 45 additions & 35 deletions Class_CustomFont.ahk
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,60 @@
    ; =======================================================================================
    ; Scriptname : Class_CustomFont.ahk
    ; Description : Load font from a font file, without need installed to system.
    ; Date : 2013-12-5
    ; Tested On : AutoHotkey 1.1.13.01 A32/U32, Windows XP SP3
    ; Author : tmplinshi
    ; Credits : ResRead(), and some other codes by SKAN.
    ; =======================================================================================
    /*
    CustomFont v2.00 (2016-2-24)
    ---------------------------------------------------------
    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 FontList := []
    static MemFontList := []
    static FR_PRIVATE := 0x10

    Add(FontFile)
    {
    This.FontList.Insert( FontFile )
    Return, DllCall( "AddFontResourceEx", "Str", FontFile, "UInt", This.FR_PRIVATE, "UInt", 0 )
    __New(FontFile, FontName="", FontSize=30) {
    if RegExMatch(FontFile, "i)res:\K.*", _FontFile) {
    this.AddFromResource(_FontFile, FontName, FontSize)
    } else {
    this.AddFromFile(FontFile)
    }
    }

    ; Reference: http://www.autohotkey.com/board/topic/29396-crazy-scripting-include-and-use-truetype-font-from-script/
    AddFromResource(hCtrl, FontFile, FontName, FontSize = 30, ByRef hFont="")
    {
    static FW_NORMAL := 400, DEFAULT_CHARSET := 0x1
    AddFromFile(FontFile) {
    DllCall( "AddFontResourceEx", "Str", FontFile, "UInt", this.FR_PRIVATE, "UInt", 0 )
    this.data := FontFile
    }

    if !hFont
    {
    nSize := This.ResRead(fData, FontFile)
    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 )
    AddFromResource(ResourceName, FontName, FontSize = 30) {
    static FW_NORMAL := 400, DEFAULT_CHARSET := 0x1

    This.MemFontList.Insert( {"fh":fh, "hFont":hFont} )
    }
    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 )

    SendMessage, 0x30, hFont, 1,, ahk_id %hCtrl%
    this.data := {fh: fh, hFont: hFont}
    }

    Remove()
    {
    Loop, % This.FontList.MaxIndex()
    DllCall( "RemoveFontResourceEx" , "Str", This.FontList[A_Index], "UInt", This.FR_PRIVATE, "UInt", 0 )
    ApplyTo(hCtrl) {
    SendMessage, 0x30, this.data.hFont, 1,, ahk_id %hCtrl%
    }

    Loop, % This.MemFontList.MaxIndex()
    {
    DllCall( "RemoveFontMemResourceEx", "UInt", This.MemFontList[A_Index]["fh"] )
    DllCall( "DeleteObject" , "UInt", This.MemFontList[A_Index]["hFont"] )
    __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 )
    }
    }

    8 changes: 1 addition & 7 deletions Sample1.ahk
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,12 @@
    #Include Class_CustomFont.ahk
    SetBatchLines, -1
    OnExit, ExitSub
    font1 := New CustomFont("CHOCD TRIAL___.otf")

    Gui, Margin, 30, 10
    Gui, Color, DECFB2
    CustomFont.Add("CHOCD TRIAL___.otf")
    Gui, Font, s100 c510B01, Chocolate Dealer
    Gui, Add, Text, w400, Chocolate
    Gui, Show
    Return

    GuiClose:
    ExitApp

    ExitSub:
    CustomFont.Remove()
    ExitApp
    27 changes: 11 additions & 16 deletions Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,23 @@
    #Include Class_CustomFont.ahk
    SetBatchLines, -1
    OnExit, ExitSub

    Gui, Color, Black
    Gui, Add, Text, hwndhText w500 h50 c00FF00 Center, AutoHotkey
    CustomFont.AddFromResource(hText, "moonhouse.ttf", "moonhouse", 50, hFont1)

    Gui, Add, Text, hwndhText wp hp cWhite Center, http://ahkscript.org/
    CustomFont.AddFromResource(hText, "Selavy.otf", "Selavy-Regular", 20, hFont2)

    Gui, Add, Text, hwndhText wp hp cWhite Center cRed, *.ttf files can be compiled into the executable
    CustomFont.AddFromResource(hText, "Selavy.otf", "Selavy-Regular", 20, hFont2)
    ; hFont2 is already exist, the function will not add the font again.

    font1 := New CustomFont("res:moonhouse.ttf", "moonhouse", 50)
    font1.applyTo(hText)
    Gui, Add, Text, hwndhText wp hp cWhite Center, https://www.autohotkey.com/
    font2 := New CustomFont("res:Selavy.otf", "Selavy-Regular", 20)
    font2.applyTo(hText)
    Gui, Add, Button, xm gRemoveFont, Remove Font
    Gui, Show
    Return

    GuiClose:
    ExitApp
    RemoveFont:
    font1 := ""
    WinSet, Redraw,, A
    Return

    ExitSub:
    CustomFont.Remove()
    GuiClose:
    ExitApp

    ; *.ttf files can be compiled into the executable.
    FileInstall, moonhouse.ttf, -
    FileInstall, trench100free.otf, -
  10. @tmplinshi tmplinshi revised this gist Dec 4, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -6,11 +6,11 @@ Gui, Color, Black
    Gui, Add, Text, hwndhText w500 h50 c00FF00 Center, AutoHotkey
    CustomFont.AddFromResource(hText, "moonhouse.ttf", "moonhouse", 50, hFont1)

    Gui, Add, Text, hwndhText wp hp cWhite Center, http://ahkscript.org/boards/
    CustomFont.AddFromResource(hText, "trench100free.otf", "Trench", 20, hFont2)
    Gui, Add, Text, hwndhText wp hp cWhite Center, http://ahkscript.org/
    CustomFont.AddFromResource(hText, "Selavy.otf", "Selavy-Regular", 20, hFont2)

    Gui, Add, Text, hwndhText wp hp cWhite Center cRed, *.ttf files can be compiled into the executable
    CustomFont.AddFromResource(hText, "trench100free.otf", "Trench", 20, hFont2)
    CustomFont.AddFromResource(hText, "Selavy.otf", "Selavy-Regular", 20, hFont2)
    ; hFont2 is already exist, the function will not add the font again.

    Gui, Show
  11. @tmplinshi tmplinshi revised this gist Dec 4, 2013. 3 changed files with 9 additions and 17 deletions.
    6 changes: 2 additions & 4 deletions Class_CustomFont.ahk
    Original file line number Diff line number Diff line change
    @@ -12,21 +12,19 @@ Class CustomFont
    static MemFontList := []
    static FR_PRIVATE := 0x10

    Add(FontFile, ByRef FontName="")
    Add(FontFile)
    {
    FontName := IsByRef(FontName) ? This.TT_GetFontName(FontFile) : ""
    This.FontList.Insert( FontFile )
    Return, DllCall( "AddFontResourceEx", "Str", FontFile, "UInt", This.FR_PRIVATE, "UInt", 0 )
    }

    ; Reference: http://www.autohotkey.com/board/topic/29396-crazy-scripting-include-and-use-truetype-font-from-script/
    AddFromResource(hCtrl, FontFile, FontName="", FontSize = 30, ByRef hFont="")
    AddFromResource(hCtrl, FontFile, FontName, FontSize = 30, ByRef hFont="")
    {
    static FW_NORMAL := 400, DEFAULT_CHARSET := 0x1

    if !hFont
    {
    FontName := (FontName = "") ? This.TT_GetFontName(FontFile) : FontName
    nSize := This.ResRead(fData, FontFile)
    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
    12 changes: 5 additions & 7 deletions Sample1.ahk
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,18 @@
    #Include Class_CustomFont.ahk
    #NoEnv
    #SingleInstance Force
    SetWorkingDir %A_ScriptDir%
    SetBatchLines -1
    SetBatchLines, -1
    OnExit, ExitSub

    Gui, Margin, 30, 10
    Gui, Color, DECFB2
    CustomFont.Add("CHOCD TRIAL___.otf")
    Gui, Font, s100, Chocolate Dealer
    Gui, Add, Text, c510B01 w500 Center, Chocolate
    Gui, Font, s100 c510B01, Chocolate Dealer
    Gui, Add, Text, w400, Chocolate
    Gui, Show
    Return

    GuiClose:
    ExitApp

    ExitSub:
    CustomFont.Remove() ; Remove fonts on exit. But seems not necessary.
    CustomFont.Remove()
    ExitApp
    8 changes: 2 additions & 6 deletions Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,5 @@
    #Include Class_CustomFont.ahk
    #NoEnv
    #SingleInstance Force
    SetWorkingDir %A_ScriptDir%
    SetBatchLines -1
    ListLines Off
    SetBatchLines, -1
    OnExit, ExitSub

    Gui, Color, Black
    @@ -24,7 +20,7 @@ GuiClose:
    ExitApp

    ExitSub:
    CustomFont.Remove() ; Remove fonts on exit. But seems not necessary.
    CustomFont.Remove()
    ExitApp

    ; *.ttf files can be compiled into the executable.
  12. @tmplinshi tmplinshi revised this gist Dec 4, 2013. 2 changed files with 2 additions and 66 deletions.
    54 changes: 2 additions & 52 deletions Class_CustomFont.ahk
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,17 @@
    ; =======================================================================================
    ; Scriptname : Class_CustomFont.ahk
    ; Description : Load font from a font file, without need installed to system.
    ; Version : 1.00 / 2013-11-30
    ; Date : 2013-12-5
    ; Tested On : AutoHotkey 1.1.13.01 A32/U32, Windows XP SP3
    ; Author : tmplinshi
    ; Credits : ResRead(), TT_GetFontName(), and some other codes by SKAN.
    ; Credits : ResRead(), and some other codes by SKAN.
    ; =======================================================================================
    Class CustomFont
    {
    static FontList := []
    static MemFontList := []
    static FR_PRIVATE := 0x10

    ; Note: Only *.ttf file's font name can be retrieved.

    Add(FontFile, ByRef FontName="")
    {
    FontName := IsByRef(FontName) ? This.TT_GetFontName(FontFile) : ""
    @@ -69,52 +67,4 @@ Class CustomFont
    , DllCall( "RtlMoveMemory", Str,Var, UInt,pData, UInt,nSize )
    Return 0
    }

    TT_GetFontName( TTF="" ) { ; By SKAN ; www.autohotkey.com/forum/viewtopic.php?t=39572
    Static SearchParameters="3-1033-4|1-0-1", StrGet="StrGet" ; CD:07-Oct-2009 LM:25-Oct-2010
    ;Refer : The 'name' table - http://developer.apple.com/fonts/ttrefman/RM06/Chap6name.html
    If ( hFil := DllCall( "_lopen", A_IsUnicode ? "AStr" : "Str",TTF, UInt,0x40 ) ) < 1
    Return
    VarSetCapacity(TTOT,12,0), DllCall( "_lread",Int,hFil,Str,TTOT,UInt,12 ), P := &TTOT
    If ((*(P+0)<<8)+(*(P+1))!=1)||((*(P+2)<<8)+(*(P+3))!=0)||((nTab:=(*(P+4)<<8)+(*(P+5)))<1)
    Return SubStr( DllCall( "_lclose",UInt,hFil ),1,0 )
    VarSetCapacity(TD,nTab*16,0), DllCall("_lread",Int,hFil,Str,TD,UInt,nTab*16), P := &TD-16
    Loop %nTab% {
    If ( NumGet( P+0 ) <> 0x656D616E && P := P+16 )
    Continue
    dOff := (*(P+08)<<24)+(*(P+09)<<16)+(*(P+10)<<8)+(*(P+11))
    dLen := (*(P+12)<<24)+(*(P+13)<<16)+(*(P+14)<<8)+(*(P+15))
    Break
    }
    If ( dOff="" || dLen="" )
    Return SubStr( DllCall( "_lclose",UInt,hFil ),1,0 )
    sOff := DllCall("_llseek", UInt,hFil, UInt,dOff, Int,0)
    VarSetCapacity(TTNTH,6,0), DllCall( "_lread", Int,hFil, Str,TTNTH, UInt,6 ), P := &TTNTH
    nRec:=(*(P+2)<<8)+(*(P+3)),sOff:=sOff+(*(P+4)<<8)+(*(P+5)),VarSetCapacity(TTNR,12*nRec,0)
    DllCall( "_lread", UInt,hFil, Str,TTNR, UInt,12*nRec )
    Loop, Parse, SearchParameters, |
    {
    P := &TTNR-12, Ansi := A_Index-1
    StringSplit, Parameter, A_LoopField,-
    Loop, %nRec% {
    uPFID:=(*(P:=P+12)<<8)+(*(P+1)), uLID:=(*(P+4)<<8)+(*(P+5)), uNID:=(*(P+6)<<8)+(*(P+7))
    If ( uPFID=Parameter1 && uLID=Parameter2 && uNID=Parameter3 ) {
    nOff := (*(P+10)<<8)+(*(P+11)), Len:=(*(P+8)<<8)+(*(P+9)), Break := True
    Break
    }
    }
    IfEqual, Break, %True%, Break
    }
    If ( nOff="" || Len="" )
    Return SubStr( DllCall("_lclose",UInt,hFil), 1,0 )
    DllCall( "_llseek", UInt,hFil, UInt,nOff+sOff, Int,0 ), VarSetCapacity( FN,Len+2,0 )
    DllCall( "_lread", Int,hFil, Str,FN, UInt,Len ), DllCall( "_lclose", UInt,hFil )
    IfEqual, Ansi, 1, Return, A_IsUnicode ? %StrGet%(&FN,"") : FN
    VarSetCapacity(FLE,Len+2,0), LCMAP_BYTEREV:=0x800, N := DllCall( "lstrlenW", str,FN ) + 1
    DllCall( "LCMapStringW", Int,0, UInt,LCMAP_BYTEREV, Str,FN, UInt,N, Str,FLE, UInt,N )
    IfEqual, A_IsUnicode, 1, Return, FLE
    VarSetCapacity( Ansi, N+1 )
    DllCall( "WideCharToMultiByte", Int,0, Int,0, Str,FLE, UInt,N, Str,Ansi, UInt,N+1, Int,0, Int,0 )
    Return Ansi
    }
    }
    14 changes: 0 additions & 14 deletions Sample1.ahk
    Original file line number Diff line number Diff line change
    @@ -6,23 +6,9 @@ SetBatchLines -1
    OnExit, ExitSub

    Gui, Color, DECFB2

    ; Load font, and store font name into FontName1
    CustomFont.Add("Flim-Flam.ttf", FontName1)

    ; Now specify the gui font
    Gui, Font, s30, %FontName1%
    Gui, Add, Text, w500 Center, AutoHotkey

    ; ----------------------------------------------------

    ; Load another font.
    CustomFont.Add("CHOCD TRIAL___.otf")

    ; You need to specify the font name this time, because the function can only get font name of *.ttf files.
    Gui, Font, s100, Chocolate Dealer
    Gui, Add, Text, c510B01 w500 Center, Chocolate

    Gui, Show
    Return

  13. @tmplinshi tmplinshi revised this gist Nov 30, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,6 @@ Gui, Add, Text, hwndhText w500 h50 c00FF00 Center, AutoHotkey

    Gui, Add, Text, hwndhText wp hp cWhite Center, http://ahkscript.org/boards/
    CustomFont.AddFromResource(hText, "trench100free.otf", "Trench", 20, hFont2)
    ; Note: Only *.ttf file's font name can be retrieved. So here we need to specify the font name.

    Gui, Add, Text, hwndhText wp hp cWhite Center cRed, *.ttf files can be compiled into the executable
    CustomFont.AddFromResource(hText, "trench100free.otf", "Trench", 20, hFont2)
  14. @tmplinshi tmplinshi revised this gist Nov 30, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ OnExit, ExitSub

    Gui, Color, Black
    Gui, Add, Text, hwndhText w500 h50 c00FF00 Center, AutoHotkey
    CustomFont.AddFromResource(hText, "moonhouse.ttf", "", 50, hFont1)
    CustomFont.AddFromResource(hText, "moonhouse.ttf", "moonhouse", 50, hFont1)

    Gui, Add, Text, hwndhText wp hp cWhite Center, http://ahkscript.org/boards/
    CustomFont.AddFromResource(hText, "trench100free.otf", "Trench", 20, hFont2)
  15. @tmplinshi tmplinshi created this gist Nov 30, 2013.
    120 changes: 120 additions & 0 deletions Class_CustomFont.ahk
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    ; =======================================================================================
    ; Scriptname : Class_CustomFont.ahk
    ; Description : Load font from a font file, without need installed to system.
    ; Version : 1.00 / 2013-11-30
    ; Tested On : AutoHotkey 1.1.13.01 A32/U32, Windows XP SP3
    ; Author : tmplinshi
    ; Credits : ResRead(), TT_GetFontName(), and some other codes by SKAN.
    ; =======================================================================================
    Class CustomFont
    {
    static FontList := []
    static MemFontList := []
    static FR_PRIVATE := 0x10

    ; Note: Only *.ttf file's font name can be retrieved.

    Add(FontFile, ByRef FontName="")
    {
    FontName := IsByRef(FontName) ? This.TT_GetFontName(FontFile) : ""
    This.FontList.Insert( FontFile )
    Return, DllCall( "AddFontResourceEx", "Str", FontFile, "UInt", This.FR_PRIVATE, "UInt", 0 )
    }

    ; Reference: http://www.autohotkey.com/board/topic/29396-crazy-scripting-include-and-use-truetype-font-from-script/
    AddFromResource(hCtrl, FontFile, FontName="", FontSize = 30, ByRef hFont="")
    {
    static FW_NORMAL := 400, DEFAULT_CHARSET := 0x1

    if !hFont
    {
    FontName := (FontName = "") ? This.TT_GetFontName(FontFile) : FontName
    nSize := This.ResRead(fData, FontFile)
    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.MemFontList.Insert( {"fh":fh, "hFont":hFont} )
    }

    SendMessage, 0x30, hFont, 1,, ahk_id %hCtrl%
    }

    Remove()
    {
    Loop, % This.FontList.MaxIndex()
    DllCall( "RemoveFontResourceEx" , "Str", This.FontList[A_Index], "UInt", This.FR_PRIVATE, "UInt", 0 )

    Loop, % This.MemFontList.MaxIndex()
    {
    DllCall( "RemoveFontMemResourceEx", "UInt", This.MemFontList[A_Index]["fh"] )
    DllCall( "DeleteObject" , "UInt", This.MemFontList[A_Index]["hFont"] )
    }
    }

    ; 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
    }

    TT_GetFontName( TTF="" ) { ; By SKAN ; www.autohotkey.com/forum/viewtopic.php?t=39572
    Static SearchParameters="3-1033-4|1-0-1", StrGet="StrGet" ; CD:07-Oct-2009 LM:25-Oct-2010
    ;Refer : The 'name' table - http://developer.apple.com/fonts/ttrefman/RM06/Chap6name.html
    If ( hFil := DllCall( "_lopen", A_IsUnicode ? "AStr" : "Str",TTF, UInt,0x40 ) ) < 1
    Return
    VarSetCapacity(TTOT,12,0), DllCall( "_lread",Int,hFil,Str,TTOT,UInt,12 ), P := &TTOT
    If ((*(P+0)<<8)+(*(P+1))!=1)||((*(P+2)<<8)+(*(P+3))!=0)||((nTab:=(*(P+4)<<8)+(*(P+5)))<1)
    Return SubStr( DllCall( "_lclose",UInt,hFil ),1,0 )
    VarSetCapacity(TD,nTab*16,0), DllCall("_lread",Int,hFil,Str,TD,UInt,nTab*16), P := &TD-16
    Loop %nTab% {
    If ( NumGet( P+0 ) <> 0x656D616E && P := P+16 )
    Continue
    dOff := (*(P+08)<<24)+(*(P+09)<<16)+(*(P+10)<<8)+(*(P+11))
    dLen := (*(P+12)<<24)+(*(P+13)<<16)+(*(P+14)<<8)+(*(P+15))
    Break
    }
    If ( dOff="" || dLen="" )
    Return SubStr( DllCall( "_lclose",UInt,hFil ),1,0 )
    sOff := DllCall("_llseek", UInt,hFil, UInt,dOff, Int,0)
    VarSetCapacity(TTNTH,6,0), DllCall( "_lread", Int,hFil, Str,TTNTH, UInt,6 ), P := &TTNTH
    nRec:=(*(P+2)<<8)+(*(P+3)),sOff:=sOff+(*(P+4)<<8)+(*(P+5)),VarSetCapacity(TTNR,12*nRec,0)
    DllCall( "_lread", UInt,hFil, Str,TTNR, UInt,12*nRec )
    Loop, Parse, SearchParameters, |
    {
    P := &TTNR-12, Ansi := A_Index-1
    StringSplit, Parameter, A_LoopField,-
    Loop, %nRec% {
    uPFID:=(*(P:=P+12)<<8)+(*(P+1)), uLID:=(*(P+4)<<8)+(*(P+5)), uNID:=(*(P+6)<<8)+(*(P+7))
    If ( uPFID=Parameter1 && uLID=Parameter2 && uNID=Parameter3 ) {
    nOff := (*(P+10)<<8)+(*(P+11)), Len:=(*(P+8)<<8)+(*(P+9)), Break := True
    Break
    }
    }
    IfEqual, Break, %True%, Break
    }
    If ( nOff="" || Len="" )
    Return SubStr( DllCall("_lclose",UInt,hFil), 1,0 )
    DllCall( "_llseek", UInt,hFil, UInt,nOff+sOff, Int,0 ), VarSetCapacity( FN,Len+2,0 )
    DllCall( "_lread", Int,hFil, Str,FN, UInt,Len ), DllCall( "_lclose", UInt,hFil )
    IfEqual, Ansi, 1, Return, A_IsUnicode ? %StrGet%(&FN,"") : FN
    VarSetCapacity(FLE,Len+2,0), LCMAP_BYTEREV:=0x800, N := DllCall( "lstrlenW", str,FN ) + 1
    DllCall( "LCMapStringW", Int,0, UInt,LCMAP_BYTEREV, Str,FN, UInt,N, Str,FLE, UInt,N )
    IfEqual, A_IsUnicode, 1, Return, FLE
    VarSetCapacity( Ansi, N+1 )
    DllCall( "WideCharToMultiByte", Int,0, Int,0, Str,FLE, UInt,N, Str,Ansi, UInt,N+1, Int,0, Int,0 )
    Return Ansi
    }
    }
    34 changes: 34 additions & 0 deletions Sample1.ahk
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #Include Class_CustomFont.ahk
    #NoEnv
    #SingleInstance Force
    SetWorkingDir %A_ScriptDir%
    SetBatchLines -1
    OnExit, ExitSub

    Gui, Color, DECFB2

    ; Load font, and store font name into FontName1
    CustomFont.Add("Flim-Flam.ttf", FontName1)

    ; Now specify the gui font
    Gui, Font, s30, %FontName1%
    Gui, Add, Text, w500 Center, AutoHotkey

    ; ----------------------------------------------------

    ; Load another font.
    CustomFont.Add("CHOCD TRIAL___.otf")

    ; You need to specify the font name this time, because the function can only get font name of *.ttf files.
    Gui, Font, s100, Chocolate Dealer
    Gui, Add, Text, c510B01 w500 Center, Chocolate

    Gui, Show
    Return

    GuiClose:
    ExitApp

    ExitSub:
    CustomFont.Remove() ; Remove fonts on exit. But seems not necessary.
    ExitApp
    33 changes: 33 additions & 0 deletions Sample2.ahk
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #Include Class_CustomFont.ahk
    #NoEnv
    #SingleInstance Force
    SetWorkingDir %A_ScriptDir%
    SetBatchLines -1
    ListLines Off
    OnExit, ExitSub

    Gui, Color, Black
    Gui, Add, Text, hwndhText w500 h50 c00FF00 Center, AutoHotkey
    CustomFont.AddFromResource(hText, "moonhouse.ttf", "", 50, hFont1)

    Gui, Add, Text, hwndhText wp hp cWhite Center, http://ahkscript.org/boards/
    CustomFont.AddFromResource(hText, "trench100free.otf", "Trench", 20, hFont2)
    ; Note: Only *.ttf file's font name can be retrieved. So here we need to specify the font name.

    Gui, Add, Text, hwndhText wp hp cWhite Center cRed, *.ttf files can be compiled into the executable
    CustomFont.AddFromResource(hText, "trench100free.otf", "Trench", 20, hFont2)
    ; hFont2 is already exist, the function will not add the font again.

    Gui, Show
    Return

    GuiClose:
    ExitApp

    ExitSub:
    CustomFont.Remove() ; Remove fonts on exit. But seems not necessary.
    ExitApp

    ; *.ttf files can be compiled into the executable.
    FileInstall, moonhouse.ttf, -
    FileInstall, trench100free.otf, -