/* * GTK/Cairo print test * * gcc -o printtest `pkg-config --cflags gtk+-2.0` \ * `pkg-config --libs gtk+-2.0` printtest.c */ #include static void draw_page( GtkPrintOperation *operation, GtkPrintContext *context, int page_nr, gpointer data ) { cairo_t *cr = gtk_print_context_get_cairo_context( context ); PangoLayout *layout; gdouble wdth, txtHght; gint layoutHght; PangoFontDescription *desc; int x = 0, y = 0; layout = gtk_print_context_create_pango_layout( context ); wdth = gtk_print_context_get_width( context ); desc = pango_font_description_from_string( "Arial 10" ); pango_layout_set_font_description( layout, desc ); pango_font_description_free( desc ); pango_layout_set_markup( layout, "Test Text", -1 ); pango_layout_set_width( layout, wdth * PANGO_SCALE ); pango_layout_set_alignment( layout, PANGO_ALIGN_LEFT ); pango_layout_get_size( layout, NULL, &layoutHght ); txtHght = ( gdouble ) layoutHght / PANGO_SCALE; cairo_move_to( cr, wdth / 2 - ( 100 - x ), ( y - txtHght ) / 2 ); pango_cairo_show_layout( cr, layout ); g_object_unref( layout ); } int main( int argc, char *argv[] ) { GtkPrintOperation *op; gtk_init( &argc, &argv ); op = gtk_print_operation_new( ); gtk_print_operation_set_n_pages( op, 1 ); gtk_print_operation_set_unit( op, GTK_UNIT_MM ); g_signal_connect( op, "draw_page", G_CALLBACK( draw_page ), NULL ); gtk_print_operation_run( op, GTK_PRINT_OPERATION_ACTION_PRINT, NULL, NULL ); g_object_unref( op ); gtk_main( ); return 0; }