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

Post your working scripts, libraries and tools
m3user
Posts: 150
Joined: 17 Jan 2014, 18:11

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

18 Oct 2019, 04:36

Thanks tmplinshi, looks promising. Unfortunately I only get a rectangle image on the menu, regardless of the icon no.
tmplinshi
Posts: 1543
Joined: 01 Oct 2013, 14:57

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

18 Oct 2019, 04:58

Works fine here with latest AHK U32/U64. I've packed all necessary files, you can download at: https://drive.google.com/open?id=1a1_RYPLDjE88d90ppc-ealUX3VhGJFxj
m3user
Posts: 150
Joined: 17 Jan 2014, 18:11

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

18 Oct 2019, 08:12

Thanks, very kind. I tried on Win10 and it works great. :D
It still doesn't work on my Win7 machine, I'll check what might be the issue.
tmplinshi
Posts: 1543
Joined: 01 Oct 2013, 14:57

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

23 Oct 2019, 07:28

m3user wrote:
18 Oct 2019, 08:12
Thanks, very kind. I tried on Win10 and it works great. :D
It still doesn't work on my Win7 machine, I'll check what might be the issue.
Guess what, It works on Win10, it's because the font "Segoe MDL2 Assets" is a built-in font on Win10 :shock:
Then I tried another font generated by http://fontello.com/, it didn't work.

Here is another method without using Class_CustomFont.ahk.

Code: Select all

SetWorkingDir %A_ScriptDir%

#Include <Gdip_All>
#Include <Subtitle> ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=36384

pToken := Gdip_Startup()

hFamily := Gdip_CreateFontFamilyFromFile("fontello.ttf")
pBitmap := Subtitle.RenderToBitmap(Chr(0xe800), {color:"Transparent"}, {s:24, hFamily:hFamily, color:"black"})
hIcon := Gdip_CreateHICONFromBitmap(pBitmap)

Menu, TestMenu, Add, Smile, MenuOnClick
Menu, TestMenu, Icon, Smile, hIcon:%hIcon%,, 24
Menu, TestMenu, Show
Menu, TestMenu, Delete

Gdip_Shutdown(pToken)
ExitApp

Gdip_CreateFontFamilyFromFile(FontFile, FontName := "") {
	if !FileExist(FontFile)
		throw "Font file not exist: " . FontFile

	DllCall("gdiplus\GdipNewPrivateFontCollection", "ptr*", hCollection)

	; FileRead, fontData, *c %FontFile%
	; DllCall("gdiplus\GdipPrivateAddMemoryFont", "ptr", hCollection, "ptr", &fontData, "uint", VarSetCapacity(fontData))
	DllCall("gdiplus\GdipPrivateAddFontFile", "ptr", hCollection, "str", FontFile)

	if (FontName = "") {
		VarSetCapacity(pFontFamily, 10, 0)
		DllCall("gdiplus\GdipGetFontCollectionFamilyList", "ptr", hCollection, "int", 1, "ptr", &pFontFamily, "int*", found)

		VarSetCapacity(FontName, 100)
		DllCall("gdiplus\GdipGetFamilyName", "ptr", NumGet(pFontFamily, 0, "ptr"), "str", FontName, "ushort", 1033)
	}

	DllCall("gdiplus\GdipCreateFontFamilyFromName", "str", FontName, "ptr", hcollection, "uint*", hFamily)
	return hFamily
}

MenuOnClick(ItemName) {
	MsgBox, % ItemName
}
Note that I added 3 lines to Subtitle.ahk:

Code: Select all

      ; Create Font.
      if style2.hFamily
         hFamily := style2.hFamily
      else
         hFamily := (___ := Gdip_FontFamilyCreate(f)) ? ___ : Gdip_FontFamilyCreate("Arial") ; Default font is Arial.
Attachments
Gidplus load ttf font file example.zip
(37.37 KiB) Downloaded 15 times
m3user
Posts: 150
Joined: 17 Jan 2014, 18:11

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

23 Oct 2019, 09:26

It works, thanks!

But not sure Class_CustomFont is a problem. First part of this works in Win7:

Code: Select all

SetWorkingDir %A_ScriptDir%

