pango can not draw text on default root window ???
- From: "zhu shenqi" <zhusqi hotmail com>
- To: fonts gnome org
- Subject: pango can not draw text on default root window ???
- Date: Fri, 04 Nov 2005 17:35:13 +0800
Hi, all
I want to draw on screen use gdk and pango but I fail. I can draw text on
root window user XDrawImageString() but gdk_draw_layout can not do it and
my pango code can draw text on a gtk window. I don't know why ???
My code:
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
void convert_color( GdkColor *gcolor, PangoAttrColor *attr )
{
gcolor->red = attr->color.red;
gcolor->green = attr->color.green;
gcolor->blue = attr->color.blue;
}
int SetPangoTextColor( PangoLayout *layout, GdkColor *color1, GdkColor
*color2, PangoAttrType nFlag )
{
GdkColor oldcolor;
PangoAttrList *attr_list = pango_layout_get_attributes( layout );
if (attr_list == NULL)
{
/* Create attr list if there wasn't one */
attr_list = pango_attr_list_new ();
pango_layout_set_attributes (layout, attr_list);
pango_attr_list_unref (attr_list);
}
PangoAttrIterator *iter = pango_attr_list_get_iterator( attr_list );
if( iter == NULL )
return FALSE;
PangoAttribute *attr = pango_attr_iterator_get( iter, nFlag );
if( attr && color2 )
{
convert_color( &oldcolor, (PangoAttrColor *)attr );
color2->red = oldcolor.red;
color2->green = oldcolor.green;
color2->blue = oldcolor.blue;
}
PangoAttribute* tmp_attr = NULL;
if( nFlag == PANGO_ATTR_FOREGROUND )
tmp_attr = pango_attr_foreground_new (color1->red, color1->green,
color1->blue);
else
tmp_attr = pango_attr_background_new (color1->red, color1->green,
color1->blue);
if( tmp_attr )
{
tmp_attr->start_index = 0;
tmp_attr->end_index = 2147483647;
attr = pango_attribute_copy( tmp_attr );
pango_attribute_destroy( tmp_attr );
pango_attr_list_change( attr_list, attr );
}
pango_attr_iterator_destroy( iter );
return TRUE;
}
int draw_some_text(GdkDrawable *drawable, GdkGC * gc , PangoContext
*context, GdkColormap * colorMap)
{
PangoFontDescription *font_desc;
PangoLayout *layout = pango_layout_new(context);
gunichar uc = 0x00D4;
gchar text[64];
gint ui = 0;
PangoRectangle ink_rect, log_rect;
ui = g_unichar_to_utf8(uc, text);
text[ui] = 0;
font_desc = pango_font_description_new();
pango_font_description_set_family(font_desc, "ZYSong18030");
pango_font_description_set_size(font_desc, 48*PANGO_SCALE);
pango_layout_set_font_description(layout, font_desc);
pango_layout_set_text(layout, "Hello", -1);
pango_layout_get_pixel_extents(layout, &ink_rect, &log_rect);
GdkColor textColor;
textColor.red = 0;
textColor.green = 0;
textColor.blue = 0;
if (!gdk_colormap_alloc_color(colorMap, &textColor, TRUE, TRUE))
{
g_error ("couldn't allocate color");
}
int ret = SetPangoTextColor( layout, &textColor, NULL,
PANGO_ATTR_FOREGROUND );
if( !ret )
return FALSE;
//g_print("Ink: {%d, %d, %d, %d}\n", ink_rect.x, ink_rect.y,
ink_rect.width, ink_rect.height);
//g_print("Log: {%d, %d, %d, %d}\n", log_rect.x, log_rect.y,
log_rect.width, log_rect.height);
gdk_draw_layout(drawable, gc, 30, 20, layout);
pango_font_description_free(font_desc);
g_object_unref(layout);
return TRUE;
}
int main( int argc, char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
//GtkWidget *button;
/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GdkRectangle rect;
rect.x = 50;
rect.y = 50;
rect.width = 500;
rect.height = 500;
GdkColor bkcr;// = { 0, 65535, 0, 0 };
GdkColor fecr;// = { 0, 0, 0, 0 };
GdkWindow * pWindow = gdk_get_default_root_window();
GdkColormap *colormap = gdk_colormap_get_system();
bkcr.red = 0xffff;
bkcr.green = 0;
bkcr.blue = 0;
fecr.red = 0;
fecr.green = 0xffff;
fecr.blue = 0;
if (!gdk_colormap_alloc_color(colormap, &bkcr, TRUE, TRUE))
{
g_error ("couldn't allocate color");
}
if (!gdk_colormap_alloc_color(colormap, &fecr, TRUE, TRUE))
{
g_error ("couldn't allocate color");
}
GdkGC * gc = gdk_gc_new(pWindow);
GdkFill fill = GDK_SOLID;
gdk_gc_set_fill(gc, fill);
gdk_gc_set_colormap(gc, colormap);
gdk_gc_set_exposures(gc, TRUE);
gdk_gc_set_subwindow(gc, GDK_INCLUDE_INFERIORS);
gdk_gc_set_background(gc, &bkcr);
gdk_gc_set_foreground(gc, &fecr);
gdk_gc_set_clip_rectangle (gc, &rect);
gdk_draw_rectangle((GdkWindow*)pWindow, gc, 1, 50, 50, 500,500);
PangoContext *context = gdk_pango_context_get();
pango_context_set_base_dir(context, PANGO_DIRECTION_LTR);
pango_context_set_language(context, gtk_get_default_language());
gdk_pango_context_set_colormap(context, colormap);
int ret = draw_some_text(pWindow, gc, context, colormap);
if( !ret )
printf("draw text fail\n");
gdk_window_show_unraised(pWindow);
gtk_widget_show (window);
gtk_main ();
return 0;
}
_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn
[Date Prev][
Date Next] [Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]