I have written a small code with gtk-3 and C++. I set cairo_select_face and query the reference to font-face. I get count as one and it is expected.
But when i use cairo_show_text, the font_face reference count becomes 2. After that, any number of cairo_show_text ()do not change the reference count and reference count is always 2. I am wondering why
cairo_show_text () when called the first time increases the count to 2 for the first time. expect reference count to say 1 always.
My code is below
#include <cairo.h>
#include <gtk/gtk.h>
gboolean timer = TRUE;
int i = 1;
int current_count =0;
cairo_font_face_t* font_face;
static gboolean do_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data)
{
cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
cairo_select_font_face(cr, "Purisa",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
font_face = cairo_get_font_face (cr);
current_count =cairo_font_face_get_reference_count (font_face);
fprintf (stderr, "refernce_count in drawing: %d\n", current_count); ----------> prints 1
cairo_set_font_size(cr, 13);
font_face = cairo_get_font_face (cr);
current_count =cairo_font_face_get_reference_count (font_face);
fprintf (stderr, "refernce_count in drawing: %d\n", current_count);---------> prints 1
cairo_move_to(cr, 20, 30);
cairo_show_text(cr, "Most relationships seem so transitory");
font_face = cairo_get_font_face (cr);
current_count =cairo_font_face_get_reference_count (font_face);
fprintf (stderr, "reference_count in drawing------: %d\n", current_count);----------------->print 2
cairo_move_to(cr, 20, 60);
cairo_show_text(cr, "They're all good but not the permanent one");
font_face = cairo_get_font_face (cr);
current_count =cairo_font_face_get_reference_count (font_face);
fprintf (stderr, "reference_count in drawing: %d\n", current_count);------------->prints 2from here
cairo_move_to(cr, 20, 120);
cairo_show_text(cr, "Who doesn't long for someone to hold");
font_face = cairo_get_font_face (cr);
current_count =cairo_font_face_get_reference_count (font_face);
fprintf (stderr, "refernce_count in drawing: %d\n", current_count);font_face = cairo_get_font_face (cr);
current_count =cairo_font_face_get_reference_count (font_face);
fprintf (stderr, "reference_count in drawing: %d\n", current_count);
cairo_move_to(cr, 20, 180);
cairo_show_text(cr, "Somebody tell me why I'm on my own");
font_face = cairo_get_font_face (cr);
current_count =cairo_font_face_get_reference_count (font_face);
fprintf (stderr, "reference_count in drawing: %d\n", current_count);
cairo_move_to(cr, 20, 210);
cairo_show_text(cr, "If there's a soulmate for everyone");
font_face = cairo_get_font_face (cr);
current_count =cairo_font_face_get_reference_count (font_face);
fprintf (stderr, "reference_count in drawing: %d\n", current_count);------------->prints 2
}
static gboolean time_handler(GtkWidget *widget)
{
if (i > 15) return FALSE;
printf("drawing frame %d\n", i);
gtk_widget_queue_draw(widget);
i++;
return TRUE;
}
int main(int argc, char *argv[])
{
// gtk inits
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *drawarea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window), drawarea);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
g_signal_connect(G_OBJECT(drawarea), "draw", G_CALLBACK(do_draw), NULL);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_timeout_add(200, (GSourceFunc) time_handler, (gpointer) window);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
cairo_move_to(cr, 20, 150);
cairo_show_text(cr, "Who knows how to love you without being told");