Can gdi+ load custom fonts from .ttf files? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Can gdi+ load custom fonts from .ttf files?

05 Feb 2018, 22:35

Edited subject name from Can gdi+ render on desktop behind icons and can i somehow load custom fonts from .ttf files? for separation purposes.
Pretty much the topics subject. I could not find a function for tics GDI lib to use custom fonts from files, which is a shame.
I would like to render some text or image on desktop but behind icons. If possible of course.
GDI seems much more powerful than standard GUI so perhaps its possible with it? Or any other way to achieve this?

While im on the GDI subject, how come GDI lib is not added to AHK natively?
Also, could for instance multiple functions from GDI lib needed to show text be replaced with something like:

Code: Select all

GDI, ADD, Text, Options, Some Text
GDI, Show, Options
and AHK to interpret / replace them as:

Code: Select all

	; replace with GDI, ADD and Options (W H)
	hbm := CreateDIBSection(Width, Height)
	hdc := CreateCompatibleDC()
	obm := SelectObject(hdc, hbm)
	G := Gdip_GraphicsFromHDC(hdc)

	; replace with Some Text and Options (W, H, Font, FontSize, FontColor etc...)
	Gdip_TextToGraphics(G, Some Text, Options, Font, Width, Height)
	
	; replace with GDI, Show and Options (Gui Number on which to show to if specified (default 1), X, Y, W, H)
	UpdateLayeredWindow(hwnd, hdc, PosX, PosY, Width, Height)
	
	SelectObject(hdc, obm)
	DeleteObject(hbm)
	DeleteDC(hdc)
	Gdip_DeleteGraphics(G)
Could this be somehow added to AHK? That would be amazing. :)
Otherwise i will just need to find a way to condense this, because there is just a lot of lines of functions for something that is rather mandatory.
At least for a simple text or image to show.
Last edited by theimmersion on 17 Mar 2018, 08:48, edited 1 time in total.
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: Can gdi+ render on desktop behind icons and can i somehow load custom fonts from .ttf files?  Topic is solved

06 Feb 2018, 00:54

This is an adaption from the default using Private Fonts.You have to have the "real" name of the font and its filename.

Code: Select all

