Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

GDI+ standard library 1.45 by tic


  • Please log in to reply
1385 replies to this topic
dumpster.monkey
  • Members
  • 12 posts
  • Last active: Sep 25 2011 12:59 PM
  • Joined: 30 Jul 2011
I've seen it mentioned that gdip cannot use temporary (ie non installed) fonts - is this true?

If not does anyone have a working example?

thanks

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008

I've seen it mentioned that gdip cannot use temporary (ie non installed) fonts - is this true?

You can use a PrivateFontFamily (from what I've heard). Here's an example in AutoIT -- you have to translate it. That mostly consists of rearranging underscores in the Gdi+ calls and stripping the $ for variable names.
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

GollyJer
  • Members
  • 24 posts
  • Last active: Dec 21 2012 06:35 PM
  • Joined: 15 Oct 2007

But, now that I have it correct, Gdip_ImageSearch only returns 0.

Heh....0 means there was a match :wink:

LOL! Well then... it seems to be working pretty well! :D
And after a little searching now I understand how 0, as an affirmative return value, makes sense.

The rest of my feedback can be foundat this dedicated GDI+ ImageSearch thread.

dumpster.monkey
  • Members
  • 12 posts
  • Last active: Sep 25 2011 12:59 PM
  • Joined: 30 Jul 2011

I've seen it mentioned that gdip cannot use temporary (ie non installed) fonts - is this true?

You can use a PrivateFontFamily (from what I've heard). Here's an example in AutoIT -- you have to translate it. That mostly consists of rearranging underscores in the Gdi+ calls and stripping the $ for variable names.


Thanks very much :D

dumpster.monkey
  • Members
  • 12 posts
  • Last active: Sep 25 2011 12:59 PM
  • Joined: 30 Jul 2011

I've seen it mentioned that gdip cannot use temporary (ie non installed) fonts - is this true?

You can use a PrivateFontFamily (from what I've heard). Here's an example in AutoIT -- you have to translate it. That mostly consists of rearranging underscores in the Gdi+ calls and stripping the $ for variable names.


I'm a little stuck...

FontFile := "LED BOARD REVERSED.ttf"
FontName := "LED BOARD REVERSED"

[color=green];$hCollection = DllCall($ghGDIPDll, 'int', 'GdipNewPrivateFontCollection', 'ptr*', 0)[/color]
hCollection := DllCall("gdiplus\GdipNewPrivateFontCollection", "Ptr *", 0)
If (ErrorLevel) 
   MsgBox, GdipNewPrivateFontCollection ErrorLevel: %ErrorLevel%`n 

[color=green];DllCall($ghGDIPDll, 'int', 'GdipPrivateAddFontFile', 'ptr', $hCollection, 'wstr', @ScriptDir & '\FreeMonoBold.ttf')[/color]
DllCall("gdiplus\GdipPrivateAddFontFile", "Ptr", hCollection, "WStr", FontFile)
If (ErrorLevel) 
   MsgBox, GdipPrivateAddFontFile ErrorLevel: %ErrorLevel%`n 

[color=green];$hFamily = DllCall($ghGDIPDll, 'int', 'GdipCreateFontFamilyFromName', 'wstr', 'FreeMono', 'ptr', $hCollection, 'ptr*', 0)[/color]
[color=green];$hFamily = $hFamily[3][/color] [color=red]<---NOT SURE WHAT TO DO WITH THIS[/color]
hFamily := DllCall("gdiplus\GdipCreateFontFamilyFromName", "WStr", FontName, "Ptr", hCollection, "Ptr *", 0)
If (ErrorLevel) 
   MsgBox, GdipCreateFontFamilyFromName ErrorLevel: %ErrorLevel%`n 

[color=green];$hFont = _GDIPlus_FontCreate($hFamily, 36, 1)[/color] 
hFont := Gdip_FontCreate(hFamily, 36, 1)
If (ErrorLevel) 
   MsgBox, Gdip_FontCreate ErrorLevel: %ErrorLevel%`n  [color=red]<--- GETTING ERROR: 0xc0000005[/color]

As you can see, the original .au3 code is commented out, with my attempt to convert to ahk/gdip alongside. Of course start of script has standard ahk and gdip stuff, and I'm using AHK_L ansi. There's just a few issues shown in red. (OK, I have no idea really!! :oops: )

Am I getting close?

closed
  • Members
  • 509 posts
  • Last active: Jan 14 2012 06:14 PM
  • Joined: 07 Feb 2008
Image Uploaded with Sumon's zizorz <!-- m -->http://www.autohotke...topic70759.html<!-- m -->

Posted Image

@dumpster.monkey

Spent some time making a new function based on the Gdip_TextToGraphics in Tic's library:

Gdip_TextToGraphics_private(pGraphics, Text, Options, Fontfile ,Font, Width="", Height="", Measure=0)

It is important to use the correct fontname as font!!

Not tested very much but here is an example with a font from Skan (code example <!-- m -->http://www.autohotke...m/viewtopic.php<!-- m -->? p=209409#209409 ) :)



#SingleInstance, Force
#NoEnv
SetBatchLines, -1

If !pToken := Gdip_Startup()
{
   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
   ExitApp
}

IfNotExist, DS-DIGIB.TTF
       URLDownloadToFile, http://autohotkey.net/~goyyah/samples/DS-DIGIB.TTF, DS-DIGIB.TTF
OnExit, Exit
Gui,  +E0x80000 +LastFound +OwnDialogs +Owner  -caption 
Gui,  Show, x0 y0 
gui +lastfound
hwnd:=winexist()

width =300
height =50

text=1234567

hbm := CreateDIBSection(width, height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)

pBrush := Gdip_BrushCreateSolid(0x80000000)
Gdip_SetSmoothingMode(G, 4)
Gdip_FillRoundedRectangle(G, pBrush, 0, 0, width, height,10)
Gdip_DeleteBrush(pBrush)



  Options = x10 y5 w280 cff00ff00  Center Bold s42
  rc:=Gdip_TextToGraphics_private(G, text, Options, "DS-DIGIB.TTF","DS-Digital")
  
UpdateLayeredWindow(hwnd, hdc, 0, 0, width, height)

OnMessage(0x201, "WM_LBUTTONDOWN")
Return

WM_LBUTTONDOWN()
{
   PostMessage, 0xA1, 2
}
return

!esc::exitapp


Gdip_TextToGraphics_private(pGraphics, Text, Options, Fontfile ,Font, 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
	
	DllCall("gdiplus\GdipNewPrivateFontCollection", "uint*", hcollection)

  if !A_IsUnicode
		{
		nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &FontFile, "int", -1, "uint", 0, "int", 0)
		
		VarSetCapacity(wFontFile, nSize*2)
			DllCall("kernel32\MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &FontFile, "int", -1, "uint", &wFontFile, "int", nSize)
			DllCall("gdiplus\GdipPrivateAddFontFile", "uint", hcollection, "uint", &wFontFile)
		}
		else
			DllCall("gdiplus\GdipPrivateAddFontFile", "uint", hcollection, "uint", &FontFile)

	
  	if !A_IsUnicode
	{
		nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &Font, "int", -1, "uint", 0, "int", 0)
		
		VarSetCapacity(wFont, nSize*2)
		DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &Font, "int", -1, "uint", &wFont, "int", nSize)
    error:=DllCall("gdiplus\GdipCreateFontFamilyFromName", "uint", &wFont, "uint", hcollection, "uint*", hFamily)	
	}
	else
    DllCall("gdiplus\GdipCreateFontFamilyFromName", "uint", &Font, "uint", hcollection, "uint*", hFamily)	

    DllCall("gdiplus\GdipCreateFont", "uint", hFamily, "float", Size, "int", Style, "int", 0, "uint*", hFont)
    
	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
}





GuiClose:
Exit:
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_DeleteBrush(pBrush)
Gdip_Shutdown(pToken)
ExitApp


  • Guests
  • Last active:
  • Joined: --
Hi, can you make an example, were you use "StretchBlt" ?

dumpster.monkey
  • Members
  • 12 posts
  • Last active: Sep 25 2011 12:59 PM
  • Joined: 30 Jul 2011
@Thanh00

Thanks, exactly what I needed to keep moving :)

tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007
Tiny update to 1.45 but it was annoying me. Anyone let me know if they have any problems after this

When using Gdip_BitmapFromScreen("hwnd:" hwnd)

to get the bitmap from a window, then it would on some systems return the screens bitmap. I had to change:

hhdc := GetDCEx(Screen, 1)

to

hhdc := GetDCEx(Screen, 3)   ; DCX_WINDOW | DCX_CACHE

as it sometimes wouldn't work otherwise. Let me know if this gives any other errors

and also added

Gdip_ImageRotateFlip(pBitmap, RotateFlipType=1)

for easier rotating an image at right angles

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
Please check if the file is there - I get a blank repository page instead of the Gdip.ahk.

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007

Please check if the file is there - I get a blank repository page instead of the Gdip.ahk.


Might have still been uploading, not sure, but it seems ok.
For an example of

Gdip_ImageRotateFlip(pBitmap, RotateFlipType=1)

see here:

<!-- m -->http://www.autohotke... ... 659#472659<!-- m -->

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
I'm trying the link at the top of post #1 - still the same: blank htm page instead of downloading Gdip.ahk. :?

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


  • Guests
  • Last active:
  • Joined: --

I'm trying the link at the top of post #1 - still the same: blank htm page instead of downloading Gdip.ahk. :?

I clicked on link and I get Gdip.ahk - everything is fine, no problems.
Thanks for update tic!

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
Clear your cache/use Chrome in incognito to download it :wink:

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
Indeed, there was a Gdip.ahk of size 0 bytes in cache, which produced this effect. Strangely, can't remember having experienced such inconvenience in a very long time. Thanks for the tip! ;)

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here