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

5
  • 1
    Please use something like 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. Commented May 3, 2017 at 8:57
  • @jku I didnt knew about g_file_contents(). I am using it now but the problem remains same as per my edit. Commented May 3, 2017 at 9:10
  • 1
    there must be some utf8 errors on your file. Commented May 3, 2017 at 10:57
  • @JoséFonte Its not about file. I tried this too char *str = g_strdup_printf ("<span font=\"20\" color=\"white\">" "%s""</span>","Text which I want to display"); it still gives the error Commented May 3, 2017 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 Commented May 3, 2017 at 11:08

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.