GTK & Pango string measurement

I also had a similar problem. Here is how I ended up doing it...

extern GttWidget * curdlg;  // of current dialog

void gettextwdht ( char * family , int ptsize , int weight , bool normalstyle ,
                   char * stringtomeasure ,
                   int * wdret , int * htret )
   PangoFontDescription * fd = pango_font_description_new ( );

   pango_font_description_set_family (fd, family );
   pango_font_description_set_style (fd, normalstyle ? PANGO_STYLE_NORMAL :
                                                       PANGO_STYLE_ITALIC );
   pango_font_description_set_variant (fd, PANGO_VARIANT_NORMAL);
   pango_font_description_set_weight (fd, (PangoWeight)weight );
   pango_font_description_set_stretch (fd, PANGO_STRETCH_NORMAL);
   pango_font_description_set_size (fd, ptsize * PANGO_SCALE);

   PangoContext * context = gtk_widget_get_pango_context ( curdlg ) ;
   PangoLayout * layout = pango_layout_new ( context );
   pango_layout_set_text ( layout, stringtomeasure, -1 );
   pango_layout_set_font_description ( layout, fd );
   pango_layout_get_pixel_size (layout, wdret , htret );
   g_object_unref ( layout );

You can 'borrow' any widget for this (I think). It worked with a dialog widget for me.

I really wanted a PangoContext that is not associated with a widget, so the calculations can be carried out totally 'offline'. I could not see how to do that.
If anyone knows, I would be interested in finding out.

P.S. The code shown above combines getting a font and measuring it. The actual code I am using splits these into two steps for efficiency.

I'm banging my head trying to figure out how to measure the text width
of a string to be displayed in a GtkCelRendererText.  From the code
I've found, I need a PangoContext and GtkCelRendererText has plenty of
Pango properties but none that return a PangoContext.  In addition,
GtkCelRendererText is not a GtkWidget, so I can't use
gtk_widget_get_pango_context().  I'm at a loss as to how to do this.
Any ideas?

Get your new Email address!
Grab the Email name you've always wanted before someone else does!

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]