/* Compile with:
   gcc `pkg-config --cflags --libs freetype2` test-freetype.c
*/

#include <ft2build.h>
#include FT_FREETYPE_H

FT_Library library;
FT_Face face;

int
main (int argc, char *argv[])
{
  FT_Error error;
  FT_Pos horiAdvance1, horiAdvance2;
  int glyph_index;

  /* These char size values are the ones that occurred in my test app. */
  FT_F26Dot6 size1 = 853, size2 = 597;

  /* These values result in a much closer advance to the one expected. */
  /*FT_F26Dot6 size1 = 853000, size2 = 597000;*/

  error = FT_Init_FreeType( &library );
  if ( error )
    {
      fprintf (stderr, "Error initializeing freetype\n");
      exit (0);
    }

  error = FT_New_Face (library, "/usr/share/fonts/bitstream-vera/Vera.ttf", 0,
		       &face);
  if (error)
    {
      fprintf (stderr, "Error loading font face\n");
      exit (0);
    }

  glyph_index = FT_Get_Char_Index (face, 'T');

  error = FT_Set_Char_Size (face, size1, size1, 0, 0);
  error = FT_Load_Glyph (face, glyph_index, FT_LOAD_NO_HINTING);
  horiAdvance1 = face->glyph->metrics.horiAdvance;

  error = FT_Set_Char_Size (face, size2, size2, 0, 0);
  error = FT_Load_Glyph (face, glyph_index, FT_LOAD_NO_HINTING);
  horiAdvance2 = face->glyph->metrics.horiAdvance;

  printf ("Glyph 1 Size: %li Advance: %li\n", size1, horiAdvance1);
  printf ("Glyph 2 Size: %li Advance: %li Expected Advance: %g\n",
	  size2, horiAdvance2, ((double) size2 / size1) * horiAdvance1);

  /* For me this outputs:
   *   Glyph 1 Size: 853.312 Advance: 508
   *   Glyph 2 Size: 597.319 Advance: 352 Expected Advance: 355.6
   */
}
