/* LIBPIKA - The PIKA Library
 * Copyright (C) 1995-2000 Peter Mattis and Spencer Kimball
 *
 * pikatextlayer.c
 * Copyright (C) 2022 Jehan
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "pika.h"


struct _PikaTextLayer
{
  PikaLayer parent_instance;
};


static PikaLayer * pika_text_layer_copy (PikaLayer *layer);


G_DEFINE_TYPE (PikaTextLayer, pika_text_layer, PIKA_TYPE_LAYER)

#define parent_class pika_text_layer_parent_class


static void
pika_text_layer_class_init (PikaTextLayerClass *klass)
{
  PikaLayerClass *layer_class = PIKA_LAYER_CLASS (klass);

  layer_class->copy = pika_text_layer_copy;
}

static void
pika_text_layer_init (PikaTextLayer *layer)
{
}


/* Public API. */

/**
 * pika_text_layer_get_by_id:
 * @layer_id: The layer id.
 *
 * Returns a #PikaTextLayer representing @layer_id. This function calls
 * pika_item_get_by_id() and returns the item if it is layer or %NULL
 * otherwise.
 *
 * Returns: (nullable) (transfer none): a #PikaTextLayer for @layer_id or
 *          %NULL if @layer_id does not represent a valid layer. The
 *          object belongs to libpika and you must not modify or unref
 *          it.
 *
 * Since: 3.0
 **/
PikaTextLayer *
pika_text_layer_get_by_id (gint32 layer_id)
{
  PikaItem *item = pika_item_get_by_id (layer_id);

  if (PIKA_IS_TEXT_LAYER (item))
    return (PikaTextLayer *) item;

  return NULL;
}

/**
 * pika_text_layer_new:
 * @image: The image to which to add the layer.
 * @text:  The text to generate (in UTF-8 encoding).
 * @font:  The name of the font.
 * @size:  The size of text in either pixels or points.
 * @unit:  The units of specified size.
 *
 * Create a new layer.
 *
 * This procedure creates a new text layer displaying the specified @text. By
 * default the width and height of the layer will be determined by the @text
 * contents, the @fontname, @size and @unit.
 *
 * The new layer still needs to be added to the image, as this is not automatic.
 * Add the new layer with the pika_image_insert_layer() command. Other
 * attributes such as layer mask modes, and offsets should be set with explicit
 * procedure calls.
 *
 * Returns: (transfer none): The newly created text layer.
 *          The object belongs to libpika and you should not free it.
 *
 * Since: 3.0
 */
PikaTextLayer *
pika_text_layer_new (PikaImage   *image,
                     const gchar *text,
                     PikaFont    *font,
                     gdouble      size,
                     PikaUnit     unit)
{
  return _pika_text_layer_new (image, text, font, size, unit);
}


/*  private functions  */

static PikaLayer *
pika_text_layer_copy (PikaLayer *layer)
{
  PikaTextLayer *new_layer;
  gchar         *text;
  PikaFont      *font;
  gdouble        size;
  PikaUnit       unit;

  g_return_val_if_fail (PIKA_IS_TEXT_LAYER (layer), NULL);

  text      = pika_text_layer_get_text (PIKA_TEXT_LAYER (layer));
  font      = pika_text_layer_get_font (PIKA_TEXT_LAYER (layer));
  size      = pika_text_layer_get_font_size (PIKA_TEXT_LAYER (layer), &unit);
  new_layer = pika_text_layer_new (pika_item_get_image (PIKA_ITEM (layer)),
                                   text, font, size, unit);
  g_free (text);

  return PIKA_LAYER (new_layer);
}
