/* 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 Spencer Kimball and Peter Mattis
 *
 * pikadrawable-stroke.c
 * Copyright (C) 2003 Simon Budig  <simon@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 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/>.
 */

#include "config.h"

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

#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "pikachannel.h"
#include "pikadrawable-fill.h"
#include "pikadrawable-stroke.h"
#include "pikaerror.h"
#include "pikaimage.h"
#include "pikascanconvert.h"
#include "pikastrokeoptions.h"

#include "vectors/pikavectors.h"

#include "pika-intl.h"


/*  public functions  */

void
pika_drawable_stroke_boundary (PikaDrawable       *drawable,
                               PikaStrokeOptions  *options,
                               const PikaBoundSeg *bound_segs,
                               gint                n_bound_segs,
                               gint                offset_x,
                               gint                offset_y,
                               gboolean            push_undo)
{
  PikaScanConvert *scan_convert;

  g_return_if_fail (PIKA_IS_DRAWABLE (drawable));
  g_return_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)));
  g_return_if_fail (PIKA_IS_STROKE_OPTIONS (options));
  g_return_if_fail (bound_segs == NULL || n_bound_segs != 0);
  g_return_if_fail (pika_fill_options_get_style (PIKA_FILL_OPTIONS (options)) !=
                    PIKA_FILL_STYLE_PATTERN ||
                    pika_context_get_pattern (PIKA_CONTEXT (options)) != NULL);

  scan_convert = pika_scan_convert_new_from_boundary (bound_segs, n_bound_segs,
                                                      offset_x, offset_y);

  if (scan_convert)
    {
      pika_drawable_stroke_scan_convert (drawable, options,
                                         scan_convert, push_undo);
      pika_scan_convert_free (scan_convert);
    }
}

gboolean
pika_drawable_stroke_vectors (PikaDrawable       *drawable,
                              PikaStrokeOptions  *options,
                              PikaVectors        *vectors,
                              gboolean            push_undo,
                              GError            **error)
{
  const PikaBezierDesc *bezier;

  g_return_val_if_fail (PIKA_IS_DRAWABLE (drawable), FALSE);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)), FALSE);
  g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), FALSE);
  g_return_val_if_fail (PIKA_IS_VECTORS (vectors), FALSE);
  g_return_val_if_fail (pika_fill_options_get_style (PIKA_FILL_OPTIONS (options)) !=
                        PIKA_FILL_STYLE_PATTERN ||
                        pika_context_get_pattern (PIKA_CONTEXT (options)) != NULL,
                        FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  bezier = pika_vectors_get_bezier (vectors);

  if (bezier && bezier->num_data >= 2)
    {
      PikaScanConvert *scan_convert = pika_scan_convert_new ();

      pika_scan_convert_add_bezier (scan_convert, bezier);
      pika_drawable_stroke_scan_convert (drawable, options,
                                         scan_convert, push_undo);

      pika_scan_convert_free (scan_convert);

      return TRUE;
    }

  g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                       _("Not enough points to stroke"));

  return FALSE;
}

void
pika_drawable_stroke_scan_convert (PikaDrawable      *drawable,
                                   PikaStrokeOptions *options,
                                   PikaScanConvert   *scan_convert,
                                   gboolean           push_undo)
{
  gdouble  width;
  PikaUnit unit;

  g_return_if_fail (PIKA_IS_DRAWABLE (drawable));
  g_return_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)));
  g_return_if_fail (PIKA_IS_STROKE_OPTIONS (options));
  g_return_if_fail (scan_convert != NULL);
  g_return_if_fail (pika_fill_options_get_style (PIKA_FILL_OPTIONS (options)) !=
                    PIKA_FILL_STYLE_PATTERN ||
                    pika_context_get_pattern (PIKA_CONTEXT (options)) != NULL);

  if (! pika_item_mask_intersect (PIKA_ITEM (drawable), NULL, NULL, NULL, NULL))
    return;

  width = pika_stroke_options_get_width (options);
  unit  = pika_stroke_options_get_unit (options);

  if (unit != PIKA_UNIT_PIXEL)
    {
      PikaImage *image = pika_item_get_image (PIKA_ITEM (drawable));
      gdouble    xres;
      gdouble    yres;

      pika_image_get_resolution (image, &xres, &yres);

      pika_scan_convert_set_pixel_ratio (scan_convert, yres / xres);

      width = pika_units_to_pixels (width, unit, yres);
    }

  pika_scan_convert_stroke (scan_convert, width,
                            pika_stroke_options_get_join_style (options),
                            pika_stroke_options_get_cap_style (options),
                            pika_stroke_options_get_miter_limit (options),
                            pika_stroke_options_get_dash_offset (options),
                            pika_stroke_options_get_dash_info (options));

  pika_drawable_fill_scan_convert (drawable, PIKA_FILL_OPTIONS (options),
                                   scan_convert, push_undo);
}
