/* Quick hack to check for handle leak on win32, based on gimptextlayout.c */
/* Link with glib, freetype, pangoft2 and fontconfig */

/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * GimpText
 * Copyright (C) 2002-2003  Sven Neumann <sven@gimp.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
#include "config.h"
*/
#include <glib-object.h>
#include <pango/pangoft2.h>

extern unsigned int sleep(unsigned int secs);

static PangoContext *
gimp_text_get_pango_context (gdouble       xres,
                             gdouble       yres);

static void
gimp_text_layout_position (PangoLayout *layout);

void
gimp_text_layout_new_test ()
{
  PangoContext         *context;
  PangoFontDescription *font_desc;
  PangoLayout          *layout;

  font_desc = pango_font_description_from_string ("Sans");
  g_return_if_fail (font_desc != NULL);
  if (!font_desc)
    return;

  pango_font_description_set_size (font_desc, 10);

  context = gimp_text_get_pango_context (72, 72);

  layout = pango_layout_new (context);

  g_object_unref (context);

  pango_layout_set_font_description (layout, font_desc);
  pango_font_description_free (font_desc);

  pango_layout_set_text (layout, "Just testing", -1);

  /* if this call is removed there is no leak */
  gimp_text_layout_position (layout);

  g_print ("layout: %p\n", layout);
  g_object_unref (layout);
}

static void
gimp_text_layout_position (PangoLayout *layout)
{
  PangoRectangle  ink;
  PangoRectangle  logical;

  pango_layout_get_pixel_extents (layout, &ink, &logical);

  g_print ("ink rect: %d x %d @ %d, %d\n",
           ink.width, ink.height, ink.x, ink.y);
  g_print ("logical rect: %d x %d @ %d, %d\n",
           logical.width, logical.height, logical.x, logical.y);
}

#if 0
static void
gimp_text_ft2_subst_func (FcPattern *pattern,
                          gpointer   data)
{
  FcPatternAddBool (pattern, FC_HINTING,   0);
  FcPatternAddBool (pattern, FC_AUTOHINT,  0);
  FcPatternAddBool (pattern, FC_ANTIALIAS, 1);
}
#endif

static PangoContext *
gimp_text_get_pango_context (gdouble   xres,
                             gdouble   yres)
{
  PangoContext    *context;
  PangoFT2FontMap *fontmap;

  fontmap = PANGO_FT2_FONT_MAP (pango_ft2_font_map_new ());

#if 0
  pango_ft2_font_map_set_resolution (fontmap, xres, yres);

  pango_ft2_font_map_set_default_substitute (fontmap,
                                             gimp_text_ft2_subst_func,
                                             NULL,
                                             (GDestroyNotify) NULL);
#endif

  context = pango_ft2_font_map_create_context (fontmap);
  g_object_unref (fontmap);

  pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);

  return context;
}

int main(int argc, char *argv[])
{
  int i;
  g_type_init();
  for (i=0;i<100;i++)
    {
      gimp_text_layout_new_test();
      sleep(1);
    }
  return 0;
}