#Include <Gdip_All>
#Include <Subtitle> ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=36384
#Include <Class_CustomFont>

font1 := New CustomFont("SegMDL2.ttf")
gui, font, s12, Segoe MDL2 Assets
gui, add, text,, % chr(0xE713)
gui, show

pToken := Gdip_Startup()
pBitmap := Subtitle.RenderToBitmap(Chr(0xE713), "color:Transparent", "s:24 f:(Segoe MDL2 Assets) color:black")
hIcon := Gdip_CreateHICONFromBitmap(pBitmap)

Menu, TestMenu, Add, Settings, MenuOnClick
Menu, TestMenu, Icon, Settings, hIcon:%hIcon%,, 24
Menu, TestMenu, Show
Menu, TestMenu, Delete
ExitApp

MenuOnClick(ItemName) {
	MsgBox, % ItemName
}
robodesign
Posts: 551
Joined: 30 Sep 2017, 03:59
Facebook: marius.sucan
GitHub: mariussucan
Location: Romania
Contact:

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

27 Oct 2019, 11:46

I will include this into the GDI+ library edition I created. It works great.

Please let me know if you wish not to have it included in my GDI+ lib.

Best regards, Marius.

tmplinshi wrote:
23 Oct 2019, 07:28
m3user wrote:
18 Oct 2019, 08:12
Thanks, very kind. I tried on Win10 and it works great. :D
It still doesn't work on my Win7 machine, I'll check what might be the issue.
Guess what, It works on Win10, it's because the font "Segoe MDL2 Assets" is a built-in font on Win10 :shock:
Then I tried another font generated by http://fontello.com/, it didn't work.

Here is another method without using Class_CustomFont.ahk.

Code: Select all

SetWorkingDir %A_ScriptDir%

#Include <Gdip_All>
#Include <Subtitle> ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=36384

pToken := Gdip_Startup()

hFamily := Gdip_CreateFontFamilyFromFile("fontello.ttf")
pBitmap := Subtitle.RenderToBitmap(Chr(0xe800), {color:"Transparent"}, {s:24, hFamily:hFamily, color:"black"})
hIcon := Gdip_CreateHICONFromBitmap(pBitmap)

Menu, TestMenu, Add, Smile, MenuOnClick
Menu, TestMenu, Icon, Smile, hIcon:%hIcon%,, 24
Menu, TestMenu, Show
Menu, TestMenu, Delete

Gdip_Shutdown(pToken)
ExitApp

Gdip_CreateFontFamilyFromFile(FontFile, FontName := "") {
	if !FileExist(FontFile)
		throw "Font file not exist: " . FontFile

	DllCall("gdiplus\GdipNewPrivateFontCollection", "ptr*", hCollection)

	; FileRead, fontData, *c %FontFile%
	; DllCall("gdiplus\GdipPrivateAddMemoryFont", "ptr", hCollection, "ptr", &fontData, "uint", VarSetCapacity(fontData))
	DllCall("gdiplus\GdipPrivateAddFontFile", "ptr", hCollection, "str", FontFile)

	if (FontName = "") {
		VarSetCapacity(pFontFamily, 10, 0)
		DllCall("gdiplus\GdipGetFontCollectionFamilyList", "ptr", hCollection, "int", 1, "ptr", &pFontFamily, "int*", found)

		VarSetCapacity(FontName, 100)
		DllCall("gdiplus\GdipGetFamilyName", "ptr", NumGet(pFontFamily, 0, "ptr"), "str", FontName, "ushort", 1033)
	}

	DllCall("gdiplus\GdipCreateFontFamilyFromName", "str", FontName, "ptr", hcollection, "uint*", hFamily)
	return hFamily
}

MenuOnClick(ItemName) {
	MsgBox, % ItemName
}
Note that I added 3 lines to Subtitle.ahk:

Code: Select all

      ; Create Font.
      if style2.hFamily
         hFamily := style2.hFamily
      else
         hFamily := (___ := Gdip_FontFamilyCreate(f)) ? ___ : Gdip_FontFamilyCreate("Arial") ; Default font is Arial.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
My home page.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bing [Bot], Pintu and 47 guests