Re: Pango Speed



Hi again list,

I have not been able to improve the speed of this pango code myself, so I am definately still looking for suggestions.  I am using this code in a web environment so I really need it to be as fast as possible.  

Can anyone tell me if a second is slow for creating a fontmap, setting the context and getting the pixel width? What is causing the time length?  Is it because of creating a new fontmap object and setting the resolution?  If so is there some way I could create a object once, and then reuse this object?  Is there anything I can do to optimise the code or my pango installation?  

My only other current thought is to rewrite the code to take multiple calls to char_width_from_layout which would help, but anything I can do to improve performance would really make a difference.

Any suggestions anyone has are **greatly** appreciated.

Z.

>Greetings All,
>
>With the help of some awesome pango people I managed to get the code going >below that gives me back the width of some text given the font and size.  This >works great.  I was just wondering if I could make it any faster!!!
>
>Basically I'm calling it from the command line while x is going, and it takes >about a second.  Thanks heaps that would be great.
>
>Z.

Code::
/*
pango_get_width

Compile Instructions: 
 gcc -Wall `pkg-config --cflags pangoft2` -o pango_get_width pango_get_width.c `pkg-config --libs pangoft2`

API Call:
./pango_get_width "font fontsize" "text"     
*/

#include <pango/pango.h>
#include <pango/pangoft2.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* 
	 Version 1.0  by Attin Cotteral
	 		Original Code
			
	 Version 1.1  by Zitan Broth with lots of help from Noah Levitt 
            Pango Content is set with fontmapping so X is not required
			
	 Version 1.2  ZB
	 		Code tidy up
*/

static gint char_width_from_layout (PangoFontDescription *pfd, const gchar *s)
{
    PangoLayout *pl;
    gint width;
    PangoContext *context;
    PangoFontMap *fontmap;

   //Set Context using fontmap
  fontmap = pango_ft2_font_map_new ();
  pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (fontmap), 96, 96);
  context = pango_ft2_font_map_create_context (PANGO_FT2_FONT_MAP (fontmap));

  pango_context_set_font_description(context, pfd);

  pl = pango_layout_new(context);
  pango_layout_set_text(pl, s, -1);
  pango_layout_get_pixel_size(pl, &width, NULL);
		
  g_object_unref(G_OBJECT(pl));
  g_object_unref(G_OBJECT(context));
 
  return width;
}

int main (int argc, char *argv[])
{
  gint i, width;
  gchar fontname[128];
  gchar inputtext[255];

  PangoFontDescription *pfd;

  if (argc < 2) {
   fprintf(stderr, "%s: error fontname < 128 chars\n",argv[0]);
   exit(EXIT_FAILURE);
  }

  *fontname = 0;

  strcpy(fontname, argv[1]);
  strcpy(inputtext, argv[2]);
  i = 2;

  printf("Got fontname '%s' and input text '%s' \n", fontname, inputtext);
		
  pfd = pango_font_description_from_string(fontname);

  width = char_width_from_layout(pfd, inputtext);
  printf("Got char width of %d\n", width);
		
  return 0;
}


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