#include Once "window9.bi"

Dim Shared As PangoFontDescription Ptr font_desc

Function Fontrequester(Byval hwnd As HWND = 0, Byval nColor As Integer = 0) As Integer Export
	
	Var dialog = gtk_font_selection_dialog_new( "Font selection")
	
	If hwnd Then
		
		gtk_window_set_transient_for(GTK_WINDOW(dialog) , Cast(Any Ptr,hwnd))
		
	Endif
	
	If GTK_RESPONSE_OK = gtk_dialog_run(  GTK_DIALOG (dialog) ) Then
		
		Var newFont = gtk_font_selection_dialog_get_font_name ( _
		GTK_FONT_SELECTION_DIALOG (dialog))
		
		If font_desc Then 
			
			pango_font_description_free( font_desc )
			
		Endif
		
		font_desc = pango_font_description_from_string (newFont)
		
		g_free( newFont )
		
		Function  = Cint(font_desc) 
		
	End If
	
	gtk_widget_destroy( GTK_WIDGET(dialog) )
	
End Function

Function Selectedfontname() As String Export
	
	Return *pango_font_description_get_family(font_desc)
	
End Function

Function Selectedfontsize() As Long Export
	
	Return pango_font_description_get_size(font_desc)\1024
	
End Function

Function Selectedfontstyle(Byval iStyle As Byte) As Byte Export ' стили UnderLine и StrikeOut не поддерживаются
	
	Select Case iStyle
			
		Case 1
			
			If pango_font_description_get_weight(font_desc) > 600 Then
				
				Return 1
				
			Endif
			
		Case 2
			
			If pango_font_description_get_style(font_desc) = PANGO_STYLE_ITALIC Then
				
				Return 1
				
			Endif
			
	End Select
	
End Function

Function GetSystemDefaultFont() As Integer
	
	Return Cint(pGlobalTypeWindow9->fontDefault)
	
End Function

Function GetCurentDefaultFont() As Integer
	
	Return Cint(pGlobalTypeWindow9->fontCurent)
	
End Function

Function GetNameFromFont(font As Integer) As String
	
	Dim As Const Zstring Ptr psz = pango_font_description_get_family(Cast(Any Ptr,font))
	
	If psz Then
		
		Return *psz
		
	Endif
	
End Function

Function GetSizeFromFont(font As Integer) As Long
	
	Return pango_font_description_get_size(Cast(Any Ptr,font))\PANGO_SCALE
	
End Function

Function GetStyleFromFont(font As Integer , iStyle As Long) As Long
	
	If iStyle = 1 Then
		If pango_font_description_get_weight(Cast(Any Ptr,font)) > 600 Then
			Return 1
		Endif
	Elseif iStyle = 2 Then
		If pango_font_description_get_style(Cast(Any Ptr,font)) = PANGO_STYLE_ITALIC Then
			Return 1
		Endif
	Endif
	
End Function

Sub GetSizeStringFromFont(sText As String , font As Integer , Byref iW As Long , Byref iH As Long)
	
	Dim As PangoLayout Ptr layout
	
	Dim As PangoFontDescription Ptr desc_temp
	
	Dim As HDC cr
	
	Dim As cairo_surface_t Ptr surface
	
	surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 50)
	
	cr = cairo_create (surface)
	
	layout = pango_cairo_create_layout (cr)
	
	If font Then
		
		desc_temp = pango_font_description_copy(Cast(Any Ptr ,font))
		
		pango_layout_set_font_description (layout, desc_temp)
		
		pango_font_description_free (desc_temp)
		
	Endif
	
	pango_layout_set_text (layout, Strptr(sText) , -1)
	
	Dim As PangoRectangle pr_ink , pr_log
	
	pango_layout_get_pixel_extents (layout, @pr_ink, @pr_log)
	
	iW = pr_log.Width
	
	iH = pr_log.Height
	
	g_object_unref (layout)
	
	cairo_destroy (cr)
	
	cairo_surface_destroy(surface)
	
End Sub