[Class] CustomFont - Load font from file or resource, without needed install to system.

Post your working scripts, libraries and tools
tmplinshi
Posts: 1436
Joined: 01 Oct 2013, 14:57

[Class] CustomFont - Load font from file or resource, without needed install to system.

30 Nov 2013, 05:11

CustomFont v2.00 (2016-2-24)

Useage Examples:

Code: Select all

* 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 := "").
Source: https://gist.github.com/tmplinshi/7717340
Download Samples and fonts: https://drive.google.com/open?id=1EWH56 ... O1t_1_Sjlw
Last edited by tmplinshi on 07 May 2018, 00:25, edited 4 times in total.
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Class_CustomFont - Load font from font file

05 Dec 2013, 06:58

very nice work :)
for direct help, tweet at me: https://twitter.com/CW_DTech
garry
Posts: 1845
Joined: 22 Dec 2013, 12:50

Re: Class_CustomFont - Load font from font file

06 Feb 2016, 15:57

thank you tmplinshi
script below works, but I don't know how to get the variable F2
(I woul'd try to make a loop with all fonts)

1- How can I get e.g. the name > Selavy-Regular ( from Selavy.otf ) ?
( when I put the file in folder C:\WINDOWS\Fonts I can see this )

example splitpath with "moonhouse.ttf" was ok , F2=moonhouse


and
2- How change the color again ?

;....
i=2
f1=%a_scriptdir%\moonhouse.ttf
;f1=%a_scriptdir%\Selavy.otf ; not works , name_no_ext should be >> Selavy-Regular
SplitPath,F1,name, dir, ext, name_no_ext, drive
f2:=name_no_ext
CustomFont.AddFromResource(hText, "%f1%", f2, 50, hFont%i%)
;.....

Code: Select all

/*
tmplinshi:
https://autohotkey.com/boards/viewtopic.php?t=813

Add( FontFile)
AddFromResource( hCtrl, FontFile, FontName, [FontSize = 30] , ByRef hFont="" )
Remove()

C:\WINDOWS\Fonts
*/

;- try to change font

#Include Class_CustomFont.ahk
SetBatchLines, -1
OnExit, ExitSub
Gui,3:Color, Black
Gui,3:Color, ControlColor, Black
f2=Fixedsys
Gui,3:Font,,%f2%
Gui,3:show ,x10 y10 h250 w400,TEST
Gui,3:add,edit,x10 y10 h180 w370 vED1 hwndhText cRed,Line1`nLine2
;       CustomFont.AddFromResource(hText, "Selavy.otf", "Selavy-Regular", 50, hFont1)
;	CustomFont.AddFromResource(hText, "moonhouse.ttf", "moonhouse", 50, hFont1)
Gui,3:add,edit,x10 y200 h25 w370 readonly cGray vED2
guicontrol,3: ,ED2,%f2%
gosub,a1
return

3Guiclose:
exitapp
ExitSub:
CustomFont.Remove()
ExitApp


a1:
sleep,2000
f2=Lucida Console
GUI,3:Font,s36 cYellow ,%f2%    ;- change color and font
guicontrol,3: font, ED1
guicontrol,3: ,ED2,%f2%

i=2
sleep,2000
f1=%a_scriptdir%\moonhouse.ttf
;f1=%a_scriptdir%\Selavy.otf              ; not works , name_no_ext should be >> Selavy-Regular
SplitPath,F1,name, dir, ext, name_no_ext, drive
f2:=name_no_ext
CustomFont.AddFromResource(hText, "%f1%", f2, 50, hFont%i%)
guicontrol,3: ,ED2,%f2%

i=3
sleep,2000
f1=%a_scriptdir%\Selavy.otf
f2:="Selavy-Regular"
CustomFont.AddFromResource(hText, "%f1%", f2, 50, hFont%i%)
guicontrol,3: ,ED2,%f2%
return
tmplinshi
Posts: 1436
Joined: 01 Oct 2013, 14:57

Re: Class_CustomFont - Load font from font file

07 Feb 2016, 02:32

@garry
I use otfinfo to get font name.

To change font color, use GuiControl command. See the documentation for details.
garry
Posts: 1845
Joined: 22 Dec 2013, 12:50

Re: Class_CustomFont - Load font from font file

07 Feb 2016, 09:56

@tmplinshi , thank you for help and link
lblb
Posts: 129
Joined: 30 Sep 2013, 11:31

Re: Class_CustomFont - Load font from font file

22 Feb 2016, 04:18

Hi tmplinshi,

Great work, this will be very useful for one of my projects. As I understand it, your "remove" function removes all loaded fonts. Would it be possible for you to add a function to which we could pass which specific font(s) from the list of loaded fonts we want removed (and have the font list arrays updated appropriately)?

If that's too complicated, could you please show me what code I would need to remove from memory one specific font, for example the font that was loaded from a specific location such as "C:\Program Files\Your Program\ThisFont.ttf"? For some reason, I can't seem to get the code right.

Thanks for your work on this!
tmplinshi
Posts: 1436
Joined: 01 Oct 2013, 14:57

Re: Class_CustomFont - Load font from file or resource, without needed install to system.

24 Feb 2016, 02:39

@lblb
I have updated the script. See the top post.
lblb
Posts: 129
Joined: 30 Sep 2013, 11:31

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

24 Feb 2016, 12:21

Hi tmplinshi,

Oh that looks great! I'll have a chance to try it this weekend and will let you know if I encounter any issue. Thank you very much for all your work on this and for your quick and impressive response to my request, much appreciated.
garry
Posts: 1845
Joined: 22 Dec 2013, 12:50

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

12 Mar 2016, 10:23

I was playing with fonts with this test script

used installed C:\WINDOWS\FONTS , see how FONT's look like, from user Alguimist
https://autohotkey.com/boards/viewtopic.php?f=5&t=14838

Code: Select all

;-------- saved at Sonntag, 13. März 2016 11:14:50 --------------
;-------- https://autohotkey.com/boards/viewtopic.php?f=5&t=14838 ---
;-------- from user = Alguimist

#NoEnv                        ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn
SendMode Input                ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetBatchLines, -1


Gui,3: default
Gui,3:Color, Black
Gui,3:Color, ControlColor, Black
Gui,3:Font,,FixedSys


   Menu,S1,add,Script_tmplinshi  ,MH1
   Menu,S1,add,Script_Source     ,MH1
   Menu,S1,add,Script_ZIP        ,MH1
   Menu,S1,add,Script_alguimist  ,MH1
   Menu,S1,add,Ahk_Font_Help     ,MH1
   Menu,S1,add,OTFINFO_HELP      ,MH1
   Menu,S1,add,OTFINFO_Download  ,MH1
   Menu,S1,add,SKAN              ,MH1
   Menu,S1,add,Latofonts         ,MH1
   Menu,S1,add,FontsOldSchool    ,MH1
   Menu,S1,add,Fontsquirrel      ,MH1


   Menu,S2,add,AHK_Version       ,MH2
   Menu,S3,add,WindowsFonts      ,MH3
   ;----------------------------
   menu,myMenuBar,Add,Font_Links    ,:S1
   menu,myMenuBar,Add,AHK_Version   ,:S2
   menu,myMenuBar,Add,WindowsFonts  ,:S3
   ;----------------------------
   gui,3:menu,MyMenuBar
   ;----------------------------


Gui,3: Add, ListView, x10 y10 w270 h360 sort gLV1 vLVA1 cYellow +hscroll altsubmit, Font name
LV_ModifyCol(1, 250)

gosub,functionx

Loop, % FontList.MaxIndex()
   LV_Add("", FontList[A_Index])

e4x:=""
loop,15
 e4x .= "This is a testline. This is Line- #@-" . a_index . "`r`n"

Gui,3:add,edit   ,x290  y10  h360 w470 vED1 hwndhText cTeal,%e4x%
Gui,3:add,edit   ,x290  y380 h25  w470 readonly cRed    vED2,Fixedsys
Gui,3:add,button ,x10   y380 h25  w80 gStart1 vSt1,Start
Gui,3:add,button ,x100  y380 h25  w80 gStop1 ,Stop
Gui,3:show ,x10 y10 h420 w800,WINDOWS-FONTS_TEST
Guicontrol,3:focus,st1
return
;---------------------------------------------------------------
3Guiclose:
exitapp
;---------------------------------------------------------------


LV1:
Gui,3:default
Gui,3:ListView,LVA1
Gui,3:submit,nohide

;RN:=LV_GetNext("C") ;2 selected checked
;RF:=LV_GetNext("F") ;2 selected focused
;GC:=LV_GetCount()   ;4 total

if A_GuiEvent = Normal
{
LV_GetText(C1,A_EventInfo,1)
;Gui,3: font, s14 cF16400 strike, %c1%           ;- change text-COLOR / SIZE / FONT
Gui,3: font, s12 cYellow normal , %c1%
Guicontrol,3: ,ED2,%c1%
Guicontrol,3: Font, ED1
Guicontrol,3: ,ED1,%ed1%                        ;- maybe CHANGE TEXT
}
return
;----------------------------------------


;----------------------------------------
start1:
breakloop=0
Gui,3:ListView,LVA1
i=0
loop,
{
if (breakloop=1)
   break

LV_Modify(I, "-Select -Focus")
i++
LV_Modify(I, "+Select +Focus")
RF:=LV_GetNext("F") ;2 selected focused
LV_GetText(C1,RF,1)
LV_Modify(RF, "Vis")    ;- scrolltox

Gui,3: font, s16 cF16400, %c1%                    ;- change text-COLOR / SIZE / FONT
Guicontrol,3: ,ED2,%c1%
Guicontrol,3: Font, ED1
Guicontrol,3: ,ED1,%ed1%                          ;- maybe CHANGE TEXT
sleep,1000
}
return
;-----------------------------

;----------------
stop1:
breakloop=1
return
;----------------

;--------------------------------------------------------------
mh1:


if A_thisMenuItem=Script_tmplinshi
   run,https://autohotkey.com/boards/viewtopic.php?f=6&t=813     ;- classcustomfont tmplinshi
if A_thisMenuItem=Script_Source
    run,https://gist.github.com/tmplinshi/7717340
if A_thisMenuItem=Script_ZIP
    run,https://www.dropbox.com/s/4h4e559tggvk1cq/Class_CustomFont_Samples.7z?raw=1
if A_thisMenuItem=Script_alguimist
    run,https://autohotkey.com/boards/viewtopic.php?f=5&t=14838
if A_thisMenuItem=Ahk_Font_Help
   run,https://autohotkey.com/docs/misc/FontsStandard.htm
if A_thisMenuItem=otfinfo_Help
   run,http://www.lcdf.org/type/otfinfo.1.html
if A_thisMenuItem=otfinfo_Download
   run,http://math.sut.ac.th/lab/software/texlive/bin/win32/otfinfo.exe
if A_thisMenuItem=SKAN
   run,https://autohotkey.com/board/topic/29396-crazy-scripting-include-and-use-truetype-font-from-script/
if A_thisMenuItem=Latofonts
   run,http://www.latofonts.com/lato-free-fonts/
if A_thisMenuItem=Fontsquirrel
   run,http://www.fontsquirrel.com/tools/webfont-generator
if A_thisMenuItem=FontsOldSchool
   run,http://int10h.org/oldschool-pc-fonts/

Guicontrol,3:focus,st1
return

mh2:
if A_thisMenuItem=AHK_version
  msgbox, 262208,AHK_Version ,Ahk-Version =%A_AHKVERSION%
Guicontrol,3:focus,st1
return

mh3:
ifexist,C:\windows\fonts
    run,C:\windows\fonts
Guicontrol,3:focus,st1
return
;--------------------------------------------------------------


;----------- from user alguimist ------------------------------
;-https://autohotkey.com/boards/viewtopic.php?f=5&t=14838
functionx:
Global FontList := []
EnumFonts() {
    hDC := DllCall("GetDC", "UInt", DllCall("GetDesktopWindow"))
    Callback := RegisterCallback("EnumFontsCallback", "F")
    DllCall("EnumFontFamilies", "UInt", hDC, "UInt", 0, "Ptr", Callback, "UInt", lParam := 0)
    DllCall("ReleaseDC", "UInt", hDC)
}

EnumFontsCallback(lpelf) {
    FontList.Push(StrGet(lpelf + 28, 32))
    Return True
}

EnumFonts()
aa:=FontList.MaxIndex()
return
;--------------------------------------------------------------
;==============================================================


other examples if don't know the FONT-name
needs to download otfinfo.exe to get FONT-name

Code: Select all

;- Modified    = 20160312
;- Created     = 20160312
;- Description = a testscript to show/select/change  [ fonts , font-size, text-color ]

/*
https://autohotkey.com/boards/viewtopic.php?f=6&t=813
CustomFont v2.00 (2016-2-24) user tmplinshi
Description: Load font from file or resource, without needed install to system.
https://gist.github.com/tmplinshi/7717340
https://www.dropbox.com/s/4h4e559tggvk1cq/Class_CustomFont_Samples.7z?raw=1
*/

#NoEnv                        ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input                ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetBatchLines, -1

ifnotexist,otfinfo.exe
    {
    msgbox,download otfinfo.exe from=`nhttp://math.sut.ac.th/lab/software/texlive/bin/win32/otfinfo.exe
    exitapp
    }

