Pango Speed



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.

/*
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]