pixel dimensions of string



Many thanks!

It is now working.

Here are the steps combined into a single routine...

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 );
   }

I really wanted a PangoContext that is not associated with the screen, like above with a dialog widget, so that the calculations can be carried out totally 'offline'. I could not see how to do that. 

Again, many thanks.

----------------------------------


you'll have to fill in a few blanks (previously created context and description), but my code to do this is:

int fontSIZE(char *str, int fType, int type)
{
    PangoLayout *layout;
    int    width, height, ret;

    layout = pango_layout_new (font[fType].context);
    pango_layout_set_text (layout, str, -1);
    pango_layout_set_font_description (layout, font[fType].desc);
    pango_layout_get_pixel_size (layout, &width, &height);
    g_object_unref (layout);

    switch(type)
    {
        case HEIGHT:
            ret = height;
        break;
        case WIDTH:
            ret = width;
        break;
    }
    return(ret);
}



-----------------
On Tue, Aug 25, 2009 at 11:50 AM, Ken Resander <kresander yahoo com> wrote:

    I am translating a bunch of Windows programs that critically need to work out pixel dimensions of a string (with no newlines in it).
    On Windows I first call getfont:

    void getfont ( char * fontfacename , int sizeinpts , int weight , HFONT * hfret )
       {
       LOGFONT lf = usewindowsapifunctions ( fontfacename , sizeinpts , weight );
       *hfret = CreateFontIndirect ( &lf ) ;
       }

    and then for all strings to be measured call this:

    int getpixeldims ( HFONT hf , char * stringtomeasure ,
                       int * widthret , int * heightret )
       {
       HDC hdc = GetDC ( 0 );      // device context of screen
       HGIOBJ prev = SelectObject ( hdc , hf );  // add font to context
       SIZE s;
       int len = strlen ( stringtomeasure );
       GetTextExtentPoint ( hdc , stringtomeasure , len , &s );  // measures
       SelectObject ( hdc , prev );
       ReleaseDC ( 0 , hdc );

       *widthret = s.cx;
       *heightret = s.cy;
       }

    I think I need Pango for this, but I have no idea how to use it. I have the Pango reference manual and have read the tutorials by Tony Graham, but have not found any examples to learn from.

    Help would be most appreciated.







Get your preferred Email name!
Now you can @ymail.com and @rocketmail.com.

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