;- maybe automatically download otfinfo.exe
; ifnotexist,otfinfo.exe
;    urldownloadtofile,http://math.sut.ac.th/lab/software/texlive/bin/win32/otfinfo.exe,otfinfo.exe


extensions:="otf,ttf"

Gui,3: default
Gui,3:Color, Black
Gui,3:Color, ControlColor, Black
Gui,3:Font,,FixedSys

   Menu,S1,add,Script_Forum_AHK  ,MH1
   Menu,S1,add,Script_Source     ,MH1
   Menu,S1,add,Script_Script_ZIP ,MH1
   Menu,S1,add,Ahk_Font_Help     ,MH1
   Menu,S1,add,OTFINFO_HELP      ,MH1
   Menu,S1,add,OTFINFO_Download  ,MH1
   Menu,S1,add,SKAN              ,MH1
   Menu,S1,add,Latofonts         ,MH1
   Menu,S1,add,FontsOldSchool    ,MH1
   Menu,S1,add,Fontsquirrel      ,MH1

   Menu,S2,add,AHK_Version       ,MH2

   Menu,S3,add,WindowsFonts      ,MH3

   ;----------------------------
   menu,myMenuBar,Add,FONT_Help     ,:S1
   menu,myMenuBar,Add,AHK_Version   ,:S2
   menu,myMenuBar,Add,WindowsFonts  ,:S3

   ;----------------------------
   gui,3:menu,MyMenuBar
   ;----------------------------

