/* 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
 *
 * pikaviewrenderervectors.c
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
 *                    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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikamath/pikamath.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikaimage.h"
#include "core/pikaitem.h"

#include "vectors/pikastroke.h"
#include "vectors/pikavectors.h"

#include "pikaviewrenderervectors.h"


static void   pika_view_renderer_vectors_draw (PikaViewRenderer *renderer,
                                               GtkWidget        *widget,
                                               cairo_t          *cr,
                                               gint              available_width,
                                               gint              available_height);


G_DEFINE_TYPE (PikaViewRendererVectors, pika_view_renderer_vectors,
               PIKA_TYPE_VIEW_RENDERER)

#define parent_class pika_view_renderer_vectors_parent_class


static void
pika_view_renderer_vectors_class_init (PikaViewRendererVectorsClass *klass)
{
  PikaViewRendererClass *renderer_class = PIKA_VIEW_RENDERER_CLASS (klass);

  renderer_class->draw = pika_view_renderer_vectors_draw;
}

static void
pika_view_renderer_vectors_init (PikaViewRendererVectors *renderer)
{
}

static void
pika_view_renderer_vectors_draw (PikaViewRenderer *renderer,
                                 GtkWidget        *widget,
                                 cairo_t          *cr,
                                 gint              available_width,
                                 gint              available_height)
{
  PikaVectors          *vectors = PIKA_VECTORS (renderer->viewable);
  const PikaBezierDesc *desc;

  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);

  cairo_translate (cr,
                   (available_width  - renderer->width)  / 2,
                   (available_height - renderer->height) / 2);
  cairo_rectangle (cr, 0, 0, renderer->width, renderer->height);
  cairo_clip_preserve (cr);
  cairo_fill (cr);

  desc = pika_vectors_get_bezier (vectors);

  if (desc)
    {
      gdouble xscale;
      gdouble yscale;

      xscale = ((gdouble) renderer->width /
                (gdouble) pika_item_get_width  (PIKA_ITEM (vectors)));
      yscale = ((gdouble) renderer->height /
                (gdouble) pika_item_get_height (PIKA_ITEM (vectors)));

      cairo_scale (cr, xscale, yscale);

      /* determine line width */
      xscale = yscale = 0.5;
      cairo_device_to_user_distance (cr, &xscale, &yscale);

      cairo_set_line_width (cr, MAX (xscale, yscale));
      cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);

      cairo_append_path (cr, (cairo_path_t *) desc);
      cairo_stroke (cr);
    }
}
