S Andrew
S Andrew

Reputation: 7268

Pango-WARNING **: Invalid UTF-8 string passed to pango_layout_set_text() in GTK C

I am getting above mentioned warning in my gtk application. I know this question is already been discussed but the problem with me is that I am not even using this funciton.

All I am doing is reading a text string from a file and then changing its font type and font size using pango and then applying the result in gtk label, Here is the code:

 FILE *fp;
 long lSize;
 char *buffer;

 fp = fopen ( "/home/user/file.txt" , "rb" );
 if( !fp ) perror("file.txt"),exit(1);

 fseek( fp , 0L , SEEK_END);
 lSize = ftell( fp );
 rewind( fp );

 buffer = calloc( 1, lSize+1 );
 if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);


 if( 1!=fread( buffer , lSize, 1 , fp) )
   fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);

 buffer[lSize+2]='\0';

 PangoFontDescription *font = pango_font_description_new ();    //NEW PANGO FONT
 pango_font_description_set_family (font,"Arial Rounded MT Bold");          //FONT FAMILY
 pango_font_description_set_size(font,60*PANGO_SCALE);  //FONT SIZE
 gtk_widget_override_font(GTK_WIDGET(label), font);         //APPLYING THE NEW FONT

 gtk_label_set_text(GTK_LABEL(label),buffer);

How can I remove this error?

Edit:

I removed the pango code and used gtk_set_markup

char *str = g_strdup_printf ("<span font=\"20\" color=\"white\">" "%s""</span>",buffer);
gtk_label_set_markup (GTK_LABEL (label), str);

The problem is if the font size is 20 or low then it displays fine with fullscreen but if I increase the font size to 30 or 40, then it gives same warning and window also resizes like below: enter image description here

In normal with low font size, it looks like below :

enter image description here

Upvotes: 3

Views: 9189

Answers (1)

ebassi
ebassi

Reputation: 8815

GTK+ widgets expect UTF-8 encoded text for all the user visible strings.

You must ensure that the contents of the file you are loading are encoded using UTF-8 before loading them into any GTK+ widget. You can use the character set conversion API provided by GLib, like g_convert() or g_locale_to_utf8().

I'd also recommend using the GIO API for loading the file contents, i.e. g_file_load_contents_async(), as it won't block the UI while loading arbitrarily sized files from arbitrarily slow storage.

Upvotes: 6

Related Questions