Gui,3:add,edit   ,x10  y10 h180 w470 vED1 hwndhText cYellow,Line1_Test`nLine2_Test`nLine3_Test
Gui,3:add,edit   ,x10  y200 h25 w470 readonly cRed vED2,Fixedsys
Gui,3:add,button ,x10  y240 h25 w80 gStart1,Start
Gui,3:add,button ,x100 y240 h25 w80 gStop1 ,Stop
Gui,3:show ,x10 y10 h275 w500,FONTS_TEST
Guicontrol,3:focus,ed2
return
;---------------------------------------------------------------
3Guiclose:
exitapp
;---------------------------------------------------------------

start1:
breakloop=0
sleep,2000     ;- see FONT  from GUI
gosub,a1       ;- see FONT  example-1
sleep,3000
gosub,a2       ;- see FONTs from C:\WINDOWS\FONTS
return

stop1:
breakloop=1
return

mh1:
if A_thisMenuItem=Script_Forum_AHK
   run,https://autohotkey.com/boards/viewtopic.php?f=6&t=813     ;- classcustomfont tmplinshi
if A_thisMenuItem=Script_Source
    run,https://gist.github.com/tmplinshi/7717340
if A_thisMenuItem=Script_ZIP
    run,https://www.dropbox.com/s/4h4e559tggvk1cq/Class_CustomFont_Samples.7z?raw=1
