Importing pango libs?

Linux specific forum
Justin
Addict
Addict
Posts: 832
Joined: Sat Apr 26, 2003 2:49 pm

Importing pango libs?

Post by Justin »

Can someone tell me how to import the pango api?
I tried this but does not work.

Code: Select all

ImportC ""
  pango_attr_list_new_() As "_pango_attr_list_new "
EndImport

ImportC "/usr/lib/x86_64-linux-gnu/libpango-1.0.so"
  pango_attr_list_new_() As "_pango_attr_list_new "
EndImport
Thanks.
Justin
Addict
Addict
Posts: 832
Joined: Sat Apr 26, 2003 2:49 pm

Re: Importing pango libs?

Post by Justin »

This worked:

Code: Select all

ImportC "/usr/lib/x86_64-linux-gnu/libpango-1.0.so.0"
pango_attr_list_new_() As "pango_attr_list_new"
EndImport
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Importing pango libs?

Post by Shardik »

The following example demonstrates how to import Pango functions and display text with Pango in GTK2 (successfully tested in PB 5.41 with subsystem "gtk2" in both ASCII and Unicode mode):

Code: Select all

; Converted from C example
; http://x11.gp2x.de/personal/google/

ImportC ""
  gdk_cairo_create(*Drawable.GdkDrawable)
  pango_cairo_create_layout(*CairoContext)
  pango_cairo_layout_path(*CairoContext, *PangoLayout)
  pango_cairo_show_layout(*CairoContext, *PangoLayout)
  pango_cairo_update_layout(*CairoContext, *PangoLayout)
  pango_font_description_free(*PangoFontDescription)
  pango_font_description_from_string(FontString.P-UTF8)
  pango_layout_set_font_description(*CairoContext, *PangoFontDescription)
  pango_layout_set_text(*PangoLayout, Text.P-UTF8, Length.I)
EndImport

ImportC "-lcairo"
  cairo_destroy(*CairoContext)
  cairo_move_to(CairoContext, xNew.D, yNew.D)
  cairo_new_path(*CairoContext)
  cairo_set_line_width(*CairoContext, LineWidth.D)
  cairo_set_source_rgb(*CairoContext, Red.D, Green.D, Blue.D)
  cairo_stroke_preserve(*CairoContext)
  cairo_surface_destroy(*CairoSurface)
  cairo_translate(*CairoContext, xOffset.D, yOffset.D)
EndImport

ProcedureC WidgetExposeHandler(*Widget.GtkWidget, *Event.GdkEventExpose)
  Protected CairoContext.I
  Protected Desc.I
  Protected Layout.I
  
  CairoContext = gdk_cairo_create(*Widget\window)
  cairo_translate(CairoContext, 10, 20)
  Layout = pango_cairo_create_layout(CairoContext)
	pango_layout_set_text(Layout, "Hello World!", -1)
	Desc = pango_font_description_from_string("Sans Bold 12")
	pango_layout_set_font_description(Layout, Desc)
	pango_font_description_free(Desc)
	cairo_set_source_rgb(CairoContext, 0.0, 0.0, 1.0)
	pango_cairo_update_layout(CairoContext, Layout)

  ; ----- Display normal text
	pango_cairo_show_layout(CairoContext, Layout)

  ; ----- Display outlined text
  cairo_new_path(CairoContext)
	cairo_move_to(CairoContext, 30, 40)
  cairo_set_line_width(CairoContext, 0.5)
  pango_cairo_update_layout(CairoContext, Layout)
	pango_cairo_layout_path(CairoContext, Layout)
  cairo_stroke_preserve(CairoContext)

	g_object_unref_(Layout)
  cairo_destroy(CairoContext)
EndProcedure

Define ChildrenList.I
Define FixedBox.I
Define VBox.I