Gdip_PrivateFontToGraphics(pGraphics, Text, Options, Fontfile,FontName, Width="", Height="", Measure=0)
{
	IWidth := Width, IHeight:= Height
	
	RegExMatch(Options, "i)X([\-\d\.]+)(p*)", xpos)
	RegExMatch(Options, "i)Y([\-\d\.]+)(p*)", ypos)
	RegExMatch(Options, "i)W([\-\d\.]+)(p*)", Width)
	RegExMatch(Options, "i)H([\-\d\.]+)(p*)", Height)
	RegExMatch(Options, "i)C(?!(entre|Enter))([a-f\d]+)", Colour)
	RegExMatch(Options, "i)Top|Up|Bottom|Down|vCentre|vCenter", vPos)
	RegExMatch(Options, "i)NoWrap", NoWrap)
	RegExMatch(Options, "i)R(\d)", Rendering)
	RegExMatch(Options, "i)S(\d+)(p*)", Size)
	
	If !Gdip_DeleteBrush(Gdip_CloneBrush(Colour2))
		PassBrush := 1, pBrush := Colour2
	
	If !(IWidth && IHeight) && (xpos2 || ypos2 || Width2 || Height2 || Size2)
		Return -1
	
	Style := 0, Styles := "Regular|Bold|italic|Bolditalic|underline|strikeout"
	Loop, Parse, Styles, |
	{
		If RegExMatch(Options, "\b" A_loopField)
			Style |= (A_LoopField != "strikeOut") ? (A_Index-1) : 8
	}
	
	Align := 0, Alignments := "Near|Left|Centre|Center|Far|Right"
	Loop, Parse, Alignments, |
	{
		If RegExMatch(Options, "\b" A_loopField)
			Align |= A_Index//2.1      ; 0|0|1|1|2|2
	}
	
	xpos := (xpos1 != "") ? xpos2 ? IWidth*(xpos1/100) : xpos1 : 0
	ypos := (ypos1 != "") ? ypos2 ? IHeight*(ypos1/100) : ypos1 : 0
	Width := Width1 ? Width2 ? IWidth*(Width1/100) : Width1 : IWidth
	Height := Height1 ? Height2 ? IHeight*(Height1/100) : Height1 : IHeight
	If !PassBrush
		Colour := "0x" (Colour2 ? Colour2 : "ff000000")
	Rendering := ((Rendering1 >= 0) && (Rendering1 <= 5)) ? Rendering1 : 4
	Size := (Size1 > 0) ? Size2 ? IHeight*(Size1/100) : Size1 : 12
	
	
	If !A_IsUnicode
	{
		nSize:=DllCall("kernel32\MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &Fontfile, "int", -1, "Uint", 0, "int", 0)
		VarSetCapacity(wFontfile, nSize * 2 + 1)
		DllCall("kernel32\MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &Fontfile, "int", -1, "Uint", &wFontfile, "int", nSize + 1)
		
		
		
		nSize:=DllCall("kernel32\MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &FontName, "int", -1, "Uint", 0, "int", 0)
		VarSetCapacity(wFontName, nSize * 2 + 1)
		DllCall("kernel32\MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &FontName, "int", -1, "Uint", &wFontName, "int", nSize + 1)
		
		
		DllCall("gdiplus\GdipNewPrivateFontCollection", "uint*", hcollection)
		DllCall("gdiplus\GdipPrivateAddFontFile", "uint", hcollection, "uint", &wFontfile)
		DllCall("gdiplus\GdipCreateFontFamilyFromName", "uint", &wFontName, "uint", hcollection, "uint*", hFamily)
		
		
	}
	Else
	{
		DllCall("gdiplus\GdipNewPrivateFontCollection", "uint*", hcollection)
		DllCall("gdiplus\GdipPrivateAddFontFile", "uint", hcollection, "uint", &FontFile)
		DllCall("gdiplus\GdipCreateFontFamilyFromName", "uint", &FontName, "uint", hcollection, "uint*", hFamily)
		
	}
	
	
	
	
	hFont := Gdip_FontCreate(hFamily, Size, Style)
	FormatStyle := NoWrap ? 0x4000 | 0x1000 : 0x4000
	hFormat := Gdip_StringFormatCreate(FormatStyle)
	pBrush := PassBrush ? pBrush : Gdip_BrushCreateSolid(Colour)
	If !(hFamily && hFont && hFormat && pBrush && pGraphics)
		Return !pGraphics ? -2 : !hFamily ? -3 : !hFont ? -4 : !hFormat ? -5 : !pBrush ? -6 : 0
	
	CreateRectF(RC, xpos, ypos, Width, Height)
	Gdip_SetStringFormatAlign(hFormat, Align)
	Gdip_SetTextRenderingHint(pGraphics, Rendering)
	ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
	
	If vPos
	{
		StringSplit, ReturnRC, ReturnRC, |
		
		If (vPos = "vCentre") || (vPos = "vCenter")
			ypos += (Height-ReturnRC4)//2
		Else If (vPos = "Top") || (vPos = "Up")
			ypos := 0
		Else If (vPos = "Bottom") || (vPos = "Down")
			ypos := Height-ReturnRC4
		
		CreateRectF(RC, xpos, ypos, Width, ReturnRC4)
		ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
	}
	
	If !Measure
		E := Gdip_DrawString(pGraphics, Text, hFont, hFormat, pBrush, RC)
	
	If !PassBrush
		Gdip_DeleteBrush(pBrush)
	Gdip_DeleteStringFormat(hFormat)
	Gdip_DeleteFont(hFont)
	Gdip_DeleteFontFamily(hFamily)
	DllCall("gdiplus\GdipDeletePrivateFontCollection", "uint*", hcollection)
	Return E ? E : ReturnRC
}
soundcloud.com/user-32706894
BoBo
Posts: 2620
Joined: 13 May 2014, 17:15

Re: Can gdi+ render on desktop behind icons and can i somehow load custom fonts from .ttf files?

06 Feb 2018, 05:26

noname wrote:This is an adaption from the default using Private Fonts.You have to have the "real" name of the font and its filename.
Spoiler
Would you mind to provide a working sample function call for that function?? I've tried several things but wasn't able to "reverse engineer" the correct syntax for several parameters (eg pGraphics/options/../fontname/../measure) from the code. And, Gdip.ahk must be #Include'd as well, right?? Thx :)
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: Can gdi+ render on desktop behind icons and can i somehow load custom fonts from .ttf files?

06 Feb 2018, 06:20

Hi BoBo ,

You can take a look at the mechanical clock / timer , it extracts all code to run it (including font and gdip lib).I only test on 32bit ahk so maybe it does not work on 64bit ahk .

https://autohotkey.com/boards/viewtopic.php?t=21300

Greetings,
soundcloud.com/user-32706894
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Can gdi+ render on desktop behind icons and can i somehow load custom fonts from .ttf files?

11 Feb 2018, 09:11

I swear this "subscribe to topic" button is messing with me. -.- we need auto sub to our own posts by default and unsub by need.
So, small question, do i replace the function within the gdip_all.ahk or to i append in my script?
Will try it later today and tell if it works. Im on 64 bits. :)
Thanks!
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Can gdi+ render on desktop behind icons and can i somehow load custom fonts from .ttf files?

15 Feb 2018, 00:36

Sorry for late reply!
Well, it does not seem to work. Nothing is showing up. Can someone confirm if it works or not on 64 bit systems or maybe i goofed somewhere?
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Can gdi+ render on desktop behind icons and can i somehow load custom fonts from .ttf files?

15 Feb 2018, 00:43

Nevermind! it works. I goofed. Grrrr. SORRY!
Thanks noname!

Return to “Ask For Help”

Who is online

Users browsing this forum: Babazule, Bing [Bot], boiler, HarryClash, JackMa, prakashG and 127 guests