3

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

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • 1
    Please use something like [g_file_get_contents()](https://developer.gnome.org/glib/stable/glib-File-Utilities.html#g-file-get-contents) to read a file -- I don't think your code is safe, at the very least you write the terminator after the end of buffer. – Jussi Kukkonen May 03 '17 at 08:57
  • @jku I didnt knew about g_file_contents(). I am using it now but the problem remains same as per my edit. – S Andrew May 03 '17 at 09:10
  • 1
    there must be some utf8 errors on your file. – José Fonte May 03 '17 at 10:57
  • @JoséFonte Its not about file. I tried this too `char *str = g_strdup_printf ("" "%s""","Text which I want to display");` it still gives the error – S Andrew May 03 '17 at 11:05
  • You dont use it but gtk uses it, gtk.label is using it so that means there are utf8 erros on your files content – José Fonte May 03 '17 at 11:08

1 Answers1

5

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.

ebassi
  • 8,648
  • 27
  • 29