OpenWindow(0, 200, 100, 300, 100, "Text rendered with Pango", #PB_Window_SizeGadget)
VBox = gtk_bin_get_child_(WindowID(0))
ChildrenList = gtk_container_get_children_(VBox)
FixedBox = g_list_nth_data_(ChildrenList, 0)
g_signal_connect_(FixedBox, "expose-event", @WidgetExposeHandler(), 0)

; ----- Force initial drawing of window
gtk_widget_queue_draw_(WindowID(0))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
The 2nd example is modified to work equally well in GTK2 and GTK3 in both ASCII and Unicode mode:

Code: Select all

; Converted from C example
; http://x11.gp2x.de/personal/google/

ImportC ""
  gdk_cairo_create(*Drawable.GdkDrawable)
  pango_cairo_create_layout(*CairoContext)
  pango_cairo_layout_path(*CairoContext, *PangoLayout)
  pango_cairo_show_layout(*CairoContext, *PangoLayout)
  pango_cairo_update_layout(*CairoContext, *PangoLayout)
  pango_font_description_free(*PangoFontDescription)
  pango_font_description_from_string(FontString.P-UTF8)
  pango_layout_set_font_description(*CairoContext, *PangoFontDescription)
  pango_layout_set_text(*PangoLayout, Text.P-UTF8, Length.I)
EndImport

ImportC "-lcairo"
  cairo_destroy(*CairoContext)
  cairo_move_to(CairoContext, xNew.D, yNew.D)
  cairo_new_path(*CairoContext)
  cairo_set_line_width(*CairoContext, LineWidth.D)
  cairo_set_source_rgb(*CairoContext, Red.D, Green.D, Blue.D)
  cairo_stroke_preserve(*CairoContext)
  cairo_surface_destroy(*CairoSurface)
  cairo_translate(*CairoContext, xOffset.D, yOffset.D)
EndImport

CompilerIf Subsystem("gtk2")
  ProcedureC WidgetExposeHandler(*Widget.GtkWidget, *Event.GdkEventExpose)
    Protected CairoContext.I

    CairoContext = gdk_cairo_create(*Widget\window)
CompilerElse
  ProcedureC WidgetDrawHandler(*Widget.GtkWidget, CairoContext.I, *UserData)
CompilerEndIf
  Protected Desc.I
  Protected Layout.I
  
  cairo_translate(CairoContext, 10, 20)
  Layout = pango_cairo_create_layout(CairoContext)
  pango_layout_set_text(Layout, "Hello World!", -1)
  Desc = pango_font_description_from_string("Sans Bold 12")
  pango_layout_set_font_description(Layout, Desc)
  pango_font_description_free(Desc)
  cairo_set_source_rgb(CairoContext, 0.0, 0.0, 1.0)
  pango_cairo_update_layout(CairoContext, Layout)
  
  ; ----- Display normal text
  pango_cairo_show_layout(CairoContext, Layout)
  
  ; ----- Display outlined text
  cairo_new_path(CairoContext)
  cairo_move_to(CairoContext, 30, 40)
  cairo_set_line_width(CairoContext, 0.5)
  pango_cairo_update_layout(CairoContext, Layout)
  pango_cairo_layout_path(CairoContext, Layout)
  cairo_stroke_preserve(CairoContext)
  
  g_object_unref_(Layout)
  
  CompilerIf Subsystem("gtk2") = #True
    cairo_destroy(CairoContext)
  CompilerEndIf
EndProcedure ;}

Procedure.I GetContainer(WindowID.I, ContainerName.S)
  Protected Child.I
  Protected ChildrenList.I
  Protected *Name
  Protected Widget.I = WindowID(WindowID)

  Child = gtk_bin_get_child_(Widget)

  Repeat
    ChildrenList = gtk_container_get_children_(Widget)
    Child = g_list_nth_data_(ChildrenList, 0)

    If Child = 0
      Break
    Else
      *Name = gtk_widget_get_name_(Child)
      Widget = Child
    EndIf
  Until PeekS(*Name, -1, #PB_UTF8) = ContainerName

  ProcedureReturn Child
EndProcedure

Define ChildrenList.I
Define FixedBox.I

OpenWindow(0, 200, 100, 300, 100, "Text rendered with Pango", #PB_Window_SizeGadget)

CompilerIf Subsystem("gtk2")
  g_signal_connect_(GetContainer(0, "GtkFixed"), "expose-event",
    @WidgetExposeHandler(), 0)
CompilerElse
  g_signal_connect_(GetContainer(0, "GtkFixed"), "draw",
    @WidgetDrawHandler(), 0)
CompilerEndIf

; ----- Force initial drawing of window
gtk_widget_queue_draw_(WindowID(0))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Justin
Addict
Addict
Posts: 832
Joined: Sat Apr 26, 2003 2:49 pm

Re: Importing pango libs?

Post by Justin »

This will be very useful, thanks.
My import failed because i was missing the gtk_init() call, so this works:
ImportC ""
pango_attr_list_new_() As "pango_attr_list_new"
EndImport
Post Reply