if A_thisMenuItem=Ahk_Font_Help
   run,https://autohotkey.com/docs/misc/FontsStandard.htm
if A_thisMenuItem=otfinfo_Help
   run,http://www.lcdf.org/type/otfinfo.1.html
if A_thisMenuItem=otfinfo_Download
   run,http://math.sut.ac.th/lab/software/texlive/bin/win32/otfinfo.exe
if A_thisMenuItem=SKAN
   run,https://autohotkey.com/board/topic/29396-crazy-scripting-include-and-use-truetype-font-from-script/
if A_thisMenuItem=Latofonts
   run,http://www.latofonts.com/lato-free-fonts/
if A_thisMenuItem=Fontsquirrel
   run,http://www.fontsquirrel.com/tools/webfont-generator
if A_thisMenuItem=FontsOldSchool
   run,http://int10h.org/oldschool-pc-fonts/
Guicontrol,3:focus,ed2
return

mh2:
if A_thisMenuItem=AHK_version
  msgbox, 262208,AHK_Version ,Ahk-Version =%A_AHKVERSION%
Guicontrol,3:focus,ed2
return

mh3:
ifexist,C:\windows\fonts
    run,C:\windows\fonts
Guicontrol,3:focus,ed2
return


;------ example-1 or ahk  ------------------------------------------------------
a1:
Gui,3:submit,nohide
;- color c510B01 =brown
filex:="CHOCD TRIAL___.otf"
ifexist,%filex%
  {
  Font1 := New CustomFont(filex)
  Gui,3: Font, s100 cGray, Chocolate Dealer    ;- change text-COLOR / SIZE / FONT
  Guicontrol,3: ,ED2,Chocolate Dealer
  }
