#include <pango/pango-fontmap.h>
#include <pango/pangoxft.h>
#include <X11/Xft/Xft.h>
#include "pangographite.h"


Display *     display;
int           screen_num;

static PangoCoverageLevel
graphite_engine_covers (PangoFont *font)
{	
  PangoFontDescription *desc;
  PangoFcFont *fcfont;
  char * fontcap;
  char *fontdesc;

  //g_message("graphite_engine_covers %s", pango_language_to_string(lang));
  if (!PANGO_IS_FONT (font))
  {
	  g_message("not a PangoFont");
  	return PANGO_COVERAGE_NONE;
  }
  if (!PANGO_IS_FC_FONT (font))
  {
	  g_message("not a PangoFcFont");
  	return PANGO_COVERAGE_NONE;
  }
  if (!PANGO_XFT_IS_FONT (font))
  {
	  g_message("not a PangoXftFont");
  	return PANGO_COVERAGE_NONE;
  }
  desc = pango_font_describe(font);
  fontdesc = pango_font_description_to_string (desc);
  pango_font_description_free(desc);
  g_message("fontdesc %s", fontdesc);
  g_free(fontdesc);
  fcfont = (PangoFcFont *)font;
  //patt = fcfont->font_pattern;
  //FcPatternGet (patt, "fontcap", 0, val);
  if (FcPatternGetString (fcfont->font_pattern, FC_CAPABILITY, 0, (FcChar8 **)&fontcap) != FcResultMatch)
  {
	  g_message("no string fontcap");
	  return PANGO_COVERAGE_NONE;
  }
  g_message("fontcap %s", fontcap);
  if (strstr(fontcap, "ttable:Silf"))
  {
	  //g_warning("got graphite engine coverage for %ld", wc);
	  //g_message("got graphite engine coverage for %ld", wc);
	  //return PANGO_COVERAGE_NONE;
	  return PANGO_COVERAGE_EXACT;
  }
  else
	  return PANGO_COVERAGE_NONE;
  // the patch for fontconfig has fontcap for font capabilities inc silgraphite tttables 
  
  #if 0
  //FcResult FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)

  /* Returns in `v' the `id'th value associated with the property `object'. 
     The value returned is not a copy, but rather refers to the data stored 
       within the pattern directly. Applications must not free this value.
  */
  #endif
}

    
int main(int argc, char *argv[])
{
gchar *text;
gsize len;

//if (!g_file_get_contents("burmese4.txt", &text, &len, NULL))
if (!g_file_get_contents("ipa_test.txt", &text, &len, NULL))
{
printf("failed to read burmese file\n");
exit(-1);
}


	PangoGlyphString *glyphs;
	PangoFont *font;
	//XftFont *xftfont;
	char *       display_name = NULL;
    if ( (display = XOpenDisplay(display_name)) == NULL ) {
	fprintf(stderr, "%s: couldn't connect to X server %s\n",
		argv[0], display_name);
	exit(EXIT_FAILURE);
    }


    /*  Get screen size from display structure macro  */

    screen_num     = DefaultScreen(display);
#if 0
	xftfont = XftFontOpen(display, screen_num,XFT_FAMILY, XftTypeString,
				     "Std SILDoulos",
				     XFT_SIZE, XftTypeInteger, 12, 0);
#endif
	PangoFontMap *fmap = pango_xft_get_font_map(display, screen_num);
	PangoFontDescription *fdesc = pango_font_description_from_string("Doulos SIL");
	//PangoFontDescription *fdesc = pango_font_description_from_string("Padauk");
	pango_font_description_set_size(fdesc, 12*PANGO_SCALE);
	//PangoContext *context;
	font = pango_font_map_load_font (fmap, NULL, fdesc);
	pango_font_description_free (fdesc);
	if (graphite_engine_covers(font) != PANGO_COVERAGE_EXACT)
	{
	fprintf(stderr, "didn't get the font - no coverage\n");
		exit(EXIT_FAILURE);
	}
	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
	for (int i=0; i < 10; i++)
	{
		glyphs = pango_glyph_string_new ();

		g_static_mutex_lock (&mutex);
		// Do Graphite Shaping
		graphite_PangoGlyphString(text, 100, font, glyphs, "en");

		g_static_mutex_unlock (&mutex);
		pango_glyph_string_free (glyphs);
		fprintf(stdout, "%d..", i);
	}
	XCloseDisplay(display);
	return 1;
}
