Question

Je suis en train d'obtenir un bitmap rendu par Pango avec un caractère donné, dans ce cas, la lettre « T ». Je pensais que cela devrait fonctionner, mais il se contente d'afficher des ordures ..

Dans ce code, gtk_window est déjà initialisé comme une fenêtre GTK.

int width, height;
PangoLayout *layout = gtk_widget_create_pango_layout(gtk_window, "T");
PangoFontDescription *fontdesc = pango_font_description_from_string("Terminus 12");
pango_layout_set_font_description (layout, fontdesc);
pango_layout_get_pixel_size (layout, &width, &height);

GdkPixmap *temp_pixmap = gdk_pixmap_new(NULL, width, height, 24) ;
GdkGC *gc = gdk_gc_new(temp_pixmap);
gdk_draw_layout( temp_pixmap, gc, 0, 0, layout) ;

GdkColormap *cmap = gdk_colormap_get_system() ;
GdkPixbuf *temp_pixbuf = gdk_pixbuf_get_from_drawable(NULL, temp_pixmap,
                                       cmap, 0, 0, 0, 0, width, height);

int n_channels = gdk_pixbuf_get_n_channels (temp_pixbuf);
int rowstride = gdk_pixbuf_get_rowstride(temp_pixbuf);
guchar *pixels = gdk_pixbuf_get_pixels(temp_pixbuf);

int i,j;
for (j=0; j<height; j++) {
    for (i=0; i<(width*n_channels); i++) {
        printf("%02x ",
               *(pixels + i + j*rowstride));
    }
    printf("\n");
}

La sortie est différent à chaque fois, mais un exemple:

dd 24 f8 dd 24 f8 8f 28 28 8f 28 28 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fc 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
ef 14 ce 00 00 00 d0 00 20 00 00 00 ef 02 02 d0 00 01 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 fc 00 00 00 00 00 ef 02 02 00 03 40 00 00 b9 00 00 c8 
00 00 01 00 00 07 00 00 07 00 00 00 00 00 02 00 00 00 00 00 d0 00 00 c8 
00 00 01 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 
00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 12 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 7a 4e 00 00 00 01 00 00 00 00 00 00 00 00 00 01 ff ff ff ff ff ff 
ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

Ce qui ne ressemble en rien à la lettre "T"!

Toute idée de ce que je fais mal? Merci!

Était-ce utile?

La solution

D'accord, je compris. Je avais besoin de régler correctement les couleurs, et nettoyer la zone avant de tirer à l'aide gdk_draw_rectangle:

PangoLayout *layout = gtk_widget_create_pango_layout(me->gtk_window, "T");
int width, height;
PangoFontDescription *fontdesc = pango_font_description_from_string("Sans 10");
pango_layout_set_font_description (layout, fontdesc);
pango_layout_get_pixel_size (layout, &width, &height);

GdkPixmap *temp_pixmap = gdk_pixmap_new(NULL, width, height, 24);
GdkGC *gc = gdk_gc_new(temp_pixmap);

GdkColor white = {0,0xFF00,0xFF00,0xFF00};
GdkColor black = {0,0,0,0};

GdkColormap *cmap = gdk_gc_get_colormap(gc);

gdk_gc_set_rgb_fg_color(gc, &black);
gdk_gc_set_rgb_bg_color(gc, &white);
gdk_draw_rectangle(temp_pixmap, gc, TRUE, 0, 0, width, height);

gdk_gc_set_rgb_fg_color(gc, &white);
gdk_gc_set_rgb_bg_color(gc, &black);
gdk_draw_layout(temp_pixmap, gc, 0, 0, layout);

GdkPixbuf *temp_pixbuf = gdk_pixbuf_get_from_drawable(NULL, temp_pixmap,
                                        cmap, 0, 0, 0, 0, width, height);

int n_channels = gdk_pixbuf_get_n_channels (temp_pixbuf);
int rowstride = gdk_pixbuf_get_rowstride(temp_pixbuf);
guchar *pixels = gdk_pixbuf_get_pixels(temp_pixbuf);

int i,j;
for (j=0; j<height; j++) {
    for (i=0; i<(width*n_channels); i++) {
        printf("%02x ",
               *(pixels + i + j*rowstride));
    }
    printf("\n");
}

me donne:

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
c5 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff e3 
25 30 33 33 33 33 33 33 3d 68 ba f6 ff d8 86 4a 35 33 33 33 33 33 31 2a 
00 00 00 00 00 00 00 00 0c 41 a7 f1 fe cc 66 1c 03 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 0c 41 a7 f1 fe cc 66 1c 03 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 0c 41 a7 f1 fe cc 66 1c 03 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 0c 41 a7 f1 fe cc 66 1c 03 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 0c 41 a7 f1 fe cc 66 1c 03 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 0c 41 a7 f1 fe cc 66 1c 03 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 0c 41 a7 f1 fe cc 66 1c 03 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 0c 41 a7 f1 fe cc 66 1c 03 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

Autres conseils

Juste quelques conseils de base, ont pas essayé votre code:

  • Vous ne mentionnez pas de contrôle d'erreur; vous devriez passer et voir que tous les différents GTK + / GDK appelle succeeed. Ils devraient tous faire NULL vérification sur les entrées et émettent des avertissements, mais quand même.
  • Vous imprimez tous les canaux en même temps, qui peut conduire à un disque à interpréter visuellement l'image. J'ai essayé filtrer deux des canaux dans vos données, mais cela n'a pas aidé. Bien sûr, puisque vous dites que la sortie est différent à chaque fois, ce fut un long shot.
  • Je ne suis pas sûr de la synchronicité des appels de dessin GDK, s'ils sont garantis pour arriver immédiatement, puis revenir, ou si elles peuvent être reportées à plus tard certains gestionnaire « mettre à jour les zones sales ».
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top