else
  {
  ;Gui,3: font, cGray,                          ;- change text-COLOR Edit ED1
  Gui,3: font, s14 cGray, Lucida Console        ;- change text-COLOR / SIZE / FONT
  Guicontrol,3: ,ED2,Lucida Console
  }

Guicontrol,3: Font, ED1
Guicontrol,3: ,ED1,%ed1%                        ;- maybe CHANGE TEXT
return



;---- example-2 --------------------------------------------------------
a2:
Gui,3:submit,nohide
fdx=C:\WINDOWS\Fonts
;fdx=%a_scriptdir%
ifexist,%fdx%
{
Loop,%fdx%\*.*
{
if (BreakLoop = 1)
   break
If A_LoopFileExt in %Extensions%
  {
  f1=%A_loopfilefullpath%
  if f1=
     continue
  pr=otfinfo -a "%f1%"
  pr:=pr
  F2:=StdoutToVar_CreateProcess(pr)
  stringtrimright,f2,f2,2
  font1:=""
  fontx=res:%f1%
  fontx:=fontx
  ;msgbox,F2=%f2%`nFONTX=%fontx%
  ;font1 := New CustomFont("res:moonhouse.ttf", "moonhouse", 50)
  font1 := New CustomFont(fontx,F2, 50)                ;- ( res:fullpath , otfinfo-name , size )
  font1.applyTo(hText)                                 ;- hwnd Edit ED1
  guicontrol,3: ,ED2,%f2%                              ;- show otfinfo-name
  fontx:=""
  F2:=""
  sleep,1600
  }
}
}
return
;================================================================


/*
;- sample-1
#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
*/


/*
;sample-2
#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, -
*/



;================= CustomFont.ahk tmplinshi =================================================
;- https://autohotkey.com/boards/viewtopic.php?f=6&t=813
/*
	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 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) {
		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
	}
}
;=========================================================================



;--------- http://ahkscript.org/boards/viewtopic.php?f=6&t=791 ---
;--        from user cyruz
;-- Last edited by cyruz on Sun Mar 09, 2014 12:51 pm, edited 6 times in total
StdoutToVar_CreateProcess(sCmd, sDir:="", ByRef nExitCode:=0) {
    DllCall( "CreatePipe",           PtrP,hStdOutRd, PtrP,hStdOutWr, Ptr,0, UInt,0 )
    DllCall( "SetHandleInformation", Ptr,hStdOutWr,  UInt,1,         UInt,1        )

            VarSetCapacity( pi, (A_PtrSize == 4) ? 16 : 24,  0 )
    siSz := VarSetCapacity( si, (A_PtrSize == 4) ? 68 : 104, 0 )
    NumPut( siSz,      si,  0,                          "UInt" )
    NumPut( 0x100,     si,  (A_PtrSize == 4) ? 44 : 60, "UInt" )
    NumPut( hStdInRd,  si,  (A_PtrSize == 4) ? 56 : 80, "Ptr"  )
    NumPut( hStdOutWr, si,  (A_PtrSize == 4) ? 60 : 88, "Ptr"  )
    NumPut( hStdOutWr, si,  (A_PtrSize == 4) ? 64 : 96, "Ptr"  )

    If ( !DllCall( "CreateProcess", Ptr,0, Ptr,&sCmd, Ptr,0, Ptr,0, Int,True, UInt,0x08000000
                                  , Ptr,0, Ptr,sDir?&sDir:0, Ptr,&si, Ptr,&pi ) )
        Return ""
      , DllCall( "CloseHandle", Ptr,hStdOutWr )
      , DllCall( "CloseHandle", Ptr,hStdOutRd )

    DllCall( "CloseHandle", Ptr,hStdOutWr ) ; The write pipe must be closed before reading the stdout.
    VarSetCapacity(sTemp, 4095)
    While ( DllCall( "ReadFile", Ptr,hStdOutRd, Ptr,&sTemp, UInt,4095, PtrP,nSize, Ptr,0 ) )
        sOutput .= StrGet(&sTemp, nSize, A_FileEncoding)

    DllCall( "GetExitCodeProcess", Ptr,NumGet(pi,0), Ptr,&nExitCode ), nExitCode := NumGet(nExitCode)
    DllCall( "CloseHandle",        Ptr,NumGet(pi,0)                 )
    DllCall( "CloseHandle",        Ptr,NumGet(pi,A_PtrSize)         )
    DllCall( "CloseHandle",        Ptr,hStdOutRd                    )
    Return sOutput
}
;------------------------------------------------------------------------------------------------------
;====================== end script ====================================================================


used installed C:\WINDOWS\FONTS ( in this case without function ClassCustomFont )
downloads otfinfo.exe to get FONT-name

Code: Select all

;- Modified    = 20160312
;- Created     = 20160312
;- Description = a testscript to show/select/change  [ fonts , font-size, text-color ] with ahk
;                this script uses C:\WINDOWS\FONTS
;- Needs       = downloads otfinfo.exe to get FONT-name
---------------------------------------------------------

#NoEnv                        ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input                ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetBatchLines, -1

ifnotexist,otfinfo.exe
    urldownloadtofile,http://math.sut.ac.th/lab/software/texlive/bin/win32/otfinfo.exe,otfinfo.exe


extensions:="otf,ttf"

   Menu,S2,add,AHK_Version       ,MH2
   Menu,S3,add,WindowsFonts      ,MH3
   ;----------------------------
   menu,myMenuBar,Add,AHK_Version   ,:S2
   menu,myMenuBar,Add,WindowsFonts  ,:S3
   ;----------------------------
   gui,3:menu,MyMenuBar
   ;----------------------------

Gui,3: default
Gui,3:Color, Black
Gui,3:Color, ControlColor, Black
Gui,3:Font,,FixedSys

Gui,3:add,edit   ,x10  y10 h180 w470 vED1 hwndhText cYellow,Line1_Test`nLine2_Test`nLine3_Test
Gui,3:add,edit   ,x10  y200 h25 w470 readonly cRed    vED2,Fixedsys
Gui,3:add,edit   ,x10  y230 h25 w470 readonly cYellow vED3,
Gui,3:add,button ,x10  y270 h25 w80 gStart1,Start
Gui,3:add,button ,x100 y270 h25 w80 gStop1 ,Stop
Gui,3:show ,x10 y10 h305 w500,WINDOWS-FONTS_TEST
Guicontrol,3:focus,ed2
return
;---------------------------------------------------------------
3Guiclose:
exitapp
;---------------------------------------------------------------

start1:
breakloop=0
sleep,2000     ;- see FONT  from GUI
gosub,a1       ;- see FONT  example-1
sleep,3000
gosub,a2       ;- see FONTs from C:\WINDOWS\FONTS
return

stop1:
breakloop=1
return


mh2:
if A_thisMenuItem=AHK_version
  msgbox, 262208,AHK_Version ,Ahk-Version =%A_AHKVERSION%
Guicontrol,3:focus,ed2
return

mh3:
ifexist,C:\windows\fonts
    run,C:\windows\fonts
Guicontrol,3:focus,ed2
return


;------ example-1   ------------------------------------------------------
a1:
Gui,3:submit,nohide
Gui,3: font, s14 cGray, Lucida Console        ;- change text-COLOR / SIZE / FONT
Guicontrol,3: ,ED2,Lucida Console
Guicontrol,3: Font, ED1
Guicontrol,3: ,ED1,%ed1%                        ;- maybe CHANGE TEXT
return



;---- example-2 --------------------------------------------------------
a2:
Gui,3:submit,nohide
fdx=C:\WINDOWS\Fonts
ifexist,%fdx%
{
Loop,%fdx%\*.*
 {
if (BreakLoop = 1)
   break
If A_LoopFileExt in %Extensions%
  {
  f1=%A_loopfilefullpath%
  if f1=
     continue
  SplitPath,f1, name, dir, ext, name_no_ext, drive
  pr=otfinfo -a "%f1%"
  pr:=pr
  F2:=StdoutToVar_CreateProcess(pr)
  stringtrimright,f2,f2,2
  Gui,3: font, s40 cTeal,%f2%        ;- change text-COLOR / SIZE / FONT
  Guicontrol,3: ,ED2,%f2%
  Guicontrol,3: ,ED3,%name%
  Guicontrol,3: Font, ED1
  Guicontrol,3: ,ED1,%ed1%
  F2:=""
  pr:=""
  sleep,1500
  }
 }
}
return
;================================================================


;--------- http://ahkscript.org/boards/viewtopic.php?f=6&t=791 ---
;--        from user cyruz
;-- Last edited by cyruz on Sun Mar 09, 2014 12:51 pm, edited 6 times in total
StdoutToVar_CreateProcess(sCmd, sDir:="", ByRef nExitCode:=0) {
    DllCall( "CreatePipe",           PtrP,hStdOutRd, PtrP,hStdOutWr, Ptr,0, UInt,0 )
    DllCall( "SetHandleInformation", Ptr,hStdOutWr,  UInt,1,         UInt,1        )

            VarSetCapacity( pi, (A_PtrSize == 4) ? 16 : 24,  0 )
    siSz := VarSetCapacity( si, (A_PtrSize == 4) ? 68 : 104, 0 )
    NumPut( siSz,      si,  0,                          "UInt" )
    NumPut( 0x100,     si,  (A_PtrSize == 4) ? 44 : 60, "UInt" )
    NumPut( hStdInRd,  si,  (A_PtrSize == 4) ? 56 : 80, "Ptr"  )
    NumPut( hStdOutWr, si,  (A_PtrSize == 4) ? 60 : 88, "Ptr"  )
    NumPut( hStdOutWr, si,  (A_PtrSize == 4) ? 64 : 96, "Ptr"  )

    If ( !DllCall( "CreateProcess", Ptr,0, Ptr,&sCmd, Ptr,0, Ptr,0, Int,True, UInt,0x08000000
                                  , Ptr,0, Ptr,sDir?&sDir:0, Ptr,&si, Ptr,&pi ) )
        Return ""
      , DllCall( "CloseHandle", Ptr,hStdOutWr )
      , DllCall( "CloseHandle", Ptr,hStdOutRd )

    DllCall( "CloseHandle", Ptr,hStdOutWr ) ; The write pipe must be closed before reading the stdout.
    VarSetCapacity(sTemp, 4095)
    While ( DllCall( "ReadFile", Ptr,hStdOutRd, Ptr,&sTemp, UInt,4095, PtrP,nSize, Ptr,0 ) )
        sOutput .= StrGet(&sTemp, nSize, A_FileEncoding)

    DllCall( "GetExitCodeProcess", Ptr,NumGet(pi,0), Ptr,&nExitCode ), nExitCode := NumGet(nExitCode)
    DllCall( "CloseHandle",        Ptr,NumGet(pi,0)                 )
    DllCall( "CloseHandle",        Ptr,NumGet(pi,A_PtrSize)         )
    DllCall( "CloseHandle",        Ptr,hStdOutRd                    )
    Return sOutput
}
;------------------------------------------------------------------------------------------------------
;====================== end script ====================================================================
User avatar
KRG-23
Posts: 19
Joined: 07 Jun 2016, 05:45
Location: France

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

03 Aug 2018, 03:57

Hello and thank you very much for this script.

I'm using it in one of my Gui scripts and I find it very convenient not to have the users install the interface font.
However I am facing a problem with it. After sometime it is not working, the default system font displays. It worked for some time and then the loaded font disappears.
And I can't figure out why...

Have you already heard of this kind of problem? I'm using CustomFont v2.00 (2016-2-24) downloaded from Github.

Thank you for your help!
tmplinshi
Posts: 1436
Joined: 01 Oct 2013, 14:57

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

03 Aug 2018, 04:17

@KRG-23 The only reason I can think of is maybe you cleared the variable returned by CustomFont.
User avatar
KRG-23
Posts: 19
Joined: 07 Jun 2016, 05:45
Location: France

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

03 Aug 2018, 04:29

tmplinshi wrote:@KRG-23 The only reason I can think of is maybe you cleared the variable returned by CustomFont.
Thank you for your answer.

I'll make tests with some MsgBox to check the content of the variable. I'll keep you posted :)
User avatar
KRG-23
Posts: 19
Joined: 07 Jun 2016, 05:45
Location: France

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

14 Aug 2018, 06:31

Ok, I've made some tests and CustomFont() is working with a small test script but not with the complete one... I'm working on it.
User avatar
KRG-23
Posts: 19
Joined: 07 Jun 2016, 05:45
Location: France

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

24 Aug 2018, 02:19

I've solved my problem.

In my test script, there was no SetWorkingDir whereas in my working script, it was set to a log subfolder. Therefore, the font could not be found...
tmplinshi
Posts: 1436
Joined: 01 Oct 2013, 14:57

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

24 Aug 2018, 11:30

KRG-23 wrote:I've solved my problem.

In my test script, there was no SetWorkingDir whereas in my working script, it was set to a log subfolder. Therefore, the font could not be found...
My bad. I added file exists checking:

Code: Select all

...
	AddFromFile(FontFile) {
		if !FileExist(FontFile) {
			throw "Unable to find font file: " FontFile
		}
...
burque505
Posts: 990
Joined: 22 Jan 2017, 19:37

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

24 Aug 2018, 13:02

@tmplinshi, that's very nice indeed, thank you.
Regards,
burque505
User avatar
KRG-23
Posts: 19
Joined: 07 Jun 2016, 05:45
Location: France

Re: [Class] CustomFont - Load font from file or resource, without needed install to system.

07 Sep 2018, 02:36

tmplinshi wrote:
KRG-23 wrote:I've solved my problem.

In my test script, there was no SetWorkingDir whereas in my working script, it was set to a log subfolder. Therefore, the font could not be found...
My bad. I added file exists checking:

Code: Select all

...
	AddFromFile(FontFile) {
		if !FileExist(FontFile) {
			throw "Unable to find font file: " FontFile
		}
...
Oh, this is nice! Thank you tmplinshi :thumbup:

Return to “Scripts and Functions”

Who is online

Users browsing this forum: hasantr and 62 guests