# GIMP - The GNU Image Manipulation Program
# Copyright (C) 1995 Spencer Kimball and Peter Mattis

# New Text API Design for the PDB
# Copyright (C) 2008 Marcus Heese <heese@cip.ifi.lmu.de>

# 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.

# This is just for me :)
sub marcus_pdb_misc {
    contrib_pdb_misc('Marcus Heese', 'heese@cip.ifi.lmu.de', '2008', @_);
}

# The next could also be integrated into other files, for checks if it is a text layer
sub drawable_is_text_layer {
    $blurb = 'Checks if the drawable is a Text Layer.';

    $help = <<'HELP';
With this procedure you can check if your drawable 'drawable' is a text layer. If it is the function will return TRUE and FALSE otherwise.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'drawable', type => 'drawable',
	  desc => 'The drawable' }
    );

    @outargs = (
        { name => 'is_text_layer', type => 'boolean',
	  desc => 'True if the drawable is a text layer. False if it is not.' }
    );

    %invoke = (
        code => <<'CODE'
{
  is_text_layer = gimp_drawable_is_text_layer (drawable);
}
CODE
    );
}

# Here begins the new api
sub text_layer_new {
    $blurb = 'Creates a new text layer.';

    $help = <<'HELP';
This procedure creates a new text layer. The arguments are kept as simple as basic as necessary for the normal case. All text attributes, however, can be modified with the appropriate gimp_text_layer_set_*() commands. The new layer still needs to be added to the image, as this is not automatic. Add the new layer with the gimp_image_add_layer() command.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'image', type => 'image',
          desc => 'The image' },
        { name => 'text', type => 'string',
          desc => 'The text to generate (in UTF-8 encoding)' },
        { name => 'fontname', type => 'string',
          desc => 'The name of the font' },
        { name => 'x', type => 'float',
          desc => 'The x coordinate for the left of the text bounding box' },
        { name => 'y', type => 'float',
          desc => 'The y coordinate for the top of the text bounding box' },
        { name => 'size', type => '0 < float',
          desc => 'The size of text in either pixels or points' },
        { name => 'unit', type => 'unit',
          desc => 'The units of specified size' }
    );

    @outargs = (
        { name => 'layer', type => 'layer',
          desc => 'The new text layer or NULL if something went wrong.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;
  
  /* Immitate the text tool UI behaviour: The foreground color is used by default, not black */
  GimpRGB color;
  gimp_context_get_foreground (context, &color);
  
  gimp_text = g_object_new (GIMP_TYPE_TEXT,
                            "text", text,
			    "font", fontname,
			    "font-size", size,
			    "font-size-unit", unit,
			    "offset-x", x,
			    "offset-y", y,
			    "color", &color,
			    NULL);
  
  layer = gimp_text_layer_new (image, gimp_text);

  g_object_unref (gimp_text);

  if (! layer)
    {
      success = FALSE;
      return NULL;
    }

  /* It seems that the gimp-text object doesn't care about the offset-x/y thing
   * so here a little workaround from the old api... FIXME? */
  GIMP_ITEM (layer)->offset_x = x;
  GIMP_ITEM (layer)->offset_y = y;
}
CODE
    );
}

sub text_layer_discard_text {
    $blurb = 'Discards text information in a layer.';

    $help = <<'HELP';
This procedure discards the text information in a text layer. The result is a text layer "converted" to a normal layer. You can then go on and use the layer as a normal layer. Pay attention, however, that you cannot convert a normal layer back to a text layer!
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' }
    );

    %invoke = (
        code => <<'CODE'
{
  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_discard (GIMP_TEXT_LAYER (layer));
    }
}
CODE
    );
}

