[4.61B3] Unsharp font when using AlphaBlend

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

[4.61B3] Unsharp font when using AlphaBlend

Post by Michael Vogel »

Get blurry output on some machines, when using DrawText and 2DDrawing_AlphaBlend. Below is a short example, showing correct font on gray boxes, but an unsharp bolder output when the text overlays the button by alpha blending. How to get the best of thw two worlds?

Code: Select all


Procedure SetGadgetIconText(Gadget,IconHandle,Text.s,Flags=#Null)

	Protected Font=GetGadgetFont(#PB_Default)
	Protected BW,BH
	Protected OX,OY
	Protected Prefix,PX,PY,PW




	#NoTransparency=$ff000000
	#FullTransparency=$00000000
	#HalfTransparency=$80000000

	BW=GadgetWidth(Gadget)
	BH=GadgetHeight(Gadget)

	CreateImage(Gadget,1,1)
	StartDrawing(ImageOutput(Gadget))
	DrawingFont(Font)

	Prefix=FindString(Text,"&",1)
	If Prefix
		Text=ReplaceString(Text,"&","")
	EndIf

	OX=30
	OY=(BH-TextHeight("Wg"))>>1

	StopDrawing()

	CreateImage(Gadget,BW,BH,32)
	StartDrawing(ImageOutput(Gadget))

	If Random(1)
		DrawingMode(#PB_2DDrawing_Default)
		Box(0,0,BW,BH,$e0e0e0)
		DrawingFont(Font)
		DrawText(OX,OY,Text,#noTransparency|#Black,$e0e0e0)

	Else
		DrawingMode(#PB_2DDrawing_AlphaChannel)
		Box(0,0,BW,BH,#FullTransparency)

		DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
		DrawingFont(Font)
		DrawText(OX,OY,Text,#noTransparency|#Black,$e0e0e0)

	EndIf

	StopDrawing()

	SetGadgetAttribute(Gadget,#PB_Button_Image,ImageID(Gadget))


EndProcedure

Procedure GadgetIconText(Gadget,X,Y,W,H,IconHandle,Text.s,Flags=0)

	ButtonImageGadget(Gadget,X,Y,W,H,0,Flags)
	SetGadgetIconText(Gadget,IconHandle,Text,Flags);	ReplaceString(Text,"&","")

EndProcedure

Enumeration
	#mainID
EndEnumeration

main=OpenWindow(#mainID,0,0,474,310,"!",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ResizeWindow(#mainID,#PB_Ignore,WindowY(#mainID)-ScreenY>>4,#PB_Ignore,#PB_Ignore)

For i=1 To 7
	GadgetIconText(i,20,i*30,150,25,0,"Buttontext Example")
Next i

Repeat
Until WaitWindowEvent()=#WM_CHAR
Last edited by Michael Vogel on Sat May 12, 2012 7:16 am, edited 1 time in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Unsharp font when using AlphaBlend

Post by STARGÅTE »

I do not see your problem:
Image

System?

NOTE:
If your system uses clear type fonts, then this will also only be used if the background be set, otherwise use normal AA (on transparent background)
Image
top: transparent, anti aliasing
bottom: background, clear type
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Unsharp font when using AlphaBlend

Post by Foz »

I do see it - it looks like it's a problem with XP themes then...
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Unsharp font when using AlphaBlend

Post by c4s »

Yes, the systems Cleartype settings are responsible for this issue. Loading fonts without being affected by this was already requested:
http://www.purebasic.fr/english/viewtop ... =3&t=31374

In the above thread is a workaround: You just have to load the font yourself and define the quality as you need it.
http://www.purebasic.fr/english/viewtop ... 65#p235065
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Unsharp font when using AlphaBlend

Post by Michael Vogel »

Thanks,
in the mean time I also found this interesting thread and a ffff (="fixed" feedback from freak) :)

I would like to have this fixed in general or a better control of font flags (#PB_Font_DefaultQuality, #PB_Font_Semibold,...) when using LoadFont and GetGadgetFont.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Unsharp font when using AlphaBlend

Post by Michael Vogel »

Will this bug be fixed in the final 4.60 release of PB?
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Unsharp font when using AlphaBlend

Post by Michael Vogel »

Michael Vogel wrote:Will this bug be fixed in the final 4.60 release of PB?
No. :(
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Unsharp font when using AlphaBlend

Post by Michael Vogel »

Interestingly, white text on dark background looks fine, but black text on light background has problems...

Image
Enumeration
#CatWindow
#CatImage
#CatIcon
EndEnumeration

LoadFont(0,"Calibri",-32,#PB_Font_HighQuality)

Procedure CreateBox(Image,w,h,Title.s,Text.s)

Protected n,c

#NoTransparency=$FF000000
#LightColorHigh=$FFFFFF
#LightColorMid=$FFF8F8
#LightColorLow=$F8E0E0

CreateImage(Image,w,h,32)
StartDrawing(ImageOutput(Image))

DrawingMode(#PB_2DDrawing_Gradient)
GradientColor(0,$202020)
GradientColor(1,$f0f0f0)
LinearGradient(0,0,w,h)
Box(0,0,w,h)

DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
DrawingFont(FontID(0))

DrawText(50,20,Text,$FFFFFFFF)
DrawText(50,80,text,$FF000000)

StopDrawing()

EndProcedure

CreateBox(#CatImage,480,160,"Title","This is a nice text – or not ?!")
OpenWindow(#CatWindow,0,200,480,160,"Test",#PB_Window_BorderLess|#PB_Window_ScreenCentered|#PB_Window_Invisible)
ImageGadget(0,0,0,480,160,ImageID(#CatImage))

HideWindow(0,0)

Repeat

Event=WaitWindowEvent()

If Event=#WM_LBUTTONDOWN
SendMessage_(WindowID(0),#WM_NCLBUTTONDOWN,#HTCAPTION,0)
EndIf

Until Event=#PB_Event_CloseWindow Or Event=#WM_CHAR
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Unsharp font when using AlphaBlend

Post by STARGÅTE »

invert the colors of the image, and the effect is also to be seen!

Its a visual effekt of your desktop and your gamma-settings

LoadFont() don't use clear type font!
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Unsharp font when using AlphaBlend

Post by Michael Vogel »

STARGÅTE wrote:invert the colors of the image, and the effect is also to be seen!
You're right :wink:
STARGÅTE wrote:Its a visual effekt of your desktop and your gamma-settings
No, no... no - this effect is only seen when using Alpha_Blend :?
STARGÅTE wrote:LoadFont() don't use clear type font!
Will this ever change?

Hm, also this does not look that fine (but already a little bit better)...

Code: Select all

Enumeration
	#CatWindow
	#CatImage
	#CatIcon
EndEnumeration

Global Font

Font=LoadFont(0,"Calibri",-32,#PB_Font_HighQuality)

LF.LOGFONT
LF\lfHeight=32
LF\lfWidth=0
LF\lfEscapement=0
LF\lfOrientation=0
LF\lfWeight=#FW_DONTCARE
LF\lfItalic=0
LF\lfUnderline=0
LF\lfStrikeOut=0
LF\lfCharSet=#DEFAULT_CHARSET
LF\lfOutPrecision=#OUT_DEFAULT_PRECIS
LF\lfClipPrecision=#CLIP_DEFAULT_PRECIS
LF\lfQuality=#CLEARTYPE_QUALITY
LF\lfPitchAndFamily=#DEFAULT_PITCH|#FF_DONTCARE
PokeS(@LF\lfFaceName[0], "Calibri")

Font=CreateFontIndirect_(@LF)

Procedure CreateBox(Image,w,h,Title.s,Text.s)

	Protected n,c

	#NoTransparency=$FF000000
	#LightColorHigh=$FFFFFFFF
	#LightColorMid=$FFFFF8F8
	#LightColorLow=$FFF8E0E0

	CreateImage(Image,w,h,32)
	StartDrawing(ImageOutput(Image))

	DrawingMode(#PB_2DDrawing_Gradient)
	GradientColor(0,$202020)
	GradientColor(1,$f0f0f0)
	LinearGradient(0,0,w,h)
	Box(0,0,w,h)

	DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
	DrawingFont(Font)
	
	DrawText(50,20,Text,$FFFFFFFF)
	DrawText(50,120,text,$FF000000)
	
	DrawingMode(#PB_2DDrawing_AlphaBlend)
	DrawText(50,70,Text,$FFFFFFFF,$FF00FFFF)
	DrawText(50,170,text,$FF000000,$FF00FFFF)
	
	StopDrawing()

EndProcedure

CreateBox(#CatImage,480,240,"Title","This is a nice text – or not ?!")
OpenWindow(#CatWindow,0,200,480,240,"Test",#PB_Window_BorderLess|#PB_Window_ScreenCentered|#PB_Window_Invisible)
ImageGadget(0,0,0,480,240,ImageID(#CatImage))

HideWindow(0,0)

Repeat

	Event=WaitWindowEvent()

	If Event=#WM_LBUTTONDOWN
		SendMessage_(WindowID(0),#WM_NCLBUTTONDOWN,#HTCAPTION,0)
	EndIf

Until Event=#PB_Event_CloseWindow Or  Event=#WM_CHAR
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Unsharp font when using AlphaBlend

Post by STARGÅTE »

on your screen gray (128,128,128) isn't the same color (for your eyes) as the mixture of white and black lines
http://en.wikipedia.org/wiki/Gamma_correction

so, if the font mix the black letters with white background, it looks not like white lettes on black background.
With and without alphablending:

Code: Select all

Enumeration
	#Window : #Gadget : #Image : #Font
EndEnumeration


CreateImage(#Image, 128, 128, 32|#PB_Image_Transparent)


LoadFont(#Font, "Calibri", 37)

StartDrawing(ImageOutput(#Image))
	DrawingMode(#PB_2DDrawing_Transparent|#PB_2DDrawing_AlphaBlend)
	DrawingFont(FontID(#Font))
	Box(0, 0, 128, 64, $FFFFFFFF)
	Box(0, 64, 128, 64, $FF000000)
	DrawingMode(#PB_2DDrawing_Transparent|#PB_2DDrawing_AlphaBlend)
	DrawText(20, 5, "!", $FF000000)
	DrawText(20, 69, "!", $FFFFFFFF)
	DrawingMode(#PB_2DDrawing_Transparent)
	DrawText(84, 5, "!", $FF000000)
	DrawText(84, 69, "!", $FFFFFFFF)
StopDrawing()


SetClipboardImage(#Image)


OpenWindow(#Window, 0, 0, ImageWidth(#Image), ImageHeight(#Image), "Image", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
ImageGadget(#Gadget, 0, 0, ImageWidth(#Image), ImageHeight(#Image), ImageID(#Image))

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Image

You always have the edge because DrawText makes no anti-aliasing!

if you want it, use GDI+:
http://www.purebasic.fr/english/viewtop ... 12&t=46987
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Unsharp font when using AlphaBlend

Post by Michael Vogel »

But if DrawText does no antialiasing, why your example has not show just black and white pixels? :?

I try to find a workaround (Fred knows, that DrawText has already worked smooth in earlier PB versions), because GDI+ must not be available on all machines.

Here are two first code snippets for showing a alpha blended text on a window, would be interesting which results you can see on your PC?

Code: Select all

Enumeration
	#CatWindow
	#CatImage
	#CatIcon
	#FontIDs
	#FontIDsEnde=#FontIDs+6
EndEnumeration

#MaxFontSize=1
#MaxFontStyle=1
#FontFace="Calibri"
;#FontFace="Arial"

Global Dim TextFont(#MaxFontSize,#MaxFontStyle)
s=-10
For i=0 To 1
	s-6
	t=0
	For j=0 To 1
		TextFont(i,j)=LoadFont(#FontIDs+i*(#MaxFontStyle+1)+j,#FontFace,s,#PB_Font_HighQuality|t)
		t=#PB_Font_Bold
	Next j
Next i


Procedure CreateText(x,y,w,h,Text.s,Flags=0)

	Protected FontSize
	Protected FontStyle

	; Absatz
	#TA=#CR;   	Absatz
	#TZ=#LF;   	Zeilenumbruch
	#TT=#TAB;	Tabulator

	; Flags
	#T1=#SI;   	Farbe 1 (rot)
	#T2=#SO;   	Farbe 2 (grün)
	#TB=#ACK;	Fettschrift
	#CB=Chr(#TB)
	;#TU=#BEL;	Unterstrichen
	;#CU=Chr(#TU)
	#TH=#ENQ;	Highlight (gelb)
	#CH=Chr(#TH)
		
	#FontBold=1
	#FontUnderline=2
	#FontHighlight=4
	
	l=Len(Text)

	px=x
	n=0
	DrawingFont(TextFont(FontSize,FontStyle))
	DrawingFont(FontID(#FontIDs))

	While n<l

		c=PeekC(@Text+n)
		n+1

		Select c
		Case #TB
			FontStyle!#FontBold
			DrawingFont(TextFont(FontSize,FontStyle&#FontBold))
		Case #TH
			FontStyle!#FontUnderline

		Default
			If c=' '
				xmem=x
				nmem=n
				MemFontStyle=FontStyle
			EndIf

			px=DrawText(px,y,Chr(c),$FF000000,$FFFFFF00)

			If c<>' ' And px>w+x And nmem
				px=x
				n=nmem
				FontStyle=MemFontStyle
				y+20
			EndIf

		EndSelect

	Wend

EndProcedure

Procedure CreateBox(Image,w,h,Title.s,Text.s)

	Protected n,c

	#NoTransparency=$FF000000
	
	#Radius=10
	CreateImage(Image,w,h,32|#PB_Image_Transparent)
	StartDrawing(ImageOutput(Image))

	DrawingMode(#PB_2DDrawing_AllChannels)
	RoundBox(1,1,w-2,h-2,#Radius-1,#Radius-1,#NoTransparency|$A0A0A0)
	RoundBox(2,2,w-4,h-4,#Radius-2,#Radius-2)

	DrawingMode(#PB_2DDrawing_AlphaBlend)
	DrawText(20,30,Title,#NoTransparency)
	Text=Text+" "+#CH+Text+#CH+" "
	CreateText(20,60,260,200,Text+Text+Text)
	StopDrawing()

EndProcedure
Procedure AlphaImageWindow(WindowID,ImageID)

	Protected Image_HDC
	Protected Image_Bitmap.BITMAP,Image_BitmapInfo.BITMAPINFO
	Protected ContextOffset.POINT,Blend.BLENDFUNCTION
	Protected x,y,w,h,Alpha

	SetWindowLong_(WindowID,#GWL_EXSTYLE,GetWindowLong_(WindowID,#GWL_EXSTYLE)|#WS_EX_LAYERED)

	Image_HDC=CreateCompatibleDC_(#Null)
	Image_Ancienne=SelectObject_(Image_HDC,ImageID)

	GetObject_(ImageID,SizeOf(BITMAP),@Image_Bitmap)
	Image_BitmapInfo\bmiHeader\biSize=SizeOf(BITMAPINFOHEADER)
	Image_BitmapInfo\bmiHeader\biWidth=Image_Bitmap\bmWidth
	Image_BitmapInfo\bmiHeader\biHeight=Image_Bitmap\bmHeight
	Image_BitmapInfo\bmiHeader\biPlanes=1
	Image_BitmapInfo\bmiHeader\biBitCount=32

	w=Image_Bitmap\bmWidth-1
	h=Image_Bitmap\bmHeight-1
	Protected Dim Image.l(w,h)

	GetDIBits_(Image_HDC,ImageID,0,Image_Bitmap\bmHeight,@Image(),@Image_BitmapInfo,#DIB_RGB_COLORS)

	For x=0 To w
		For y=0 To h
			Couleur=Image(x,y)
			Alpha=Couleur>>24&$FF
			If Alpha<$FF
				Image(x,y)=Alpha<<24|MulDiv_(Couleur&$FF,Alpha,255)|(MulDiv_(Couleur&$FF00,Alpha,255)&$FF00)|(MulDiv_(Couleur&$FF0000,Alpha,255)&$FF0000)
			EndIf
		Next
	Next

	SetDIBits_(Image_HDC,ImageID,0,Image_Bitmap\bmHeight,@Image(),@Image_BitmapInfo,#DIB_RGB_COLORS)

	Blend\SourceConstantAlpha=255
	Blend\AlphaFormat=1
	Blend\BlendOp=0
	Blend\BlendFlags=0
	UpdateLayeredWindow_(WindowID,0,0,@Image_BitmapInfo+4,Image_HDC,@ContextOffset,0,@Blend,2)

	SelectObject_(Image_HDC,Image_Ancienne)
	DeleteDC_(Image_HDC)

EndProcedure

CreateBox(#CatImage,300,220,"Title","This is a nice text with "+#CB+"extremly"+#CB+" nonsence.")
OpenWindow(#CatWindow,0,200,ImageWidth(#CatImage),ImageHeight(#CatImage),"Test",#PB_Window_BorderLess|#PB_Window_ScreenCentered|#PB_Window_Invisible)
AlphaImageWindow(WindowID(#CatWindow),ImageID(#CatImage))

HideWindow(0,0)

Repeat

	Event=WaitWindowEvent()

	If Event=#WM_LBUTTONDOWN
		SendMessage_(WindowID(0),#WM_NCLBUTTONDOWN,#HTCAPTION,0)
	EndIf

Until Event=#PB_Event_CloseWindow Or  Event=#WM_CHAR
In the second code I have tried to use larger fonts to get better results, but everything looks blurred when setting the #FontQuality>0...

BTW: the CatText procedure is used to calculate word wrapping and the height of the text in one pass and to draw the result in a second pass...

Code: Select all

#FontQuality=1;	0: original font size, 1: size*2, 2: size*4 etc.

Procedure Init()

	Enumeration
		#CatWindow
		#CatImage
		#CatText
		#CatOnOff
	EndEnumeration

	#MaxFontSize=1
	#MaxFontColors=2

	#FontFace="Calibri"

	Structure FontType
		ID.i[2]
		Height.i
	EndStructure

	Global Dim Font.FontType(#MaxFontSize)
	Global Dim FontColor.i(#MaxFontColors)

	FontColor(0)=$FF000000
	FontColor(1)=$FFA02020
	FontColor(2)=$FF20A020

	Protected i,s
	Protected LF.LOGFONT

	;LF\lfHeight=32
	;LF\lfWidth=0
	;LF\lfEscapement=0
	;LF\lfOrientation=0
	;LF\lfWeight=#FW_DONTCARE
	;LF\lfItalic=0
	;LF\lfUnderline=0
	;LF\lfStrikeOut=0
	LF\lfCharSet=#DEFAULT_CHARSET
	LF\lfOutPrecision=#OUT_DEFAULT_PRECIS
	LF\lfClipPrecision=#CLIP_DEFAULT_PRECIS
	LF\lfQuality=#CLEARTYPE_NATURAL_QUALITY;#CLEARTYPE_QUALITY
	LF\lfPitchAndFamily=#DEFAULT_PITCH|#FF_DONTCARE

	PokeS(@LF\lfFaceName[0],#FontFace)

	For i=0 To 1
		With Font(i)
			\Height=(31+i*18)<<#FontQuality>>1
			LF\lfHeight=-\Height
			LF\lfWeight=#FW_NORMAL
			;LF\lfWeight=#FW_DEMIBOLD
			\ID[0]=CreateFontIndirect_(@LF)

			;LF\lfWeight=#FW_BLACK
			LF\lfWeight=#FW_BOLD
			\ID[1]=CreateFontIndirect_(@LF)

		EndWith
	Next i

	Font(0)\Height+2<<#FontQuality
	Font(1)\Height-1<<#FontQuality

EndProcedure
Procedure AlphaImageWindow(Window,Image)

	Protected Image_HDC,Image_Object
	Protected Image_Bitmap.BITMAP,Image_BitmapInfo.BITMAPINFO
	Protected ContextOffset.POINT,Blend.BLENDFUNCTION
	Protected x,y,w,h,Alpha,Color

	Window=WindowID(Window)
	Image=ImageID(Image)

	SetWindowLong_(Window,#GWL_EXSTYLE,GetWindowLong_(Window,#GWL_EXSTYLE)|#WS_EX_LAYERED)

	Image_HDC=CreateCompatibleDC_(#Null)
	Image_Object=SelectObject_(Image_HDC,Image)

	GetObject_(Image,SizeOf(BITMAP),@Image_Bitmap)
	Image_BitmapInfo\bmiHeader\biSize=SizeOf(BITMAPINFOHEADER)
	Image_BitmapInfo\bmiHeader\biWidth=Image_Bitmap\bmWidth
	Image_BitmapInfo\bmiHeader\biHeight=Image_Bitmap\bmHeight
	Image_BitmapInfo\bmiHeader\biPlanes=1
	Image_BitmapInfo\bmiHeader\biBitCount=32

	w=Image_Bitmap\bmWidth-1
	h=Image_Bitmap\bmHeight-1

	SetWindowPos_(Window,#HWND_TOPMOST,100,100,w,h,#SWP_NOACTIVATE)

	Protected Dim Image.l(w,h)

	GetDIBits_(Image_HDC,Image,0,Image_Bitmap\bmHeight,@Image(),@Image_BitmapInfo,#DIB_RGB_COLORS)

	For x=0 To w
		For y=0 To h
			Color=Image(x,y)
			Alpha=Color>>24&$FF
			If Alpha<$FF
				Image(x,y)=Alpha<<24|MulDiv_(Color&$FF,Alpha,255)|(MulDiv_(Color&$FF00,Alpha,255)&$FF00)|(MulDiv_(Color&$FF0000,Alpha,255)&$FF0000)
			EndIf
		Next
	Next

	SetDIBits_(Image_HDC,Image,0,Image_Bitmap\bmHeight,@Image(),@Image_BitmapInfo,#DIB_RGB_COLORS)

	Blend\SourceConstantAlpha=255
	Blend\AlphaFormat=1
	Blend\BlendOp=0
	Blend\BlendFlags=0
	UpdateLayeredWindow_(Window,0,0,@Image_BitmapInfo+4,Image_HDC,@ContextOffset,0,@Blend,2)

	SelectObject_(Image_HDC,Image_Object)
	DeleteDC_(Image_HDC)

EndProcedure
Procedure.i CatText(w,h,Text.s,Flags=0)

	Debug "***"+Str(w)+"***"

	#FontBold=%0001

	#FlagBig=			%00000001
	#FlagVCenter=	%00000010
	#FlagCenter=		%00000100
	#FlagIndent=		%00001000
	#FlagDrawing=	%10000000

	Protected FontSize=Flags&#FlagBig
	Protected FontStyle,MemFontStyle
	Protected FontColor,MemFontColor

	Protected Px,Py,MemPx,n,MemN,Word
	Protected c,l,z

	; Absatz
	#TR=#CR;   	Absatz

	#TZ=#LF;   	Zeilenumbruch
	#CZ=#LF$
	#TT=#TAB;	Tabulator
	#CT=#TAB$
	; Flags
	#T1=#SI;   	Farbe 1 (rot)
	#C1=Chr(#T1)
	#T2=#SO;   	Farbe 2 (grün)
	#C2=Chr(#T2)
	#TB=#ACK;	Fettschrift
	#CB=Chr(#TB)

	#FontBold=1

	;LineXY(x,y,x,y+h,$FFFF0000)
	;LineXY(x+w,y,x+w,y+h,$FFFF0000)

	w<<#FontQuality
	h<<#FontQuality

	If Flags&#FlagDrawing
		StopDrawing()
	EndIf

	CreateImage(#CatText,w,h,32|#PB_Image_Transparent)
	StartDrawing(ImageOutput(#CatText))
	;DrawingMode(#PB_2DDrawing_AlphaBlend)
	;DrawingMode(#PB_2DDrawing_Transparent)
	DrawingMode(#PB_2DDrawing_AllChannels)

	With Font(FontSize)

		z=0
		Word=0
		l=Len(Text)

		Repeat

			FontStyle=0
			FontColor=0

			n=0
			Px=0
			If z And Flags&#FlagVCenter
				Py=(h-Py-MulDiv_(\Height,10,8))>>1
			Else
				Py=0
			EndIf

			DrawingFont(\ID[FontStyle])

			While n<l

				c=PeekC(@Text+n)
				n+1

				Select c
				Case #TB
					FontStyle!#FontBold
					DrawingFont(\ID[FontStyle])
				Case #TR
					Px=0
					Py+\Height+\Height>>2
					Word=0
				Case #T1
					If FontColor=1
						FontColor=0
					Else
						FontColor=1
					EndIf
				Case #T2
					If FontColor=2
						FontColor=0
					Else
						FontColor=2
					EndIf
				Case #TZ
					Px=0
					Py+\Height
					Word=0

				Default
					If c=' '
						MemPx=Px
						MemN=n
						MemFontStyle=FontStyle
						MemFontColor=FontColor
						Word+1
					EndIf

					If z
						Px=DrawText(Px,Py,Chr(c),FontColor(FontColor),#Null)
					Else
						Px+TextWidth(Chr(c))
					EndIf

					If c<>' ' And Px>w And MemN
						;LineXY(MemPx,Py,MemPx,Py+\Height,$FF00FF00)
						Px=0
						If Word
							n=MemN
							FontStyle=MemFontStyle
							FontColor=MemFontColor
							PokeC(@Text+n-1,#TZ)
							Word=0
						Else
							Text=Left(Text,n-1)+#CZ+Mid(Text,n)
							l+1
						EndIf
						Py+\Height

					EndIf

				EndSelect

			Wend

			z+1+(Flags!#FlagDrawing)&#FlagDrawing

		Until z>1

		StopDrawing()
		ResizeImage(#CatText,w>>#FontQuality,h>>#FontQuality)

		If Flags&#FlagDrawing
			StartDrawing(ImageOutput(#CatImage))
			DrawingMode(#PB_2DDrawing_AlphaBlend)
		EndIf

		ProcedureReturn (Py+MulDiv_(\Height,10,8))>>#FontQuality

	EndWith

EndProcedure
Procedure CatHelp(Title.s,Text.s)

	#CatWidth=380
	#CatCorner=6
	#CatSpace=15

	Protected n,c,h

	#NoTransparency=$FF000000
	For n=0 To Random(2)
		Text=Text+#C1+Text+#C1+#C2+Text+#C2
	Next n

	n=CatText(#CatWidth-#CatSpace<<1,60,Text,#False)
	h=n+50

	CreateImage(#CatImage,#CatWidth,h,32|#PB_Image_Transparent)
	StartDrawing(ImageOutput(#CatImage))
	DrawingMode(#PB_2DDrawing_AllChannels)
	RoundBox(1,1,#CatWidth-2,h-2,#CatCorner-1,#CatCorner-1,#NoTransparency|$A0A0A0)
	RoundBox(2,2,#CatWidth-4,h-4,#CatCorner-2,#CatCorner-2)

	DrawingMode(#PB_2DDrawing_AlphaBlend)
	CatText(#CatWidth-#CatSpace<<1,50,Title,#FlagBig|#FlagVCenter|#FlagDrawing)
	DrawImage(ImageID(#CatText),#CatSpace,0)
	CatText(#CatWidth-#CatSpace<<1,h-50,Text,#FlagVCenter|#FlagDrawing)
	DrawImage(ImageID(#CatText),#CatSpace,50)
	StopDrawing()

	AlphaImageWindow(#CatWindow,#CatImage)
	HideWindow(#CatWindow,0)

EndProcedure

Init()

OpenWindow(#CatWindow,0,0,0,0,"Cat",#PB_Window_BorderLess|#PB_Window_Invisible)
StickyWindow(#CatWindow,1)

CatHelp("Hi, stranger!","Need some training? This is a "+#C2+"nice"+#C2+" text with "+#CB+"extremly"+#CB+" nonsence.")


Repeat

	Event=WaitWindowEvent()
	Select Event
	Case #WM_LBUTTONDOWN
		SendMessage_(WindowID(0),#WM_NCLBUTTONDOWN,#HTCAPTION,0)
	Case #PB_Event_CloseWindow,#WM_CHAR
		End
	EndSelect

ForEver
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Unsharp font when using AlphaBlend

Post by STARGÅTE »

Michael Vogel wrote:But if DrawText does no antialiasing, why your example has not show just black and white pixels?
anti-aliasing does not mean that there is no mixing (alpha blend) with the background!
If the font need a half pixel, he mix it to gray.
But we have not the correction method "anti-aliasing"

The result with your codes:
first:
Image
second code:
Image

Edit:
because GDI+ must not be available on all machines
the font "Calibri" is also not on any system!
Calibri looks better because this font use hinting
http://en.wikipedia.org/wiki/Font_hinting
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Unsharp font when using AlphaBlend

Post by Michael Vogel »

Thanks for the screen shots, I wasn't sure if the terrible output would be seen on other machines as well (code example 1) - I can't see a coding error, maybe it's a DrawText problem?

Code example 2 (is more close to my actual work) is not that bad, but using FontQuality>0 means to use larger fonts (slows down the program) and resizing the resulting image (steals some information, can be seen on the character "g").


PS nice try about Calibri :lol:

Code: Select all

OpenWindow(0,0,0,480,200,"")
CreateImage(0,480,200,32)
LoadFont(0,"Arial",-60)
StartDrawing(ImageOutput(0))
Box(0,0,240,200,#White)
Title.s="Test Hanburg"
DrawingMode(#PB_2DDrawing_AlphaBlend)
;DrawingMode(#PB_2DDrawing_Default)
DrawingFont(FontID(0))
DrawText(20,10,Title,$ff000000,$ffffffff)
DrawText(20,100,Title,$ffffffff,$ff000000)
StopDrawing()
ImageGadget(0,0,0,0,0,ImageID(0))
HideWindow(0,0)

Repeat

	Event=WaitWindowEvent()

	If Event=#WM_LBUTTONDOWN
		SendMessage_(WindowID(0),#WM_NCLBUTTONDOWN,#HTCAPTION,0)
	EndIf

Until Event=#PB_Event_CloseWindow Or  Event=#WM_CHAR
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Unsharp font when using AlphaBlend

Post by Michael Vogel »

The drawtext result of the last code above is ok?!
Image
Post Reply