/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995-2003 Spencer Kimball and Peter Mattis
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

/* NOTE: This file is auto-generated by pdbgen.pl. */

#include "config.h"

#include "stamp-pdbgen.h"

#include <gegl.h>

#include <gdk-pixbuf/gdk-pixbuf.h>

#include "libpikabase/pikabase.h"

#include "libpikabase/pikabase.h"

#include "pdb-types.h"

#include "core/pikadrawable.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "core/pikaparamspecs.h"
#include "text/pikafont.h"
#include "text/pikatext-compat.h"

#include "pikapdb.h"
#include "pikapdb-utils.h"
#include "pikaprocedure.h"
#include "internal-procs.h"


static PikaValueArray *
text_font_invoker (PikaProcedure         *procedure,
                   Pika                  *pika,
                   PikaContext           *context,
                   PikaProgress          *progress,
                   const PikaValueArray  *args,
                   GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  PikaImage *image;
  PikaDrawable *drawable;
  gdouble x;
  gdouble y;
  const gchar *text;
  gint border;
  gboolean antialias;
  gdouble size;
  PikaFont *font;
  PikaLayer *text_layer = NULL;

  image = g_value_get_object (pika_value_array_index (args, 0));
  drawable = g_value_get_object (pika_value_array_index (args, 1));
  x = g_value_get_double (pika_value_array_index (args, 2));
  y = g_value_get_double (pika_value_array_index (args, 3));
  text = g_value_get_string (pika_value_array_index (args, 4));
  border = g_value_get_int (pika_value_array_index (args, 5));
  antialias = g_value_get_boolean (pika_value_array_index (args, 6));
  size = g_value_get_double (pika_value_array_index (args, 7));
  font = g_value_get_object (pika_value_array_index (args, 8));

  if (success)
    {
      if (drawable &&
          (! pika_pdb_item_is_attached (PIKA_ITEM (drawable), image,
                                        PIKA_PDB_ITEM_CONTENT, error) ||
           ! pika_pdb_item_is_not_group (PIKA_ITEM (drawable), error)))
        success = FALSE;

      if (success)
        text_layer = text_render (image, drawable, context,
                                  x, y, font, size, text,
                                  border, antialias);
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    g_value_set_object (pika_value_array_index (return_vals, 1), text_layer);

  return return_vals;
}

static PikaValueArray *
text_get_extents_font_invoker (PikaProcedure         *procedure,
                               Pika                  *pika,
                               PikaContext           *context,
                               PikaProgress          *progress,
                               const PikaValueArray  *args,
                               GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  const gchar *text;
  gdouble size;
  PikaFont *font;
  gint width = 0;
  gint height = 0;
  gint ascent = 0;
  gint descent = 0;

  text = g_value_get_string (pika_value_array_index (args, 0));
  size = g_value_get_double (pika_value_array_index (args, 1));
  font = g_value_get_object (pika_value_array_index (args, 2));

  if (success)
    {
      success = text_get_extents (pika,
                                  font, size, text,
                                  &width, &height,
                                  &ascent, &descent);
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    {
      g_value_set_int (pika_value_array_index (return_vals, 1), width);
      g_value_set_int (pika_value_array_index (return_vals, 2), height);
      g_value_set_int (pika_value_array_index (return_vals, 3), ascent);
      g_value_set_int (pika_value_array_index (return_vals, 4), descent);
    }

  return return_vals;
}

void
register_text_tool_procs (PikaPDB *pdb)
{
  PikaProcedure *procedure;

  /*
   * pika-text-font
   */
  procedure = pika_procedure_new (text_font_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-text-font");
  pika_procedure_set_static_help (procedure,
                                  "Add text at the specified location as a floating selection or a new layer.",
                                  "The x and y parameters together control the placement of the new text by specifying the upper left corner of the text bounding box. If the specified drawable parameter is valid, the text will be created as a floating selection attached to the drawable. If the drawable parameter is not valid (%NULL), the text will appear as a new layer. Finally, a border can be specified around the final rendered text. The border is measured in pixels.\n"
                                  "The size is always in pixels. If you need to display a font in points, divide the size in points by 72.0 and multiply it by the image's vertical resolution.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Martin Edlman & Sven Neumann",
                                         "Spencer Kimball & Peter Mattis",
                                         "1998- 2001");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_drawable ("drawable",
                                                         "drawable",
                                                         "The affected drawable: (%NULL for a new text layer)",
                                                         TRUE,
                                                         PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_double ("x",
                                                    "x",
                                                    "The x coordinate for the left of the text bounding box",
                                                    -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_double ("y",
                                                    "y",
                                                    "The y coordinate for the top of the text bounding box",
                                                    -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_string ("text",
                                                       "text",
                                                       "The text to generate (in UTF-8 encoding)",
                                                       FALSE, FALSE, FALSE,
                                                       NULL,
                                                       PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("border",
                                                 "border",
                                                 "The size of the border",
                                                 -1, G_MAXINT32, -1,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_boolean ("antialias",
                                                     "antialias",
                                                     "Antialiasing",
                                                     FALSE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_double ("size",
                                                    "size",
                                                    "The size of text in pixels",
                                                    0, G_MAXDOUBLE, 0,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_font ("font",
                                                     "font",
                                                     "The font",
                                                     FALSE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_layer ("text-layer",
                                                          "text layer",
                                                          "The new text layer or %NULL if no layer was created.",
                                                          TRUE,
                                                          PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-text-get-extents-font
   */
  procedure = pika_procedure_new (text_get_extents_font_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-text-get-extents-font");
  pika_procedure_set_static_help (procedure,
                                  "Get extents of the bounding box for the specified text.",
                                  "This tool returns the width and height of a bounding box for the specified text rendered with the specified font information. Ascent and descent of the glyph extents are returned as well.\n"
                                  "The ascent is the distance from the baseline to the highest point of the character. This is positive if the glyph ascends above the baseline. The descent is the distance from the baseline to the lowest point of the character. This is positive if the glyph descends below the baseline.\n"
                                  "The size is always in pixels. If you need to set a font in points, divide the size in points by 72.0 and multiply it by the vertical resolution of the image you are taking into account.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Martin Edlman & Sven Neumann",
                                         "Spencer Kimball & Peter Mattis",
                                         "1998- 2001");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_string ("text",
                                                       "text",
                                                       "The text to generate (in UTF-8 encoding)",
                                                       FALSE, FALSE, FALSE,
                                                       NULL,
                                                       PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_double ("size",
                                                    "size",
                                                    "The size of text in either pixels or points",
                                                    0, G_MAXDOUBLE, 0,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_font ("font",
                                                     "font",
                                                     "The name of the font",
                                                     FALSE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("width",
                                                     "width",
                                                     "The width of the glyph extents",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("height",
                                                     "height",
                                                     "The height of the glyph extents",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("ascent",
                                                     "ascent",
                                                     "The ascent of the glyph extents",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("descent",
                                                     "descent",
                                                     "The descent of the glyph extents",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);
}