# Here follow the *_set_* functions now
sub text_layer_set_text {
    $blurb = 'Set the text of a Text Layer.';

    $help = <<'HELP';
With this procedure you can set/reset/change the text of a Text Layer. Just give it an appropriate "text" argument to do it.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'text', type => 'string',
	  desc => 'The new text to set' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "text", text,
  		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_font {
    $blurb = 'Set the font of a Text Layer.';

    $help = <<'HELP';
With this procedure you can modify the font used in the specified Text Layer. Just give it an appropriate "font" argument to do it.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'font', type => 'string',
	  desc => 'The new font to use' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";
  
  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "font", font,
		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_fontsize {
    $blurb = 'Set the font size.';

    $help = <<'HELP';
With this procedure you can set the font size of your text. The size of your font will be a double 'fontsize' of 'unit' units. So to set the size of your fonts to 18 pixels use '18.0' for fontsize and '0' for 'unit' (as pixels have the unit type 0). Additionally you have to provide a correct text layer.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'fontsize', type => 'float',
	  desc => 'The fontsize...' },
	{ name => 'unit', type => 'unit',
	  desc => '... in units' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "font-size-unit", unit,
		           "font-size", fontsize,
		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_hinting {
    $blurb = 'Enable/Disable the use of hinting on the text in a Text Layer.';

    $help = <<'HELP';
With this procedure you can enable or disable hinting on the text of a Text Layer. If the font doesn't provide any hinting information, you can also decide to use the automatic hinter provided within FreeType. So if you set 'autohinter' to true, it will result in using FreeType's automatic hinter. Pay attention, however, that if you enable 'the autohinter' flag, that always the automatic hinter will be used disregarding the hinting information if available!
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'hinting', type => 'boolean',
	  desc => 'Enable/Disable the use of hinting on the text' },
	{ name => 'autohinter', type => 'boolean',
	  desc => 'Force the use of the autohinter provided through FreeType' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "hinting", hinting,
                           "autohint", autohinter,
	  	           NULL);

      if ((autohinter == TRUE) && (hinting == FALSE))
        {
          gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                               "autohint", autohinter,
	  	   	       "hinting", TRUE,
	 		       NULL);
        }
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_antialias {
    $blurb = 'Enable/Disable antialiasing of the text in a Text Layer.';

    $help = <<'HELP';
With this procedure you can enable or disable antialiasing of the text in a Text Layer. Just set the 'antialias' flag to true or false and provide a text layer in 'layer'. That's it!
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'antialias', type => 'boolean',
	  desc => 'Enable/Disable antialiasing of the text' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "antialias", antialias,
		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_kerning {
    $blurb = 'Enable/Disable kerning in the text of a Text Layer.';

    $help = <<'HELP';
With this procedure you can enable kerning. Just set the 'kerning' flag to true or false (to disable kerning) and provide a text layer in 'layer'. That's it! However, the use of kerning is not always desired, so use this function only if you know what you are doing.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'kerning', type => 'boolean',
	  desc => 'Enable/Disable kerning in the text' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "kerning", kerning,
	  	           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_language {
    $blurb = 'Set the language of the the Text Layer.';

    $help = <<'HELP';
With this procedure you can set the language of the text layer. FIXME: This is good for ... what?!?!
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'language', type => 'string',
	  desc => 'The new language to use for the text layer' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "language", language,
		 	   NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_textdirection {
    $blurb = 'Set the text direction in the text layer.';

    $help = <<'HELP';
With this procedure you can set the text direction of your text. At the moment you have only two possibilities: You can set the direction "From left to right" (GIMP_TEXT_DIRECTION_LTR) or "From right to left" (GIMP_TEXT_DIRECTION_RTL). Text directions "From top to bottom" or "From bottom to top" or something else aren't supported yet. So to set the text direction set the 'direction' and give it a correct text layer 'layer'.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'direction', type => 'enum GimpTextDirection',
	  desc => 'The base text direction of your text.' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "base-direction", direction,
		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_justification {
    $blurb = 'Set the justification of the text in the Text Layer.';

    $help = <<'HELP';
With this procedure you can set the justification of your text in the text box. You can decide if you want your text "left justified" (GIMP_TEXT_JUSTIFY_LEFT), "right justified" (GIMP_TEXT_JUSTIFY_RIGHT), "centered" (GIMP_TEXT_JUSTIFY_CENTER) or "filled" (GIMP_TEXT_JUSTIFY_FILL). So just provide a correct justification 'justify' and a text layer 'layer' and you can set the justification of your text in the text layer.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'justify', type => 'enum GimpTextJustification',
	  desc => 'The justification for your text.' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "justify", justify,
		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_color {
    $blurb = 'Set the color of the text in the Text Layer.';

    $help = <<'HELP';
With this procedure you can set the text color of the text in the text layer 'layer'.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'color', type => 'color', void_ret => 1,
	  desc => 'The color for the text to use' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "color", &color,
  		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_indent {
    $blurb = 'Set the indentation of the first line in the specified Text Layer.';

    $help = <<'HELP';
With this procedure you can set the indentation of the first line in the text layer. Just give it a correct double 'indent' and a text layer 'layer'.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'indent', type => 'float',
	  desc => 'The indentation for the first line.' }
     );

     %invoke = (
         code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "indent", indent,
		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_linespacing {
    $blurb = 'Set the line spacing used between lines in the specified Text Layer.';

    $help = <<'HELP';
With this procedure you can set the line spacing between the lines in your text. Just give it a correct double 'linespacing' and a text layer 'layer'.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'linespacing', type => 'float',
	  desc => 'The line spacing used between the lines in your text.' }
    );

    %invoke = (
        code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "line-spacing", linespacing,
		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_set_letterspacing {
    $blurb = 'Set the letter spacing used between the letters in your text.';

    $help = <<'HELP';
With this procedure you can set the letter spacing between the single letters in your text. Just give it a correct double 'letterspacing' and a text layer 'layer'.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' },
	{ name => 'letterspacing', type => 'float',
	  desc => 'The letter spacing used between the single letters in your text.' }
    );
    
    %invoke = (    
	code => <<'CODE'
{
  const gchar *undo_desc = "Set Text Layer attribute";

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text_layer_set (GIMP_TEXT_LAYER (layer), undo_desc,
                           "letter-spacing", letterspacing,
		           NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

# Here follow the *_get_* functions now....
sub text_layer_get_text {
    $blurb = 'Get the text from a text layer as string.';

    $help = <<'HELP';
With this procedure you can get the text from a text layer as a string. Just pass the text layer as argument and you will receive a string in return.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' }
    );

    @outargs = (
        { name => 'text', type => 'string',
	  desc => 'The text from the specified text layer.' }
    );
    
    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "text", &text,
		    NULL);
      if (text == NULL)
        {
          success = FALSE;
        }
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_font {
    $blurb = 'Get the font from a text layer as string.';

    $help = <<'HELP';
With this procedure you can get the name of the font from a text layer as a string. Just pass the text layer as argument and you will receive a string in return.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' }
    );

    @outargs = (
        { name => 'font', type => 'string',
	  desc => 'The font which is used in the specified text layer.' }
    );
    
    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "font", &font,
 		    NULL);
      if (font == NULL)
        {
          success = FALSE;
        }
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_fontsize {
    $blurb = 'Get the font size from a text layer.';

    $help = <<'HELP';
With this procedure you can get the size of the font which is used in a text layer. You will receive the size as a float 'font-size' in 'unit' units. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' }
    );

    @outargs = (
        { name => 'fontsize', type => 'float',
	  desc => 'The fontsize ...' },
	{ name => 'unit', type => 'unit',
	  desc => '... in units' }
    );
    
    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "font-size", &fontsize,
		    "font-size-unit", &unit,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_hinting {
    $blurb = 'Get information about the used hinting in the specified text layer.';

    $help = <<'HELP';
With this procedure you can check if hinting is used on the font. You will additionally get information if the automatic hinting from FreeType is used.  Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' }
    );

    @outargs = (
        { name => 'hinting', type => 'boolean',
	  desc => 'A flag which is true if hinting is used on the font.' },
	{ name => 'autohinter', type => 'boolean',
	  desc => 'A flag which is true if the text layer is forced to use the autohinter from FreeType.' }
    );
    
    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "hinting", &hinting,
		    "autohint", &autohinter,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_antialias {
    $blurb = 'Check if Antialiasing is used in the text layer.';

    $help = <<'HELP';
With this procedure you can check if antialiasing is enabled in the specified text layer. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' }
    );

    @outargs = (
        { name => 'antialias', type => 'boolean',
	  desc => 'A flag which is true if antialiasing is used for rendering the font in the text layer.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "antialias", &antialias,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_kerning {
    $blurb = 'Check if kerning is used for the text in the text layer.';

    $help = <<'HELP';
With this procedure you can check if kerning is enabled in the specified text layer. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer' }
    );

    @outargs = (
        { name => 'kerning', type => 'boolean',
	  desc => 'A flag which is true if kerning is used in the text layer.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "kerning", &kerning,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_language {
    $blurb = 'Get the language used in the text layer.';

    $help = <<'HELP';
With this procedure you can get the language string which is used in the text layer. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer.' }
    );

    @outargs = (
        { name => 'language', type => 'string',
	  desc => 'The language used in the text layer.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "language", &language,
		    NULL);
      if (language == NULL)
        {
          success = FALSE;
        }
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_textdirection {
    $blurb = 'Get the text direction information of the text layer.';

    $help = <<'HELP';
This procedure returns the text direction of the text in the text layer. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer.' }
    );

    @outargs = (
        { name => 'direction', type => 'enum GimpTextDirection',
	  desc => 'The text direction of the text in the text layer.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "base-direction", &direction,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}


sub text_layer_get_justification {
    $blurb = 'Get the text justification information of the text layer.';

    $help = <<'HELP';
This procedure returns the text justification of the text in the text layer box. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer.' }
    );

    @outargs = (
        { name => 'justify', type => 'enum GimpTextJustification',
	  desc => 'The justification used in the text box.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "justify", &justify,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_color {
    $blurb = 'Get the color of the text.';

    $help = <<'HELP';
With this procedure it is possible to get the color of the font. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer.' }
    );

    @outargs = (
        { name => 'color', type => 'color', void_ret => 1,
	  desc => 'The color of the font.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "color", &color,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_indent {
    $blurb = 'Get the indentation value.';

    $help = <<'HELP';
With this procedure it is possible to get the value of indentation of the first line. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer.' }
    );

    @outargs = (
        { name => 'indent', type => 'float',
	  desc => 'The indentation value of the first line.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "indent", &indent,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_linespacing {
    $blurb = 'Get the line-spacing value.';

    $help = <<'HELP';
With this procedure it is possible to get the value of the used line-spacing between the lines of text. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer.' }
    );

    @outargs = (
        { name => 'linespacing', type => 'float',
	  desc => 'The line-spacing value.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "line-spacing", &linespacing,
		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

sub text_layer_get_letterspacing {
    $blurb = 'Get the letter-spacing value.';

    $help = <<'HELP';
With this procedure it is possible to get the value of the used letter-spacing between the single characters in text. Just pass a text layer as argument to achieve the eligible result.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'layer', type => 'layer',
	  desc => 'The text layer.' }
    );

    @outargs = (
        { name => 'letterspacing', type => 'float',
	  desc => 'The letter-spacing value.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      g_object_get (gimp_text,
                    "letter-spacing", &letterspacing,
 		    NULL);
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

###
### I commented out the "text_layer_get_coordinates" function because Sven said it needs further discussion
### Anyway, as the text_layer API will be converted to pangocairo in this GSoC, this code should be replaced by some appropriate code anyway
###

#sub text_layer_get_coordinates {
#    $blurb = 'Get the position from a text layer at the specified letter.';
#
#    $help = <<'HELP';
#With this procedure you will be able to get the exact position in x and y coordinates from a text layer. If you want to get the position before the first letter, you have to give "0" for "text_position". At the moment the "horizontal_alignment" can only be "0" for top or "1" for bottom. An explaining example: Imagine you have got a text layer with the text field "I am a test text layer!" and you want to get the coordinates at the the beginning from the word test. Give "7" as argument for the position. Depending on if you want to get the bottom or the top coordinates for the letter "t", you have to specify "0" for the top or "1" for the bottom. However, pay attention that the "horizontal_alignment" argument may change in future releases. It is planned that you can specify the descent, baseline, meanline or ascent as "horizontal_alignment".
#HELP
#
#    &marcus_pdb_misc;
#
#    @inargs = (
#        { name => 'layer', type => 'layer',
#	  desc => 'The text layer.' },
#	{ name => 'text_position', type => 'int32',
#	  desc => 'The position in the text field' },
#	{ name => 'horizontal_alignment', type => 'int32',
#	  desc => 'Specifies if the returned coordinates will be at the bottom or the top of a letter. 0 = top, 1 = bottom' }
#    );
#
#    @outargs = (
#        { name => 'x', type => 'float',
#	  desc => 'The x coordinate of the returned position.' },
#	{ name => 'y', type => 'float',
#	  desc => 'The y coordinate of the returned position.' },
#	{ name => 'unit', type => 'unit',
#	  desc => 'The unit type of the returned position.' }
#    );
#
#    %invoke = (
#        code => <<'CODE'
#{
#  gchar *text = NULL;
#  GimpText *gimp_text = NULL;
#  GimpItem *gimp_item = NULL;
#  GimpImage *gimp_image = NULL;
#  GimpTextLayout *gimp_layout = NULL;
#  
#  gdouble xoff, yoff;
#  gint gimp_layout_xoff, gimp_layout_yoff;
#
#  /*gint pango_ascent, pango_descent;*/
#  PangoRectangle strong_pos, weak_pos;
#  PangoLayout *pango_layout = NULL;
#
#  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
#    {
#      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
#      g_object_get (gimp_text,
#                    "text", &text,
#		    NULL);
#      
#      if (text && (strlen (text) >= text_position))
#        {
#          gimp_item = GIMP_ITEM (layer);
#          gimp_image = gimp_item_get_image (gimp_item);
#      
#          if (gimp_image)
#            {
#	      gimp_layout = gimp_text_layout_new (gimp_text, gimp_image);
#	  
#	      if (gimp_layout)
#	        {
#	          pango_layout = GIMP_TEXT_LAYOUT (gimp_layout)->layout;
#	      
#	          gimp_text_layout_get_offsets (gimp_layout, &gimp_layout_xoff, &gimp_layout_yoff);
#	          xoff = gimp_item->offset_x + gimp_layout_xoff;
#	          yoff = gimp_item->offset_y + gimp_layout_yoff;
#
#	          pango_layout_get_cursor_pos (pango_layout, text_position, &strong_pos, &weak_pos);
#
#	          /*pango_ascent = PANGO_ASCENT (strong_pos);
#	          pango_descent = PANGO_DESCENT (strong_pos);*/
#
#	          switch (horizontal_alignment)
#	            {
#		    case 0: /* top */
#		      x = xoff + pango_units_to_double (strong_pos.x);
#		      y = yoff + pango_units_to_double (strong_pos.y);
#		      break;
#		    case 1: /* bottom */
#		      x = xoff + pango_units_to_double (strong_pos.x);
#		      y = yoff + pango_units_to_double (strong_pos.y) + pango_units_to_double (strong_pos.height);
#		      break;
#		    default: /* wrong parameter */
#		      /* Reason: parameter for horizontal alignment is not valid */
#		      success = FALSE;
#		      break;
#		    }
#	      
#	          g_object_unref (gimp_layout);
#
#	          /* FIXME: return positions in gimp_text units, not only in pixels */
#	          unit = 0;
#	        }
#	      else
#	        {
#	          /* Reason: couldn't create a GimpTextLayout */
#	          success = FALSE;
#	        }
#	    }
#          else
#            {
#	      /* Reason: couldn't get GimpImage or GimpText */
#	      success = FALSE;
#	    }
#        }
#      else
#        {
#	  /* Reason: text_position parameter not valid, probably too high */
#	  success = FALSE;
#	}
#    }
#  else
#    {
#      /* Reason: layer is no valid text layer */
#      success = FALSE;
#    }
#}
#CODE
#    );
#}

# Get Vectors from text layer
sub vectors_from_text_layer {
    $blurb = 'Get vectors from a text layer.';

    $help = <<'HELP';
With this procedure it is possible to get the vectors from a text layer. Pay attention to  pass a text layer as argument, a normal layer will result in an error. If properly executed, you will get 'vectors' in return.
HELP

    &marcus_pdb_misc;

    @inargs = (
        { name => 'image', type => 'image',
	  desc => 'The image.' },
        { name => 'layer', type => 'layer',
	  desc => 'The text layer.' }
    );

    @outargs = (
        { name => 'vectors', type => 'vectors',
	  desc => 'The vectors of the text layer.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpText *gimp_text;

  if (gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
      gimp_text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer));
      vectors = gimp_text_vectors_new (image, gimp_text);
      if (! vectors)
        {
	  success = FALSE;
	}
    }
  else
    {
      success = FALSE;
    }
}
CODE
    );
}

# Here is the deprecated api
sub text_fontname {
    &std_pdb_deprecated ('gimp-text-layer-new');

    @inargs = (
        { name => 'image', type => 'image',
          desc => 'The image' },
        { name => 'drawable', type => 'drawable',
          desc => 'The affected drawable: (-1 for a new text layer)',
          none_ok => 1 },
        { name => 'x', type => 'float',
          desc => 'The x coordinate for the left of the text bounding box' },
        { name => 'y', type => 'float',
          desc => 'The y coordinate for the top of the text bounding box' },
        { name => 'text', type => 'string',
          desc => 'The text to generate (in UTF-8 encoding)' },
        { name => 'border', type => '-1 <= int32',
          desc => 'The size of the border' },
        { name => 'antialias', type => 'boolean',
          desc => 'Antialiasing' },
        { name => 'size', type => '0 < float',
          desc => 'The size of text in either pixels or points' },
        { name => 'size_type', type => 'enum GimpSizeType',
          desc => 'The units of specified size' },
        { name => 'fontname', type => 'string',
          desc => 'The name of the font' }
    );

    @outargs = (
        { name => 'text_layer', type => 'layer',
          desc => 'The new text layer or -1 if no layer was created.' }
    );

    %invoke = (
        code => <<'CODE'
{
  if (drawable && ! gimp_item_is_attached (GIMP_ITEM (drawable)))
    success = FALSE;

  if (success)
    {
      gchar *real_fontname = g_strdup_printf ("%s %d", fontname, (gint) size);

      text_layer = text_render (image, drawable, context,
                                x, y, real_fontname, text,
                                border, antialias);

      g_free (real_fontname);
    }
}
CODE
    );
}

sub text_get_extents_fontname {
    &std_pdb_deprecated ('gimp-text-layer-*');

    @inargs = (
        { name => 'text', type => 'string',
          desc => 'The text to generate (in UTF-8 encoding)' },
        { name => 'size', type => '0 < float',
          desc => 'The size of text in either pixels or points' },
        { name => 'size_type', type => 'enum GimpSizeType',
          desc => 'The units of specified size' },
        { name => 'fontname', type => 'string',
          desc => 'The name of the font' }
    );

    @outargs = (
        { name => 'width', type => 'int32', void_ret => 1,
          desc => 'The width of the specified font' },
        { name => 'height', type => 'int32',
          desc => 'The height of the specified font' },
        { name => 'ascent', type => 'int32',
          desc => 'The ascent of the specified font' },
        { name => 'descent', type => 'int32',
          desc => 'The descent of the specified font' }
    );

    %invoke = (
        code => <<'CODE'
{
  gchar *real_fontname = g_strdup_printf ("%s %d", fontname, (gint) size);

  success = text_get_extents (real_fontname, text,
                              &width, &height,
                              &ascent, &descent);

  g_free (real_fontname);
}
CODE
    );
}

sub text {
    &std_pdb_deprecated ('gimp-text-layer-new');

    @inargs = (
        { name => 'image', type => 'image',
          desc => 'The image' },
        { name => 'drawable', type => 'drawable',
          desc => 'The affected drawable: (-1 for a new text layer)',
          none_ok => 1 },
        { name => 'x', type => 'float',
          desc => 'The x coordinate for the left of the text bounding box' },
        { name => 'y', type => 'float',
          desc => 'The y coordinate for the top of the text bounding box' },
        { name => 'text', type => 'string',
          desc => 'The text to generate (in UTF-8 encoding)' },
        { name => 'border', type => '-1 <= int32',
          desc => 'The size of the border' },
        { name => 'antialias', type => 'boolean',
          desc => 'Antialiasing' },
        { name => 'size', type => '0 < float',
          desc => 'The size of text in either pixels or points' },
        { name => 'size_type', type => 'enum GimpSizeType',
          desc => 'The units of specified size' },
        { name => 'foundry', type => 'string', allow_non_utf8 => 1,
          desc => 'The font foundry' },
        { name => 'family', type => 'string', allow_non_utf8 => 1,
          desc => 'The font family' },
        { name => 'weight', type => 'string', allow_non_utf8 => 1,
          desc => 'The font weight' },
        { name => 'slant', type => 'string', allow_non_utf8 => 1,
          desc => 'The font slant' },
        { name => 'set_width', type => 'string', allow_non_utf8 => 1,
          desc => 'The font set-width' },
        { name => 'spacing', type => 'string', allow_non_utf8 => 1,
          desc => 'The font spacing' },
        { name => 'registry', type => 'string', allow_non_utf8 => 1,
          desc => 'The font registry' },
        { name => 'encoding', type => 'string', allow_non_utf8 => 1,
          desc => 'The font encoding' }
    );

    @outargs = (
        { name => 'text_layer', type => 'layer',
          desc => 'The new text layer or -1 if no layer was created.' }
    );

    %invoke = (
        code => <<'CODE'
{
  if (drawable && ! gimp_item_is_attached (GIMP_ITEM (drawable)))
    success = FALSE;

  if (success)
    {
      gchar *real_fontname = g_strdup_printf ("%s %d", family, (gint) size);

      text_layer = text_render (image, drawable, context,
                                x, y, real_fontname, text,
                                border, antialias);

      g_free (real_fontname);
    }
}
CODE
   );
}

sub text_get_extents {
    &std_pdb_deprecated ('gimp-text-layer-*');

    @inargs = (
        { name => 'text', type => 'string',
          desc => 'The text to generate (in UTF-8 encoding)' },
        { name => 'size', type => '0 < float',
          desc => 'The size of text in either pixels or points' },
        { name => 'size_type', type => 'enum GimpSizeType',
          desc => 'The units of specified size' },
        { name => 'foundry', type => 'string', allow_non_utf8 => 1,
          desc => 'The font foundry' },
        { name => 'family', type => 'string', allow_non_utf8 => 1,
          desc => 'The font family' },
        { name => 'weight', type => 'string', allow_non_utf8 => 1,
          desc => 'The font weight' },
        { name => 'slant', type => 'string', allow_non_utf8 => 1,
          desc => 'The font slant' },
        { name => 'set_width', type => 'string', allow_non_utf8 => 1,
          desc => 'The font set-width' },
        { name => 'spacing', type => 'string', allow_non_utf8 => 1,
          desc => 'The font spacing' },
        { name => 'registry', type => 'string', allow_non_utf8 => 1,
          desc => 'The font registry' },
        { name => 'encoding', type => 'string', allow_non_utf8 => 1,
          desc => 'The font encoding' }
    );

    @outargs = (
        { name => 'width', type => 'int32', void_ret => 1,
          desc => 'The width of the specified font' },
        { name => 'height', type => 'int32',
          desc => 'The height of the specified font' },
        { name => 'ascent', type => 'int32',
          desc => 'The ascent of the specified font' },
        { name => 'descent', type => 'int32',
          desc => 'The descent of the specified font' }
    );

    %invoke = (
        code => <<'CODE'
{
  gchar *real_fontname = g_strdup_printf ("%s %d", family, (gint) size);

  success = text_get_extents (real_fontname, text,
                              &width, &height,
                              &ascent, &descent);

  g_free (real_fontname);
}
CODE
    );
}


@headers = qw("libgimpbase/gimpbase.h"
              "core/gimpcontext.h"
              "vectors/gimpvectors.h"
              <pango/pangoft2.h>
              "text/gimptext-private.h"
              "text/gimptextlayout.h"
              "text/gimptext-vectors.h"
              "text/gimptext.h"
              "text/gimptextlayer.h"
              "text/text-enums.h"
              "text/gimptext-compat.h");

@procs = qw(drawable_is_text_layer
            text_layer_new
            text_layer_discard_text
            text_layer_set_text
            text_layer_set_font
            text_layer_set_fontsize
            text_layer_set_hinting
            text_layer_set_antialias
            text_layer_set_kerning
            text_layer_set_language
            text_layer_set_textdirection
            text_layer_set_justification
            text_layer_set_color
            text_layer_set_indent
            text_layer_set_linespacing
            text_layer_set_letterspacing
            text_layer_get_text
            text_layer_get_font
            text_layer_get_fontsize
            text_layer_get_hinting
            text_layer_get_antialias
            text_layer_get_kerning
            text_layer_get_language
            text_layer_get_textdirection
            text_layer_get_justification
            text_layer_get_color
            text_layer_get_indent
            text_layer_get_linespacing
            text_layer_get_letterspacing
            text_layer_get_coordinates
            vectors_from_text_layer
            text_fontname
            text_get_extents_fontname
            text
            text_get_extents);

%exports = (app => [@procs], lib => [@procs]);

$desc = 'Text procedures';

1;
